test_walk_topdown() attempts to handle all possible visitation orders, but the result is difficult to read. We can enforce a specific order by sorting dirnames.
|
def test_walk_topdown(self): |
|
all = list(self.walk_path.walk()) |
|
|
|
self.assertEqual(len(all), 4) |
|
# We can't know which order SUB1 and SUB2 will appear in. |
|
# Not flipped: TESTFN, SUB1, SUB11, SUB2 |
|
# flipped: TESTFN, SUB2, SUB1, SUB11 |
|
flipped = all[0][1][0] != "SUB1" |
|
all[0][1].sort() |
|
all[3 - 2 * flipped][-1].sort() |
|
all[3 - 2 * flipped][1].sort() |
|
self.assertEqual(all[0], (self.walk_path, ["SUB1", "SUB2"], ["tmp1"])) |
|
self.assertEqual(all[1 + flipped], (self.sub1_path, ["SUB11"], ["tmp2"])) |
|
self.assertEqual(all[2 + flipped], (self.sub11_path, [], [])) |
|
self.assertEqual(all[3 - 2 * flipped], self.sub2_tree) |
test_walk_bottom_up() suffers similar problems, and also makes unjustified assertions about the order that siblings are visited (which is arbitrary and cannot be influenced by the user, contrary to top-down mode). It can be simplified to ensure that children are yielded before parents.
|
def test_walk_bottom_up(self): |
|
all = list(self.walk_path.walk( top_down=False)) |
|
|
|
self.assertEqual(len(all), 4, all) |
|
# We can't know which order SUB1 and SUB2 will appear in. |
|
# Not flipped: SUB11, SUB1, SUB2, TESTFN |
|
# flipped: SUB2, SUB11, SUB1, TESTFN |
|
flipped = all[3][1][0] != "SUB1" |
|
all[3][1].sort() |
|
all[2 - 2 * flipped][-1].sort() |
|
all[2 - 2 * flipped][1].sort() |
|
self.assertEqual(all[3], |
|
(self.walk_path, ["SUB1", "SUB2"], ["tmp1"])) |
|
self.assertEqual(all[flipped], |
|
(self.sub11_path, [], [])) |
|
self.assertEqual(all[flipped + 1], |
|
(self.sub1_path, ["SUB11"], ["tmp2"])) |
|
self.assertEqual(all[2 - 2 * flipped], |
|
self.sub2_tree) |
Linked PRs
test_walk_topdown()attempts to handle all possible visitation orders, but the result is difficult to read. We can enforce a specific order by sortingdirnames.cpython/Lib/test/test_pathlib.py
Lines 2680 to 2694 in a6f9594
test_walk_bottom_up()suffers similar problems, and also makes unjustified assertions about the order that siblings are visited (which is arbitrary and cannot be influenced by the user, contrary to top-down mode). It can be simplified to ensure that children are yielded before parents.cpython/Lib/test/test_pathlib.py
Lines 2717 to 2735 in a6f9594
Linked PRs
pathlib.Path.walk()#103518