summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAkira Matsuda <ronnie@dio.jp>2015-04-29 14:04:51 +0900
committerAndre Arko <andre@arko.net>2015-04-30 09:57:04 -0700
commit65a4a0ea41a4e17c0a6e288939a4f314449cf741 (patch)
treee6fe30791b5c3a5a190d2c8c9333b12f073410c4
parentab9c5cbf3e8b8e4b1cab5f7e6c413bc6ff84e530 (diff)
downloadbundler-65a4a0ea41a4e17c0a6e288939a4f314449cf741.tar.gz
A gem extending an existing const should be creatable
Since 17a4fc47bfad02de553e5a53b00ad38b4c905e18, any gem name starting with an existing const name has been regarded as "Invalid gem name" by `bundle gem` command. However, "gem-foo" or "rails-bar" should be totally valid and rather preferable naming for plugins for RubyGems/Rails. This commit changes the rule to raise error only when the new gem name fully matches an existing const name.
-rw-r--r--lib/bundler/cli/gem.rb4
-rw-r--r--spec/commands/newgem_spec.rb29
2 files changed, 31 insertions, 2 deletions
diff --git a/lib/bundler/cli/gem.rb b/lib/bundler/cli/gem.rb
index c1d9d84274..1d65f4480a 100644
--- a/lib/bundler/cli/gem.rb
+++ b/lib/bundler/cli/gem.rb
@@ -21,8 +21,8 @@ module Bundler
underscored_name = name.tr('-', '_')
namespaced_path = name.tr('-', '/')
constant_name = name.gsub(/-[_-]*(?![_-]|$)/){ '::' }.gsub(/([_-]+|(::)|^)(.|$)/){ $2.to_s + $3.upcase }
-
constant_array = constant_name.split('::')
+
git_user_name = `git config user.name`.chomp
git_user_email = `git config user.email`.chomp
@@ -185,7 +185,7 @@ 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 Object.const_defined?(constant_array.first)
+ 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
diff --git a/spec/commands/newgem_spec.rb b/spec/commands/newgem_spec.rb
index e3050cf7a6..ae2947264a 100644
--- a/spec/commands/newgem_spec.rb
+++ b/spec/commands/newgem_spec.rb
@@ -603,6 +603,35 @@ describe "bundle gem" do
end
end
+ describe "#ensure_safe_gem_name" do
+ before do
+ bundle "gem #{subject}"
+ end
+ after do
+ Bundler.clear_gemspec_cache
+ end
+
+ context "with an existing const name" do
+ subject { "gem" }
+ it { expect(out).to include("Invalid gem name #{subject}") }
+ end
+
+ context "with an existing hyphenated const name" do
+ subject { "gem-specification" }
+ it { expect(out).to include("Invalid gem name #{subject}") }
+ end
+
+ context "starting with an existing const name" do
+ subject { "gem-somenewconstantname" }
+ it { expect(out).not_to include("Invalid gem name #{subject}") }
+ end
+
+ context "ending with an existing const name" do
+ subject { "somenewconstantname-gem" }
+ it { expect(out).not_to include("Invalid gem name #{subject}") }
+ end
+ end
+
context "on first run" do
before do
in_app_root