diff --git a/lib/inspec/plugin/v2/plugin_types/reporter.rb b/lib/inspec/plugin/v2/plugin_types/reporter.rb index 17523fd6e..4f6baf8b3 100644 --- a/lib/inspec/plugin/v2/plugin_types/reporter.rb +++ b/lib/inspec/plugin/v2/plugin_types/reporter.rb @@ -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 diff --git a/lib/inspec/reporters/base.rb b/lib/inspec/reporters/base.rb index c39cbacf9..31bf6cfec 100644 --- a/lib/inspec/reporters/base.rb +++ b/lib/inspec/reporters/base.rb @@ -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 diff --git a/test/functional/inspec_exec_json_test.rb b/test/functional/inspec_exec_json_test.rb index cf9a93241..33c619983 100644 --- a/test/functional/inspec_exec_json_test.rb +++ b/test/functional/inspec_exec_json_test.rb @@ -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) }