Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Count controls in the summary output. Fix #852 #860

Merged
merged 2 commits into from
Aug 5, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion lib/inspec/rspec_json_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def format_example(example)
end

class InspecRspecJson < InspecRspecMiniJson
RSpec::Core::Formatters.register self, :start, :stop
RSpec::Core::Formatters.register self, :start, :stop, :dump_summary
attr_writer :backend

def initialize(*args)
Expand Down Expand Up @@ -133,6 +133,35 @@ def stop(notification)
@output_hash[:other_checks] = missing
end

def dump_summary(summary)
super(summary)
total = 0
failed = 0
skipped = 0
passed = 0

@profiles_info.each do |_name, profile|
total += profile[:controls].length
profile[:controls].each do |_control_name, control|
next unless control[:results]
if control[:results].any? { |r| r[:status] == 'failed' }
failed += 1
elsif control[:results].any? { |r| r[:status] == 'skipped' }
skipped += 1
else
passed += 1
end
end
end

@output_hash[:control_summary] = {
total: total,
failed: failed,
skipped: skipped,
passed: passed,
}
end

private

def profile_info(profile)
Expand Down
9 changes: 9 additions & 0 deletions test/functional/inspec_exec_json_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

describe 'execute a profile with json formatting' do
let(:json) { JSON.load(inspec('exec ' + example_profile + ' --format json').stdout) }
let(:control_summary) { json['control_summary'] }
let(:profile) { json['profiles']['profile'] }
let(:controls) { profile['controls'] }
let(:ex1) { controls['tmp-1.0'] }
Expand Down Expand Up @@ -60,6 +61,7 @@

it 'must have 4 controls' do
controls.length.must_equal 4
control_summary['total'].must_equal 4
end

it 'has an id for every control' do
Expand Down Expand Up @@ -107,6 +109,13 @@
"code" => "control \"tmp-1.0\" do # A unique ID for this control\n impact 0.7 # The criticality, if this control fails.\n title \"Create /tmp directory\" # A human-readable title\n desc \"An optional description...\" # Describe why this is needed\n tag data: \"temp data\" # A tag allows you to associate key information\n tag \"security\" # to the test\n ref \"Document A-12\", url: 'http://...' # Additional references\n\n describe file('/tmp') do # The actual test\n it { should be_directory }\n end\nend\n",
})
end

it 'must report 3 passing controls' do
control_summary['passed'].must_equal 3
end
it 'must report 1 skipped control' do
control_summary['skipped'].must_equal 1
end
end

describe 'with a profile that is not supported on this OS/platform' do
Expand Down