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

Commit

Permalink
Auto merge of #5298 - bundler:seg-cli-gem-override, r=indirect
Browse files Browse the repository at this point in the history
[CLI] Dont override Kernel#gem for bundle gem

Should fix #5296

Since `Kernel#require` calls into `Kernel#gem` as an instance method on `self`, overriding the `gem` method can lead to bad things happening

(cherry picked from commit 5135469)
  • Loading branch information
bundlerbot authored and segiddins committed Jan 11, 2017
1 parent 9100676 commit 438c2b1
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
26 changes: 24 additions & 2 deletions lib/bundler/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,8 @@ def viz
Viz.new(options.dup).run
end

old_gem = instance_method(:gem)

desc "gem GEM [OPTIONS]", "Creates a skeleton for creating a rubygem"
method_option :exe, :type => :boolean, :default => false, :aliases => ["--bin", "-b"], :desc => "Generate a binary executable for your library."
method_option :coc, :type => :boolean, :desc => "Generate a code of conduct file. Set a default with `bundle config gem.coc true`."
Expand All @@ -412,10 +414,30 @@ def viz
method_option :test, :type => :string, :lazy_default => "rspec", :aliases => "-t", :banner => "rspec",
:desc => "Generate a test directory for your library, either rspec or minitest. Set a default with `bundle config gem.test rspec`."
def gem(name)
require "bundler/cli/gem"
Gem.new(options, name, self).run
end

commands["gem"].tap do |gem_command|
def gem_command.run(instance, args = [])
arity = 1 # name

require "bundler/cli/gem"
cmd_args = args + [instance]
cmd_args.unshift(instance.options)

cmd = begin
Gem.new(*cmd_args)
rescue ArgumentError => e
instance.class.handle_argument_error(self, e, args, arity)
end

cmd.run
end
end

undef_method(:gem)
define_method(:gem, old_gem)
private :gem

def self.source_root
File.expand_path(File.join(File.dirname(__FILE__), "templates"))
end
Expand Down
8 changes: 8 additions & 0 deletions spec/commands/newgem_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,14 @@ def create_temporary_dir(dir)
bundle "gem 'foo bar'"
expect(out).to end_with("Invalid gem name foo bar -- `Foo bar` is an invalid constant name")
end

it "fails gracefully when multiple names are passed" do
bundle "gem foo bar baz"
expect(out).to eq(<<-E.strip)
ERROR: "bundle gem" was called with arguments ["foo", "bar", "baz"]
Usage: "bundle gem GEM [OPTIONS]"
E
end
end

describe "#ensure_safe_gem_name" do
Expand Down

0 comments on commit 438c2b1

Please sign in to comment.