Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Add tests
  • Loading branch information
barneygale committed Apr 24, 2021
commit 43e7456dbddf7300df87472f3fc209f2d79833bc
73 changes: 73 additions & 0 deletions Lib/test/test_pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -2559,6 +2559,42 @@ def test_expanduser(self):
self.assertEqual(p6.expanduser(), p6)
self.assertRaises(RuntimeError, p7.expanduser)

@unittest.skipUnless(hasattr(pwd, 'getpwall'),
'pwd module does not expose getpwall()')
@unittest.skipIf(sys.platform == "vxworks",
"no home directory on VxWorks")
def test_home(self):
P = self.cls
import_helper.import_module('pwd')
import pwd
pwdent = pwd.getpwuid(os.getuid())
username = pwdent.pw_name
userhome = pwdent.pw_dir.rstrip('/') or '/'
# Find arbitrary different user (if exists).
for pwdent in pwd.getpwall():
othername = pwdent.pw_name
otherhome = pwdent.pw_dir.rstrip('/')
if othername != username and otherhome:
break
else:
othername = username
otherhome = userhome

with os_helper.EnvironmentVarGuard() as env:
env.pop('HOME', None)

self.assertEqual(P.home(), P(userhome))
self.assertEqual(P.home(username), P(userhome))
self.assertEqual(P.home(othername), P(otherhome))
self.assertRaises(RuntimeError, P.home, 'missinguser')

env['HOME'] = '/tmp'

self.assertEqual(P.home(), P('/tmp'))
self.assertEqual(P.home(username), P(userhome))
self.assertEqual(P.home(othername), P(otherhome))
self.assertRaises(RuntimeError, P.home, 'missinguser')

@unittest.skipIf(sys.platform != "darwin",
"Bad file descriptor in /dev/fd affects only macOS")
def test_handling_bad_descriptor(self):
Expand Down Expand Up @@ -2654,6 +2690,43 @@ def check():
env['HOME'] = 'C:\\Users\\eve'
check()

def test_home(self):
P = self.cls
with os_helper.EnvironmentVarGuard() as env:
env.pop('HOME', None)
env.pop('USERPROFILE', None)
env.pop('HOMEPATH', None)
env.pop('HOMEDRIVE', None)
env['USERNAME'] = 'alice'

self.assertRaises(RuntimeError, P.home)
self.assertRaises(RuntimeError, P.home, 'alice')
self.assertRaises(RuntimeError, P.home, 'bob')

def check():
env.pop('USERNAME', None)
self.assertEqual(P.home(), P('C:/Users/alice'))
self.assertRaises(RuntimeError, P.home, 'alice')
env['USERNAME'] = 'alice'
self.assertEqual(P.home('alice'), P('C:/Users/alice'))
self.assertEqual(P.home('bob'), P('C:/Users/bob'))

env['HOMEPATH'] = 'C:\\Users\\alice'
check()

env['HOMEDRIVE'] = 'C:\\'
env['HOMEPATH'] = 'Users\\alice'
check()

env.pop('HOMEDRIVE', None)
env.pop('HOMEPATH', None)
env['USERPROFILE'] = 'C:\\Users\\alice'
check()

# bpo-38883: ignore `HOME` when set on windows
env['HOME'] = 'C:\\Users\\eve'
check()


class CompatiblePathTest(unittest.TestCase):
"""
Expand Down