File tree 2 files changed +23
-7
lines changed
2 files changed +23
-7
lines changed Original file line number Diff line number Diff line change @@ -57,20 +57,21 @@ def rmdir(self, path):
57
57
58
58
def ls (self , path , detail = False , ** kwargs ):
59
59
path = self ._strip_protocol (path )
60
- info = self .info (path )
61
- if info ["type" ] == "directory" :
60
+ path_info = self .info (path )
61
+ infos = []
62
+ if path_info ["type" ] == "directory" :
62
63
with os .scandir (path ) as it :
63
- infos = []
64
64
for f in it :
65
65
try :
66
- infos .append (self .info (f ))
66
+ # Only get the info if requested since it is a bit expensive (the stat call inside)
67
+ # The strip_protocol is also used in info() and calls make_path_posix to always return posix paths
68
+ info = self .info (f ) if detail else self ._strip_protocol (f .path )
69
+ infos .append (info )
67
70
except FileNotFoundError :
68
71
pass
69
72
else :
70
- infos = [info ]
73
+ infos = [path_info ] if detail else [ path_info [ "name" ] ]
71
74
72
- if not detail :
73
- return [i ["name" ] for i in infos ]
74
75
return infos
75
76
76
77
def info (self , path , ** kwargs ):
Original file line number Diff line number Diff line change @@ -402,6 +402,21 @@ def test_ls_on_file(tmpdir):
402
402
assert fs .exists (resource )
403
403
assert fs .ls (tmpdir ) == fs .ls (resource )
404
404
assert fs .ls (resource , detail = True )[0 ] == fs .info (resource )
405
+ assert fs .ls (resource , detail = False )[0 ] == resource
406
+
407
+
408
+ def test_ls_on_files (tmpdir ):
409
+ tmpdir = make_path_posix (str (tmpdir ))
410
+ fs = LocalFileSystem ()
411
+ resources = [f"{ tmpdir } /{ file } " for file in ["file_1.json" , "file_2.json" ]]
412
+ for r in resources :
413
+ fs .touch (r )
414
+
415
+ actual_files = fs .ls (tmpdir , detail = True )
416
+ assert {f ["name" ] for f in actual_files } == set (resources )
417
+
418
+ actual_files = fs .ls (tmpdir , detail = False )
419
+ assert set (actual_files ) == set (resources )
405
420
406
421
407
422
@pytest .mark .parametrize ("file_protocol" , ["" , "file://" ])
You can’t perform that action at this time.
0 commit comments