Merge pull request #5165 from inspec/ns/messages_should_only_be_noted_as_trunncated_if_they_were_actually_truncated

Ensure truncate message only returns on truncated values
This commit is contained in:
Nick Schwaderer 2020-07-29 11:55:39 +01:00 committed by GitHub
commit 986dd1aafb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
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) }