Skip to content

Commit e6ce491

Browse files
authored
Ensure flat YAML build report (heroku#1574)
The `bin/report` structure is flat. All keys must map to values that are un-nested. Ruby's `to_yaml` will happily serialize ruby objects: ``` irb(main):004> puts Gem::Version.new("3.4.2").to_yaml --- !ruby/object:Gem::Version version: 3.4.2 ``` This commit fixes this problem by inspecting if the value being serialized is an object or not. If it is, it is then converted into a string.
1 parent c4e930d commit e6ce491

2 files changed

Lines changed: 28 additions & 0 deletions

File tree

lib/heroku_build_report.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,22 @@ def clear!
2727
@path.write("")
2828
end
2929

30+
def complex_object?(value)
31+
value.to_yaml.match?(/!ruby\/object:/)
32+
end
33+
3034
def capture(metrics = {})
3135
metrics.each do |(key, value)|
3236
return if key.nil? || key.to_s.strip.empty?
3337

3438
key = key&.strip
3539
raise "Key cannot be empty (#{key.inspect} => #{value})" if key.nil? || key.empty?
3640

41+
# Don't serialize complex values by accident
42+
if complex_object?(value)
43+
value = value.to_s
44+
end
45+
3746
@data["#{key}"] = value
3847
end
3948

spec/helpers/heroku_build_report_spec.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,25 @@
11
require 'spec_helper'
22

33
describe "Build report" do
4+
it "handles complex object serialization by converting them to strings" do
5+
Dir.mktmpdir do |dir|
6+
path = Pathname(dir).join(".report.yml")
7+
report = HerokuBuildReport::YamlReport.new(
8+
path: path
9+
)
10+
value = Gem::Version.new("3.4.2")
11+
expect(report.complex_object?(value)).to eq(true)
12+
expect(value.to_yaml).to_not eq(value.to_s.to_yaml)
13+
report.capture("key" => value)
14+
15+
expect(report.data).to eq({"key" => "3.4.2"})
16+
expect(path.read).to eq(<<~EOF)
17+
---
18+
key: 3.4.2
19+
EOF
20+
end
21+
end
22+
423
it "writes valid yaml" do
524
Dir.mktmpdir do |dir|
625
path = Pathname(dir).join(".report.yml")

0 commit comments

Comments
 (0)