diff options
Diffstat (limited to 'tests/test_hooks.py')
| -rw-r--r-- | tests/test_hooks.py | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/tests/test_hooks.py b/tests/test_hooks.py index 9d52d1849..14c226c75 100644 --- a/tests/test_hooks.py +++ b/tests/test_hooks.py @@ -14,6 +14,9 @@ """Unittests for the hooks.py module.""" +from io import StringIO +import sys + import pytest import hooks @@ -58,3 +61,47 @@ def test_direct_interp(shebang: str, interp: str) -> None: def test_env_interp(shebang: str, interp: str) -> None: """Lines whose shebang launches through `env`.""" assert hooks.RepoHook._ExtractInterpFromShebang(shebang) == interp + + +def test_post_sync_argument_validation() -> None: + """Test that post-sync hook requires exact API arguments.""" + + class FakeProject: + + def __init__(self): + self.worktree = "/some/path" + self.enabled_repo_hooks = ["post-sync"] + + hook = hooks.RepoHook( + hook_type="post-sync", + hooks_project=FakeProject(), + repo_topdir="/topdir", + manifest_url="https://gerrit", + allow_all_hooks=True, + ) + + old_stderr = sys.stderr + sys.stderr = StringIO() + + try: + # Call with missing arg `sync_duration_seconds` + res = hook.Run(repo_topdir="/topdir") + assert res is False + assert "hook 'post-sync' called incorrectly" in sys.stderr.getvalue() + + # Mock _CheckHook and _ExecuteHook to test success path + hook._CheckHook = lambda: None + + executed_kwargs = {} + + def fake_execute(**kw): + executed_kwargs.update(kw) + + hook._ExecuteHook = fake_execute + + res = hook.Run(repo_topdir="/topdir", sync_duration_seconds=12.345) + assert res is True + assert executed_kwargs.get("sync_duration_seconds") == 12.345 + + finally: + sys.stderr = old_stderr |
