YAML reporter test fix (#6563)

* Fix for yaml reporter data, not matching empty values with spaces

Signed-off-by: Nikita Mathur <nikita.mathur@chef.io>

* Additional comment to document affecting fields

Signed-off-by: Nikita Mathur <nikita.mathur@chef.io>

---------

Signed-off-by: Nikita Mathur <nikita.mathur@chef.io>
This commit is contained in:
Nikita Mathur 2023-07-11 14:23:16 +00:00 committed by GitHub
parent 39e91eb0b3
commit 0ca221af5b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 49 deletions

View file

@ -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

View file

@ -2,25 +2,27 @@ require "helper"
require "inspec/reporters" require "inspec/reporters"
describe Inspec::Reporters::Yaml do describe Inspec::Reporters::Yaml do
RUBY3_PLUS = Gem.ruby_version >= Gem::Version.new("3.0")
let(:report) do let(:report) do
data = YAML.load_file("test/fixtures/reporters/run_data.yml") data = YAML.load_file("test/fixtures/reporters/run_data.yml")
Inspec::Reporters::Yaml.new({ run_data: data }) Inspec::Reporters::Yaml.new({ run_data: data })
end end
describe "#render" do # Match yaml reporter data
it "confirm render output" do # Strip leading white spaces from empty scalar values for testing
if ( windows? || darwin? ) && RUBY3_PLUS # In Ruby, depending on its version, empty values are generated with or without leading spaces
# On Ruby 3+, empty scalar values are generated without a trailing space # This was affecting the title: and desc: fields
# this affects the title: and desc: fields def strip_white_spaces_from_empty_values(output)
output = File.read("test/fixtures/reporters/yaml_output_ruby3plus") output.gsub(/:\s*\n/, ":\n")
else
output = File.read("test/fixtures/reporters/yaml_output")
end end
describe "#render" do
it "confirm render output" do
output = File.read("test/fixtures/reporters/yaml_output")
report.render 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 end
end end