tests: add some DvcTree unit tests (#3640)

This commit is contained in:
Ruslan Kuprieiev
2020-04-15 21:39:39 +03:00
committed by GitHub
parent c60f364774
commit 11e56dbdf9
2 changed files with 103 additions and 7 deletions
+8 -7
View File
@@ -1,3 +1,4 @@
import os
import errno
from dvc.scm.tree import BaseTree
@@ -27,12 +28,12 @@ class DvcTree(BaseTree):
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 @@ class DvcTree(BaseTree):
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 @@ class DvcTree(BaseTree):
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 @@ class DvcTree(BaseTree):
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()
+95
View File
@@ -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")