Skip to content
This repository has been archived by the owner on Apr 14, 2021. It is now read-only.

Commit

Permalink
Handle invalid RubyGems config files
Browse files Browse the repository at this point in the history
Fixes #4042.
  • Loading branch information
agis committed Oct 27, 2015
1 parent 25a979c commit 412d283
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/bundler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,17 @@ def message
status_code(23)
end

class YAMLSyntaxError < BundlerError
attr_reader :orig_exception

def initialize(orig_exception, msg)
super(msg)
@orig_exception = orig_exception
end

status_code(25)
end

class << self
attr_writer :bundle_path

Expand Down
4 changes: 4 additions & 0 deletions lib/bundler/friendly_errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
module Bundler
def self.with_friendly_errors
yield
rescue YAMLSyntaxError => e
Bundler.ui.error e.message
Bundler.ui.trace e.orig_exception
exit e.status_code
rescue Bundler::Dsl::DSLError => e
Bundler.ui.error e.message
exit e.status_code
Expand Down
3 changes: 3 additions & 0 deletions lib/bundler/rubygems_integration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ def configuration
Bundler.ui.error "#{e.class}: #{e.message}"
Bundler.ui.trace e
raise Gem::SystemExitException
rescue Psych::SyntaxError => e
raise YAMLSyntaxError.new(e, "Your RubyGems configuration file (usually located at ~/.gemrc) " \
"contains invalid YAML syntax and cannot be parsed.")
end

def ruby_engine
Expand Down
26 changes: 26 additions & 0 deletions spec/bundler/friendly_errors_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,32 @@
require "bundler/friendly_errors"

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

after do
FileUtils.rm(Gem.configuration.config_file_name)
end

it "reports a relevant friendly error message" do
gemfile <<-G
source "file://#{gem_repo1}"
gem "rack"
G

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

expect(out).to include("Your RubyGems configuration file")
expect(out).to include("Psych::SyntaxError")
expect(out).not_to include("ERROR REPORT TEMPLATE")
expect(exitstatus).to eq(25) if exitstatus
end
end

it "rescues Thor::AmbiguousTaskError and raises SystemExit" do
expect {
Bundler.with_friendly_errors do
Expand Down

0 comments on commit 412d283

Please sign in to comment.