Skip to content

Commit

Permalink
Merge pull request #3206 from Suor/tests
Browse files Browse the repository at this point in the history
test: add git_dir
  • Loading branch information
efiop authored Jan 21, 2020
2 parents 6958901 + 5af7a9d commit aa86eab
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 109 deletions.
17 changes: 16 additions & 1 deletion tests/dir_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,15 @@
from dvc.compat import fspath, fspath_py35


__all__ = ["make_tmp_dir", "tmp_dir", "scm", "dvc", "run_copy", "erepo_dir"]
__all__ = [
"make_tmp_dir",
"tmp_dir",
"scm",
"dvc",
"run_copy",
"erepo_dir",
"git_dir",
]


class TmpDir(pathlib.Path):
Expand Down Expand Up @@ -261,3 +269,10 @@ def erepo_dir(make_tmp_dir):
path.scm_add([path.dvc.config.config_file], commit="add remote")

return path


@pytest.fixture
def git_dir(make_tmp_dir):
path = make_tmp_dir("git-erepo", scm=True)
path.scm.commit("init repo")
return path
40 changes: 13 additions & 27 deletions tests/func/test_get.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,10 @@ def test_get_repo_rev(tmp_dir, erepo_dir):
assert (tmp_dir / "file_imported").read_text() == "contents"


def test_get_from_non_dvc_repo(tmp_dir, erepo_dir):
erepo_dir.scm.repo.index.remove([erepo_dir.dvc.dvc_dir], r=True)
erepo_dir.scm.commit("remove dvc")
erepo_dir.scm_gen({"some_file": "contents"}, commit="create file")
def test_get_from_non_dvc_repo(tmp_dir, git_dir):
git_dir.scm_gen({"some_file": "contents"}, commit="create file")

Repo.get(fspath(erepo_dir), "some_file", "file_imported")
Repo.get(fspath(git_dir), "some_file", "file_imported")
assert (tmp_dir / "file_imported").read_text() == "contents"


Expand All @@ -106,11 +104,9 @@ def test_get_full_dvc_path(tmp_dir, erepo_dir, tmp_path_factory):
external_data.write_text("ext_data")

with erepo_dir.chdir():
erepo_dir.dvc.add(fspath(external_data))
erepo_dir.scm_add(["ext_data.dvc"], commit="add external data")
erepo_dir.dvc_add(fspath(external_data), commit="add external data")

Repo.get(fspath(erepo_dir), fspath(external_data), "ext_data_imported")
assert (tmp_dir / "ext_data_imported").is_file()
assert (tmp_dir / "ext_data_imported").read_text() == "ext_data"


Expand All @@ -122,8 +118,7 @@ def test_non_cached_output(tmp_dir, erepo_dir):
erepo_dir.dvc.run(
outs_no_cache=[src], cmd="echo hello > non_cached_file"
)
erepo_dir.scm.add([src, src + ".dvc"])
erepo_dir.scm.commit("add non-cached output")
erepo_dir.scm_add([src, src + ".dvc"], commit="add non-cached output")

Repo.get(fspath(erepo_dir), src, dst)

Expand All @@ -138,12 +133,9 @@ def test_absolute_file_outside_repo(tmp_dir, erepo_dir):
Repo.get(fspath(erepo_dir), "/root/")


def test_absolute_file_outside_git_repo(tmp_dir, erepo_dir):
erepo_dir.scm.repo.index.remove([erepo_dir.dvc.dvc_dir], r=True)
erepo_dir.scm.commit("remove dvc")

def test_absolute_file_outside_git_repo(tmp_dir, git_dir):
with pytest.raises(PathMissingError):
Repo.get(fspath(erepo_dir), "/root/")
Repo.get(fspath(git_dir), "/root/")


def test_unknown_path(tmp_dir, erepo_dir):
Expand All @@ -164,28 +156,22 @@ def test_get_to_dir(tmp_dir, erepo_dir, dname):
assert (tmp_dir / dname / "file").read_text() == "contents"


