Skip to content

Commit d61542b

Browse files
authored
pathlib tests: create test hierarchy without using class under test (#128200)
In the pathlib tests, avoid using the path class under test (`self.cls`) in test setup. Instead we use `os` functions in `test_pathlib`, and direct manipulation of `DummyPath` internal data in `test_pathlib_abc`.
1 parent c5b0c90 commit d61542b

File tree

2 files changed

+55
-29
lines changed

2 files changed

+55
-29
lines changed

Lib/test/test_pathlib/test_pathlib.py

+36-2
Original file line numberDiff line numberDiff line change
@@ -578,10 +578,44 @@ def setUp(self):
578578
if name in _tests_needing_symlinks and not self.can_symlink:
579579
self.skipTest('requires symlinks')
580580
super().setUp()
581-
os.chmod(self.parser.join(self.base, 'dirE'), 0)
581+
582+
def createTestHierarchy(self):
583+
os.mkdir(self.base)
584+
os.mkdir(os.path.join(self.base, 'dirA'))
585+
os.mkdir(os.path.join(self.base, 'dirB'))
586+
os.mkdir(os.path.join(self.base, 'dirC'))
587+
os.mkdir(os.path.join(self.base, 'dirC', 'dirD'))
588+
os.mkdir(os.path.join(self.base, 'dirE'))
589+
with open(os.path.join(self.base, 'fileA'), 'wb') as f:
590+
f.write(b"this is file A\n")
591+
with open(os.path.join(self.base, 'dirB', 'fileB'), 'wb') as f:
592+
f.write(b"this is file B\n")
593+
with open(os.path.join(self.base, 'dirC', 'fileC'), 'wb') as f:
594+
f.write(b"this is file C\n")
595+
with open(os.path.join(self.base, 'dirC', 'novel.txt'), 'wb') as f:
596+
f.write(b"this is a novel\n")
597+
with open(os.path.join(self.base, 'dirC', 'dirD', 'fileD'), 'wb') as f:
598+
f.write(b"this is file D\n")
599+
os.chmod(os.path.join(self.base, 'dirE'), 0)
600+
if self.can_symlink:
601+
# Relative symlinks.
602+
os.symlink('fileA', os.path.join(self.base, 'linkA'))
603+
os.symlink('non-existing', os.path.join(self.base, 'brokenLink'))
604+
os.symlink('dirB',
605+
os.path.join(self.base, 'linkB'),
606+
target_is_directory=True)
607+
os.symlink(os.path.join('..', 'dirB'),
608+
os.path.join(self.base, 'dirA', 'linkC'),
609+
target_is_directory=True)
610+
# This one goes upwards, creating a loop.
611+
os.symlink(os.path.join('..', 'dirB'),
612+
os.path.join(self.base, 'dirB', 'linkD'),
613+
target_is_directory=True)
614+
# Broken symlink (pointing to itself).
615+
os.symlink('brokenLinkLoop', os.path.join(self.base, 'brokenLinkLoop'))
582616

583617
def tearDown(self):
584-
os.chmod(self.parser.join(self.base, 'dirE'), 0o777)
618+
os.chmod(os.path.join(self.base, 'dirE'), 0o777)
585619
os_helper.rmtree(self.base)
586620

587621
def tempdir(self):

Lib/test/test_pathlib/test_pathlib_abc.py

+19-27
Original file line numberDiff line numberDiff line change
@@ -1437,33 +1437,25 @@ class DummyPathTest(DummyPurePathTest):
14371437

14381438
def setUp(self):
14391439
super().setUp()
1440-
parser = self.cls.parser
1441-
p = self.cls(self.base)
1442-
p.mkdir(parents=True)
1443-
p.joinpath('dirA').mkdir()
1444-
p.joinpath('dirB').mkdir()
1445-
p.joinpath('dirC').mkdir()
1446-
p.joinpath('dirC', 'dirD').mkdir()
1447-
p.joinpath('dirE').mkdir()
1448-
with p.joinpath('fileA').open('wb') as f:
1449-
f.write(b"this is file A\n")
1450-
with p.joinpath('dirB', 'fileB').open('wb') as f:
1451-
f.write(b"this is file B\n")
1452-
with p.joinpath('dirC', 'fileC').open('wb') as f:
1453-
f.write(b"this is file C\n")
1454-
with p.joinpath('dirC', 'novel.txt').open('wb') as f:
1455-
f.write(b"this is a novel\n")
1456-
with p.joinpath('dirC', 'dirD', 'fileD').open('wb') as f:
1457-
f.write(b"this is file D\n")
1458-
if self.can_symlink:
1459-
p.joinpath('linkA').symlink_to('fileA')
1460-
p.joinpath('brokenLink').symlink_to('non-existing')
1461-
p.joinpath('linkB').symlink_to('dirB', target_is_directory=True)
1462-
p.joinpath('dirA', 'linkC').symlink_to(
1463-
parser.join('..', 'dirB'), target_is_directory=True)
1464-
p.joinpath('dirB', 'linkD').symlink_to(
1465-
parser.join('..', 'dirB'), target_is_directory=True)
1466-
p.joinpath('brokenLinkLoop').symlink_to('brokenLinkLoop')
1440+
self.createTestHierarchy()
1441+
1442+
def createTestHierarchy(self):
1443+
cls = self.cls
1444+
cls._files = {
1445+
f'{self.base}/fileA': b'this is file A\n',
1446+
f'{self.base}/dirB/fileB': b'this is file B\n',
1447+
f'{self.base}/dirC/fileC': b'this is file C\n',
1448+
f'{self.base}/dirC/dirD/fileD': b'this is file D\n',
1449+
f'{self.base}/dirC/novel.txt': b'this is a novel\n',
1450+
}
1451+
cls._directories = {
1452+
f'{self.base}': {'fileA', 'dirA', 'dirB', 'dirC', 'dirE'},
1453+
f'{self.base}/dirA': set(),
1454+
f'{self.base}/dirB': {'fileB'},
1455+
f'{self.base}/dirC': {'fileC', 'dirD', 'novel.txt'},
1456+
f'{self.base}/dirC/dirD': {'fileD'},
1457+
f'{self.base}/dirE': set(),
1458+
}
14671459

14681460
def tearDown(self):
14691461
cls = self.cls

0 commit comments

Comments
 (0)