Skip to content

Commit fd94c6a

Browse files
authored
pathlib tests: create walk() test hierarchy without using class under test (#128338)
In the tests for `pathlib.Path.walk()`, 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 a0088b4 commit fd94c6a

File tree

2 files changed

+58
-39
lines changed

2 files changed

+58
-39
lines changed

Lib/test/test_pathlib/test_pathlib.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3029,6 +3029,42 @@ def setUp(self):
30293029
if name in _tests_needing_symlinks and not self.can_symlink:
30303030
self.skipTest('requires symlinks')
30313031
super().setUp()
3032+
3033+
def createTestHierarchy(self):
3034+
# Build:
3035+
# TESTFN/
3036+
# TEST1/ a file kid and two directory kids
3037+
# tmp1
3038+
# SUB1/ a file kid and a directory kid
3039+
# tmp2
3040+
# SUB11/ no kids
3041+
# SUB2/ a file kid and a dirsymlink kid
3042+
# tmp3
3043+
# link/ a symlink to TEST2
3044+
# broken_link
3045+
# broken_link2
3046+
# TEST2/
3047+
# tmp4 a lone file
3048+
t2_path = self.cls(self.base, "TEST2")
3049+
os.makedirs(self.sub11_path)
3050+
os.makedirs(self.sub2_path)
3051+
os.makedirs(t2_path)
3052+
3053+
tmp1_path = self.walk_path / "tmp1"
3054+
tmp2_path = self.sub1_path / "tmp2"
3055+
tmp3_path = self.sub2_path / "tmp3"
3056+
tmp4_path = self.cls(self.base, "TEST2", "tmp4")
3057+
for path in tmp1_path, tmp2_path, tmp3_path, tmp4_path:
3058+
with open(path, "w", encoding='utf-8') as f:
3059+
f.write(f"I'm {path} and proud of it. Blame test_pathlib.\n")
3060+
3061+
if self.can_symlink:
3062+
broken_link_path = self.sub2_path / "broken_link"
3063+
broken_link2_path = self.sub2_path / "broken_link2"
3064+
os.symlink(t2_path, self.link_path, target_is_directory=True)
3065+
os.symlink('broken', broken_link_path)
3066+
os.symlink(os.path.join('tmp3', 'broken'), broken_link2_path)
3067+
self.sub2_tree = (self.sub2_path, [], ["broken_link", "broken_link2", "link", "tmp3"])
30323068
sub21_path= self.sub2_path / "SUB21"
30333069
tmp5_path = sub21_path / "tmp3"
30343070
broken_link3_path = self.sub2_path / "broken_link3"
@@ -3052,7 +3088,7 @@ def setUp(self):
30523088
def tearDown(self):
30533089
if 'SUB21' in self.sub2_tree[1]:
30543090
os.chmod(self.sub2_path / "SUB21", stat.S_IRWXU)
3055-
super().tearDown()
3091+
os_helper.rmtree(self.base)
30563092

30573093
def test_walk_bad_dir(self):
30583094
errors = []

Lib/test/test_pathlib/test_pathlib_abc.py

Lines changed: 21 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1580,52 +1580,35 @@ class DummyPathWalkTest(unittest.TestCase):
15801580
can_symlink = False
15811581

15821582
def setUp(self):
1583-
# Build:
1584-
# TESTFN/
1585-
# TEST1/ a file kid and two directory kids
1586-
# tmp1
1587-
# SUB1/ a file kid and a directory kid
1588-
# tmp2
1589-
# SUB11/ no kids
1590-
# SUB2/ a file kid and a dirsymlink kid
1591-
# tmp3
1592-
# link/ a symlink to TEST2
1593-
# broken_link
1594-
# broken_link2
1595-
# TEST2/
1596-
# tmp4 a lone file
15971583
self.walk_path = self.cls(self.base, "TEST1")
15981584
self.sub1_path = self.walk_path / "SUB1"
15991585
self.sub11_path = self.sub1_path / "SUB11"
16001586
self.sub2_path = self.walk_path / "SUB2"
1601-
tmp1_path = self.walk_path / "tmp1"
1602-
tmp2_path = self.sub1_path / "tmp2"
1603-
tmp3_path = self.sub2_path / "tmp3"
16041587
self.link_path = self.sub2_path / "link"
1605-
t2_path = self.cls(self.base, "TEST2")
1606-
tmp4_path = self.cls(self.base, "TEST2", "tmp4")
1607-
broken_link_path = self.sub2_path / "broken_link"
1608-
broken_link2_path = self.sub2_path / "broken_link2"
1609-
1610-
self.sub11_path.mkdir(parents=True)
1611-
self.sub2_path.mkdir(parents=True)
1612-
t2_path.mkdir(parents=True)
1613-
1614-
for path in tmp1_path, tmp2_path, tmp3_path, tmp4_path:
1615-
with path.open("w", encoding='utf-8') as f:
1616-
f.write(f"I'm {path} and proud of it. Blame test_pathlib.\n")
1588+
self.sub2_tree = (self.sub2_path, [], ["tmp3"])
1589+
self.createTestHierarchy()
16171590

1618-
if self.can_symlink:
1619-
self.link_path.symlink_to(t2_path, target_is_directory=True)
1620-
broken_link_path.symlink_to('broken')
1621-
broken_link2_path.symlink_to(self.cls('tmp3', 'broken'))
1622-
self.sub2_tree = (self.sub2_path, [], ["broken_link", "broken_link2", "link", "tmp3"])
1623-
else:
1624-
self.sub2_tree = (self.sub2_path, [], ["tmp3"])
1591+
def createTestHierarchy(self):
1592+
cls = self.cls
1593+
cls._files = {
1594+
f'{self.base}/TEST1/tmp1': b'this is tmp1\n',
1595+
f'{self.base}/TEST1/SUB1/tmp2': b'this is tmp2\n',
1596+
f'{self.base}/TEST1/SUB2/tmp3': b'this is tmp3\n',
1597+
f'{self.base}/TEST2/tmp4': b'this is tmp4\n',
1598+
}
1599+
cls._directories = {
1600+
f'{self.base}': {'TEST1', 'TEST2'},
1601+
f'{self.base}/TEST1': {'SUB1', 'SUB2', 'tmp1'},
1602+
f'{self.base}/TEST1/SUB1': {'SUB11', 'tmp2'},
1603+
f'{self.base}/TEST1/SUB1/SUB11': set(),
1604+
f'{self.base}/TEST1/SUB2': {'tmp3'},
1605+
f'{self.base}/TEST2': {'tmp4'},
1606+
}
16251607

16261608
def tearDown(self):
1627-
base = self.cls(self.base)
1628-
base._delete()
1609+
cls = self.cls
1610+
cls._files.clear()
1611+
cls._directories.clear()
16291612

16301613
def test_walk_topdown(self):
16311614
walker = self.walk_path.walk()

0 commit comments

Comments
 (0)