Skip to content

Commit

Permalink
(PDK-465) Improve output from spec_prep/spec_clean failures
Browse files Browse the repository at this point in the history
  • Loading branch information
rodjek committed Sep 12, 2017
1 parent 14e3bdb commit 5953aa1
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 18 deletions.
66 changes: 50 additions & 16 deletions lib/pdk/tests/unit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,24 @@ module Test
class Unit
def self.cmd(_tests, opts = {})
# TODO: test selection
rake_task = opts.key?(:parallel) ? 'parallel_spec' : 'spec'
[File.join(PDK::Util.module_root, 'bin', 'rake'), rake_task]
opts.key?(:parallel) ? 'parallel_spec' : 'spec'
end

def self.rake_bin
@rake ||= File.join(PDK::Util.module_root, 'bin', 'rake')
end

def self.rake(task, spinner_text, environment = {})
argv = [rake_bin, task]
argv.unshift('ruby') if Gem.win_platform?

command = PDK::CLI::Exec::Command.new(*argv).tap do |c|
c.context = :module
c.add_spinner(spinner_text)
c.environment = environment
end

command.execute!
end

def self.parallel_with_no_tests?(ran_in_parallel, json_result, result)
Expand All @@ -18,25 +34,41 @@ def self.parallel_with_no_tests?(ran_in_parallel, json_result, result)
result[:stderr].strip =~ %r{Pass files or folders to run$}
end

def self.invoke(report, options = {})
PDK::Util::Bundler.ensure_bundle!
PDK::Util::Bundler.ensure_binstubs!('rake')
def self.print_failure(result, exception)
$stderr.puts ''
result[:stdout].each_line { |line| $stderr.puts line.rstrip } unless result[:stdout].nil?
result[:stderr].each_line { |line| $stderr.puts line.rstrip } unless result[:stderr].nil?
$stderr.puts ''
raise PDK::CLI::FatalError, exception
end

tests = options.fetch(:tests)
def self.tear_down
result = rake('spec_clean', _('Cleaning up after running unit tests.'))

cmd_argv = cmd(tests, options)
cmd_argv.unshift('ruby') if Gem.win_platform?
return if result[:exit_code].zero?

command = PDK::CLI::Exec::Command.new(*cmd_argv).tap do |c|
c.context = :module
spinner_msg = options.key?(:parallel) ? _('Running unit tests in parallel.') : _('Running unit tests.')
c.add_spinner(spinner_msg)
c.environment['CI_SPEC_OPTIONS'] = '--format j'
end
PDK.logger.error(_('The spec_clean rake task failed with the following error(s):'))
print_failure(result, _('Failed to clean up after running unit tests'))
end

def self.setup
result = rake('spec_prep', _('Preparing to run the unit tests.'))

return if result[:exit_code].zero?

PDK.logger.error(_('The spec_prep rake task failed with the following error(s):'))
print_failure(result, _('Failed to prepare to run the unit tests.'))
end

def self.invoke(report, options = {})
PDK::Util::Bundler.ensure_bundle!
PDK::Util::Bundler.ensure_binstubs!('rake')

PDK.logger.debug(_('Running %{cmd}') % { cmd: command.argv.join(' ') })
setup

result = command.execute!
tests = options.fetch(:tests)
spinner_msg = options.key?(:parallel) ? _('Running unit tests in parallel.') : _('Running unit tests.')
result = rake(cmd(tests, options), spinner_msg, 'CI_SPEC_OPTIONS' => '--format j')

json_result = if options.key?(:parallel)
PDK::Util.find_all_json_in(result[:stdout])
Expand All @@ -56,6 +88,8 @@ def self.invoke(report, options = {})
parse_output(report, json_result)

result[:exit_code]
ensure
tear_down
end

def self.parse_output(report, json_data)
Expand Down
6 changes: 4 additions & 2 deletions spec/unit/pdk/test/unit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,15 @@
it 'uses the parallel_spec rake task' do
cmd = described_class.cmd(nil, parallel: true)

expect(cmd.join('/')).to match(%r{bin/rake/parallel_spec})
expect(cmd).to eq('parallel_spec')
end
end

context 'when run without the parallel option' do
it 'uses the spec rake task' do
cmd = described_class.cmd(nil)

expect(cmd.join('/')).to match(%r{bin/rake/spec})
expect(cmd).to eq('spec')
end
end
end
Expand Down Expand Up @@ -171,6 +171,8 @@
allow(PDK::Util::Bundler).to receive(:ensure_bundle!)
allow(PDK::Util::Bundler).to receive(:ensure_binstubs!)
allow(PDK::Util).to receive(:module_root).and_return('/path/to/module')
allow(described_class).to receive(:setup)
allow(described_class).to receive(:tear_down)
allow_any_instance_of(PDK::CLI::Exec::Command).to receive(:execute!).and_return(stdout: rspec_json_output, exit_code: -1)
allow(described_class).to receive(:parse_output)
end
Expand Down

0 comments on commit 5953aa1

Please sign in to comment.