From 0ca221af5b313cf866056a217db88603df54dfdc Mon Sep 17 00:00:00 2001 From: Nikita Mathur Date: Tue, 11 Jul 2023 14:23:16 +0000 Subject: [PATCH] YAML reporter test fix (#6563) * Fix for yaml reporter data, not matching empty values with spaces Signed-off-by: Nikita Mathur * Additional comment to document affecting fields Signed-off-by: Nikita Mathur --------- Signed-off-by: Nikita Mathur --- test/fixtures/reporters/yaml_output_ruby3plus | 38 ------------------- test/unit/reporters/yaml_test.rb | 24 ++++++------ 2 files changed, 13 insertions(+), 49 deletions(-) delete mode 100644 test/fixtures/reporters/yaml_output_ruby3plus diff --git a/test/fixtures/reporters/yaml_output_ruby3plus b/test/fixtures/reporters/yaml_output_ruby3plus deleted file mode 100644 index 11b857c65..000000000 --- a/test/fixtures/reporters/yaml_output_ruby3plus +++ /dev/null @@ -1,38 +0,0 @@ ---- -:platform: - :name: fedora - :release: '28' - :target_id: '' -:profiles: -- :name: tests from t.rb - :sha256: 9260af15d2b7568443df4d9d2556f773f425f92491c97eb1d201c535c7a9f5e0 - :title: tests from t.rb - :supports: [] - :attributes: [] - :groups: - - :id: t.rb - :controls: - - "(generated from t.rb:1 0aa70d93be7b0cf41b97a1363bb5e8b8)" - :controls: - - :id: "(generated from t.rb:1 0aa70d93be7b0cf41b97a1363bb5e8b8)" - :title: - :desc: - :descriptions: [] - :impact: 0.5 - :refs: [] - :tags: {} - :code: '' - :source_location: - :line: 89 - :ref: "/home/frezbo/git/work/ruby/inspec/lib/inspec/control_eval_context.rb" - :waiver_data: {} - :results: - - :status: passed - :code_desc: File /tmp should exist - :run_time: 0.001313935 - :start_time: '2018-05-31T16:22:19+05:30' - :resource_params: '' - :resource_id: '' -:statistics: - :duration: 0.002678506 -:version: 2.1.83 diff --git a/test/unit/reporters/yaml_test.rb b/test/unit/reporters/yaml_test.rb index 7813b6fb2..d73a6175e 100644 --- a/test/unit/reporters/yaml_test.rb +++ b/test/unit/reporters/yaml_test.rb @@ -2,25 +2,27 @@ require "helper" require "inspec/reporters" describe Inspec::Reporters::Yaml do - RUBY3_PLUS = Gem.ruby_version >= Gem::Version.new("3.0") - let(:report) do data = YAML.load_file("test/fixtures/reporters/run_data.yml") Inspec::Reporters::Yaml.new({ run_data: data }) end + # Match yaml reporter data + # Strip leading white spaces from empty scalar values for testing + # In Ruby, depending on its version, empty values are generated with or without leading spaces + # This was affecting the title: and desc: fields + def strip_white_spaces_from_empty_values(output) + output.gsub(/:\s*\n/, ":\n") + end + describe "#render" do it "confirm render output" do - if ( windows? || darwin? ) && RUBY3_PLUS - # On Ruby 3+, empty scalar values are generated without a trailing space - # this affects the title: and desc: fields - output = File.read("test/fixtures/reporters/yaml_output_ruby3plus") - else - output = File.read("test/fixtures/reporters/yaml_output") - end - + output = File.read("test/fixtures/reporters/yaml_output") report.render - _(report.rendered_output).must_equal output + stripped_rendered_output = strip_white_spaces_from_empty_values(report.rendered_output) + stripped_output = strip_white_spaces_from_empty_values(output) + # Match successful parsing of YAML reporter data + _(stripped_rendered_output).must_equal stripped_output end end end