diff --git a/lib/bundler/cli.rb b/lib/bundler/cli.rb index 75cceee80da..6c6a150dbd0 100644 --- a/lib/bundler/cli.rb +++ b/lib/bundler/cli.rb @@ -14,6 +14,7 @@ def self.start(*) raise e ensure Bundler::SharedHelpers.print_major_deprecations! + Bundler::SharedHelpers.check_home_dir_permissions end def self.dispatch(*) diff --git a/lib/bundler/shared_helpers.rb b/lib/bundler/shared_helpers.rb index 69543356a2e..86e6bc50436 100644 --- a/lib/bundler/shared_helpers.rb +++ b/lib/bundler/shared_helpers.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true require "pathname" require "rubygems" +require "etc" require "bundler/constants" require "bundler/rubygems_integration" @@ -136,6 +137,14 @@ def print_major_deprecations! major_deprecation("Bundler will only support rubygems >= 2.0, you are running #{Bundler.rubygems.version}") end + def check_home_dir_permissions + message = "There was error while trying to use your home path:" + home_path = File.expand_path(Dir.home(Etc.getlogin)) + message += "\n * Your home directory #{home_path} is not writable" unless File.writable?(home_path) + message += "\n * Your home directory #{home_path} is not a directory" unless File.directory?(home_path) + raise PathError, message unless File.writable?(home_path) && File.directory?(home_path) + end + private def find_gemfile diff --git a/spec/bundler/shared_helpers_spec.rb b/spec/bundler/shared_helpers_spec.rb index 4c0d61cf0a5..d98242a5e8b 100644 --- a/spec/bundler/shared_helpers_spec.rb +++ b/spec/bundler/shared_helpers_spec.rb @@ -412,4 +412,16 @@ module TargetNamespace end end end + + describe "#check_home_dir_permissions" do + it "raises a PathError when directory is not writable" do + allow(File).to receive(:writable?).with(File.expand_path(Dir.home(Etc.getlogin))).and_return(false) + expect { subject.check_home_dir_permissions }.to raise_error(Bundler::PathError, /is not writable/) + end + + it "raise a PathError when directory doesn't exists" do + allow(File).to receive(:directory?).with(File.expand_path(Dir.home(Etc.getlogin))).and_return(false) + expect { subject.check_home_dir_permissions }.to raise_error(Bundler::PathError, /is not a directory/) + end + end end