def test_get_from_non_dvc_master(tmp_dir, erepo_dir, caplog):
with erepo_dir.chdir():
with erepo_dir.branch("branch", new=True):
erepo_dir.scm_gen(
{"some_file": "some_contents"}, commit="create some file"
)

erepo_dir.dvc.scm.repo.index.remove([".dvc"], r=True)
erepo_dir.dvc.scm.commit("remove .dvc")
def test_get_from_non_dvc_master(tmp_dir, git_dir, caplog):
with git_dir.chdir(), git_dir.branch("branch", new=True):
git_dir.init(dvc=True)
git_dir.dvc_gen("some_file", "some text", commit="create some file")

caplog.clear()
dst = "file_imported"

# removing `git` import in conftest resulted in unexpected logs from
# that package, see https://github.com/iterative/dvc/issues/3167
with caplog.at_level(logging.INFO, logger="git"), caplog.at_level(
logging.INFO, logger="dvc"
):
Repo.get(fspath(erepo_dir), "some_file", out=dst, rev="branch")
Repo.get(fspath(git_dir), "some_file", out="some_dst", rev="branch")

assert caplog.text == ""
assert (tmp_dir / dst).read_text() == "some_contents"
assert (tmp_dir / "some_dst").read_text() == "some text"


def test_get_url_positive(tmp_dir, erepo_dir, caplog):
Expand Down
52 changes: 20 additions & 32 deletions tests/func/test_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,19 @@ def test_import(tmp_dir, scm, dvc, erepo_dir):


@pytest.mark.parametrize("src_is_dvc", [True, False])
def test_import_git_file(erepo_dir, tmp_dir, dvc, scm, src_is_dvc):
if not src_is_dvc:
erepo_dir.dvc.scm.repo.index.remove([".dvc"], r=True)
erepo_dir.dvc.scm.commit("remove .dvc")
def test_import_git_file(tmp_dir, scm, dvc, git_dir, src_is_dvc):
if src_is_dvc:
git_dir.init(dvc=True)

src = "some_file"
dst = "some_file_imported"
git_dir.scm_gen("src", "hello", commit="add a git file")

erepo_dir.scm_gen({src: "hello"}, commit="add a regular file")

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

assert (tmp_dir / dst).is_file()
assert filecmp.cmp(
fspath(erepo_dir / src), fspath(tmp_dir / dst), shallow=False
)
assert tmp_dir.scm.repo.git.check_ignore(fspath(tmp_dir / dst))
assert (tmp_dir / "dst").read_text() == "hello"
assert tmp_dir.scm.repo.git.check_ignore(fspath(tmp_dir / "dst"))
assert stage.deps[0].def_repo == {
"url": fspath(erepo_dir),
"rev_lock": erepo_dir.scm.get_rev(),
"url": fspath(git_dir),
"rev_lock": git_dir.scm.get_rev(),
}


Expand All @@ -77,24 +70,20 @@ def test_import_cached_file(erepo_dir, tmp_dir, dvc, scm, monkeypatch):


@pytest.mark.parametrize("src_is_dvc", [True, False])
def test_import_git_dir(erepo_dir, tmp_dir, dvc, scm, src_is_dvc):
if not src_is_dvc:
erepo_dir.dvc.scm.repo.index.remove([".dvc"], r=True)
erepo_dir.dvc.scm.commit("remove .dvc")
def test_import_git_dir(tmp_dir, scm, dvc, git_dir, src_is_dvc):
if src_is_dvc:
git_dir.init(dvc=True)

src = "some_directory"
dst = "some_directory_imported"
git_dir.scm_gen({"src": {"file.txt": "hello"}}, commit="add a dir")

erepo_dir.scm_gen({src: {"file.txt": "hello"}}, commit="add a dir")
stage = dvc.imp(fspath(git_dir), "src", "dst")

