Skip to content

Commit

Permalink
[rubygems/rubygems] Bundler::YAMLSerializer.load couldn't raise error…
Browse files Browse the repository at this point in the history
… when invalid yaml was provided

rubygems/rubygems@cfcfde04c7
  • Loading branch information
hsbt authored and matzbot committed Apr 19, 2023
1 parent 92ab4e4 commit 8b95b33
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 71 deletions.
61 changes: 28 additions & 33 deletions lib/rubygems/config_file.rb
Expand Up @@ -521,51 +521,46 @@ def self.dump_with_rubygems_yaml(content)
Bundler::YAMLSerializer.dump(content)
end

def self.load_with_rubygems_config_hash(hash)
def self.load_with_rubygems_config_hash(yaml)
require "bundler/yaml_serializer"

content = Bundler::YAMLSerializer.load(hash)

if content.is_a? Hash
content.transform_keys! do |k|
if k.match?(/\A:(.*)\Z/)
k[1..-1].to_sym
elsif k.include?("__")
if k.is_a?(Symbol)
k.to_s.gsub(/__/,".").gsub(%r{/\Z}, "").to_sym
else
k.dup.gsub(/__/,".").gsub(%r{/\Z}, "")
end
content = Bundler::YAMLSerializer.load(yaml)

content.transform_keys! do |k|
if k.match?(/\A:(.*)\Z/)
k[1..-1].to_sym
elsif k.include?("__")
if k.is_a?(Symbol)
k.to_s.gsub(/__/,".").gsub(%r{/\Z}, "").to_sym
else
k
k.dup.gsub(/__/,".").gsub(%r{/\Z}, "")
end
else
k
end
end

content.transform_values! do |v|
if v.is_a?(String)
if v.match?(/\A:(.*)\Z/)
v[1..-1].to_sym
elsif v.match?(/\A[+-]?\d+\Z/)
v.to_i
elsif v.match?(/\Atrue|false\Z/)
v == "true"
elsif v.empty?
nil
else
v
end
elsif v.is_a?(Hash) && v.empty?
content.transform_values! do |v|
if v.is_a?(String)
if v.match?(/\A:(.*)\Z/)
v[1..-1].to_sym
elsif v.match?(/\A[+-]?\d+\Z/)
v.to_i
elsif v.match?(/\Atrue|false\Z/)
v == "true"
elsif v.empty?
nil
else
v
end
elsif v.is_a?(Hash) && v.empty?
nil
else
v
end

content
else
warn "Failed to load #{filename} because it doesn't contain valid YAML hash"
{}
end

content
end

private
Expand Down
23 changes: 0 additions & 23 deletions spec/bundler/bundler/friendly_errors_spec.rb
Expand Up @@ -5,29 +5,6 @@
require "cgi"

RSpec.describe Bundler, "friendly errors" do
context "with invalid YAML in .gemrc" do
before do
File.open(home(".gemrc"), "w") do |f|
f.write "invalid: yaml: hah"
end
end

after do
FileUtils.rm(home(".gemrc"))
end

it "reports a relevant friendly error message" do
gemfile <<-G
source "#{file_uri_for(gem_repo1)}"
gem "rack"
G

bundle :install, :env => { "DEBUG" => "true" }

expect(err).to include("Failed to load #{home(".gemrc")}")
end
end

it "calls log_error in case of exception" do
exception = Exception.new
expect(Bundler::FriendlyErrors).to receive(:exit_status).with(exception).and_return(1)
Expand Down
15 changes: 0 additions & 15 deletions test/rubygems/test_gem_config_file.rb
Expand Up @@ -465,21 +465,6 @@ def test_write_from_hash
assert_equal %w[http://even-more-gems.example.com], Gem.sources
end

def test_ignore_invalid_config_file
File.open @temp_conf, "w" do |fp|
fp.puts "invalid: yaml:"
end

begin
verbose = $VERBOSE
$VERBOSE = nil

util_config_file
ensure
$VERBOSE = verbose
end
end

def test_load_ssl_verify_mode_from_config
File.open @temp_conf, "w" do |fp|
fp.puts ":ssl_verify_mode: 1"
Expand Down

0 comments on commit 8b95b33

Please sign in to comment.