diff options
author | The Bundler Bot <bot@bundler.io> | 2018-01-30 23:53:07 +0000 |
---|---|---|
committer | The Bundler Bot <bot@bundler.io> | 2018-01-30 23:53:07 +0000 |
commit | c4b022cd8a938f7640ca11be87e25d5461d66331 (patch) | |
tree | aaa06d9757b36a547b639fdeabf0b1f81a4f9d37 | |
parent | d60aeb9a29b7d7670a0b6ef8f2ebf7f64a4b66e6 (diff) | |
parent | 205844f55ba3270083fdfc943d966372e96799a8 (diff) | |
download | bundler-c4b022cd8a938f7640ca11be87e25d5461d66331.tar.gz |
Auto merge of #6266 - nilsding:init-check-dir-writable, r=colby-swandale
[Init] Check if the current directory is writeable
### What was the end-user problem that led to this PR?
The problem was that when running `bundle init` inside a directory which is not writable by the current user (e.g. `/` as demonstrated in #6219) Bundler prints out an `EACCES` error with a huge backtrace. In the mentioned PR @segiddins suggested to print out a better error message. This PR addresses that.
### What was your diagnosis of the problem?
See [this comment on said PR](https://github.com/bundler/bundler/issues/6219#issuecomment-359002919).
### What is your fix for the problem, implemented in this PR?
My fix is simple: adding a check whether the current directory is writeable before trying to create `gems.rb`/`Gemfile`. If that's not the case, print out an error and exit.
### Why did you choose this fix out of the possible options?
I chose this fix because... it was really simple to implement.
-rw-r--r-- | lib/bundler/cli/init.rb | 5 | ||||
-rw-r--r-- | spec/commands/init_spec.rb | 18 |
2 files changed, 23 insertions, 0 deletions
diff --git a/lib/bundler/cli/init.rb b/lib/bundler/cli/init.rb index fa53e7c74b..40df797269 100644 --- a/lib/bundler/cli/init.rb +++ b/lib/bundler/cli/init.rb @@ -13,6 +13,11 @@ module Bundler exit 1 end + unless File.writable?(Dir.pwd) + Bundler.ui.error "Can not create #{gemfile} as the current directory is not writable." + exit 1 + end + if options[:gemspec] gemspec = File.expand_path(options[:gemspec]) unless File.exist?(gemspec) diff --git a/spec/commands/init_spec.rb b/spec/commands/init_spec.rb index c1cd7b90c8..9b5bd95814 100644 --- a/spec/commands/init_spec.rb +++ b/spec/commands/init_spec.rb @@ -64,6 +64,24 @@ RSpec.describe "bundle init" do end end + context "when the dir is not writable by the current user" do + let(:subdir) { "child_dir" } + + it "notifies the user that it can not write to it" do + FileUtils.mkdir bundled_app(subdir) + # chmod a-w it + mode = File.stat(bundled_app(subdir)).mode ^ 0o222 + FileUtils.chmod mode, bundled_app(subdir) + + Dir.chdir bundled_app(subdir) do + bundle :init + end + + expect(out).to include("directory is not writable") + expect(Dir[bundled_app("#{subdir}/*")]).to be_empty + end + end + context "when a gems.rb file exists in a parent directory", :bundler => ">= 2" do let(:subdir) { "child_dir" } |