Method: PDK::Test::Unit.invoke

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

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



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/pdk/tests/unit.rb', line 83

def self.invoke(report, options = {})
  require 'pdk/util'
  require 'pdk/util/bundler'

  PDK::Util::Bundler.ensure_binstubs!('rake', 'rspec-core')

  setup

  tests = options[:tests]
  # Due to how rake handles paths in the command line options, any backslashed path (Windows platforms) needs to be converted
  # to forward slash. We can't use File.expand_path as the files aren't guaranteed to be on-disk
  #
  # Ref - https://github.com/puppetlabs/pdk/issues/828
  tests = tests.tr('\\', '/') unless tests.nil?

  environment = { 'CI_SPEC_OPTIONS' => '--format j' }
  environment['PUPPET_GEM_VERSION'] = options[:puppet] if options[:puppet]
  spinner_msg = options[:parallel] ? _('Running unit tests in parallel.') : _('Running unit tests.')

  if options[:interactive]
    environment['CI_SPEC_OPTIONS'] = if options[:verbose]
                                       '--format documentation'
                                     else
                                       '--format progress'
                                     end
    result = interactive_rake(cmd(tests, options), environment)
    return result[:exit_code]
  end

  result = rake(cmd(tests, options), spinner_msg, environment)

  json_result = if options[: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[: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) if options[:parallel]

  parse_output(report, json_result, result[:duration])

  result[:exit_code]
ensure
  tear_down if options[:'clean-fixtures']
end