From efcf4e331258ea0893b98275cf5ca28c8bb87b8d Mon Sep 17 00:00:00 2001 From: Ruslan Kuprieiev Date: Tue, 23 Jun 2020 16:32:28 +0300 Subject: [PATCH] git-hook: don't run when not in dvc repo (#4091) Related to #4073 --- dvc/command/git_hook.py | 4 ++-- tests/unit/command/test_git_hook.py | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 tests/unit/command/test_git_hook.py diff --git a/dvc/command/git_hook.py b/dvc/command/git_hook.py index 6c64c582d..11c8b59ca 100644 --- a/dvc/command/git_hook.py +++ b/dvc/command/git_hook.py @@ -28,7 +28,7 @@ class CmdPreCommit(CmdHookBase): class CmdPostCheckout(CmdHookBase): - def run(self): + def _run(self): # when we are running from pre-commit tool, it doesn't provide CLI # flags, but instead provides respective env vars that we could use. flag = os.environ.get("PRE_COMMIT_CHECKOUT_TYPE") @@ -52,7 +52,7 @@ class CmdPostCheckout(CmdHookBase): class CmdPrePush(CmdHookBase): - def run(self): + def _run(self): from dvc.main import main return main(["push"]) diff --git a/tests/unit/command/test_git_hook.py b/tests/unit/command/test_git_hook.py new file mode 100644 index 000000000..e10779bb5 --- /dev/null +++ b/tests/unit/command/test_git_hook.py @@ -0,0 +1,21 @@ +import pytest + +from dvc.cli import parse_args +from dvc.command.git_hook import CmdPostCheckout, CmdPreCommit, CmdPrePush + + +@pytest.mark.parametrize( + "hook, cls", + [ + ("pre-commit", CmdPreCommit), + ("post-checkout", CmdPostCheckout), + ("pre-push", CmdPrePush), + ], +) +def test_out_of_repo(tmp_dir, hook, cls, mocker): + cli_args = parse_args(["git-hook", hook]) + cli_args.func == cls + cmd = cli_args.func(cli_args) + mock_main = mocker.patch("dvc.main.main") + assert cmd.run() == 0 + assert not mock_main.called