mirror of
https://github.com/inspec/inspec
synced 2025-02-17 06:28:40 +00:00
Move reporter option handling to base
They don't need to be json specific, they should apply to any reporter if the user has chosen these settings. Signed-off-by: James Stocks <jstocks@chef.io>
This commit is contained in:
parent
ae66aee2b2
commit
69fb6e0782
4 changed files with 37 additions and 24 deletions
|
@ -5,11 +5,36 @@ module Inspec::Reporters
|
|||
def initialize(config)
|
||||
@config = config
|
||||
@run_data = config[:run_data]
|
||||
@message_truncation = config[:message_truncation] || 'ALL'
|
||||
@include_backtrace = config[:include_backtrace].nil? ? true : config[:include_backtrace]
|
||||
apply_options_to_run_data
|
||||
@output = ""
|
||||
end
|
||||
|
||||
# Apply options such as message truncation and removal of backtraces
|
||||
def apply_options_to_run_data
|
||||
message_truncation = @config[:message_truncation] || "ALL"
|
||||
trunc = -1
|
||||
if message_truncation != "ALL"
|
||||
if message_truncation.to_i.to_s != message_truncation
|
||||
Inspec::Log.warn("Messages will not be truncated because #{message_truncation} is not an integer value")
|
||||
else
|
||||
trunc = message_truncation.to_i
|
||||
end
|
||||
end
|
||||
include_backtrace = @config[:include_backtrace].nil? ? true : @config[:include_backtrace]
|
||||
|
||||
@run_data[:profiles]&.each do |p|
|
||||
p[:controls].each do |c|
|
||||
c[:results]&.map! do |r|
|
||||
r.delete(:backtrace) unless include_backtrace
|
||||
if r.key?(:message) && r[:message] != "" && trunc > -1
|
||||
r[:message] = r[:message][0...trunc] + "[Truncated to #{trunc} characters]"
|
||||
end
|
||||
r
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def output(str, newline = true)
|
||||
@output << str
|
||||
@output << "\n" if newline
|
||||
|
|
|
@ -29,16 +29,8 @@ module Inspec::Reporters
|
|||
end
|
||||
|
||||
def profile_results(control)
|
||||
trunc = -1
|
||||
if @message_truncation != 'ALL'
|
||||
if @message_truncation.to_i.to_s != @message_truncation
|
||||
Inspec::Log.warn("Messages will not be truncated because #{@message_truncation} is not an integer value")
|
||||
else
|
||||
trunc = @message_truncation.to_i
|
||||
end
|
||||
end
|
||||
(control[:results] || []).map do |r|
|
||||
res = {
|
||||
(control[:results] || []).map { |r|
|
||||
{
|
||||
status: r[:status],
|
||||
code_desc: r[:code_desc],
|
||||
run_time: r[:run_time],
|
||||
|
@ -48,12 +40,8 @@ module Inspec::Reporters
|
|||
message: r[:message],
|
||||
exception: r[:exception],
|
||||
backtrace: r[:backtrace],
|
||||
}
|
||||
res.reject! { |_k, v| v.nil? }
|
||||
res[:message] = res[:message][0...trunc] + "[Truncated to #{trunc} characters]" if res.key?(:message) && res[:message] != "" && trunc > -1
|
||||
res.delete(:backtrace) unless @include_backtrace
|
||||
res
|
||||
end
|
||||
}.reject { |_k, v| v.nil? }
|
||||
}
|
||||
end
|
||||
|
||||
def profiles
|
||||
|
|
|
@ -286,7 +286,7 @@ describe "inspec exec with json formatter" do
|
|||
let(:json) { JSON.load(raw) }
|
||||
let(:profile) { json["profiles"][0] }
|
||||
let(:control_with_exception) { profile["controls"].find { |c| c["id"] == "Raises an exception" } }
|
||||
let(:failed_result) { control_with_exception["results"].find { |r| r['exception'] == 'NoMethodError' } }
|
||||
let(:failed_result) { control_with_exception["results"].find { |r| r["exception"] == "NoMethodError" } }
|
||||
it "reports backtrace by default" do
|
||||
_(failed_result["backtrace"]).wont_be :nil?
|
||||
_(failed_result["backtrace"]).must_be_instance_of Array
|
||||
|
@ -299,7 +299,7 @@ describe "inspec exec with json formatter" do
|
|||
let(:json) { JSON.load(raw) }
|
||||
let(:profile) { json["profiles"][0] }
|
||||
let(:control_with_exception) { profile["controls"].find { |c| c["id"] == "Raises an exception" } }
|
||||
let(:failed_result) { control_with_exception["results"].find { |r| r['exception'] == 'NoMethodError' } }
|
||||
let(:failed_result) { control_with_exception["results"].find { |r| r["exception"] == "NoMethodError" } }
|
||||
it "reports backtrace" do
|
||||
_(failed_result["backtrace"]).wont_be :nil?
|
||||
_(failed_result["backtrace"]).must_be_instance_of Array
|
||||
|
@ -312,7 +312,7 @@ describe "inspec exec with json formatter" do
|
|||
let(:json) { JSON.load(raw) }
|
||||
let(:profile) { json["profiles"][0] }
|
||||
let(:control_with_exception) { profile["controls"].find { |c| c["id"] == "Raises an exception" } }
|
||||
let(:failed_result) { control_with_exception["results"].find { |r| r['exception'] == 'NoMethodError' } }
|
||||
let(:failed_result) { control_with_exception["results"].find { |r| r["exception"] == "NoMethodError" } }
|
||||
it "does not report backtrace" do
|
||||
_(failed_result["backtrace"]).must_be :nil?
|
||||
end
|
||||
|
|
|
@ -281,7 +281,7 @@ Test Summary: 0 successful, 0 failures, 0 skipped
|
|||
let(:out) { inspec("exec " + File.join(profile_path, "failures") + " --no-distinct-exit --no-create-lockfile") }
|
||||
|
||||
it "exits with code 1" do
|
||||
_(stdout).must_include "Profile Summary: 0 successful controls, 2 control failures, 0 controls skipped"
|
||||
_(stdout).must_include "Profile Summary: 0 successful controls, 4 control failures, 0 controls skipped"
|
||||
|
||||
_(stderr).must_equal ""
|
||||
|
||||
|
@ -366,7 +366,7 @@ Test Summary: 2 successful, 0 failures, 0 skipped\n"
|
|||
it "should print all the results" do
|
||||
_(stdout).must_include "× tmp-1.0: Create / directory (1 failed)"
|
||||
_(stdout).must_include "× is expected not to be directory\n"
|
||||
_(stdout).must_include "× undefined method `should_nota'"
|
||||
_(stdout).must_include "× File / \n undefined method `should_nota'"
|
||||
_(stdout).must_include "× is expected not to be directory\n expected `File /.directory?` to return false, got true"
|
||||
_(stdout).must_include "× 7 is expected to cmp >= 9\n"
|
||||
_(stdout).must_include "× 7 is expected not to cmp == /^\\d$/\n"
|
||||
|
@ -382,7 +382,7 @@ Test Summary: 2 successful, 0 failures, 0 skipped\n"
|
|||
it "should print all the results" do
|
||||
_(stdout).must_include "× tmp-1.0: Create / directory (1 failed)"
|
||||
_(stdout).must_include "× cmp-1.0: Using the cmp matcher for numbers (2 failed)"
|
||||
_(stdout).must_include "× undefined method `should_nota'"
|
||||
_(stdout).must_include "× File / \n undefined method `should_nota'"
|
||||
_(stdout).must_include "× is expected not to be directory\n expected `File /.directory?` to return false, got true"
|
||||
_(stdout).must_include "✔ profiled-1: Create / directory (profile d)"
|
||||
end
|
||||
|
|
Loading…
Add table
Reference in a new issue