From: Serhiy Storchaka Date: Tue, 3 May 2016 18:17:03 +0000 (+0300) Subject: Issue #24950: Fixed expanduser tests when the users home directory in pwd is "/". X-Git-Tag: v3.6.0a1~81^2 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=a3fd0b26ba7d302475e051b698c86f512d0e7a14;p=python Issue #24950: Fixed expanduser tests when the users home directory in pwd is "/". Based on patch by SilentGhost. --- diff --git a/Lib/test/test_pathlib.py b/Lib/test/test_pathlib.py index 247a6b323a..0ea4aef470 100644 --- a/Lib/test/test_pathlib.py +++ b/Lib/test/test_pathlib.py @@ -2062,7 +2062,7 @@ class PosixPathTest(_BasePathTest, unittest.TestCase): 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 diff --git a/Lib/test/test_posixpath.py b/Lib/test/test_posixpath.py index 9d20471591..acf110204c 100644 --- a/Lib/test/test_posixpath.py +++ b/Lib/test/test_posixpath.py @@ -216,6 +216,13 @@ class PosixPathTest(unittest.TestCase): 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: @@ -239,14 +246,12 @@ class PosixPathTest(unittest.TestCase): 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(""), ".")