Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Tests forpathlib.Path.walk() are a little fragile #103517

Closed
Labels
testsTests in the Lib/test dirtopic-pathlibtype-bugAn unexpected behavior, bug, or error
@barneygale

Description

@barneygale

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.

deftest_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.

deftest_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

Metadata

Metadata

Assignees

No one assigned

    Labels

    testsTests in the Lib/test dirtopic-pathlibtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp