Bug #15324


Dir.home test doesn't match actual Ruby or system behaviour

Added by chrisseaton (Chris Seaton) over 2 years ago. Updated over 2 years ago.

Target version:


This test currently asserts that Dir.home(user) should return ENV['HOME'] if user is the current user.

We don't think this is correct - Dir.home(user) uses the expanded value of ~user, which explictly uses the login database, and explicitly not any current value of HOME.

See the documentation on tilde expansion here:

"[if there is an explict user name after the tilde then] the tilde-prefix shall be replaced by a pathname of the initial working directory associated with the login name obtained using the getpwnam() function as defined in the System Interfaces volume of POSIX.1-2017 [as opposed to reading HOME]"

getpwnam is what File.expand_path does.

To see the problem, run MRI's test with HOME set to something else (again, see the link above to see that setting HOME is explicitly allowed).

$ HOME=/nothing make test-all
  1) Failure:
TestDir#test_home [/Users/chrisseaton/Documents/mri-test-upstream/ruby/test/ruby/test_dir.rb:378]:
<"/nothing"> expected but was

We've encountered this test failure for real, so it isn't hypothetical.

The patch corrects the test to match actual Ruby behaviour.


dir.patch (538 Bytes) dir.patch chrisseaton (Chris Seaton), 11/20/2018 04:09 PM
Actions #1

Updated by naruse (Yui NARUSE) over 2 years ago

  • Status changed from Open to Closed

Applied in changeset trunk|r65926.

Use File.expand_path("~username") instead of ENV["HOME"]

File.expand_path("~username") uses getpwnam() independently from
Dir.home. [Bug #15324]

From: Chris Seaton

Updated by MSP-Greg (Greg L) over 2 years ago

naruse (Yui NARUSE)

Re r65926, I believe Windows has never processed '~'. Also, Window doesn't natively have ENV['USER'], but Ruby sets it. Regardless, the revised test fails...

Thanks, Greg

Updated by MSP-Greg (Greg L) over 2 years ago

I'm mistaken. Windows 10 (Appveyor?) will process a plain '~'

The test should pass with:

        if windows?
          assert_equal(File.expand_path("~"), Dir.home(user))
          assert_equal(File.expand_path("~#{user}"), Dir.home(user))

Also available in: Atom PDF