Issue #24950: Fixed expanduser tests when the users home directory in pwd is "/".

Based on patch by SilentGhost.
This commit is contained in:
Serhiy Storchaka 2016-05-03 21:17:52 +03:00
commit 0e120525f0
2 changed files with 10 additions and 5 deletions

View file

@ -2062,7 +2062,7 @@ def test_expanduser(self):
import pwd
pwdent = pwd.getpwuid(os.getuid())
username = pwdent.pw_name
userhome = pwdent.pw_dir.rstrip('/')
userhome = pwdent.pw_dir.rstrip('/') or '/'
# find arbitrary different user (if exists)
for pwdent in pwd.getpwall():
othername = pwdent.pw_name

View file

@ -214,6 +214,13 @@ def fake_lstat(path):
def test_expanduser(self):
self.assertEqual(posixpath.expanduser("foo"), "foo")
self.assertEqual(posixpath.expanduser(b"foo"), b"foo")
with support.EnvironmentVarGuard() as env:
for home in '/', '', '//', '///':
with self.subTest(home=home):
env['HOME'] = home
self.assertEqual(posixpath.expanduser("~"), "/")
self.assertEqual(posixpath.expanduser("~/"), "/")
self.assertEqual(posixpath.expanduser("~/foo"), "/foo")
try:
import pwd
except ImportError:
@ -237,14 +244,12 @@ def test_expanduser(self):
self.assertIsInstance(posixpath.expanduser(b"~foo/"), bytes)
with support.EnvironmentVarGuard() as env:
env['HOME'] = '/'
self.assertEqual(posixpath.expanduser("~"), "/")
self.assertEqual(posixpath.expanduser("~/foo"), "/foo")
# expanduser should fall back to using the password database
del env['HOME']
home = pwd.getpwuid(os.getuid()).pw_dir
# $HOME can end with a trailing /, so strip it (see #17809)
self.assertEqual(posixpath.expanduser("~"), home.rstrip("/"))
home = home.rstrip("/") or '/'
self.assertEqual(posixpath.expanduser("~"), home)
def test_normpath(self):
self.assertEqual(posixpath.normpath(""), ".")