stage = dvc.imp(fspath(erepo_dir), src, dst)

assert (tmp_dir / dst).is_dir()
trees_equal(fspath(erepo_dir / src), fspath(tmp_dir / dst))
assert tmp_dir.scm.repo.git.check_ignore(fspath(tmp_dir / dst))
assert (tmp_dir / "dst").is_dir()
trees_equal(fspath(git_dir / "src"), fspath(tmp_dir / "dst"))
assert tmp_dir.scm.repo.git.check_ignore(fspath(tmp_dir / "dst"))
assert stage.deps[0].def_repo == {
"url": fspath(erepo_dir),
"rev_lock": erepo_dir.scm.get_rev(),
"url": fspath(git_dir),
"rev_lock": git_dir.scm.get_rev(),
}


Expand Down Expand Up @@ -123,8 +112,7 @@ def test_import_non_cached(erepo_dir, tmp_dir, dvc, scm):
cwd=fspath(erepo_dir),
)

erepo_dir.scm.add([fspath(erepo_dir / src)])
erepo_dir.scm.commit("add a non-cached output")
erepo_dir.scm_add([fspath(erepo_dir / src)], commit="add a non-cached out")

stage = tmp_dir.dvc.imp(fspath(erepo_dir), src, dst)

Expand Down
45 changes: 17 additions & 28 deletions tests/func/test_status.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import os
import shutil

from mock import patch

from dvc.repo import Repo
from dvc.main import main
from dvc.compat import fspath
from dvc.external_repo import clean_repos
Expand All @@ -29,13 +27,11 @@ def test_implied_cloud(self, mock_status):
mock_status.assert_called()


def test_status_non_dvc_repo_import(tmp_dir, dvc, erepo_dir):
with erepo_dir.branch("branch", new=True), erepo_dir.chdir():
erepo_dir.scm.repo.index.remove([".dvc"], r=True)
shutil.rmtree(".dvc")
erepo_dir.scm_gen("file", "first version", commit="first version")
def test_status_non_dvc_repo_import(tmp_dir, dvc, git_dir):
with git_dir.branch("branch", new=True):
git_dir.scm_gen("file", "first version", commit="first version")

dvc.imp(fspath(erepo_dir), "file", "file", rev="branch")
dvc.imp(fspath(git_dir), "file", "file", rev="branch")

status = dvc.status(["file.dvc"])

Expand All @@ -45,37 +41,30 @@ def test_status_non_dvc_repo_import(tmp_dir, dvc, erepo_dir):
# cli call, so we need to clean the caches to see the changes.
clean_repos()

with erepo_dir.branch("branch", new=False), erepo_dir.chdir():
erepo_dir.scm_gen("file", "second_version", commit="update file")
with git_dir.branch("branch", new=False):
git_dir.scm_gen("file", "second version", commit="update file")

status, = dvc.status(["file.dvc"])["file.dvc"]

assert status == {
"changed deps": {
"file ({})".format(fspath(erepo_dir)): "update available"
}
"changed deps": {"file ({})".format(git_dir): "update available"}
}


def test_status_before_and_after_dvc_init(tmp_dir, dvc, erepo_dir):
with erepo_dir.chdir():
erepo_dir.scm.repo.index.remove([".dvc"], r=True)
shutil.rmtree(".dvc")
erepo_dir.scm_gen("file", "first version", commit="first verison")
old_rev = erepo_dir.scm.get_rev()
def test_status_before_and_after_dvc_init(tmp_dir, dvc, git_dir):
git_dir.scm_gen("file", "first version", commit="first verison")
old_rev = git_dir.scm.get_rev()

dvc.imp(fspath(erepo_dir), "file", "file")
dvc.imp(fspath(git_dir), "file", "file")

assert dvc.status(["file.dvc"]) == {}

