diff --git a/lib/bundler/shared_helpers.rb b/lib/bundler/shared_helpers.rb index 6f22bffc009..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" @@ -137,7 +138,11 @@ def print_major_deprecations! end def check_home_dir_permissions - raise PathError, "There was an error while trying to use your home path #{Dir.home}" unless File.writable?(Dir.home) + 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 diff --git a/spec/bundler/shared_helpers_spec.rb b/spec/bundler/shared_helpers_spec.rb index 334263cccb6..d98242a5e8b 100644 --- a/spec/bundler/shared_helpers_spec.rb +++ b/spec/bundler/shared_helpers_spec.rb @@ -414,9 +414,14 @@ module TargetNamespace end describe "#check_home_dir_permissions" do - it "raises a PathError" do - allow(File).to receive(:writable?).with(Dir.home).and_return(false) - expect { subject.check_home_dir_permissions }.to raise_error(Bundler::PathError, /There was an error while trying to use your home path/) + 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