@@ -2702,20 +2702,20 @@ def setUp(self):
2702
2702
del self .sub2_tree [1 ][:1 ]
2703
2703
2704
2704
def test_walk_topdown (self ):
2705
- all = list ( self .walk_path .walk () )
2706
-
2707
- self . assertEqual ( len ( all ), 4 )
2708
- # We can't know which order SUB1 and SUB2 will appear in.
2709
- # Not flipped: TESTFN, SUB1, SUB11, SUB2
2710
- # flipped: TESTFN, SUB2, SUB1, SUB11
2711
- flipped = all [ 0 ][ 1 ][ 0 ] != "SUB1"
2712
- all [ 0 ][ 1 ]. sort ( )
2713
- all [ 3 - 2 * flipped ][ - 1 ]. sort ( )
2714
- all [ 3 - 2 * flipped ] [1 ].sort ()
2715
- self . assertEqual ( all [ 0 ], ( self . walk_path , [ "SUB1" , "SUB2" ], [ "tmp1" ]) )
2716
- self .assertEqual (all [ 1 + flipped ], ( self .sub1_path , [ "SUB11" ], [ "tmp2" ]) )
2717
- self . assertEqual ( all [ 2 + flipped ], ( self .sub11_path , [], []))
2718
- self . assertEqual ( all [ 3 - 2 * flipped ], self . sub2_tree )
2705
+ walker = self .walk_path .walk ()
2706
+ entry = next ( walker )
2707
+ entry [ 1 ]. sort () # Ensure we visit SUB1 before SUB2
2708
+ self . assertEqual ( entry , ( self . walk_path , [ " SUB1" , " SUB2" ], [ "tmp1" ]))
2709
+ entry = next ( walker )
2710
+ self . assertEqual ( entry , ( self . sub1_path , [ "SUB11" ], [ "tmp2" ]))
2711
+ entry = next ( walker )
2712
+ self . assertEqual ( entry , ( self . sub11_path , [], []) )
2713
+ entry = next ( walker )
2714
+ entry [1 ].sort ()
2715
+ entry [ 2 ]. sort ( )
2716
+ self .assertEqual (entry , self .sub2_tree )
2717
+ with self .assertRaises ( StopIteration ):
2718
+ next ( walker )
2719
2719
2720
2720
def test_walk_prune (self , walk_path = None ):
2721
2721
if walk_path is None :
@@ -2739,24 +2739,37 @@ def test_file_like_path(self):
2739
2739
self .test_walk_prune (FakePath (self .walk_path ).__fspath__ ())
2740
2740
2741
2741
def test_walk_bottom_up (self ):
2742
- all = list (self .walk_path .walk ( top_down = False ))
2743
-
2744
- self .assertEqual (len (all ), 4 , all )
2745
- # We can't know which order SUB1 and SUB2 will appear in.
2746
- # Not flipped: SUB11, SUB1, SUB2, TESTFN
2747
- # flipped: SUB2, SUB11, SUB1, TESTFN
2748
- flipped = all [3 ][1 ][0 ] != "SUB1"
2749
- all [3 ][1 ].sort ()
2750
- all [2 - 2 * flipped ][- 1 ].sort ()
2751
- all [2 - 2 * flipped ][1 ].sort ()
2752
- self .assertEqual (all [3 ],
2753
- (self .walk_path , ["SUB1" , "SUB2" ], ["tmp1" ]))
2754
- self .assertEqual (all [flipped ],
2755
- (self .sub11_path , [], []))
2756
- self .assertEqual (all [flipped + 1 ],
2757
- (self .sub1_path , ["SUB11" ], ["tmp2" ]))
2758
- self .assertEqual (all [2 - 2 * flipped ],
2759
- self .sub2_tree )
2742
+ seen_testfn = seen_sub1 = seen_sub11 = seen_sub2 = False
2743
+ for path , dirnames , filenames in self .walk_path .walk (top_down = False ):
2744
+ if path == self .walk_path :
2745
+ self .assertFalse (seen_testfn )
2746
+ self .assertTrue (seen_sub1 )
2747
+ self .assertTrue (seen_sub2 )
2748
+ self .assertEqual (sorted (dirnames ), ["SUB1" , "SUB2" ])
2749
+ self .assertEqual (filenames , ["tmp1" ])
2750
+ seen_testfn = True
2751
+ elif path == self .sub1_path :
2752
+ self .assertFalse (seen_testfn )
2753
+ self .assertFalse (seen_sub1 )
2754
+ self .assertTrue (seen_sub11 )
2755
+ self .assertEqual (dirnames , ["SUB11" ])
2756
+ self .assertEqual (filenames , ["tmp2" ])
2757
+ seen_sub1 = True
2758
+ elif path == self .sub11_path :
2759
+ self .assertFalse (seen_sub1 )
2760
+ self .assertFalse (seen_sub11 )
2761
+ self .assertEqual (dirnames , [])
2762
+ self .assertEqual (filenames , [])
2763
+ seen_sub11 = True
2764
+ elif path == self .sub2_path :
2765
+ self .assertFalse (seen_testfn )
2766
+ self .assertFalse (seen_sub2 )
2767
+ self .assertEqual (sorted (dirnames ), sorted (self .sub2_tree [1 ]))
2768
+ self .assertEqual (sorted (filenames ), sorted (self .sub2_tree [2 ]))
2769
+ seen_sub2 = True
2770
+ else :
2771
+ raise AssertionError (f"Unexpected path: { path } " )
2772
+ self .assertTrue (seen_testfn )
2760
2773
2761
2774
@os_helper .skip_unless_symlink
2762
2775
def test_walk_follow_symlinks (self ):
0 commit comments