Skip to content

Commit b8de8b7

Browse files
authored
bpo-46150: ensure fakeuser does not exist in PosixPathTest.test_expanduser (GH-30240)
Ensure `fakeuser` does not exist in `PosixPathTest.test_expanduser`
1 parent d888067 commit b8de8b7

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

Lib/test/test_pathlib.py

+13-5
Original file line numberDiff line numberDiff line change
@@ -2558,13 +2558,21 @@ def test_expanduser(self):
25582558
othername = username
25592559
otherhome = userhome
25602560

2561+
fakename = 'fakeuser'
2562+
# This user can theoretically exist on a test runner. Create unique name:
2563+
try:
2564+
while pwd.getpwnam(fakename):
2565+
fakename += '1'
2566+
except KeyError:
2567+
pass # Non-existent name found
2568+
25612569
p1 = P('~/Documents')
2562-
p2 = P('~' + username + '/Documents')
2563-
p3 = P('~' + othername + '/Documents')
2564-
p4 = P('../~' + username + '/Documents')
2565-
p5 = P('/~' + username + '/Documents')
2570+
p2 = P(f'~{username}/Documents')
2571+
p3 = P(f'~{othername}/Documents')
2572+
p4 = P(f'../~{username}/Documents')
2573+
p5 = P(f'/~{username}/Documents')
25662574
p6 = P('')
2567-
p7 = P('~fake800813user/Documents')
2575+
p7 = P(f'~{fakename}/Documents')
25682576

25692577
with os_helper.EnvironmentVarGuard() as env:
25702578
env.pop('HOME', None)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Now ``fakename`` in ``test_pathlib.PosixPathTest.test_expanduser`` is checked
2+
to be non-existent.

0 commit comments

Comments
 (0)