Merge pull request #794 from chef/dr/multi-profile-cli

multi-profile reporting in cli formatter
This commit is contained in:
Christoph Hartmann 2016-06-16 11:30:15 +02:00 committed by GitHub
commit 34adbd5b2e
2 changed files with 74 additions and 38 deletions

View file

@ -162,9 +162,9 @@ class InspecRspecCli < InspecRspecJson # rubocop:disable Metrics/ClassLength
}.freeze
TEST_INDICATORS = {
'failed' => ' fail: ',
'skipped' => ' skip: ',
'empty' => ' ',
'failed' => ' fail: ',
'skipped' => ' skip: ',
'empty' => ' ',
}.freeze
def initialize(*args)
@ -174,32 +174,30 @@ class InspecRspecCli < InspecRspecJson # rubocop:disable Metrics/ClassLength
@format = '%color%indicator%id%summary'
@current_control = nil
@current_profile = nil
@missing_controls = []
super(*args)
end
def start(_notification)
output.puts ''
@profiles_info ||= Hash[@profiles.map { |x| profile_info(x) }]
profiles = @profiles_info.values.find_all { |x| !x[:title].nil? || !x[:name].nil? }
profiles.each do |profile|
if profile[:title].nil?
output.puts "Profile: #{profile[:name] || 'unknown'}"
else
output.puts "Profile: #{profile[:title]} (#{profile[:name] || 'unknown'})"
end
output.puts 'Version: ' + (profile[:version] || 'unknown')
output.puts ''
end
output.puts '' unless profiles.empty?
end
def close(_notification)
flush_current_control
output.puts('') unless @current_control.nil?
@profiles_info.each do |_id, profile|
next if profile[:already_printed]
@current_profile = profile
next unless print_current_profile
print_line(
color: '', indicator: @indicators['empty'], id: '', profile: '',
summary: 'No tests executed.'
)
output.puts('')
end
output.puts '' unless @current_control.nil?
res = @output_hash[:summary]
passed = res[:example_count] - res[:failure_count] - res[:skip_count]
s = format('Summary: %3d successful %3d failures %3d skipped',
@ -263,8 +261,35 @@ class InspecRspecCli < InspecRspecJson # rubocop:disable Metrics/ClassLength
end + @colors['reset']
end
def print_line(fields)
output.puts(format_line(fields))
end
def format_lines(lines, indentation)
lines.gsub(/\n/, "\n" + indentation)
end
def print_fails_and_skips(all, color)
all.each do |x|
indicator = @test_indicators[x[:status]]
indicator = @test_indicators['empty'] if all.length == 1 || indicator.nil?
msg = x[:message] || x[:skip_message] || x[:code_desc]
print_line(
color: color,
indicator: indicator,
summary: format_lines(msg, @test_indicators['empty']),
id: nil, profile: nil
)
end
end
def flush_current_control
return if @current_control.nil?
prev_profile = @current_profile
@current_profile = @profiles_info[@current_control[:profile_id]]
print_current_profile if prev_profile != @current_profile
fails, skips, summary_indicator = current_control_infos
summary = current_control_summary(fails, skips)
@ -272,27 +297,34 @@ class InspecRspecCli < InspecRspecJson # rubocop:disable Metrics/ClassLength
control_id += ': '
control_id = '' if control_id.start_with? '(generated from '
f = format_line(
print_line(
color: @colors[summary_indicator] || '',
indicator: @indicators[summary_indicator] || @indicators['unknown'],
summary: summary,
summary: format_lines(summary, @indicators['empty']),
id: control_id,
profile: @current_control[:profile_id],
)
output.puts(f)
all_lines = (fails + skips)
all_lines.each do |x|
indicator = @test_indicators[x[:status]]
indicator = @test_indicators['empty'] if all_lines.length == 1 || indicator.nil?
f = format_line(
color: @colors[summary_indicator] || '',
indicator: indicator,
summary: x[:message] || x[:skip_message] || x[:code_desc],
id: nil, profile: nil
)
output.puts(f)
print_fails_and_skips(fails + skips, @colors[summary_indicator] || '')
end
def print_current_profile
profile = @current_profile
return false if profile.nil?
output.puts ''
profile[:already_printed] = true
return true if profile[:name].nil?
if profile[:title].nil?
output.puts "Profile: #{profile[:name] || 'unknown'}"
else
output.puts "Profile: #{profile[:title]} (#{profile[:name] || 'unknown'})"
end
output.puts 'Version: ' + (profile[:version] || 'unknown')
output.puts ''
true
end
def format_example(example)

View file

@ -14,7 +14,10 @@ describe 'inspec exec' do
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 "\n\e[37m ○ gordon-1.0: Verify the version number of Gordon (1 skipped)\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
"
stdout.must_include "\nSummary: 4 successful 0 failures 1 skipped\n"
end
@ -26,6 +29,7 @@ describe 'inspec exec' do
Profile: yumyum profile
Version: unknown
No tests executed.\e[0m
Summary: 0 successful 0 failures 0 skipped
"
@ -39,6 +43,7 @@ Summary: 0 successful 0 failures 0 skipped
Profile: title (name)
Version: 1.2.3
No tests executed.\e[0m
Summary: 0 successful 0 failures 0 skipped
"
@ -52,11 +57,10 @@ Summary: 0 successful 0 failures 0 skipped
\e[32m working should eq \"working\"\e[0m
\e[37m skippy This will be skipped intentionally.\e[0m
\e[31m failing should eq \"as intended\"
expected: \"as intended\"
got: \"failing\"
(compared using ==)
\e[0m
expected: \"as intended\"
got: \"failing\"
\n (compared using ==)
\e[0m
Summary: 1 successful 1 failures 1 skipped
"