Method: PDK::Test::Unit.invoke

Defined in:
lib/pdk/tests/unit.rb

.invoke(report, options = {}) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/pdk/tests/unit.rb', line 21

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

  tests = options.fetch(:tests)

  cmd_argv = cmd(tests, options)
  cmd_argv.unshift('ruby') if Gem.win_platform?

  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.debug(_('Running %{cmd}') % { cmd: command.argv.join(' ') })

  result = command.execute!

  json_result = if options.key?(:parallel)
                  PDK::Util.find_all_json_in(result[:stdout])
                else
                  PDK::Util.find_first_json_in(result[:stdout])
                end

  if parallel_with_no_tests?(options.key?(:parallel), json_result, result)
    json_result = [{ 'messages' => ['No examples found.'] }]
    result[:exit_code] = 0
  end

  raise PDK::CLI::FatalError, _('Unit test output did not contain a valid JSON result: %{output}') % { output: result[:stdout] } if json_result.nil? || json_result.empty?

  json_result = merge_json_results(json_result, result[:duration]) if options.key?(:parallel)

  parse_output(report, json_result)

  result[:exit_code]
end