summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorColby Swandale <colby@taplaboratories.com>2017-05-31 23:35:38 +1000
committerColby Swandale <colby@taplaboratories.com>2017-06-01 01:01:43 +1000
commitfab4838fa2680d32f3591e00ac97677d8382a517 (patch)
tree7daae22dc7598af9fabd2be8ea9f623d6139da92
parentbf26b5515926c51008f2fe9e9d0054260d482e74 (diff)
downloadbundler-fab4838fa2680d32f3591e00ac97677d8382a517.tar.gz
handle invalid gemspec and add specs for untested behaviour in bundle init --gemspec
-rw-r--r--lib/bundler/cli/init.rb6
-rw-r--r--spec/commands/init_spec.rb70
2 files changed, 54 insertions, 22 deletions
diff --git a/lib/bundler/cli/init.rb b/lib/bundler/cli/init.rb
index 4bbdee1f48..774736f708 100644
--- a/lib/bundler/cli/init.rb
+++ b/lib/bundler/cli/init.rb
@@ -18,7 +18,13 @@ module Bundler
Bundler.ui.error "Gem specification #{gemspec} doesn't exist"
exit 1
end
+
spec = Gem::Specification.load(gemspec)
+ unless spec
+ Bundler.ui.error "Gem specification #{gemspec} is invalid"
+ exit 1
+ end
+
puts "Writing new Gemfile to #{SharedHelpers.pwd}/Gemfile"
File.open("Gemfile", "wb") do |file|
file << "# Generated from #{gemspec}\n"
diff --git a/spec/commands/init_spec.rb b/spec/commands/init_spec.rb
index fbe03a78fa..c2477820a1 100644
--- a/spec/commands/init_spec.rb
+++ b/spec/commands/init_spec.rb
@@ -6,34 +6,60 @@ RSpec.describe "bundle init" do
expect(bundled_app("Gemfile")).to exist
end
- it "does not change existing Gemfiles" do
- gemfile <<-G
- gem "rails"
- G
+ context "when a Gemfile already exists" do
+ before do
+ gemfile <<-G
+ gem "rails"
+ G
+ end
+
+ it "does not change existing Gemfiles" do
+ expect { bundle :init }.not_to change { File.read(bundled_app("Gemfile")) }
+ end
- expect do
+ it "notifies the user that an existing Gemfile already exists" do
bundle :init
- end.not_to change { File.read(bundled_app("Gemfile")) }
+ expect(out).to include("Gemfile already exists")
+ end
end
- it "should generate from an existing gemspec" do
- spec_file = tmp.join("test.gemspec")
- File.open(spec_file, "w") do |file|
- file << <<-S
- Gem::Specification.new do |s|
- s.name = 'test'
- s.add_dependency 'rack', '= 1.0.1'
- s.add_development_dependency 'rspec', '1.2'
- end
- S
+ context "given --gemspec option" do
+ let(:spec_file) { tmp.join("test.gemspec") }
+
+ it "should generate from an existing gemspec" do
+ File.open(spec_file, "w") do |file|
+ file << <<-S
+ Gem::Specification.new do |s|
+ s.name = 'test'
+ s.add_dependency 'rack', '= 1.0.1'
+ s.add_development_dependency 'rspec', '1.2'
+ end
+ S
+ end
+
+ bundle :init, :gemspec => spec_file
+
+ gemfile = bundled_app("Gemfile").read
+ expect(gemfile).to match(%r{source 'https://rubygems.org'})
+ expect(gemfile.scan(/gem "rack", "= 1.0.1"/).size).to eq(1)
+ expect(gemfile.scan(/gem "rspec", "= 1.2"/).size).to eq(1)
+ expect(gemfile.scan(/group :development/).size).to eq(1)
end
- bundle :init, :gemspec => spec_file
+ context "when gemspec file is invalid" do
+ it "notifies the user that specification is invalid" do
+ File.open(spec_file, "w") do |file|
+ file << <<-S
+ Gem::Specification.new do |s|
+ s.name = 'test'
+ s.invalid_method_name
+ end
+ S
+ end
- gemfile = bundled_app("Gemfile").read
- expect(gemfile).to match(%r{source 'https://rubygems.org'})
- expect(gemfile.scan(/gem "rack", "= 1.0.1"/).size).to eq(1)
- expect(gemfile.scan(/gem "rspec", "= 1.2"/).size).to eq(1)
- expect(gemfile.scan(/group :development/).size).to eq(1)
+ bundle :init, :gemspec => spec_file
+ expect(out).to include("Gem specification #{spec_file} is invalid")
+ end
+ end
end
end