|
| 1 | +import io |
| 2 | +import json |
| 3 | + |
| 4 | +from scripts.hooks import command_from_stdin, run |
| 5 | +from scripts.hooks import git_commit, issue_create, pr_create |
| 6 | + |
| 7 | + |
| 8 | +def test_pr_create_matches_even_in_compound_command(): |
| 9 | + assert pr_create.reminder("cd repo && gh pr create --draft") == pr_create.REMINDER |
| 10 | + |
| 11 | + |
| 12 | +def test_issue_create_matches(): |
| 13 | + assert issue_create.reminder("gh issue create --label bug") == issue_create.REMINDER |
| 14 | + |
| 15 | + |
| 16 | +def test_checks_ignore_unrelated_commands(): |
| 17 | + assert pr_create.reminder("ls -la") is None |
| 18 | + assert issue_create.reminder("git status") is None |
| 19 | + assert git_commit.reminder("echo hi") is None |
| 20 | + |
| 21 | + |
| 22 | +def test_git_commit_lists_staged_and_unstaged(monkeypatch): |
| 23 | + outputs = { |
| 24 | + ("diff", "--cached", "--name-only"): "a.py", |
| 25 | + ("diff", "--name-only"): "b.py", |
| 26 | + ("ls-files", "--others", "--exclude-standard"): "c.py", |
| 27 | + } |
| 28 | + monkeypatch.setattr(git_commit, "_git", lambda *args: outputs[args]) |
| 29 | + message = git_commit.reminder("git commit -m x") |
| 30 | + assert "a.py" in message and "b.py" in message and "c.py" in message |
| 31 | + assert "Staged (will be committed)" in message |
| 32 | + |
| 33 | + |
| 34 | +def test_command_from_stdin_handles_bad_input(monkeypatch): |
| 35 | + monkeypatch.setattr("sys.stdin", io.StringIO("not json")) |
| 36 | + assert command_from_stdin() == "" |
| 37 | + |
| 38 | + |
| 39 | +def test_run_emits_valid_hook_json(monkeypatch): |
| 40 | + out = io.StringIO() |
| 41 | + monkeypatch.setattr("sys.stdin", io.StringIO(json.dumps({"tool_input": {"command": "x"}}))) |
| 42 | + monkeypatch.setattr("sys.stdout", out) |
| 43 | + assert run(lambda command: "hello") == 0 |
| 44 | + assert json.loads(out.getvalue())["hookSpecificOutput"]["additionalContext"] == "hello" |
| 45 | + |
| 46 | + |
| 47 | +def test_run_is_silent_when_check_returns_none(monkeypatch): |
| 48 | + out = io.StringIO() |
| 49 | + monkeypatch.setattr("sys.stdin", io.StringIO(json.dumps({"tool_input": {"command": "x"}}))) |
| 50 | + monkeypatch.setattr("sys.stdout", out) |
| 51 | + assert run(lambda command: None) == 0 |
| 52 | + assert out.getvalue() == "" |
0 commit comments