diff options
author | The Bundler Bot <bot@bundler.io> | 2016-11-19 20:25:31 +0000 |
---|---|---|
committer | The Bundler Bot <bot@bundler.io> | 2016-11-19 20:25:31 +0000 |
commit | 4af22f96858a481183f9bfd64378478dd99dbc37 (patch) | |
tree | 462e0bf1a8c38885d370548ff2b8247d6a5d7134 | |
parent | a0baaed77e390991cd7b7dca927bd4cc489e805b (diff) | |
parent | c7e1542bb71a54ce97bc60d43c86892ec1536be0 (diff) | |
download | bundler-4af22f96858a481183f9bfd64378478dd99dbc37.tar.gz |
Auto merge of #5186 - bundler:seg-bundle-gem-invalid-constant, r=indirect
[CLI::Gem] Fail gracefully on an invalid constant name
Closes #5185.
-rw-r--r-- | lib/bundler/cli/gem.rb | 19 | ||||
-rw-r--r-- | spec/commands/newgem_spec.rb | 17 |
2 files changed, 32 insertions, 4 deletions
diff --git a/lib/bundler/cli/gem.rb b/lib/bundler/cli/gem.rb index 4dc0dbdb6b..955989659c 100644 --- a/lib/bundler/cli/gem.rb +++ b/lib/bundler/cli/gem.rb @@ -202,10 +202,23 @@ module Bundler if name =~ /^\d/ Bundler.ui.error "Invalid gem name #{name} Please give a name which does not start with numbers." exit 1 - elsif constant_array.inject(Object) {|c, s| (c.const_defined?(s) && c.const_get(s)) || break } - Bundler.ui.error "Invalid gem name #{name} constant #{constant_array.join("::")} is already in use. Please choose another gem name." - exit 1 end + + constant_name = constant_array.join("::") + + existing_constant = constant_array.inject(Object) do |c, s| + defined = begin + c.const_defined?(s) + rescue NameError + Bundler.ui.error "Invalid gem name #{name} -- `#{constant_name}` is an invalid constant name" + exit 1 + end + (defined && c.const_get(s)) || break + end + + return unless existing_constant + Bundler.ui.error "Invalid gem name #{name} constant #{constant_name} is already in use. Please choose another gem name." + exit 1 end def open_editor(editor, file) diff --git a/spec/commands/newgem_spec.rb b/spec/commands/newgem_spec.rb index 6e80aa7a60..26dd3b4d96 100644 --- a/spec/commands/newgem_spec.rb +++ b/spec/commands/newgem_spec.rb @@ -17,7 +17,7 @@ describe "bundle gem" do end def execute_bundle_gem(gem_name, flag = "", to_remove_push_guard = true) - bundle "gem #{gem_name} #{flag}" + bundle! "gem #{gem_name} #{flag}" remove_push_guard(gem_name) if to_remove_push_guard # reset gemspec cache for each test because of commit 3d4163a Bundler.clear_gemspec_cache @@ -733,6 +733,21 @@ describe "bundle gem" do expect(bundled_app("a--a/a--a.gemspec")).to exist end + + it "fails gracefully with a ." do + bundle "gem foo.gemspec" + expect(out).to end_with("Invalid gem name foo.gemspec -- `Foo.gemspec` is an invalid constant name") + end + + it "fails gracefully with a ^" do + bundle "gem ^" + expect(out).to end_with("Invalid gem name ^ -- `^` is an invalid constant name") + end + + it "fails gracefully with a space" do + bundle "gem 'foo bar'" + expect(out).to end_with("Invalid gem name foo bar -- `Foo bar` is an invalid constant name") + end end describe "#ensure_safe_gem_name" do |