Skip to content

Commit 5492420

Browse files
committed
pathlib tests: create walk() test hierarchy without using class under test
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 ef63cca commit 5492420

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
@@ -2911,6 +2911,42 @@ def setUp(self):
29112911
if name in _tests_needing_symlinks and not self.can_symlink:
29122912
self.skipTest('requires symlinks')
29132913
super().setUp()
2914+
2915+
def createTestHierarchy(self):
2916+
# Build:
2917+
# TESTFN/
2918+
# TEST1/ a file kid and two directory kids
2919+
# tmp1
2920+
# SUB1/ a file kid and a directory kid
2921+
# tmp2
2922+
# SUB11/ no kids
2923+
# SUB2/ a file kid and a dirsymlink kid
2924+
# tmp3
2925+
# link/ a symlink to TEST2
2926+
# broken_link
2927+
# broken_link2
2928+
# TEST2/
2929+
# tmp4 a lone file
2930+
t2_path = self.cls(self.base, "TEST2")
2931+
os.makedirs(self.sub11_path)
2932+
os.makedirs(self.sub2_path)
2933+
os.makedirs(t2_path)
2934+
2935+
tmp1_path = self.walk_path / "tmp1"
2936+
tmp2_path = self.sub1_path / "tmp2"
2937+
tmp3_path = self.sub2_path / "tmp3"
2938+
tmp4_path = self.cls(self.base, "TEST2", "tmp4")
2939+
for path in tmp1_path, tmp2_path, tmp3_path, tmp4_path:
2940+
with open(path, "w", encoding='utf-8') as f:
2941+
f.write(f"I'm {path} and proud of it. Blame test_pathlib.\n")
2942+
2943+
if self.can_symlink:
2944+
broken_link_path = self.sub2_path / "broken_link"
2945+
broken_link2_path = self.sub2_path / "broken_link2"
2946+
os.symlink(t2_path, self.link_path, target_is_directory=True)
2947+
os.symlink('broken', broken_link_path)
2948+
os.symlink(os.path.join('tmp3', 'broken'), broken_link2_path)
2949+
self.sub2_tree = (self.sub2_path, [], ["broken_link", "broken_link2", "link", "tmp3"])
29142950
sub21_path= self.sub2_path / "SUB21"
29152951
tmp5_path = sub21_path / "tmp3"
29162952
broken_link3_path = self.sub2_path / "broken_link3"
@@ -2934,7 +2970,7 @@ def setUp(self):
29342970
def tearDown(self):
29352971
if 'SUB21' in self.sub2_tree[1]:
29362972
os.chmod(self.sub2_path / "SUB21", stat.S_IRWXU)
2937-
super().tearDown()
2973+
os_helper.rmtree(self.base)
29382974

29392975
def test_walk_bad_dir(self):
29402976
errors = []

Lib/test/test_pathlib/test_pathlib_abc.py

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

17001700
def setUp(self):
1701-
# Build:
1702-
# TESTFN/
1703-
# TEST1/ a file kid and two directory kids
1704-
# tmp1
1705-
# SUB1/ a file kid and a directory kid
1706-
# tmp2
1707-
# SUB11/ no kids
1708-
# SUB2/ a file kid and a dirsymlink kid
1709-
# tmp3
1710-
# link/ a symlink to TEST2
1711-
# broken_link
1712-
# broken_link2
1713-
# TEST2/
1714-
# tmp4 a lone file
17151701
self.walk_path = self.cls(self.base, "TEST1")
17161702
self.sub1_path = self.walk_path / "SUB1"
17171703
self.sub11_path = self.sub1_path / "SUB11"
17181704
self.sub2_path = self.walk_path / "SUB2"
1719-
tmp1_path = self.walk_path / "tmp1"
1720-
tmp2_path = self.sub1_path / "tmp2"
1721-
tmp3_path = self.sub2_path / "tmp3"
17221705
self.link_path = self.sub2_path / "link"
1723-
t2_path = self.cls(self.base, "TEST2")
1724-
tmp4_path = self.cls(self.base, "TEST2", "tmp4")
1725-
broken_link_path = self.sub2_path / "broken_link"
1726-
broken_link2_path = self.sub2_path / "broken_link2"
1727-
1728-
self.sub11_path.mkdir(parents=True)
1729-
self.sub2_path.mkdir(parents=True)
1730-
t2_path.mkdir(parents=True)
1731-
1732-
for path in tmp1_path, tmp2_path, tmp3_path, tmp4_path:
1733-
with path.open("w", encoding='utf-8') as f:
1734-
f.write(f"I'm {path} and proud of it. Blame test_pathlib.\n")
1706+
self.sub2_tree = (self.sub2_path, [], ["tmp3"])
1707+
self.createTestHierarchy()
17351708

1736-
if self.can_symlink:
1737-
self.link_path.symlink_to(t2_path, target_is_directory=True)
1738-
broken_link_path.symlink_to('broken')
1739-
broken_link2_path.symlink_to(self.cls('tmp3', 'broken'))
1740-
self.sub2_tree = (self.sub2_path, [], ["broken_link", "broken_link2", "link", "tmp3"])
1741-
else:
1742-
self.sub2_tree = (self.sub2_path, [], ["tmp3"])
1709+
def createTestHierarchy(self):
1710+
cls = self.cls
1711+
cls._files = {
1712+
f'{self.base}/TEST1/tmp1': b'this is tmp1\n',
1713+
f'{self.base}/TEST1/SUB1/tmp2': b'this is tmp2\n',
1714+
f'{self.base}/TEST1/SUB2/tmp3': b'this is tmp3\n',
1715+
f'{self.base}/TEST2/tmp4': b'this is tmp4\n',
1716+
}
1717+
cls._directories = {
1718+
f'{self.base}': {'TEST1', 'TEST2'},
1719+
f'{self.base}/TEST1': {'SUB1', 'SUB2', 'tmp1'},
1720+
f'{self.base}/TEST1/SUB1': {'SUB11', 'tmp2'},
1721+
f'{self.base}/TEST1/SUB1/SUB11': set(),
1722+
f'{self.base}/TEST1/SUB2': {'tmp3'},
1723+
f'{self.base}/TEST2': {'tmp4'},
1724+
}
17431725

17441726
def tearDown(self):
1745-
base = self.cls(self.base)
1746-
base._delete()
1727+
cls = self.cls
1728+
cls._files.clear()
1729+
cls._directories.clear()
17471730

17481731
def test_walk_topdown(self):
17491732
walker = self.walk_path.walk()

0 commit comments

Comments
 (0)