Skip to content

Commit

Permalink
use Dir.home for ruby 1.9+
Browse files Browse the repository at this point in the history
  • Loading branch information
dickeyxxx committed Dec 10, 2014
1 parent bb12096 commit a5cfbb9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
14 changes: 11 additions & 3 deletions lib/netrc.rb
Expand Up @@ -8,19 +8,27 @@ class Netrc
CYGWIN = RbConfig::CONFIG["host_os"] =~ /cygwin/

def self.default_path
File.join(home_path, netrc_filename)
end

def self.home_path
return Dir.home if defined? Dir.home # Ruby 1.9+
if WINDOWS && !CYGWIN
home = ENV['HOME']
home ||= ENV['HOMEDRIVE'] + ENV['HOMEPATH'] if ENV['HOMEDRIVE'] && ENV['HOMEPATH']
home ||= ENV['USERPROFILE']
File.join(home.gsub("\\","/"), "_netrc")
home.gsub("\\","/")
else
# In some cases, people run master process as "root" user, and run worker processes as "www" user.
# Fix "Permission denied" error in worker processes when $HOME is "/root".
default_dir = (ENV['HOME'] && File.exist?(ENV['HOME']) && File.stat(ENV['HOME']).readable?) ? ENV['HOME'] : './'
File.join(default_dir, ".netrc")
(ENV['HOME'] && File.exist?(ENV['HOME']) && File.stat(ENV['HOME']).readable?) ? ENV['HOME'] : './'
end
end

def self.netrc_filename
WINDOWS && !CYGWIN ? "_netrc" : ".netrc"
end

def self.config
@config ||= {}
end
Expand Down
5 changes: 3 additions & 2 deletions test/test_netrc.rb
Expand Up @@ -196,8 +196,9 @@ def test_encrypted_roundtrip
def test_missing_environment
nil_home = nil
ENV["HOME"], nil_home = nil_home, ENV["HOME"]
n = Netrc.read
assert_equal(nil, n["x"])
assert_raise_with_message ArgumentError, "couldn't find HOME environment -- expanding `~'" do
Netrc.read
end
ensure
ENV["HOME"], nil_home = nil_home, ENV["HOME"]
end
Expand Down

0 comments on commit a5cfbb9

Please sign in to comment.