Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(SDK-148) Add "test unit --list" #107

Merged
merged 2 commits into from
Jun 28, 2017

Conversation

james-stocks
Copy link

Implements the pdk test unit --list option that outputs a list of the module's spec tests

rspec_json_output = JSON.parse(output[:stdout])
if rspec_json_output['examples'].empty?
rspec_message = rspec_json_output['messages'][0]
raise PDK::CLI::FatalError, _('Unable to enumerate examples. rspec reported: %{message}' % { message: rspec_message }) unless rspec_message == 'No examples found.'
Copy link
Author

@james-stocks james-stocks Jun 26, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a fan of the 'guard clause' that rubocop pushes me towards here - the 'if' clause of this raise is off the edge of the screen :/

@james-stocks james-stocks force-pushed the SDK-148 branch 3 times, most recently from d290aad to 89a1cc6 Compare June 26, 2017 15:24
@james-stocks
Copy link
Author

Appveyor is failing where the module spec tests require rspec-puppet-facts

Repeating locally/manually; I get a bundle install failure for a generated module - I'm now trying to confirm if this is the same issue or something different

@scotje
Copy link
Contributor

scotje commented Jun 26, 2017

@james-stocks the issue is that the windows puppet-module meta gems currently don't depend on rspec-puppet-facts which is the same reason my unit test PR is currently failing on windows.

Copy link
Contributor

@rodjek rodjek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some stylistic recommendations, but one change that I think we should make is to have PDK::Test::Unit.list return and array of test names and handle printing it out to the user in the CLI.

@@ -20,20 +20,20 @@ module PDK::CLI
report = nil

if opts[:list]
puts _('List of all available unit tests: (TODO)')
PDK::Test::Unit.list
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would recommend moving the output logic from PDK::Test::Unit.list and into here. If it just returns an array of test names and leaves it to the CLI to print them out for the user, it can be potentially be utilised in other places in the future (e.g. validating the contents of opts[:tests] before handing off to rspec).

@@ -1,6 +1,8 @@
require 'pdk'
require 'pdk/cli/exec'
require 'pdk/util/bundler'
require 'rspec/core'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need to be required?

def self.list
PDK::Util::Bundler.ensure_bundle!
PDK::Util::Bundler.ensure_binstubs!('rspec-core')
command_argv = [File.join(PDK::Util.module_root, 'bin', 'rspec'), '--dry-run', '-fjson']
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My preference is to use the more verbose forms of options in scripts (--format json instead of -fjson) as it makes the intentions clearer.

@@ -21,6 +23,32 @@ def self.invoke(tests, report = nil)
output = PDK::CLI::Exec.execute(cmd(tests))
report.write(output) if report
end

def self.list
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A general stylistic suggestion for this method: Split the code up into logical paragraphs with a newline between them (e.g. paragraph 1 - bundler stuff, paragraph 2 - building the command array, paragraph 3 - executing the command, paragraph 4 - processing the output), makes it easier to grok a chunk at a time :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have split with newlines


describe command('pdk test unit --list') do
its(:exit_status) { is_expected.to eq 0 }
its(:stdout) { is_expected.to match(%r{Examples:.*demo_spec.rb\[1:1:1\].*demo_spec.rb\[1:2:1\].*demo_spec.rb\[1:3:1\]}m) }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than using a big multiline regex, I'd suggest doing multiple matches on stdout

its(:stdout) { is_expected.to match(%r{^demo_spec\.rb\[1:1:1\]}) }
its(:stdout) { is_expected.to match(%r{^demo_spec\.rb\[1:2:1\]}) }
its(:stdout) { is_expected.to match(%r{^demo_spec\.rb\[1:3:1\]}) }

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a single expectation that we match all three at the same time; though I guess multiple matches like you suggest is more readable

@DavidS
Copy link
Contributor

DavidS commented Jun 27, 2017

Rekicking appveyor after the new puppet-module-gems helped.

@james-stocks james-stocks force-pushed the SDK-148 branch 3 times, most recently from 116e6e5 to 9bd0d71 Compare June 27, 2017 13:56
@james-stocks
Copy link
Author

@rodjek Have addressed your comments, thanks

Copy link
Contributor

@rodjek rodjek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me!

rspec_json_output = JSON.parse(output[:stdout])
if rspec_json_output['examples'].empty?
rspec_message = rspec_json_output['messages'][0]
if rspec_message == 'No examples found.' # rubocop:disable Style/GuardClause
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can get rid of this rubocop disable by rewriting this conditional as a suffix to the return statement and letting it fall through to the raise if it doesn't match

return [] if rspec_message == 'No examples found.'

raise PDK::CLI::FatalError, _(...)

@DavidS
Copy link
Contributor

DavidS commented Jun 28, 2017

Rebased, and merged the test suite into the now existing pdk test unit tests.

@DavidS
Copy link
Contributor

DavidS commented Jun 28, 2017

@james-stocks feel free to merge, if you're satisfied with my changes.

@james-stocks james-stocks merged commit 7f884f5 into puppetlabs:master Jun 28, 2017
@DavidS DavidS changed the title (SDK-148) Implement pdk test unit --list (SDK-148) Add "test unit --list" Jun 28, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants