Skip to content

Commit

Permalink
adopt new json formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
arlimus committed Sep 19, 2016
1 parent 38f2680 commit 6792550
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 24 deletions.
12 changes: 9 additions & 3 deletions lib/inspec/profile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -149,16 +149,22 @@ def info!

def info(res = params.dup)
# add information about the controls
controls = res[:controls].map do |id, rule|
res[:controls] = res[:controls].map do |id, rule|
next if id.to_s.empty?
data = rule.dup
data.delete(:checks)
data[:impact] ||= 0.5
data[:impact] = 1.0 if data[:impact] > 1.0
data[:impact] = 0.0 if data[:impact] < 0.0
[id, data]
data[:id] = id
data
end.compact

# resolve hash structure in groups
res[:groups] = res[:groups].map do |id, group|
group[:id] = id
group
end
res[:controls] = Hash[controls.compact]

# add information about the required attributes
res[:attributes] = res[:attributes].map(&:to_hash) unless res[:attributes].nil? || res[:attributes].empty?
Expand Down
20 changes: 8 additions & 12 deletions lib/inspec/rspec_json_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def add_profile(profile)
def start(_notification)
# Note that the default profile may have no name - therefore
# the hash may have a valid nil => entry.
@profiles_info = Hash[@profiles.map { |x| profile_info(x) }]
@profiles_info = @profiles.map(&:info!).map(&:dup)
end

def dump_one_example(example, control)
Expand Down Expand Up @@ -195,19 +195,14 @@ def tests_summary

private

def profile_info(profile)
info = profile.info!.dup
[info[:name], info]
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) }
profiles.find { |p| profile_contains_example?(p, example) }
end

def profile_contains_example?(profile, example)
Expand All @@ -216,16 +211,17 @@ def profile_contains_example?(profile, example)
# 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])
elsif profile[:controls] && profile[:controls].any? { |x| x[:id] == example[:id] }
true
else
false
end
end

def example2control(example, profiles)
p = example2profile(example, profiles)
p[:controls][example[:id]] if p && p[:controls]
profile = example2profile(example, profiles)
return nil unless profile && profile[:controls]
profile[:controls].find { |x| x[:id] == example[:id] }
end

def format_example(example)
Expand Down Expand Up @@ -292,7 +288,7 @@ def close(_notification) # rubocop:disable Metrics/AbcSize
print_tests
output.puts('')

@profiles_info.each do |_id, profile|
@profiles_info.each do |profile|
next if profile[:already_printed]
@current_profile = profile
next unless print_current_profile
Expand Down Expand Up @@ -458,7 +454,7 @@ def flush_current_control
return if @current_control.nil?

prev_profile = @current_profile
@current_profile = @profiles_info[@current_control[:profile_id]]
@current_profile = @profiles_info.find { |i| i[:id] == @current_control[:profile_id] }
print_current_profile if prev_profile != @current_profile

fails, skips, passes, summary_indicator = current_control_infos
Expand Down
4 changes: 2 additions & 2 deletions test/functional/inspec_exec_json_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
.find { |x| x['id'] =~ /generated from example.rb/ }['id']
groups = actual.delete('groups')
actual.must_equal({
"id" => "profile",
"name" => "profile",
"title" => "InSpec Example Profile",
"maintainer" => "Chef Software, Inc.",
"copyright" => "Chef Software, Inc.",
Expand All @@ -56,9 +56,9 @@
})

groups.sort_by { |x| x['id'] }.must_equal([
{"id"=>"controls/meta.rb", "title"=>"SSH Server Configuration", "controls"=>["ssh-1"]},
{"id"=>"controls/example.rb", "title"=>"/tmp profile", "controls"=>["tmp-1.0", key]},
{"id"=>"controls/gordon.rb", "title"=>"Gordon Config Checks", "controls"=>["gordon-1.0"]},
{"id"=>"controls/meta.rb", "title"=>"SSH Server Configuration", "controls"=>["ssh-1"]},
])
end

Expand Down
4 changes: 2 additions & 2 deletions test/functional/inspec_exec_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
out.stderr.must_equal ''
out.exit_status.must_equal 0
stdout = out.stdout.force_encoding(Encoding::UTF_8)
stdout.must_include "\n\e[32m ✔ ssh-1: Allow only SSH Protocol 2\e[0m\n"
stdout.must_include "\n\e[32m ✔ tmp-1.0: Create /tmp directory\e[0m\n"
stdout.must_include "\e[32m ✔ ssh-1: Allow only SSH Protocol 2\e[0m\n"
stdout.must_include "\e[32m ✔ tmp-1.0: Create /tmp directory\e[0m\n"
stdout.must_include "
\e[37m ○ gordon-1.0: Verify the version number of Gordon (1 skipped)\e[0m
\e[37m ○ Can't find file \"/tmp/gordon/config.yaml\"\e[0m
Expand Down
2 changes: 1 addition & 1 deletion test/unit/dsl/control_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def load(content)
}
opts = { test_collector: Inspec::RunnerMock.new, backend: Inspec::Backend.create({ backend: 'mock' }) }
Inspec::Profile.for_target(data, opts)
.params[:controls].find { |x| x[:id] == '1' }
.params[:controls]['1']
end

it 'works with empty refs' do
Expand Down
8 changes: 4 additions & 4 deletions test/unit/profiles/profile_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
end

it 'has no controls' do
profile.params[:controls].must_equal([])
profile.params[:controls].must_equal({})
end
end

Expand All @@ -29,7 +29,7 @@
end

it 'has no controls' do
profile.params[:controls].must_equal([])
profile.params[:controls].must_equal({})
end
end

Expand All @@ -42,7 +42,7 @@
end

it 'has no controls' do
profile.params[:controls].must_equal([])
profile.params[:controls].must_equal({})
end

it 'can overwrite the profile ID' do
Expand All @@ -60,7 +60,7 @@
end

it 'has no controls' do
profile.params[:controls].must_equal([])
profile.params[:controls].must_equal({})
end
end

Expand Down

0 comments on commit 6792550

Please sign in to comment.