Skip to content

Commit 3d51c17

Browse files
Fix for slower exists() and info() calls (fsspec#705)
Co-authored-by: Ankita Luthra <[email protected]>
1 parent 6695146 commit 3d51c17

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

gcsfs/core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -796,7 +796,7 @@ async def _sequential_list_objects_helper(
796796
items.extend(page.get("items", []))
797797
next_page_token = page.get("nextPageToken", None)
798798

799-
while len(items) < max_results and next_page_token is not None:
799+
while len(items) + len(prefixes) < max_results and next_page_token is not None:
800800
num_items = min(items_per_call, max_results - len(items), 1000)
801801
page = await self._call(
802802
"GET",

gcsfs/tests/test_core.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,35 @@ def test_info(gcs):
224224
assert gcs.info(a)["mtime"] == gcs.modified(a)
225225

226226

227+
def test_info_on_directory_with_only_subdirectories(gcs):
228+
"""Test info() on a path that contains no direct files but has subdirectories."""
229+
# Setup: create a file inside a nested directory
230+
path = f"{TEST_BUCKET}/dir_with_only_subdirs/subdir1/file.txt"
231+
gcs.touch(path)
232+
path = f"{TEST_BUCKET}/dir_with_only_subdirs/subdir2/file.txt"
233+
gcs.touch(path)
234+
path = f"{TEST_BUCKET}/dir_with_only_subdirs/subdir3/file.txt"
235+
gcs.touch(path)
236+
path = f"{TEST_BUCKET}/dir_with_only_subdirs/subdir4/file.txt"
237+
gcs.touch(path)
238+
239+
# The path to test with info()
240+
dir_path = f"{TEST_BUCKET}/dir_with_only_subdirs"
241+
242+
# Mock the _call method to count invocations
243+
with mock.patch.object(gcs, "_call", wraps=gcs._call) as mock_call:
244+
# Get info for the directory
245+
info = gcs.info(dir_path)
246+
247+
# Assertions
248+
assert info["type"] == "directory"
249+
assert info["name"] == dir_path
250+
# one call is for exact file check and one call for directory
251+
assert (
252+
mock_call.call_count == 2
253+
), "info() should only make two calls to GCS for a directory."
254+
255+
227256
def test_ls2(gcs):
228257
assert TEST_BUCKET + "/" in gcs.ls("")
229258
with pytest.raises((OSError, IOError)):

0 commit comments

Comments
 (0)