Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion gdrive_fsspec/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,15 @@ def ls(self, path, detail=False, trashed=False):
files = self._list_directory_by_id(
file_id, trashed=trashed, path_prefix=pref
)
if files:
if (
path == ""
and not files
and self.drive is None
and file_id != "root"
and not self._file_id_exists(file_id)
):
raise FileNotFoundError(path)
if files or path == "":
self.dircache[pref] = files
else:
raise FileNotFoundError(path)
Expand Down Expand Up @@ -316,6 +324,17 @@ def info(self, path, trashed=False):
}
return super().info(path, trashed=trashed)

def _file_id_exists(self, file_id):
try:
self.files.get(
fileId=file_id, supportsAllDrives=True, fields="id"
).execute()
except HttpError as e:
if e.resp.status == 404:
return False
raise
return True

def _drive_kw(self):
if self.drive is not None:
return dict(
Expand Down
33 changes: 33 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,39 @@ def test_invalidate_cache_all(anon_fs):
assert anon_fs.dircache == {}


def test_ls_empty_root(anon_fs):
anon_fs._list_directory_by_id = mock.Mock(return_value=[])

assert anon_fs.ls("") == []
assert anon_fs.dircache[""] == []


def test_ls_empty_custom_root(anon_fs):
anon_fs.root_file_id = "empty-root"
anon_fs._list_directory_by_id = mock.Mock(return_value=[])
anon_fs._file_id_exists = mock.Mock(return_value=True)

assert anon_fs.ls("") == []
assert anon_fs.dircache[""] == []


def test_ls_missing_custom_root(anon_fs):
anon_fs.root_file_id = "missing-root"
anon_fs._list_directory_by_id = mock.Mock(return_value=[])
anon_fs._file_id_exists = mock.Mock(return_value=False)

with pytest.raises(FileNotFoundError):
anon_fs.ls("")
assert "" not in anon_fs.dircache


def test_ls_missing_path_on_empty_root(anon_fs):
anon_fs._list_directory_by_id = mock.Mock(return_value=[])

with pytest.raises(FileNotFoundError):
anon_fs.ls("missing")


def test_drive_id_from_name_single_match(anon_fs):
anon_fs.drives = [{"id": "1", "name": "foo"}, {"id": "2", "name": "bar"}]
assert anon_fs._drive_id_from_name("foo") == "1"
Expand Down
Loading