with erepo_dir.chdir():
Repo.init()
erepo_dir.scm.repo.index.remove(["file"])
with git_dir.chdir():
git_dir.init(dvc=True)
git_dir.scm.repo.index.remove(["file"])
os.remove("file")
erepo_dir.dvc_gen("file", "second version")
erepo_dir.scm.add([".dvc", "file.dvc"])
erepo_dir.scm.commit("version with dvc")
new_rev = erepo_dir.scm.get_rev()
git_dir.dvc_gen("file", "second version", commit="with dvc")
new_rev = git_dir.scm.get_rev()

assert old_rev != new_rev

Expand All @@ -87,6 +76,6 @@ def test_status_before_and_after_dvc_init(tmp_dir, dvc, erepo_dir):

assert status == {
"changed deps": {
"file ({})".format(fspath(erepo_dir)): "update available"
"file ({})".format(fspath(git_dir)): "update available"
}
}
34 changes: 13 additions & 21 deletions tests/func/test_update.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
import pytest
import os
import shutil

from dvc.repo import Repo
from dvc.stage import Stage
from dvc.compat import fspath
from dvc.external_repo import clean_repos


@pytest.mark.parametrize("cached", [True, False])
def test_update_import(tmp_dir, dvc, erepo_dir, cached):
old_rev = None
gen = erepo_dir.dvc_gen if cached else erepo_dir.scm_gen

with erepo_dir.branch("branch", new=True), erepo_dir.chdir():
gen = erepo_dir.dvc_gen if cached else erepo_dir.scm_gen
gen("version", "branch", "add version file")
old_rev = erepo_dir.scm.get_rev()

Expand All @@ -27,9 +25,7 @@ def test_update_import(tmp_dir, dvc, erepo_dir, cached):
"rev_lock": old_rev,
}

new_rev = None
with erepo_dir.branch("branch", new=False), erepo_dir.chdir():
gen = erepo_dir.dvc_gen if cached else erepo_dir.scm_gen
gen("version", "updated", "update version content")
new_rev = erepo_dir.scm.get_rev()

Expand Down Expand Up @@ -110,23 +106,19 @@ def test_update_import_after_remote_updates_to_dvc(tmp_dir, dvc, erepo_dir):
}


def test_update_before_and_after_dvc_init(tmp_dir, dvc, erepo_dir):
with erepo_dir.chdir():
erepo_dir.scm.repo.index.remove([".dvc"], r=True)
shutil.rmtree(".dvc")
erepo_dir.scm_gen("file", "first version", commit="first version")
old_rev = erepo_dir.scm.get_rev()
def test_update_before_and_after_dvc_init(tmp_dir, dvc, git_dir):
with git_dir.chdir():
git_dir.scm_gen("file", "first version", commit="first version")
old_rev = git_dir.scm.get_rev()

stage = dvc.imp(fspath(erepo_dir), "file", "file")
stage = dvc.imp(fspath(git_dir), "file", "file")

with erepo_dir.chdir():
Repo.init()
erepo_dir.scm.repo.index.remove(["file"])
with git_dir.chdir():
git_dir.init(dvc=True)
git_dir.scm.repo.index.remove(["file"])
os.remove("file")
erepo_dir.dvc_gen("file", "second version")
erepo_dir.scm.add([".dvc", "file.dvc"])
erepo_dir.scm.commit("version with dvc")
new_rev = erepo_dir.scm.get_rev()
git_dir.dvc_gen("file", "second version", commit="with dvc")
new_rev = git_dir.scm.get_rev()

assert old_rev != new_rev

Expand All @@ -138,7 +130,7 @@ def test_update_before_and_after_dvc_init(tmp_dir, dvc, erepo_dir):
"file.dvc": [
{
"changed deps": {
"file ({})".format(fspath(erepo_dir)): "update available"
"file ({})".format(fspath(git_dir)): "update available"
}
}
]
Expand Down

0 comments on commit aa86eab

Please sign in to comment.