Add option to suppress diff output

Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com>
This commit is contained in:
Clinton Wolfe 2020-05-28 13:58:21 -04:00
parent ceca77e118
commit 3aca12d622
5 changed files with 89 additions and 0 deletions

View file

@ -158,6 +158,8 @@ module Inspec
option :silence_deprecations, type: :array,
banner: "[all]|[GROUP GROUP...]",
desc: "Suppress deprecation warnings. See install_dir/etc/deprecations.json for list of GROUPs or use 'all'."
option :diff, type: :boolean, default: true,
desc: "Use --no-diff to suppress 'diff' output of failed textual test results."
end
def self.format_platform_info(params: {}, indent: 0, color: 39)

View file

@ -14,6 +14,7 @@ module Inspec
@config[:runtime_config] = Inspec::Config.cached || {}
apply_report_resize_options
redact_sensitive_inputs
suppress_diff_output
end
# Apply options such as message truncation and removal of backtraces
@ -45,6 +46,24 @@ module Inspec
end
end
# Optionally suppress diff output in the message field
def suppress_diff_output
return if @config[:runtime_config][:diff]
@run_data[:profiles]&.each do |p|
p[:controls]&.each do |c|
c[:results]&.each do |r|
next unless r[:message] # :message only set on failure
pos = r[:message].index("\n\nDiff:")
next unless pos # Only textual tests get Diffs
r[:message] = r[:message].slice(0, pos)
end
end
end
end
private
def process_message_truncation(result)

View file

@ -0,0 +1,30 @@
control "no-diff" do
expected = 1 + 1
actual = 2
describe expected do
it { should eq actual }
end
end
control "text-diff" do
expected = <<~EOT
I am the very
model of a modern
major general
EOT
# No one expects this
actual = <<~EOT
The Spanish Inquisition
EOT
describe expected do
it { should eq actual }
end
end
control "array-diff" do
expected = [1,2,3,4]
actual = [5,6,7]
describe expected do
it { should eq actual }
end
end

View file

@ -0,0 +1,10 @@
name: diff-output
title: InSpec Profile
maintainer: The Authors
copyright: The Authors
copyright_email: you@example.com
license: Apache-2.0
summary: An InSpec Compliance Profile
version: 0.1.0
supports:
platform: os

View file

@ -356,4 +356,32 @@ describe "inspec exec with json formatter" do
_(failed_result["backtrace"]).must_be :nil?
end
end
describe "JSON reporter using the --diff/--no-diff options" do
describe "JSON reporter with --diff option" do
let(:run_result) { run_inspec_process("exec #{profile_path}/diff-output --diff", json: true) }
let(:controls) { @json["profiles"][0]["controls"] }
it "runs normally with --diff" do
_(run_result.stderr).must_be_empty
_(controls[1]["results"][0]["message"]).must_include "got:"
_(controls[1]["results"][0]["message"]).must_include "Diff:"
_(controls[2]["results"][0]["message"]).must_include "got:"
assert_exit_code(100, run_result)
end
end
describe "JSON reporter with --no-diff option" do
let(:run_result) { run_inspec_process("exec #{profile_path}/diff-output --no-diff", json: true) }
let(:controls) { @json["profiles"][0]["controls"] }
it "suppresses the diff" do
_(run_result.stderr).must_be_empty
_(controls[1]["results"][0]["message"]).must_include "got:"
_(controls[1]["results"][0]["message"]).wont_include "Diff:"
_(controls[1]["results"][0]["message"]).wont_include "vegetable"
_(controls[2]["results"][0]["message"]).must_include "got:" # non-textual tests don't do diffs
assert_exit_code(100, run_result)
end
end
end
end