Skip to content

Commit cf2f3e7

Browse files
committed
Split walk() tests into their own test class
1 parent da4acf8 commit cf2f3e7

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

Lib/test/test_pathlib/test_pathlib.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1807,8 +1807,14 @@ def test_group_windows(self):
18071807
with self.assertRaises(pathlib.UnsupportedOperation):
18081808
P('c:/').group()
18091809

1810-
def setUpWalk(self):
1811-
super().setUpWalk()
1810+
1811+
class PathWalkTest(test_pathlib_abc.DummyPathWalkTest):
1812+
cls = pathlib.Path
1813+
base = PathTest.base
1814+
can_symlink = PathTest.can_symlink
1815+
1816+
def setUp(self):
1817+
super().setUp()
18121818
sub21_path= self.sub2_path / "SUB21"
18131819
tmp5_path = sub21_path / "tmp3"
18141820
broken_link3_path = self.sub2_path / "broken_link3"
@@ -1831,8 +1837,13 @@ def setUpWalk(self):
18311837
os.unlink(tmp5_path)
18321838
os.rmdir(sub21_path)
18331839

1840+
def tearDown(self):
1841+
if not is_emscripten:
1842+
sub21_path = self.sub2_path / "SUB21"
1843+
os.chmod(sub21_path, stat.S_IRWXU)
1844+
super().tearDown()
1845+
18341846
def test_walk_bad_dir(self):
1835-
self.setUpWalk()
18361847
errors = []
18371848
walk_it = self.walk_path.walk(on_error=errors.append)
18381849
root, dirs, files = next(walk_it)

Lib/test/test_pathlib/test_pathlib_abc.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2922,7 +2922,16 @@ def test_delete_missing(self):
29222922
filename = tmp / 'foo'
29232923
self.assertRaises(FileNotFoundError, filename._delete)
29242924

2925-
def setUpWalk(self):
2925+
2926+
class DummyPathWalkTest(unittest.TestCase):
2927+
cls = DummyPath
2928+
base = DummyPathTest.base
2929+
can_symlink = False
2930+
2931+
def setUp(self):
2932+
name = self.id().split('.')[-1]
2933+
if name in _tests_needing_symlinks and not self.can_symlink:
2934+
self.skipTest('requires symlinks')
29262935
# Build:
29272936
# TESTFN/
29282937
# TEST1/ a file kid and two directory kids
@@ -2966,8 +2975,11 @@ def setUpWalk(self):
29662975
else:
29672976
self.sub2_tree = (self.sub2_path, [], ["tmp3"])
29682977

2978+
def tearDown(self):
2979+
base = self.cls(self.base)
2980+
base._rmtree()
2981+
29692982
def test_walk_topdown(self):
2970-
self.setUpWalk()
29712983
walker = self.walk_path.walk()
29722984
entry = next(walker)
29732985
entry[1].sort() # Ensure we visit SUB1 before SUB2
@@ -2984,7 +2996,6 @@ def test_walk_topdown(self):
29842996
next(walker)
29852997

29862998
def test_walk_prune(self):
2987-
self.setUpWalk()
29882999
# Prune the search.
29893000
all = []
29903001
for root, dirs, files in self.walk_path.walk():
@@ -3001,7 +3012,6 @@ def test_walk_prune(self):
30013012
self.assertEqual(all[1], self.sub2_tree)
30023013

30033014
def test_walk_bottom_up(self):
3004-
self.setUpWalk()
30053015
seen_testfn = seen_sub1 = seen_sub11 = seen_sub2 = False
30063016
for path, dirnames, filenames in self.walk_path.walk(top_down=False):
30073017
if path == self.walk_path:
@@ -3036,7 +3046,6 @@ def test_walk_bottom_up(self):
30363046

30373047
@needs_symlinks
30383048
def test_walk_follow_symlinks(self):
3039-
self.setUpWalk()
30403049
walk_it = self.walk_path.walk(follow_symlinks=True)
30413050
for root, dirs, files in walk_it:
30423051
if root == self.link_path:
@@ -3048,7 +3057,6 @@ def test_walk_follow_symlinks(self):
30483057

30493058
@needs_symlinks
30503059
def test_walk_symlink_location(self):
3051-
self.setUpWalk()
30523060
# Tests whether symlinks end up in filenames or dirnames depending
30533061
# on the `follow_symlinks` argument.
30543062
walk_it = self.walk_path.walk(follow_symlinks=False)
@@ -3097,5 +3105,10 @@ class DummyPathWithSymlinksTest(DummyPathTest):
30973105
can_symlink = True
30983106

30993107

3108+
class DummyPathWithSymlinksWalkTest(DummyPathWalkTest):
3109+
cls = DummyPathWithSymlinks
3110+
can_symlink = True
3111+
3112+
31003113
if __name__ == "__main__":
31013114
unittest.main()

0 commit comments

Comments
 (0)