Skip to content

Commit d718764

Browse files
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 b8de8b7) Co-authored-by: Nikita Sobolev <[email protected]>
1 parent 97a015b commit d718764

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

Lib/test/test_pathlib.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2443,13 +2443,21 @@ def test_expanduser(self):
24432443
othername = username
24442444
otherhome = userhome
24452445

2446+
fakename = 'fakeuser'
2447+
# This user can theoretically exist on a test runner. Create unique name:
2448+
try:
2449+
while pwd.getpwnam(fakename):
2450+
fakename += '1'
2451+
except KeyError:
2452+
pass # Non-existent name found
2453+
24462454
p1 = P('~/Documents')
2447-
p2 = P('~' + username + '/Documents')
2448-
p3 = P('~' + othername + '/Documents')
2449-
p4 = P('../~' + username + '/Documents')
2450-
p5 = P('/~' + username + '/Documents')
2455+
p2 = P(f'~{username}/Documents')
2456+
p3 = P(f'~{othername}/Documents')
2457+
p4 = P(f'../~{username}/Documents')
2458+
p5 = P(f'/~{username}/Documents')
24512459
p6 = P('')
2452-
p7 = P('~fake800813user/Documents')
2460+
p7 = P(f'~{fakename}/Documents')
24532461

24542462
with support.EnvironmentVarGuard() as env:
24552463
env.pop('HOME', None)
Lines changed: 2 additions & 0 deletions
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)