[rubygems/rubygems] Handle empty array

https://github.com/rubygems/rubygems/commit/7c0afdd9af
This commit is contained in:
Hiroshi SHIBATA 2023-10-24 16:22:58 +09:00 committed by git
parent c86c6a84f5
commit 5c4978c11c
3 changed files with 38 additions and 2 deletions

View File

@ -17,7 +17,11 @@ module Bundler
if v.is_a?(Hash)
yaml << dump_hash(v).gsub(/^(?!$)/, " ") # indent all non-empty lines
elsif v.is_a?(Array) # Expected to be array of strings
yaml << "\n- " << v.map {|s| s.to_s.gsub(/\s+/, " ").inspect }.join("\n- ") << "\n"
if v.empty?
yaml << " []\n"
else
yaml << "\n- " << v.map {|s| s.to_s.gsub(/\s+/, " ").inspect }.join("\n- ") << "\n"
end
else
yaml << " " << v.to_s.gsub(/\s+/, " ").inspect << "\n"
end
@ -63,6 +67,7 @@ module Bundler
last_empty_key = key
last_hash = stack[depth]
else
val = [] if val == "[]" # empty array
stack[depth][key] = val
end
elsif match = ARRAY_REGEX.match(line)

View File

@ -17,7 +17,11 @@ module Gem
if v.is_a?(Hash)
yaml << dump_hash(v).gsub(/^(?!$)/, " ") # indent all non-empty lines
elsif v.is_a?(Array) # Expected to be array of strings
yaml << "\n- " << v.map {|s| s.to_s.gsub(/\s+/, " ").inspect }.join("\n- ") << "\n"
if v.empty?
yaml << " []\n"
else
yaml << "\n- " << v.map {|s| s.to_s.gsub(/\s+/, " ").inspect }.join("\n- ") << "\n"
end
else
yaml << " " << v.to_s.gsub(/\s+/, " ").inspect << "\n"
end
@ -63,6 +67,7 @@ module Gem
last_empty_key = key
last_hash = stack[depth]
else
val = [] if val == "[]" # empty array
stack[depth][key] = val
end
elsif match = ARRAY_REGEX.match(line)

View File

@ -57,6 +57,19 @@ RSpec.describe Bundler::YAMLSerializer do
expect(serializer.dump(hash)).to eq(expected)
end
it "handles empty array" do
hash = {
"empty_array" => [],
}
expected = <<~YAML
---
empty_array: []
YAML
expect(serializer.dump(hash)).to eq(expected)
end
end
describe "#load" do
@ -148,6 +161,19 @@ RSpec.describe Bundler::YAMLSerializer do
expect(serializer.load(yaml)).to eq(hash)
end
it "handles empty array" do
yaml = <<~YAML
---
empty_array: []
YAML
hash = {
"empty_array" => [],
}
expect(serializer.load(yaml)).to eq(hash)
end
end
describe "against yaml lib" do