@@ -21,9 +21,28 @@ def test_create(self):
2121 walk .Walker (ignore_errors = True , on_error = lambda path , error : True )
2222 walk .Walker (ignore_errors = True )
2323
24+ def test_on_error_invalid (self ):
25+ with self .assertRaises (TypeError ):
26+ walk .Walker (on_error = "nope" )
2427
25- class TestWalk (unittest .TestCase ):
28+
29+ class TestBoundWalkerBase (unittest .TestCase ):
2630 def setUp (self ):
31+ """
32+ Sets up the following file system with empty files:
33+
34+ /
35+ -foo1/
36+ - -top1.txt
37+ - -top2.txt
38+ -foo2/
39+ - -bar1/
40+ - -bar2/
41+ - - -bar3/
42+ - - - -test.txt
43+ - -top3.bin
44+ -foo3/
45+ """
2746 self .fs = MemoryFS ()
2847
2948 self .fs .makedir ("foo1" )
@@ -37,21 +56,50 @@ def setUp(self):
3756 self .fs .create ("foo2/bar2/bar3/test.txt" )
3857 self .fs .create ("foo2/top3.bin" )
3958
40- def test_invalid (self ):
41- with self .assertRaises (ValueError ):
42- self .fs .walk (search = "random" )
4359
60+ class TestBoundWalker (TestBoundWalkerBase ):
4461 def test_repr (self ):
4562 repr (self .fs .walk )
4663
47- def test_walk (self ):
64+ def test_readonly_wrapper_uses_same_walker (self ):
65+ class CustomWalker (walk .Walker ):
66+ @classmethod
67+ def bind (cls , fs ):
68+ return walk .BoundWalker (fs , walker_class = CustomWalker )
69+
70+ class CustomizedMemoryFS (MemoryFS ):
71+ walker_class = CustomWalker
72+
73+ base_fs = CustomizedMemoryFS ()
74+ base_walker = base_fs .walk
75+ self .assertEqual (base_walker .walker_class , CustomWalker )
76+
77+ readonly_fs = read_only (CustomizedMemoryFS ())
78+ readonly_walker = readonly_fs .walk
79+ self .assertEqual (readonly_walker .walker_class , CustomWalker )
80+
81+
82+ class TestWalk (TestBoundWalkerBase ):
83+ def _walk_step_names (self , * args , ** kwargs ):
84+ """Performs a walk with the given arguments and returns a list of steps.
85+
86+ Each step is a triple of the path, list of directory names, and list of file names.
87+ """
4888 _walk = []
49- for step in self .fs .walk ():
89+ for step in self .fs .walk (* args , ** kwargs ):
5090 self .assertIsInstance (step , walk .Step )
5191 path , dirs , files = step
5292 _walk .append (
5393 (path , [info .name for info in dirs ], [info .name for info in files ])
5494 )
95+ return _walk
96+
97+ def test_invalid_search (self ):
98+ with self .assertRaises (ValueError ):
99+ self .fs .walk (search = "random" )
100+
101+ def test_walk (self ):
102+ _walk = self ._walk_step_names ()
55103 expected = [
56104 ("/" , ["foo1" , "foo2" , "foo3" ], []),
57105 ("/foo1" , ["bar1" ], ["top1.txt" , "top2.txt" ]),
@@ -64,13 +112,7 @@ def test_walk(self):
64112 self .assertEqual (_walk , expected )
65113
66114 def test_walk_filter_dirs (self ):
67- _walk = []
68- for step in self .fs .walk (filter_dirs = ["foo*" ]):
69- self .assertIsInstance (step , walk .Step )
70- path , dirs , files = step
71- _walk .append (
72- (path , [info .name for info in dirs ], [info .name for info in files ])
73- )
115+ _walk = self ._walk_step_names (filter_dirs = ["foo*" ])
74116 expected = [
75117 ("/" , ["foo1" , "foo2" , "foo3" ], []),
76118 ("/foo1" , [], ["top1.txt" , "top2.txt" ]),
@@ -80,13 +122,7 @@ def test_walk_filter_dirs(self):
80122 self .assertEqual (_walk , expected )
81123
82124 def test_walk_depth (self ):
83- _walk = []
84- for step in self .fs .walk (search = "depth" ):
85- self .assertIsInstance (step , walk .Step )
86- path , dirs , files = step
87- _walk .append (
88- (path , [info .name for info in dirs ], [info .name for info in files ])
89- )
125+ _walk = self ._walk_step_names (search = "depth" )
90126 expected = [
91127 ("/foo1/bar1" , [], []),
92128 ("/foo1" , ["bar1" ], ["top1.txt" , "top2.txt" ]),
@@ -99,13 +135,7 @@ def test_walk_depth(self):
99135 self .assertEqual (_walk , expected )
100136
101137 def test_walk_directory (self ):
102- _walk = []
103- for step in self .fs .walk ("foo2" ):
104- self .assertIsInstance (step , walk .Step )
105- path , dirs , files = step
106- _walk .append (
107- (path , [info .name for info in dirs ], [info .name for info in files ])
108- )
138+ _walk = self ._walk_step_names ("foo2" )
109139 expected = [
110140 ("/foo2" , ["bar2" ], ["top3.bin" ]),
111141 ("/foo2/bar2" , ["bar3" ], []),
@@ -114,33 +144,21 @@ def test_walk_directory(self):
114144 self .assertEqual (_walk , expected )
115145
116146 def test_walk_levels_1 (self ):
117- results = list (self .fs .walk (max_depth = 1 ))
118- self .assertEqual (len (results ), 1 )
119- dirs = sorted (info .name for info in results [0 ].dirs )
120- self .assertEqual (dirs , ["foo1" , "foo2" , "foo3" ])
121- files = sorted (info .name for info in results [0 ].files )
122- self .assertEqual (files , [])
147+ _walk = self ._walk_step_names (max_depth = 1 )
148+ expected = [
149+ ("/" , ["foo1" , "foo2" , "foo3" ], []),
150+ ]
151+ self .assertEqual (_walk , expected )
123152
124153 def test_walk_levels_1_depth (self ):
125- results = list (self .fs .walk (max_depth = 1 , search = "depth" ))
126- self .assertEqual (len (results ), 1 )
127- dirs = sorted (info .name for info in results [0 ].dirs )
128- self .assertEqual (dirs , ["foo1" , "foo2" , "foo3" ])
129- files = sorted (info .name for info in results [0 ].files )
130- self .assertEqual (files , [])
154+ _walk = self ._walk_step_names (max_depth = 1 )
155+ expected = [
156+ ("/" , ["foo1" , "foo2" , "foo3" ], []),
157+ ]
158+ self .assertEqual (_walk , expected )
131159
132160 def test_walk_levels_2 (self ):
133- _walk = []
134- for step in self .fs .walk (max_depth = 2 ):
135- self .assertIsInstance (step , walk .Step )
136- path , dirs , files = step
137- _walk .append (
138- (
139- path ,
140- sorted (info .name for info in dirs ),
141- sorted (info .name for info in files ),
142- )
143- )
161+ _walk = self ._walk_step_names (max_depth = 2 )
144162 expected = [
145163 ("/" , ["foo1" , "foo2" , "foo3" ], []),
146164 ("/foo1" , ["bar1" ], ["top1.txt" , "top2.txt" ]),
@@ -149,6 +167,26 @@ def test_walk_levels_2(self):
149167 ]
150168 self .assertEqual (_walk , expected )
151169
170+
171+ class TestDirs (TestBoundWalkerBase ):
172+ def test_walk_dirs (self ):
173+ dirs = list (self .fs .walk .dirs ())
174+ self .assertEqual (
175+ dirs ,
176+ ["/foo1" , "/foo2" , "/foo3" , "/foo1/bar1" , "/foo2/bar2" , "/foo2/bar2/bar3" ],
177+ )
178+
179+ dirs = list (self .fs .walk .dirs (search = "depth" ))
180+ self .assertEqual (
181+ dirs ,
182+ ["/foo1/bar1" , "/foo1" , "/foo2/bar2/bar3" , "/foo2/bar2" , "/foo2" , "/foo3" ],
183+ )
184+
185+ dirs = list (self .fs .walk .dirs (search = "depth" , exclude_dirs = ["foo2" ]))
186+ self .assertEqual (dirs , ["/foo1/bar1" , "/foo1" , "/foo3" ])
187+
188+
189+ class TestFiles (TestBoundWalkerBase ):
152190 def test_walk_files (self ):
153191 files = list (self .fs .walk .files ())
154192
@@ -173,22 +211,6 @@ def test_walk_files(self):
173211 ],
174212 )
175213
176- def test_walk_dirs (self ):
177- dirs = list (self .fs .walk .dirs ())
178- self .assertEqual (
179- dirs ,
180- ["/foo1" , "/foo2" , "/foo3" , "/foo1/bar1" , "/foo2/bar2" , "/foo2/bar2/bar3" ],
181- )
182-
183- dirs = list (self .fs .walk .dirs (search = "depth" ))
184- self .assertEqual (
185- dirs ,
186- ["/foo1/bar1" , "/foo1" , "/foo2/bar2/bar3" , "/foo2/bar2" , "/foo2" , "/foo3" ],
187- )
188-
189- dirs = list (self .fs .walk .dirs (search = "depth" , exclude_dirs = ["foo2" ]))
190- self .assertEqual (dirs , ["/foo1/bar1" , "/foo1" , "/foo3" ])
191-
192214 def test_walk_files_filter (self ):
193215 files = list (self .fs .walk .files (filter = ["*.txt" ]))
194216
@@ -222,25 +244,6 @@ def test_walk_files_exclude(self):
222244 files = list (self .fs .walk .files (exclude = ["*" ]))
223245 self .assertEqual (files , [])
224246
225- def test_walk_info (self ):
226- walk = []
227- for path , info in self .fs .walk .info ():
228- walk .append ((path , info .is_dir , info .name ))
229-
230- expected = [
231- ("/foo1" , True , "foo1" ),
232- ("/foo2" , True , "foo2" ),
233- ("/foo3" , True , "foo3" ),
234- ("/foo1/top1.txt" , False , "top1.txt" ),
235- ("/foo1/top2.txt" , False , "top2.txt" ),
236- ("/foo1/bar1" , True , "bar1" ),
237- ("/foo2/bar2" , True , "bar2" ),
238- ("/foo2/top3.bin" , False , "top3.bin" ),
239- ("/foo2/bar2/bar3" , True , "bar3" ),
240- ("/foo2/bar2/bar3/test.txt" , False , "test.txt" ),
241- ]
242- self .assertEqual (walk , expected )
243-
244247 def test_broken (self ):
245248 original_scandir = self .fs .scandir
246249
@@ -257,10 +260,6 @@ def broken_scandir(path, namespaces=None):
257260 with self .assertRaises (FSError ):
258261 list (self .fs .walk .files (on_error = lambda path , error : False ))
259262
260- def test_on_error_invalid (self ):
261- with self .assertRaises (TypeError ):
262- walk .Walker (on_error = "nope" )
263-
264263 def test_subdir_uses_same_walker (self ):
265264 class CustomWalker (walk .Walker ):
266265 @classmethod
@@ -284,19 +283,23 @@ class CustomizedMemoryFS(MemoryFS):
284283 self .assertEqual (sub_walker .walker_class , CustomWalker )
285284 six .assertCountEqual (self , ["/c" , "/d" ], sub_walker .files ())
286285
287- def test_readonly_wrapper_uses_same_walker (self ):
288- class CustomWalker (walk .Walker ):
289- @classmethod
290- def bind (cls , fs ):
291- return walk .BoundWalker (fs , walker_class = CustomWalker )
292-
293- class CustomizedMemoryFS (MemoryFS ):
294- walker_class = CustomWalker
295286
296- base_fs = CustomizedMemoryFS ()
297- base_walker = base_fs .walk
298- self .assertEqual (base_walker .walker_class , CustomWalker )
287+ class TestInfo (TestBoundWalkerBase ):
288+ def test_walk_info (self ):
289+ walk = []
290+ for path , info in self .fs .walk .info ():
291+ walk .append ((path , info .is_dir , info .name ))
299292
300- readonly_fs = read_only (CustomizedMemoryFS ())
301- readonly_walker = readonly_fs .walk
302- self .assertEqual (readonly_walker .walker_class , CustomWalker )
293+ expected = [
294+ ("/foo1" , True , "foo1" ),
295+ ("/foo2" , True , "foo2" ),
296+ ("/foo3" , True , "foo3" ),
297+ ("/foo1/top1.txt" , False , "top1.txt" ),
298+ ("/foo1/top2.txt" , False , "top2.txt" ),
299+ ("/foo1/bar1" , True , "bar1" ),
300+ ("/foo2/bar2" , True , "bar2" ),
301+ ("/foo2/top3.bin" , False , "top3.bin" ),
302+ ("/foo2/bar2/bar3" , True , "bar3" ),
303+ ("/foo2/bar2/bar3/test.txt" , False , "test.txt" ),
304+ ]
305+ self .assertEqual (walk , expected )
0 commit comments