From 0a40d5777d29271239a35dccac1cabc96c1485bf Mon Sep 17 00:00:00 2001 From: Ruslan Kuprieiev Date: Wed, 15 Apr 2020 19:59:58 +0300 Subject: [PATCH] tests: add some DvcTree unit tests --- dvc/repo/tree.py | 15 +++--- tests/unit/repo/test_tree.py | 95 ++++++++++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+), 7 deletions(-) create mode 100644 tests/unit/repo/test_tree.py diff --git a/dvc/repo/tree.py b/dvc/repo/tree.py index 2d3992b8fd..5554f1c140 100644 --- a/dvc/repo/tree.py +++ b/dvc/repo/tree.py @@ -1,3 +1,4 @@ +import os import errno from dvc.scm.tree import BaseTree @@ -27,12 +28,12 @@ def open(self, path, mode="r", encoding="utf-8"): except OutputNotFoundError as exc: raise FileNotFoundError from exc - if len(outs) != 1 or outs[0].isdir(): + if len(outs) != 1 or outs[0].is_dir_checksum: raise IOError(errno.EISDIR) out = outs[0] if not out.changed_cache(): - return open(out.cache_path.fspath, mode=mode, encoding=encoding) + return open(out.cache_path, mode=mode, encoding=encoding) raise FileNotFoundError @@ -47,12 +48,12 @@ def isdir(self, path): if not self.exists(path): return False + path_info = PathInfo(os.path.abspath(path)) outs = self._find_outs(path, strict=False, recursive=True) - - if len(outs) != 1 or outs[0].path_info.fspath != path: + if len(outs) != 1 or outs[0].path_info != path_info: return True - return outs[0].isdir() + return outs[0].is_dir_checksum def isfile(self, path): if not self.exists(path): @@ -70,7 +71,7 @@ def _walk(self, root, trie, topdown=True): continue name = key[root_len] - if len(key) > root_len + 1 or out.isdir(): + if len(key) > root_len + 1 or out.is_dir_checksum: dirs.add(name) continue @@ -95,7 +96,7 @@ def walk(self, top, topdown=True): if not self.isdir(top): raise NotADirectoryError - root = PathInfo(top) + root = PathInfo(os.path.abspath(top)) outs = self._find_outs(top, recursive=True, strict=False) trie = Trie() diff --git a/tests/unit/repo/test_tree.py b/tests/unit/repo/test_tree.py new file mode 100644 index 0000000000..4f4bb163fd --- /dev/null +++ b/tests/unit/repo/test_tree.py @@ -0,0 +1,95 @@ +import os +import shutil + +from dvc.repo.tree import DvcTree +from dvc.compat import fspath_py35 + + +def test_exists(tmp_dir, dvc): + tmp_dir.gen("foo", "foo") + dvc.add("foo") + (tmp_dir / "foo").unlink() + + tree = DvcTree(dvc) + assert tree.exists("foo") + + +def test_open(tmp_dir, dvc): + tmp_dir.gen("foo", "foo") + dvc.add("foo") + (tmp_dir / "foo").unlink() + + tree = DvcTree(dvc) + with tree.open("foo", "r") as fobj: + assert fobj.read() == "foo" + + +def test_isdir_isfile(tmp_dir, dvc): + tmp_dir.gen({"datafile": "data", "datadir": {"foo": "foo", "bar": "bar"}}) + + tree = DvcTree(dvc) + assert not tree.isdir("datadir") + assert not tree.isfile("datadir") + assert not tree.isdir("datafile") + assert not tree.isfile("datafile") + + dvc.add(["datadir", "datafile"]) + shutil.rmtree(fspath_py35(tmp_dir / "datadir")) + (tmp_dir / "datafile").unlink() + + assert tree.isdir("datadir") + assert not tree.isfile("datadir") + assert not tree.isdir("datafile") + assert tree.isfile("datafile") + + +def test_isdir_mixed(tmp_dir, dvc): + tmp_dir.gen({"dir": {"foo": "foo", "bar": "bar"}}) + + dvc.add(str(tmp_dir / "dir" / "foo")) + + tree = DvcTree(dvc) + assert tree.isdir("dir") + assert not tree.isfile("dir") + + +def test_walk(tmp_dir, dvc): + tmp_dir.gen( + { + "dir": { + "subdir1": {"foo1": "foo1", "bar1": "bar1"}, + "subdir2": {"foo2": "foo2"}, + "foo": "foo", + "bar": "bar", + } + } + ) + + dvc.add("dir", recursive=True) + tree = DvcTree(dvc) + + expected = [ + str(tmp_dir / "dir" / "subdir1"), + str(tmp_dir / "dir" / "subdir2"), + str(tmp_dir / "dir" / "subdir1" / "foo1"), + str(tmp_dir / "dir" / "subdir1" / "bar1"), + str(tmp_dir / "dir" / "subdir2" / "foo2"), + str(tmp_dir / "dir" / "foo"), + str(tmp_dir / "dir" / "bar"), + ] + + actual = [] + for root, dirs, files in tree.walk("dir"): + for entry in dirs + files: + actual.append(os.path.join(root, entry)) + + assert set(actual) == set(expected) + assert len(actual) == len(expected) + + +def test_isdvc(tmp_dir, dvc): + tmp_dir.gen({"foo": "foo", "bar": "bar"}) + dvc.add("foo") + tree = DvcTree(dvc) + assert tree.isdvc("foo") + assert not tree.isdvc("bar")