def test_isdvc(tmp_dir, dvc): tmp_dir.gen({"foo": "foo", "bar": "bar", "dir": {"baz": "baz"}}) dvc.add("foo") dvc.add("dir") tree = RepoTree(dvc) assert tree.isdvc("foo") assert not tree.isdvc("bar") assert tree.isdvc("dir") assert not tree.isdvc("dir/baz") assert tree.isdvc("dir/baz", recursive=True)
def test_repo_tree_no_subrepos(tmp_dir, dvc, scm): tmp_dir.scm_gen( {"dir": { "repo.txt": "file to confuse RepoTree" }}, commit="dir/repo.txt", ) tmp_dir.dvc_gen({"lorem": "lorem"}, commit="add foo") subrepo = tmp_dir / "dir" / "repo" make_subrepo(subrepo, scm) subrepo.dvc_gen({"foo": "foo", "dir1": {"bar": "bar"}}, commit="FOO") subrepo.scm_gen({"ipsum": "ipsum"}, commit="BAR") # using tree that does not have dvcignore dvc.tree._reset() tree = RepoTree(dvc, subrepos=False, fetch=True) expected = [ tmp_dir / ".dvcignore", tmp_dir / ".gitignore", tmp_dir / "lorem", tmp_dir / "lorem.dvc", tmp_dir / "dir", tmp_dir / "dir" / "repo.txt", ] actual = [] for root, dirs, files in tree.walk(tmp_dir, dvcfiles=True): for entry in dirs + files: actual.append(os.path.normpath(os.path.join(root, entry))) expected = [str(path) for path in expected] assert set(actual) == set(expected) assert len(actual) == len(expected) assert tree.isfile(tmp_dir / "lorem") is True assert tree.isfile(tmp_dir / "dir" / "repo" / "foo") is False assert tree.isdir(tmp_dir / "dir" / "repo") is False assert tree.isdir(tmp_dir / "dir") is True assert tree.isdvc(tmp_dir / "lorem") is True assert tree.isdvc(tmp_dir / "dir" / "repo" / "dir1") is False assert tree.exists(tmp_dir / "dir" / "repo.txt") is True assert tree.exists(tmp_dir / "repo" / "ipsum") is False
def test_isdir_isfile(tmp_dir, dvc): tmp_dir.gen({"datafile": "data", "datadir": {"foo": "foo", "bar": "bar"}}) tree = RepoTree(dvc) assert tree.isdir("datadir") assert not tree.isfile("datadir") assert not tree.isdvc("datadir") assert not tree.isdir("datafile") assert tree.isfile("datafile") assert not tree.isdvc("datafile") dvc.add(["datadir", "datafile"]) shutil.rmtree(tmp_dir / "datadir") (tmp_dir / "datafile").unlink() assert tree.isdir("datadir") assert not tree.isfile("datadir") assert tree.isdvc("datadir") assert not tree.isdir("datafile") assert tree.isfile("datafile") assert tree.isdvc("datafile")
def collect_files(tree: BaseTree, repo_tree: RepoTree): for fname in repo_tree.walk_files(repo_tree.root_dir, dvcfiles=True): if not repo_tree.isdvc(fname): yield tree.path_info / fname.relative_to(repo_tree.root_dir)
def test_subrepos(tmp_dir, scm, dvc): tmp_dir.scm_gen( {"dir": { "repo.txt": "file to confuse RepoTree" }}, commit="dir/repo.txt", ) subrepo1 = tmp_dir / "dir" / "repo" subrepo2 = tmp_dir / "dir" / "repo2" for repo in [subrepo1, subrepo2]: make_subrepo(repo, scm) subrepo1.dvc_gen({"foo": "foo", "dir1": {"bar": "bar"}}, commit="FOO") subrepo2.dvc_gen({ "lorem": "lorem", "dir2": { "ipsum": "ipsum" } }, commit="BAR") dvc.tree._reset() tree = RepoTree(dvc, subrepos=True, fetch=True) def assert_tree_belongs_to_repo(ret_val): method = tree._get_repo def f(*args, **kwargs): r = method(*args, **kwargs) assert r.root_dir == ret_val.root_dir return r return f with mock.patch.object( tree, "_get_repo", side_effect=assert_tree_belongs_to_repo(subrepo1.dvc), ): assert tree.exists(subrepo1 / "foo") is True assert tree.exists(subrepo1 / "bar") is False assert tree.isfile(subrepo1 / "foo") is True assert tree.isfile(subrepo1 / "dir1" / "bar") is True assert tree.isfile(subrepo1 / "dir1") is False assert tree.isdir(subrepo1 / "dir1") is True assert tree.isdir(subrepo1 / "dir1" / "bar") is False assert tree.isdvc(subrepo1 / "foo") is True with mock.patch.object( tree, "_get_repo", side_effect=assert_tree_belongs_to_repo(subrepo2.dvc), ): assert tree.exists(subrepo2 / "lorem") is True assert tree.exists(subrepo2 / "ipsum") is False assert tree.isfile(subrepo2 / "lorem") is True assert tree.isfile(subrepo2 / "dir2" / "ipsum") is True assert tree.isfile(subrepo2 / "dir2") is False assert tree.isdir(subrepo2 / "dir2") is True assert tree.isdir(subrepo2 / "dir2" / "ipsum") is False assert tree.isdvc(subrepo2 / "lorem") is True