bpo-46150: ensure fakeuser does not exist in PosixPathTest.test_expanduser (GH-30240)

Ensure `fakeuser` does not exist in `PosixPathTest.test_expanduser`
(cherry picked from commit b8de8b7039)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
This commit is contained in:
Miss Islington (bot) 2021-12-24 00:31:13 -08:00 committed by GitHub
parent 1c77aa9fc3
commit 8005e22c9c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 5 deletions

View file

@ -2558,13 +2558,21 @@ class PosixPathTest(_BasePathTest, unittest.TestCase):
othername = username
otherhome = userhome
fakename = 'fakeuser'
# This user can theoretically exist on a test runner. Create unique name:
try:
while pwd.getpwnam(fakename):
fakename += '1'
except KeyError:
pass # Non-existent name found
p1 = P('~/Documents')
p2 = P('~' + username + '/Documents')
p3 = P('~' + othername + '/Documents')
p4 = P('../~' + username + '/Documents')
p5 = P('/~' + username + '/Documents')
p2 = P(f'~{username}/Documents')
p3 = P(f'~{othername}/Documents')
p4 = P(f'../~{username}/Documents')
p5 = P(f'/~{username}/Documents')
p6 = P('')
p7 = P('~fake800813user/Documents')
p7 = P(f'~{fakename}/Documents')
with os_helper.EnvironmentVarGuard() as env:
env.pop('HOME', None)

View file

@ -0,0 +1,2 @@
Now ``fakename`` in ``test_pathlib.PosixPathTest.test_expanduser`` is checked
to be non-existent.