From a3857fe747805d21988f546034b949797e70d7ed Mon Sep 17 00:00:00 2001 From: pared Date: Tue, 30 Apr 2019 20:50:50 -0700 Subject: [PATCH 1/2] install: add pre-push hook, refactor test --- dvc/scm/git/__init__.py | 1 + tests/func/test_install.py | 72 ++++++++++++++++++++++++++++---------- 2 files changed, 54 insertions(+), 19 deletions(-) diff --git a/dvc/scm/git/__init__.py b/dvc/scm/git/__init__.py index c6db51da5c..5d1b51ce5e 100644 --- a/dvc/scm/git/__init__.py +++ b/dvc/scm/git/__init__.py @@ -215,6 +215,7 @@ def _install_hook(self, name, cmd): def install(self): self._install_hook("post-checkout", "checkout") self._install_hook("pre-commit", "status") + self._install_hook("pre-push", "push") def cleanup_ignores(self): for path in self.ignored_paths: diff --git a/tests/func/test_install.py b/tests/func/test_install.py index df0c454033..66facf69f6 100644 --- a/tests/func/test_install.py +++ b/tests/func/test_install.py @@ -1,33 +1,67 @@ import os import sys -import unittest -from dvc.main import main +import pytest +from dvc.utils import file_md5 -from tests.basic_env import TestDvc +from dvc.main import main +from dvc.stage import Stage -@unittest.skipIf( - sys.platform == "win32", "Git hooks aren't supported on Windows" +@pytest.mark.skipif( + sys.platform == "win32", reason="Git hooks aren't " "supported on Windows" ) -class TestInstall(TestDvc): - def test(self): +class TestInstall(object): + def _hook(self, name): + return os.path.join(".git", "hooks", name) + + @pytest.fixture(autouse=True) + def setUp(self, dvc): ret = main(["install"]) - self.assertEqual(ret, 0) + assert ret == 0 + def test_should_not_install_twice(self, dvc): ret = main(["install"]) - self.assertNotEqual(ret, 0) + assert ret != 0 + + def test_should_create_hooks(self): + assert os.path.isfile(self._hook("post-checkout")) + assert os.path.isfile(self._hook("pre-commit")) + assert os.path.isfile(self._hook("pre-push")) + + def test_should_post_checkout_hook_checkout(self, repo_dir, dvc): + stage_file = repo_dir.FOO + Stage.STAGE_FILE_SUFFIX + + dvc.add(repo_dir.FOO) + dvc.scm.add([".gitignore", stage_file]) + dvc.scm.commit("add") + + os.unlink(repo_dir.FOO) + dvc.scm.checkout("new_branc", create_new=True) + + assert os.path.isfile(repo_dir.FOO) + + def test_should_pre_push_hook_push(self, repo_dir, dvc): + temp = repo_dir.mkdtemp() + git_remote = os.path.join(temp, "project.git") + storage_path = os.path.join(temp, "dvc_storage") + + foo_checksum = file_md5(repo_dir.FOO)[0] + expected_cache_path = dvc.cache.local.get(foo_checksum) + + ret = main(["remote", "add", "-d", "store", storage_path]) + assert ret == 0 + + ret = main(["add", repo_dir.FOO]) + assert ret == 0 - def hook(name): - return os.path.join(".git", "hooks", name) + stage_file = repo_dir.FOO + Stage.STAGE_FILE_SUFFIX + dvc.scm.git.index.add([stage_file, ".gitignore"]) + dvc.scm.git.index.commit("commit message") - self.assertTrue(os.path.isfile(hook("post-checkout"))) - self.assertTrue(os.path.isfile(hook("pre-commit"))) + dvc.scm.git.clone(git_remote) + dvc.scm.git.create_remote("origin", git_remote) - self.dvc.add(self.FOO) - self.dvc.scm.add([".gitignore", self.FOO + ".dvc"]) - self.dvc.scm.commit("add") - os.unlink(self.FOO) + dvc.scm.git.git.push("origin", "master") - self.dvc.scm.checkout("branch", create_new=True) - self.assertTrue(os.path.isfile(self.FOO)) + assert os.path.isfile(expected_cache_path) From ba1463384f7ce825ea9e7927ee5eb3bbadf585e0 Mon Sep 17 00:00:00 2001 From: Ruslan Kuprieiev Date: Wed, 1 May 2019 11:11:29 -0700 Subject: [PATCH 2/2] Update tests/func/test_install.py Co-Authored-By: pared --- tests/func/test_install.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/func/test_install.py b/tests/func/test_install.py index 66facf69f6..47ff748a38 100644 --- a/tests/func/test_install.py +++ b/tests/func/test_install.py @@ -9,7 +9,7 @@ @pytest.mark.skipif( - sys.platform == "win32", reason="Git hooks aren't " "supported on Windows" + sys.platform == "win32", reason="Git hooks aren't supported on Windows" ) class TestInstall(object): def _hook(self, name):