Skip to content

Commit 7f2a196

Browse files
committed
install: tests: migrate to dir helpers
1 parent 8041d70 commit 7f2a196

File tree

1 file changed

+39
-46
lines changed

1 file changed

+39
-46
lines changed

tests/func/test_install.py

Lines changed: 39 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
import os
2+
import pathlib
23
import sys
34

45
import pytest
56

67
from dvc.exceptions import GitHookAlreadyExistsError
7-
from dvc.main import main
8-
from dvc.stage import Stage
9-
from dvc.utils import file_md5
8+
from dvc.remote import RemoteConfig
9+
from dvc.utils import file_md5, fspath
1010

1111

1212
@pytest.mark.skipif(
1313
sys.platform == "win32", reason="Git hooks aren't supported on Windows"
1414
)
1515
class TestInstall(object):
1616
def _hook(self, name):
17-
return os.path.join(".git", "hooks", name)
17+
return pathlib.Path(".git") / "hooks" / name
1818

19-
def test_should_create_hooks(self, git, dvc_repo):
20-
assert main(["install"]) == 0
19+
def test_should_create_hooks(self, scm, dvc):
20+
scm.install()
2121

2222
hooks_with_commands = [
2323
("post-checkout", "exec dvc checkout"),
@@ -26,55 +26,48 @@ def test_should_create_hooks(self, git, dvc_repo):
2626
]
2727

2828
for fname, command in hooks_with_commands:
29-
assert os.path.isfile(self._hook(fname))
29+
hook_path = self._hook(fname)
30+
assert hook_path.is_file()
31+
assert command in hook_path.read_text()
3032

31-
with open(self._hook(fname), "r") as fobj:
32-
assert command in fobj.read()
33-
34-
def test_should_fail_if_file_already_exists(self, git, dvc_repo):
35-
with open(self._hook("post-checkout"), "w") as fobj:
36-
fobj.write("hook content")
33+
def test_should_fail_if_file_already_exists(self, scm):
34+
self._hook("post-checkout").write_text("hook content")
3735

3836
with pytest.raises(GitHookAlreadyExistsError):
39-
dvc_repo.scm.install()
40-
41-
def test_should_post_checkout_hook_checkout(self, repo_dir, git, dvc_repo):
42-
assert main(["install"]) == 0
43-
44-
stage_file = repo_dir.FOO + Stage.STAGE_FILE_SUFFIX
45-
46-
dvc_repo.add(repo_dir.FOO)
47-
dvc_repo.scm.add([".gitignore", stage_file])
48-
dvc_repo.scm.commit("add")
49-
50-
os.unlink(repo_dir.FOO)
51-
dvc_repo.scm.checkout("new_branch", create_new=True)
52-
53-
assert os.path.isfile(repo_dir.FOO)
37+
scm.install()
5438

55-
def test_should_pre_push_hook_push(self, repo_dir, git, dvc_repo):
56-
assert main(["install"]) == 0
39+
def test_should_post_checkout_hook_checkout(self, tmp_dir, scm, dvc):
40+
scm.install()
41+
tmp_dir.dvc_gen({"file": "file content"}, commit="add")
5742

58-
temp = repo_dir.mkdtemp()
59-
git_remote = os.path.join(temp, "project.git")
60-
storage_path = os.path.join(temp, "dvc_storage")
43+
os.unlink("file")
44+
scm.checkout("new_branch", create_new=True)
6145

62-
foo_checksum = file_md5(repo_dir.FOO)[0]
63-
expected_cache_path = dvc_repo.cache.local.get(foo_checksum)
46+
assert os.path.isfile("file")
6447

65-
ret = main(["remote", "add", "-d", "store", storage_path])
66-
assert ret == 0
48+
def test_should_pre_push_hook_push(
49+
self, tmp_dir, scm, dvc, tmp_path_factory
50+
):
51+
scm.install()
6752

68-
ret = main(["add", repo_dir.FOO])
69-
assert ret == 0
53+
temp = tmp_path_factory.mktemp("external")
54+
git_remote = temp / "project.git"
55+
storage_path = temp / "dvc_storage"
7056

71-
stage_file = repo_dir.FOO + Stage.STAGE_FILE_SUFFIX
72-
dvc_repo.scm.repo.index.add([stage_file, ".gitignore"])
73-
dvc_repo.scm.repo.index.commit("commit message")
57+
RemoteConfig(dvc.config).add(
58+
"store", fspath(storage_path), default=True
59+
)
60+
tmp_dir.dvc_gen("file", "file_content", "commit message")
7461

75-
dvc_repo.scm.repo.clone(git_remote)
76-
dvc_repo.scm.repo.create_remote("origin", git_remote)
62+
file_checksum = file_md5("file")[0]
63+
expected_storage_path = (
64+
storage_path / file_checksum[:2] / file_checksum[2:]
65+
)
7766

78-
dvc_repo.scm.repo.git.push("origin", "master")
67+
scm.repo.clone(fspath(git_remote))
68+
scm.repo.create_remote("origin", fspath(git_remote))
7969

80-
assert os.path.isfile(expected_cache_path)
70+
assert not expected_storage_path.is_file()
71+
scm.repo.git.push("origin", "master")
72+
assert expected_storage_path.is_file()
73+
assert expected_storage_path.read_text() == "file_content"

0 commit comments

Comments
 (0)