Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions dvc/repo/tree.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import errno

from dvc.scm.tree import BaseTree
Expand Down Expand Up @@ -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

Expand All @@ -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):
Expand All @@ -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

Expand All @@ -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()
Expand Down
95 changes: 95 additions & 0 deletions tests/unit/repo/test_tree.py
Original file line number Diff line number Diff line change
@@ -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")