[Python-checkins] cpython (3.5): Issue #24950: Fixed expanduser tests when the users home directory in pwd is

serhiy.storchaka python-checkins at python.org
Tue May 3 14:19:51 EDT 2016


https://hg.python.org/cpython/rev/194b356c84f5
changeset:   101217:194b356c84f5
branch:      3.5
parent:      101215:5ef3eda91051
user:        Serhiy Storchaka <storchaka at gmail.com>
date:        Tue May 03 21:17:03 2016 +0300
summary:
  Issue #24950: Fixed expanduser tests when the users home directory in pwd is "/".
Based on patch by SilentGhost.

files:
  Lib/test/test_pathlib.py   |   2 +-
  Lib/test/test_posixpath.py |  13 +++++++++----
  2 files changed, 10 insertions(+), 5 deletions(-)


diff --git a/Lib/test/test_pathlib.py b/Lib/test/test_pathlib.py
--- a/Lib/test/test_pathlib.py
+++ b/Lib/test/test_pathlib.py
@@ -2062,7 +2062,7 @@
         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
--- a/Lib/test/test_posixpath.py
+++ b/Lib/test/test_posixpath.py
@@ -216,6 +216,13 @@
     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 @@
             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(""), ".")

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list