Ensure truncate message only returns on truncated values

At the moment we return the "Truncate" text whenever the setting is
utilized. This PR ensures that we only advise truncation when it's been
executed.

Signed-off-by: Nick Schwaderer <nschwaderer@chef.io>
This commit is contained in:
Nick Schwaderer 2020-07-24 12:44:45 +01:00
parent 5f7e8797d8
commit d8b06560a1
3 changed files with 32 additions and 10 deletions

View file

@ -31,17 +31,14 @@ module Inspec::Plugin::V2::PluginType
runtime_config = Inspec::Config.cached.respond_to?(:final_options) ? Inspec::Config.cached.final_options : {}
message_truncation = runtime_config[:reporter_message_truncation] || "ALL"
trunc = message_truncation == "ALL" ? -1 : message_truncation.to_i
@trunc = message_truncation == "ALL" ? -1 : message_truncation.to_i
include_backtrace = runtime_config[:reporter_backtrace_inclusion].nil? ? true : runtime_config[:reporter_backtrace_inclusion]
@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
process_message_truncation(r)
end
end
end
@ -64,5 +61,14 @@ module Inspec::Plugin::V2::PluginType
def self.run_data_schema_constraints
raise NotImplementedError, "#{self.class} must implement a `run_data_schema_constraints` class method to declare its compatibiltity with the RunData API."
end
private
def process_message_truncation(result)
if result.key?(:message) && result[:message] != "" && @trunc > -1 && result[:message].length > @trunc
result[:message] = result[:message][0...@trunc] + "[Truncated to #{@trunc} characters]"
end
result
end
end
end

View file

@ -14,17 +14,14 @@ module Inspec::Reporters
runtime_config = Inspec::Config.cached.respond_to?(:final_options) ? Inspec::Config.cached.final_options : {}
message_truncation = runtime_config[:reporter_message_truncation] || "ALL"
trunc = message_truncation == "ALL" ? -1 : message_truncation.to_i
@trunc = message_truncation == "ALL" ? -1 : message_truncation.to_i
include_backtrace = runtime_config[:reporter_backtrace_inclusion].nil? ? true : runtime_config[:reporter_backtrace_inclusion]
@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
process_message_truncation(r)
end
end
end
@ -43,5 +40,14 @@ module Inspec::Reporters
def render
raise NotImplementedError, "#{self.class} must implement a `#render` method to format its output."
end
private
def process_message_truncation(result)
if result.key?(:message) && result[:message] != "" && @trunc > -1 && result[:message].length > @trunc
result[:message] = result[:message][0...@trunc] + "[Truncated to #{@trunc} characters]"
end
result
end
end
end

View file

@ -299,6 +299,16 @@ describe "inspec exec with json formatter" do
end
end
describe "JSON reporter with reporter-message-truncation set to a number and working message" do
let(:raw) { inspec("exec " + failure_control + " --reporter json --reporter-message-truncation=10000 --no-create-lockfile").stdout }
let(:json) { JSON.load(raw) }
let(:profile) { json["profiles"][0] }
let(:control_with_message) { profile["controls"].find { |c| c["id"] == "Generates a message" } }
it "does not report a truncated message" do
assert !control_with_message["results"].first["message"].include?("Truncated")
end
end
describe "JSON reporter with reporter-message-truncation set to ALL" do
let(:raw) { inspec("exec " + failure_control + " --reporter json --reporter-message-truncation=ALL --no-create-lockfile").stdout }
let(:json) { JSON.load(raw) }