Skip to content

Commit

Permalink
(PDK-373) Make test unit --list consistent with test unit
Browse files Browse the repository at this point in the history
`pdk test unit` was using a pattern defined in puppetlabs_spec_helper while `pdk test unit --list` was using no pattern.
This commit updates the --list option to use a puppetlabs_spec_helper rake task so it uses the same pattern.
  • Loading branch information
james-stocks committed Aug 2, 2017
1 parent 9cb598e commit 0b36b85
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 21 deletions.
29 changes: 8 additions & 21 deletions lib/pdk/tests/unit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,7 @@ def self.invoke(report, options = {})

result = command.execute!

# TODO: cleanup rspec and/or beaker output
# Iterate through possible JSON documents until we find one that is valid.
json_result = nil

result[:stdout].scan(%r{\{(?:[^{}]|(?:\g<0>))*\}}x) do |str|
begin
json_result = JSON.parse(str)
break
rescue JSON::ParserError
next
end
end

json_result = PDK::Util.find_valid_json_in(result[:stdout])
raise PDK::CLI::FatalError, _('Unit test output did not contain a valid JSON result: %{output}') % { output: result[:stdout] } unless json_result

parse_output(report, json_result)
Expand Down Expand Up @@ -102,29 +90,28 @@ def self.parse_output(report, json_data)
# @return array of { :id, :full_description }
def self.list
PDK::Util::Bundler.ensure_bundle!
PDK::Util::Bundler.ensure_binstubs!('rspec-core')
PDK::Util::Bundler.ensure_binstubs!('rake')

command_argv = [File.join(PDK::Util.module_root, 'bin', 'rspec'), '--dry-run', '--format', 'json']
command_argv = [File.join(PDK::Util.module_root, 'bin', 'rake'), 'spec_list_json']
command_argv.unshift('ruby') if Gem.win_platform?
list_command = PDK::CLI::Exec::Command.new(*command_argv)
list_command.context = :module
output = list_command.execute!

rspec_json_output = JSON.parse(output[:stdout])
if rspec_json_output['examples'].empty?
rspec_message = rspec_json_output['messages'][0]
rspec_json = PDK::Util.find_valid_json_in(output[:stdout])
raise PDK::CLI::FatalError, _('Failed to find valid JSON in output from rspec: %{output}' % { output: output[:stdout] }) unless rspec_json
if rspec_json['examples'].empty?
rspec_message = rspec_json['messages'][0]
return [] if rspec_message == 'No examples found.'

raise PDK::CLI::FatalError, _('Unable to enumerate examples. rspec reported: %{message}' % { message: rspec_message })
else
examples = []
rspec_json_output['examples'].each do |example|
rspec_json['examples'].each do |example|
examples << { id: example['id'], full_description: example['full_description'] }
end
examples
end
rescue JSON::ParserError => e
raise PDK::CLI::FatalError, _('Failed to parse output from rspec: %{message}' % { message: e.message })
end
end
end
Expand Down
22 changes: 22 additions & 0 deletions lib/pdk/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,5 +100,27 @@ def module_root
end
end
module_function :module_root

# Iterate through possible JSON documents until we find one that is valid.
#
# @param text the text in which to find a JSON document
#
# @return substring of text that is valid JSON, or nil if no valid
# JSON found in the text
def find_valid_json_in(text)
json_result = nil

text.scan(%r{\{(?:[^{}]|(?:\g<0>))*\}}x) do |str|
begin
json_result = JSON.parse(str)
break
rescue JSON::ParserError
next
end
end

json_result
end
module_function :find_valid_json_in
end
end
31 changes: 31 additions & 0 deletions spec/unit/pdk/util_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -239,4 +239,35 @@
it { is_expected.to be_nil }
end
end

# TODO: These expect a string rather than actual JSON. Primarily they expect the non-JSON string to be removed
describe '.find_valid_json_in' do
it 'returns JSON from start of a string' do
text = '{"version":"3.6.0", "examples":[]}bar'
json = '{"version"=>"3.6.0", "examples"=>[]}'
expect(described_class.find_valid_json_in(text).to_s).to eq(json)
end

it 'returns JSON from the end of a string' do
text = 'foo{"version":"3.6.0", "examples":[]}'
json = '{"version"=>"3.6.0", "examples"=>[]}'
expect(described_class.find_valid_json_in(text).to_s).to eq(json)
end

it 'returns JSON from the middle of a string' do
text = 'foo{"version":"3.6.0", "examples":[]}bar'
json = '{"version"=>"3.6.0", "examples"=>[]}'
expect(described_class.find_valid_json_in(text).to_s).to eq(json)
end

it 'returns nil for invalid JSON in a string' do
text = 'foo{"version"":"3.6.0", "examples":[]}bar'
expect(described_class.find_valid_json_in(text)).to be_nil
end

it 'returns nil for no JSON in a string' do
text = 'foosomethingbar'
expect(described_class.find_valid_json_in(text)).to be_nil
end
end
end

0 comments on commit 0b36b85

Please sign in to comment.