Skip to content

Commit

Permalink
fix for DVCFileSystem().ls()
Browse files Browse the repository at this point in the history
  • Loading branch information
dberenbaum authored and efiop committed Oct 31, 2023
1 parent eb8999a commit 4a2c4f7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
11 changes: 5 additions & 6 deletions dvc/fs/dvc.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ def ls( # pylint: disable=arguments-differ # noqa: C901
with suppress(FileNotFoundError, NotADirectoryError):
for info in dvc_fs.ls(dvc_path, detail=True):
dvc_infos[dvc_fs.path.name(info["name"])] = info
dvc_exists = bool(dvc_infos) or dvc_fs.exists(dvc_path)
dvc_exists = True

fs_exists = False
fs_infos = {}
Expand All @@ -333,21 +333,20 @@ def ls( # pylint: disable=arguments-differ # noqa: C901
fs, fs_path, detail=True, ignore_subrepos=ignore_subrepos
):
fs_infos[fs.path.name(info["name"])] = info
fs_exists = True
except (FileNotFoundError, NotADirectoryError):
pass

fs_exists = bool(fs_infos) or fs.exists(fs_path)
if not (dvc_exists or fs_exists):
# broken symlink or TreeError
raise FileNotFoundError(errno.ENOENT, os.strerror(errno.ENOENT), path)

dvcfiles = kwargs.get("dvcfiles", False)

infos = []
paths = []
names = set(dvc_infos.keys()) | set(fs_infos.keys())

if not names and (dvc_exists or fs_exists):
# broken symlink or TreeError
raise FileNotFoundError(errno.ENOENT, os.strerror(errno.ENOENT), path)

for name in names:
if not dvcfiles and _is_dvc_file(name):
continue
Expand Down
17 changes: 17 additions & 0 deletions tests/unit/fs/test_dvc.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,23 @@ def test_ls_dirty(tmp_dir, dvc):
assert set(fs.ls("data")) == {"data/foo", "data/bar"}


def test_ls_file_not_found(tmp_dir, dvc):
tmp_dir.dvc_gen({"data": "data"})

fs = DVCFileSystem(repo=dvc)
with pytest.raises(FileNotFoundError):
fs.ls("missing")


def test_ls_dir_empty(tmp_dir, dvc):
tmp_dir.dvc_gen({"data": "data"})
empty = tmp_dir / "empty"
empty.mkdir()

fs = DVCFileSystem(repo=dvc)
assert set(fs.ls("empty")) == set()


@pytest.mark.parametrize(
"dvcfiles,extra_expected",
[
Expand Down

0 comments on commit 4a2c4f7

Please sign in to comment.