diff options
author | The Bundler Bot <bot@bundler.io> | 2017-01-04 02:17:57 +0000 |
---|---|---|
committer | The Bundler Bot <bot@bundler.io> | 2017-01-04 02:17:57 +0000 |
commit | 513546978ab741fcba527bcc428425dce76b1e74 (patch) | |
tree | 21275a51a25e01c0c0030950a9543e80fa53bb90 | |
parent | 84494276118b76840009bf8d37d98692762609e6 (diff) | |
parent | 3436818106a6955628dbeb33cda47f64ef309782 (diff) | |
download | bundler-513546978ab741fcba527bcc428425dce76b1e74.tar.gz |
Auto merge of #5298 - bundler:seg-cli-gem-override, r=indirect
[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
-rw-r--r-- | lib/bundler/cli.rb | 26 | ||||
-rw-r--r-- | spec/commands/newgem_spec.rb | 8 |
2 files changed, 32 insertions, 2 deletions
diff --git a/lib/bundler/cli.rb b/lib/bundler/cli.rb index ba6a58db1d..eb9362a500 100644 --- a/lib/bundler/cli.rb +++ b/lib/bundler/cli.rb @@ -401,6 +401,8 @@ module Bundler 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`." @@ -412,10 +414,30 @@ module Bundler 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 diff --git a/spec/commands/newgem_spec.rb b/spec/commands/newgem_spec.rb index 26dd3b4d96..766def1644 100644 --- a/spec/commands/newgem_spec.rb +++ b/spec/commands/newgem_spec.rb @@ -748,6 +748,14 @@ describe "bundle gem" do 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 |