Skip to content

Commit

Permalink
fix inherited profile cli report for realz this time
Browse files Browse the repository at this point in the history
  • Loading branch information
Victoria Jeffrey committed Sep 2, 2016
1 parent 6e2256a commit 23e6a0c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
34 changes: 25 additions & 9 deletions lib/inspec/rspec_json_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -164,16 +164,32 @@ def profile_info(profile)
[info[:name], info]
end

def example2control(example, profiles)
profile = profiles[example[:profile_id]]
# if this is an inherited profile, the profile comes in as
# nil, so we need do dig deep to get our control
if profile.nil? || profile[:controls].nil?
profiles.each do |x|
return x[1][:controls][example[:id]]
end
#
# TODO(ssd+vj): We should probably solve this by either ensuring the example has
# the profile_id of the top level profile when it is included as a dependency, or
# by registrying all dependent profiles with the formatter. The we could remove
# this heuristic matching.
#
def example2profile(example, profiles)
profiles.values.find { |p| profile_contains_example?(p, example) }
end

def profile_contains_example?(profile, example)
# Heuristic for finding the profile an example came from:
# Case 1: The profile_id on the example matches the name of the profile
# Case 2: The profile contains a control that matches the id of the example
if profile[:name] == example[:profile_id]
true
elsif profile[:controls] && profile[:controls].key?(example[:id])
true
else
false
end
profile[:controls][example[:id]]
end

def example2control(example, profiles)
p = example2profile(example, profiles)
p[:controls][example[:id]] if p && p[:controls]
end

def format_example(example)
Expand Down
12 changes: 12 additions & 0 deletions test/functional/inspec_exec_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,16 @@
out.stdout.force_encoding(Encoding::UTF_8).must_include "✖ expected `File /tmp.directory?` to return false, got true\e[0m"
end
end

describe 'when passing in two profiles given an inherited profile that has more that one test per control block' do
let(:out) { inspec('exec ' + File.join(profile_path, 'dependencies', 'profile_d') + ' ' + simple_inheritance) }

it 'should print all the results' do
out.stdout.force_encoding(Encoding::UTF_8).must_include "✖ tmp-1.0: Create /tmp directory (1 failed)\e[0m"
out.stdout.force_encoding(Encoding::UTF_8).must_include "✖ should not be directory\e[0m"
out.stdout.force_encoding(Encoding::UTF_8).must_include "✖ undefined method `should_nota'"
out.stdout.force_encoding(Encoding::UTF_8).must_include "✖ expected `File /tmp.directory?` to return false, got true\e[0m"
out.stdout.force_encoding(Encoding::UTF_8).must_include "✔ profiled-1: Create /tmp directory (profile d)"
end
end
end

0 comments on commit 23e6a0c

Please sign in to comment.