Skip to content

[RFC] tests: make read_text() work with directories #4155

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 3, 2020
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
11 changes: 11 additions & 0 deletions tests/dir_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,17 @@ def branch(self, name, new=False):
finally:
self.scm.checkout(old)

def read_text(self, *args, **kwargs): # pylint: disable=signature-differs
# NOTE: on windows we'll get PermissionError instead of
# IsADirectoryError when we try to `open` a directory, so we can't
# rely on exception flow control
if self.is_dir():
return {
path.name: path.read_text(*args, **kwargs)
for path in self.iterdir()
}
return super().read_text(*args, **kwargs)


def _coerce_filenames(filenames):
if isinstance(filenames, (str, bytes, pathlib.PurePath)):
Expand Down
8 changes: 2 additions & 6 deletions tests/func/test_get.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from dvc.repo.get import GetDVCFileError
from dvc.system import System
from dvc.utils.fs import makedirs
from tests.utils import trees_equal


def test_get_repo_file(tmp_dir, erepo_dir):
Expand All @@ -29,8 +28,7 @@ def test_get_repo_dir(tmp_dir, erepo_dir):

Repo.get(os.fspath(erepo_dir), "dir", "dir_imported")

assert os.path.isdir("dir_imported")
trees_equal(erepo_dir / "dir", "dir_imported")
assert (tmp_dir / "dir_imported").read_text() == {"file": "contents"}


@pytest.mark.parametrize(
Expand All @@ -44,7 +42,6 @@ def test_get_git_file(tmp_dir, erepo):

Repo.get(os.fspath(erepo), src, dst)

assert (tmp_dir / dst).is_file()
assert (tmp_dir / dst).read_text() == "hello"


Expand All @@ -61,8 +58,7 @@ def test_get_git_dir(tmp_dir, erepo):

Repo.get(os.fspath(erepo), src, dst)

assert (tmp_dir / dst).is_dir()
trees_equal(erepo / src, tmp_dir / dst)
assert (tmp_dir / dst).read_text() == {"dir": {"file.txt": "hello"}}


def test_cache_type_is_properly_overridden(tmp_dir, erepo_dir):
Expand Down
10 changes: 3 additions & 7 deletions tests/func/test_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from dvc.exceptions import DownloadError, PathMissingError
from dvc.system import System
from dvc.utils.fs import makedirs, remove
from tests.utils import trees_equal


def test_import(tmp_dir, scm, dvc, erepo_dir):
Expand Down Expand Up @@ -73,8 +72,7 @@ def test_import_git_dir(tmp_dir, scm, dvc, git_dir, src_is_dvc):

stage = dvc.imp(os.fspath(git_dir), "src", "dst")

assert (tmp_dir / "dst").is_dir()
trees_equal(git_dir / "src", tmp_dir / "dst")
assert (tmp_dir / "dst").read_text() == {"file.txt": "hello"}
assert tmp_dir.scm.repo.git.check_ignore(os.fspath(tmp_dir / "dst"))
assert stage.deps[0].def_repo == {
"url": os.fspath(git_dir),
Expand All @@ -88,8 +86,7 @@ def test_import_dir(tmp_dir, scm, dvc, erepo_dir):

stage = dvc.imp(os.fspath(erepo_dir), "dir", "dir_imported")

assert os.path.isdir("dir_imported")
trees_equal(erepo_dir / "dir", "dir_imported")
assert (tmp_dir / "dir_imported").read_text() == {"foo": "foo content"}
assert scm.repo.git.check_ignore("dir_imported")
assert stage.deps[0].def_repo == {
"url": os.fspath(erepo_dir),
Expand Down Expand Up @@ -222,8 +219,7 @@ def test_pull_imported_directory_stage(tmp_dir, dvc, erepo_dir):

dvc.pull(["dir_imported.dvc"])

assert os.path.isdir("dir_imported")
trees_equal(erepo_dir / "dir", "dir_imported")
assert (tmp_dir / "dir_imported").read_text() == {"foo": "foo content"}


def test_download_error_pulling_imported_stage(tmp_dir, dvc, erepo_dir):
Expand Down
11 changes: 0 additions & 11 deletions tests/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import os
from contextlib import contextmanager
from filecmp import dircmp

from dvc.scm import Git

Expand All @@ -20,15 +19,5 @@ def cd(newdir):
os.chdir(prevdir)


def trees_equal(dir_path_1, dir_path_2):

comparison = dircmp(dir_path_1, dir_path_2)

assert set(comparison.left_only) == set(comparison.right_only) == set()

for d in comparison.common_dirs:
trees_equal(os.path.join(dir_path_1, d), os.path.join(dir_path_2, d))


def to_posixpath(path):
return path.replace("\\", "/")