From 47a9d632fc4e0e21fde7388590a2943cb195275a Mon Sep 17 00:00:00 2001 From: karajan1001 Date: Sat, 5 Jun 2021 17:22:56 +0800 Subject: [PATCH] dvc ls: not raise PathMissingError on empty dir. fix #5841 --- dvc/repo/ls.py | 6 +++--- tests/func/test_ls.py | 13 ++++++++++++- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/dvc/repo/ls.py b/dvc/repo/ls.py index 55c41030a0..7bd40a97b7 100644 --- a/dvc/repo/ls.py +++ b/dvc/repo/ls.py @@ -31,11 +31,11 @@ def ls(url, path=None, rev=None, recursive=None, dvc_only=False): with Repo.open(url, rev=rev, subrepos=True, uninitialized=True) as repo: path = path or "" - ret = _ls(repo.repo_fs, path, recursive, dvc_only) - - if path and not ret: + if path and not repo.repo_fs.exists(path): raise PathMissingError(path, repo, dvc_only=dvc_only) + ret = _ls(repo.repo_fs, path, recursive, dvc_only) + ret_list = [] for path, info in ret.items(): info["path"] = path diff --git a/tests/func/test_ls.py b/tests/func/test_ls.py index a08c92dd1b..569ecd9506 100644 --- a/tests/func/test_ls.py +++ b/tests/func/test_ls.py @@ -150,9 +150,20 @@ def test_ls_repo_with_path_dir_dvc_only_empty(tmp_dir, dvc, scm): tmp_dir.scm_gen(FS_STRUCTURE, commit="init") tmp_dir.dvc_gen(DVC_STRUCTURE, commit="dvc") tmp_dir.scm_gen({"folder/.keep": "content"}, commit="add .keep") + tmp_dir.scm_gen({"empty_scm_folder/": {}}, commit="add scm empty") + tmp_dir.dvc_gen({"empty_dvc_folder": {}}, commit="empty dvc folder") with pytest.raises(PathMissingError): - Repo.ls(os.fspath(tmp_dir), path="folder", dvc_only=True) + Repo.ls(os.fspath(tmp_dir), path="not_exist_folder") + + assert Repo.ls(os.fspath(tmp_dir), path="empty_scm_folder") == [] + + assert Repo.ls(os.fspath(tmp_dir), path="folder", dvc_only=True) == [] + + assert ( + Repo.ls(os.fspath(tmp_dir), path="empty_dvc_folder", dvc_only=True) + == [] + ) def test_ls_repo_with_path_subdir(tmp_dir, dvc, scm):