mirror of
https://github.com/inspec/inspec
synced 2024-11-27 07:00:39 +00:00
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:
parent
5f7e8797d8
commit
d8b06560a1
3 changed files with 32 additions and 10 deletions
|
@ -31,17 +31,14 @@ module Inspec::Plugin::V2::PluginType
|
||||||
runtime_config = Inspec::Config.cached.respond_to?(:final_options) ? Inspec::Config.cached.final_options : {}
|
runtime_config = Inspec::Config.cached.respond_to?(:final_options) ? Inspec::Config.cached.final_options : {}
|
||||||
|
|
||||||
message_truncation = runtime_config[:reporter_message_truncation] || "ALL"
|
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]
|
include_backtrace = runtime_config[:reporter_backtrace_inclusion].nil? ? true : runtime_config[:reporter_backtrace_inclusion]
|
||||||
|
|
||||||
@run_data[:profiles]&.each do |p|
|
@run_data[:profiles]&.each do |p|
|
||||||
p[:controls].each do |c|
|
p[:controls].each do |c|
|
||||||
c[:results]&.map! do |r|
|
c[:results]&.map! do |r|
|
||||||
r.delete(:backtrace) unless include_backtrace
|
r.delete(:backtrace) unless include_backtrace
|
||||||
if r.key?(:message) && r[:message] != "" && trunc > -1
|
process_message_truncation(r)
|
||||||
r[:message] = r[:message][0...trunc] + "[Truncated to #{trunc} characters]"
|
|
||||||
end
|
|
||||||
r
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -64,5 +61,14 @@ module Inspec::Plugin::V2::PluginType
|
||||||
def self.run_data_schema_constraints
|
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."
|
raise NotImplementedError, "#{self.class} must implement a `run_data_schema_constraints` class method to declare its compatibiltity with the RunData API."
|
||||||
end
|
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
|
||||||
end
|
end
|
||||||
|
|
|
@ -14,17 +14,14 @@ module Inspec::Reporters
|
||||||
runtime_config = Inspec::Config.cached.respond_to?(:final_options) ? Inspec::Config.cached.final_options : {}
|
runtime_config = Inspec::Config.cached.respond_to?(:final_options) ? Inspec::Config.cached.final_options : {}
|
||||||
|
|
||||||
message_truncation = runtime_config[:reporter_message_truncation] || "ALL"
|
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]
|
include_backtrace = runtime_config[:reporter_backtrace_inclusion].nil? ? true : runtime_config[:reporter_backtrace_inclusion]
|
||||||
|
|
||||||
@run_data[:profiles]&.each do |p|
|
@run_data[:profiles]&.each do |p|
|
||||||
p[:controls].each do |c|
|
p[:controls].each do |c|
|
||||||
c[:results]&.map! do |r|
|
c[:results]&.map! do |r|
|
||||||
r.delete(:backtrace) unless include_backtrace
|
r.delete(:backtrace) unless include_backtrace
|
||||||
if r.key?(:message) && r[:message] != "" && trunc > -1
|
process_message_truncation(r)
|
||||||
r[:message] = r[:message][0...trunc] + "[Truncated to #{trunc} characters]"
|
|
||||||
end
|
|
||||||
r
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -43,5 +40,14 @@ module Inspec::Reporters
|
||||||
def render
|
def render
|
||||||
raise NotImplementedError, "#{self.class} must implement a `#render` method to format its output."
|
raise NotImplementedError, "#{self.class} must implement a `#render` method to format its output."
|
||||||
end
|
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
|
||||||
end
|
end
|
||||||
|
|
|
@ -299,6 +299,16 @@ describe "inspec exec with json formatter" do
|
||||||
end
|
end
|
||||||
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
|
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(:raw) { inspec("exec " + failure_control + " --reporter json --reporter-message-truncation=ALL --no-create-lockfile").stdout }
|
||||||
let(:json) { JSON.load(raw) }
|
let(:json) { JSON.load(raw) }
|
||||||
|
|
Loading…
Reference in a new issue