diff options
author | Victor Stinner <vstinner@redhat.com> | 2018-12-05 15:49:35 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-12-05 15:49:35 (GMT) |
commit | f2f4555d8287ad217a1dba7bbd93103ad4daf3a8 (patch) | |
tree | d3c3dfaee0aa56df5cdf4d9d60db7b65c3e33db4 /Lib/test/test_site.py | |
parent | 398bd27967690f2c1a8cbf8d47a5613edd9cfb2a (diff) | |
download | cpython-f2f4555d8287ad217a1dba7bbd93103ad4daf3a8.zip cpython-f2f4555d8287ad217a1dba7bbd93103ad4daf3a8.tar.gz cpython-f2f4555d8287ad217a1dba7bbd93103ad4daf3a8.tar.bz2 |
bpo-10496: posixpath.expanduser() catchs pwd.getpwuid() error (GH-10919)
* posixpath.expanduser() now returns the input path unchanged if
the HOME environment variable is not set and pwd.getpwuid() raises
KeyError (the current user identifier doesn't exist in the password
database).
* Add test_no_home_directory() to test_site.
Diffstat (limited to 'Lib/test/test_site.py')
-rw-r--r-- | Lib/test/test_site.py | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/Lib/test/test_site.py b/Lib/test/test_site.py index 33a8f1a..f38e8d8 100644 --- a/Lib/test/test_site.py +++ b/Lib/test/test_site.py @@ -6,6 +6,7 @@ executing have not been removed. """ import unittest import test.support +from test import support from test.support import (captured_stderr, TESTFN, EnvironmentVarGuard, change_cwd) import builtins @@ -19,6 +20,7 @@ import shutil import subprocess import sysconfig import tempfile +from unittest import mock from copy import copy # These tests are not particularly useful if Python was invoked with -S. @@ -256,6 +258,7 @@ class HelperFunctionsTests(unittest.TestCase): # the call sets USER_BASE *and* USER_SITE self.assertEqual(site.USER_SITE, user_site) self.assertTrue(user_site.startswith(site.USER_BASE), user_site) + self.assertEqual(site.USER_BASE, site.getuserbase()) def test_getsitepackages(self): site.PREFIXES = ['xoxo'] @@ -274,6 +277,40 @@ class HelperFunctionsTests(unittest.TestCase): wanted = os.path.join('xoxo', 'lib', 'site-packages') self.assertEqual(dirs[1], wanted) + def test_no_home_directory(self): + # bpo-10496: getuserbase() and getusersitepackages() must not fail if + # the current user has no home directory (if expanduser() returns the + # path unchanged). + site.USER_SITE = None + site.USER_BASE = None + + with EnvironmentVarGuard() as environ, \ + mock.patch('os.path.expanduser', lambda path: path): + + del environ['PYTHONUSERBASE'] + del environ['APPDATA'] + + user_base = site.getuserbase() + self.assertTrue(user_base.startswith('~' + os.sep), + user_base) + + user_site = site.getusersitepackages() + self.assertTrue(user_site.startswith(user_base), user_site) + + with mock.patch('os.path.isdir', return_value=False) as mock_isdir, \ + mock.patch.object(site, 'addsitedir') as mock_addsitedir, \ + support.swap_attr(site, 'ENABLE_USER_SITE', True): + + # addusersitepackages() must not add user_site to sys.path + # if it is not an existing directory + known_paths = set() + site.addusersitepackages(known_paths) + + mock_isdir.assert_called_once_with(user_site) + mock_addsitedir.assert_not_called() + self.assertFalse(known_paths) + + class PthFile(object): """Helper class for handling testing of .pth files""" |