diff options
author | The Bundler Bot <bot@bundler.io> | 2017-06-01 07:42:05 +0000 |
---|---|---|
committer | The Bundler Bot <bot@bundler.io> | 2017-06-01 07:42:05 +0000 |
commit | d2d669746bc4e7f55c50f730e5fc965874ab1161 (patch) | |
tree | b32304ceeb115cdd70efd330842ea8c63b952fa3 | |
parent | 7f1411cdb3279c25e8e8f2a8e3c1f8acf3dbe8f2 (diff) | |
parent | 5d3b35f51702b20afefce40612910bebfded2fde (diff) | |
download | bundler-d2d669746bc4e7f55c50f730e5fc965874ab1161.tar.gz |
Auto merge of #5690 - bundler:colby/bundler-init-invalid-gemspec, r=segiddins
Fix `bundle init --gemspec` breaking when gemspec is invalid
Also added some specs to init what wasn't being tested.
-rw-r--r-- | lib/bundler/cli/init.rb | 4 | ||||
-rw-r--r-- | spec/commands/init_spec.rb | 70 |
2 files changed, 51 insertions, 23 deletions
diff --git a/lib/bundler/cli/init.rb b/lib/bundler/cli/init.rb index 4bbdee1f48..8ffd1db41a 100644 --- a/lib/bundler/cli/init.rb +++ b/lib/bundler/cli/init.rb @@ -18,7 +18,9 @@ module Bundler Bundler.ui.error "Gem specification #{gemspec} doesn't exist" exit 1 end - spec = Gem::Specification.load(gemspec) + + spec = Bundler.load_gemspec_uncached(gemspec) + 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..6bbbfa8c96 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("There was an error while loading `test.gemspec`") + end + end end end |