summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSamuel Giddins <segiddins@segiddins.me>2016-11-18 21:40:05 -0600
committerSamuel Giddins <segiddins@segiddins.me>2016-11-19 11:21:49 -0600
commitc7e1542bb71a54ce97bc60d43c86892ec1536be0 (patch)
tree3457e3649480cdc2b80b7b45f450d5d709387fb9
parentec3cacbf870a2d9d9109359063e0dcd94d45732c (diff)
downloadbundler-seg-bundle-gem-invalid-constant.tar.gz
[CLI::Gem] Fail gracefully on an invalid constant nameseg-bundle-gem-invalid-constant
-rw-r--r--lib/bundler/cli/gem.rb19
-rw-r--r--spec/commands/newgem_spec.rb15
2 files changed, 31 insertions, 3 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 8d14bc4119..26dd3b4d96 100644
--- a/spec/commands/newgem_spec.rb
+++ b/spec/commands/newgem_spec.rb
@@ -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