From: Serhiy Storchaka Date: Tue, 3 May 2016 19:15:29 +0000 (+0300) Subject: Backported test for posixpath.expanduser(). X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=7dc8e1e95e24bdd77bd40828abeb181fd411bec3;p=python Backported test for posixpath.expanduser(). --- diff --git a/Lib/test/test_posixpath.py b/Lib/test/test_posixpath.py index 686b6b9ff6..fbb18fd22b 100644 --- a/Lib/test/test_posixpath.py +++ b/Lib/test/test_posixpath.py @@ -198,6 +198,12 @@ class PosixPathTest(unittest.TestCase): def test_expanduser(self): self.assertEqual(posixpath.expanduser("foo"), "foo") + with test_support.EnvironmentVarGuard() as env: + for home in '/', '', '//', '///': + env['HOME'] = home + self.assertEqual(posixpath.expanduser("~"), "/") + self.assertEqual(posixpath.expanduser("~/"), "/") + self.assertEqual(posixpath.expanduser("~/foo"), "/foo") try: import pwd except ImportError: @@ -214,9 +220,12 @@ class PosixPathTest(unittest.TestCase): self.assertIsInstance(posixpath.expanduser("~foo/"), basestring) with test_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) + home = home.rstrip("/") or '/' + self.assertEqual(posixpath.expanduser("~"), home) def test_normpath(self): self.assertEqual(posixpath.normpath(""), ".")