Skip to content
This repository has been archived by the owner on Apr 14, 2021. It is now read-only.

Commit

Permalink
Auto merge of #6207 - bundler:colby/init-gemfile-path-child, r=segiddins
Browse files Browse the repository at this point in the history
Let users generate gemfiles inside paths whos parent contains a Gemfile

### What was the end-user problem that led to this PR?

Users are unable to generate a new Gemfile with `bundle init` if there is a Gemfile in any
parent folder.

See #6205

### What is your fix for the problem, implemented in this PR?

Don't use `SharedHelpers.default_gemfile` as that searches up the paths for a Gemfile. Instead only check the current directory.
  • Loading branch information
bundlerbot committed Dec 11, 2017
2 parents 6bb5666 + cbf30a8 commit 8de8086
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 32 deletions.
6 changes: 1 addition & 5 deletions lib/bundler/cli/init.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,7 @@ def run
private

def gemfile
@gemfile ||= begin
Bundler.default_gemfile
rescue GemfileNotFound
Bundler.feature_flag.init_gems_rb? ? "gems.rb" : "Gemfile"
end
@gemfile ||= Bundler.feature_flag.init_gems_rb? ? "gems.rb" : "Gemfile"
end
end
end
66 changes: 39 additions & 27 deletions spec/commands/init_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
expect(bundled_app("gems.rb")).to be_file
end

context "when a Gemfile already exists" do
context "when a Gemfile already exists", :bundler => "< 2" do
before do
gemfile <<-G
create_file "Gemfile", <<-G
gem "rails"
G
end
Expand All @@ -30,14 +30,14 @@
end
end

context "when a gems.rb already exists" do
context "when gems.rb already exists", :bundler => ">= 2" do
before do
create_file "gems.rb", <<-G
create_file("gems.rb", <<-G)
gem "rails"
G
end

it "does not change existing gem.rb files" do
it "does not change existing Gemfiles" do
expect { bundle :init }.not_to change { File.read(bundled_app("gems.rb")) }
end

Expand All @@ -47,6 +47,40 @@
end
end

context "when a Gemfile exists in a parent directory", :bundler => "< 2" do
let(:subdir) { "child_dir" }

it "lets users generate a Gemfile in a child directory" do
bundle! :init

FileUtils.mkdir bundled_app(subdir)

Dir.chdir bundled_app(subdir) do
bundle! :init
end

expect(out).to include("Writing new Gemfile")
expect(bundled_app("#{subdir}/Gemfile")).to be_file
end
end

context "when a gems.rb file exists in a parent directory", :bundler => ">= 2" do
let(:subdir) { "child_dir" }

it "lets users generate a Gemfile in a child directory" do
bundle! :init

FileUtils.mkdir bundled_app(subdir)

Dir.chdir bundled_app(subdir) do
bundle! :init
end

expect(out).to include("Writing new gems.rb")
expect(bundled_app("#{subdir}/gems.rb")).to be_file
end
end

context "given --gemspec option", :bundler => "< 2" do
let(:spec_file) { tmp.join("test.gemspec") }

Expand Down Expand Up @@ -94,28 +128,6 @@
context "when init_gems_rb setting is enabled" do
before { bundle "config init_gems_rb true" }

it "generates a gems.rb file" do
bundle :init
expect(bundled_app("gems.rb")).to exist
end

context "when gems.rb already exists" do
before do
create_file("gems.rb", <<-G)
gem "rails"
G
end

it "does not change existing Gemfiles" do
expect { bundle :init }.not_to change { File.read(bundled_app("gems.rb")) }
end

it "notifies the user that an existing gems.rb already exists" do
bundle :init
expect(out).to include("gems.rb already exists")
end
end

context "given --gemspec option", :bundler => "< 2" do
let(:spec_file) { tmp.join("test.gemspec") }

Expand Down

0 comments on commit 8de8086

Please sign in to comment.