@@ -57,19 +57,21 @@ def __getitem__(self, name):
57
57
return item
58
58
raise IndexError ("{name} not found!" .format (name = name ))
59
59
60
- def ls (self , path , detail = True , ** kwargs ):
61
- path = self ._strip_protocol (path )
62
-
63
- files = {
64
- file ["name" ]: file
65
- for file in self ._fs_contents
66
- if path == self ._parent (file ["name" ])
67
- }
60
+ def ls (self , path , detail = True , refresh = True , ** kwargs ):
61
+ if kwargs .pop ("strip_proto" , True ):
62
+ path = self ._strip_protocol (path )
63
+
64
+ files = not refresh and self ._ls_from_cache (path )
65
+ if not files :
66
+ files = [
67
+ file for file in self ._fs_contents if path == self ._parent (file ["name" ])
68
+ ]
69
+ files .sort (key = lambda file : file ["name" ])
70
+ self .dircache [path .rstrip ("/" )] = files
68
71
69
72
if detail :
70
- return [files [name ] for name in sorted (files )]
71
-
72
- return list (sorted (files ))
73
+ return files
74
+ return [file ["name" ] for file in files ]
73
75
74
76
@classmethod
75
77
def get_test_paths (cls , start_with = "" ):
@@ -296,6 +298,23 @@ def test_json():
296
298
assert DummyTestFS .from_json (outb ) is b
297
299
298
300
301
+ def test_ls_from_cache ():
302
+ fs = DummyTestFS ()
303
+ uncached_results = fs .ls ("top_level/second_level/" , refresh = True )
304
+
305
+ assert fs .ls ("top_level/second_level/" , refresh = False ) == uncached_results
306
+
307
+ # _strip_protocol removes everything by default though
308
+ # for the sake of testing the _ls_from_cache interface
309
+ # directly, we need run one time more without that call
310
+ # to actually verify that our stripping in the client
311
+ # function works.
312
+ assert (
313
+ fs .ls ("top_level/second_level/" , refresh = False , strip_proto = True )
314
+ == uncached_results
315
+ )
316
+
317
+
299
318
@pytest .mark .parametrize (
300
319
"dt" ,
301
320
[
0 commit comments