Skip to content

Commit 073d579

Browse files
shenxianpengclaude
andcommitted
test: run the real commit-check binary once, unmocked
All 32 patches of main.subprocess.run feed back the JSON shape that make_check() hard-codes, so the suite passed with commit-check removed from PATH entirely. A renamed key or a new status in the CLI would have left 191 tests green while the action rendered "—" for every value, which is the class of bug the warn status already caused once. Add TestRealCommitCheckBinary: two run_check_json calls against the pinned CLI, one passing and one failing subject, asserting the key set, the status vocabulary, the rule-id and docs-url shapes, and that a ScopeResult and the rendered report built from the real output come out right. It is skipped with a clear reason when the binary is not on PATH; CI installs it from requirements.txt, so it always runs where it matters. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018xYa7m3qup5wyN5MaXFgf6
1 parent 182bf90 commit 073d579

1 file changed

Lines changed: 75 additions & 0 deletions

File tree

‎main_test.py‎

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import json
66
import os
77
import re
8+
import shutil
89
import sys
910
import tempfile
1011
import unittest
@@ -2692,3 +2693,77 @@ def test_step_log_partial_skip_does_not_claim_all_passed(self):
26922693
out = buf.getvalue()
26932694
self.assertIn("1 of 2 checks passed, 1 skipped", out)
26942695
self.assertNotIn("all checks passed", out)
2696+
2697+
2698+
# ---------------------------------------------------------------------------
2699+
# Integration: the real commit-check binary
2700+
# ---------------------------------------------------------------------------
2701+
2702+
#: The JSON shape ``make_check()`` hard-codes and every renderer reads.
2703+
CHECK_KEYS = {
2704+
"rule_id",
2705+
"check",
2706+
"status",
2707+
"value",
2708+
"error",
2709+
"suggest",
2710+
"fix",
2711+
"docs_url",
2712+
}
2713+
STATUSES = {"pass", "fail", "warn", "skip"}
2714+
2715+
2716+
@unittest.skipUnless(
2717+
shutil.which("commit-check"),
2718+
"commit-check CLI not on PATH (CI installs it from requirements.txt)",
2719+
)
2720+
class TestRealCommitCheckBinary(unittest.TestCase):
2721+
"""Run the pinned commit-check once, unmocked.
2722+
2723+
Every other test patches ``main.subprocess.run`` and feeds back the JSON
2724+
shape ``make_check()`` hard-codes, so a renamed key or a new status in
2725+
the CLI would leave the whole suite green while the action rendered "—"
2726+
for every value. This is the one place that drift can fail a build. CI
2727+
installs requirements.txt, so the binary is always present there; the
2728+
skip only spares a contributor running the suite without it.
2729+
"""
2730+
2731+
def _run(self, message: str) -> tuple[int, dict]:
2732+
rc, data, raw = main.run_check_json(["--message"], input_text=message)
2733+
self.assertIsInstance(data, dict, f"CLI did not emit JSON:\n{raw}")
2734+
assert data is not None # for the type checker; asserted above
2735+
self.assertIn("checks", data)
2736+
self.assertTrue(data["checks"], "CLI reported no checks")
2737+
for check in data["checks"]:
2738+
self.assertEqual(set(check), CHECK_KEYS, check)
2739+
self.assertIn(check["status"], STATUSES, check)
2740+
self.assertRegex(check["rule_id"], r"^CC\d{3}$")
2741+
self.assertTrue(
2742+
check["docs_url"].startswith("https://commit-check.com/rules/#")
2743+
)
2744+
return rc, data
2745+
2746+
def test_passing_message(self):
2747+
rc, data = self._run("fix: handle the empty case\n")
2748+
self.assertEqual(rc, 0)
2749+
self.assertEqual(data["status"], "pass")
2750+
self.assertTrue(all(c["status"] == "pass" for c in data["checks"]))
2751+
scope = main.ScopeResult(label="Commit 1/1", checks=data["checks"])
2752+
self.assertEqual(scope.status, "pass")
2753+
self.assertEqual(main.overall_status([scope]), "pass")
2754+
2755+
def test_failing_message(self):
2756+
rc, data = self._run("Bad subject\n")
2757+
self.assertEqual(rc, 1)
2758+
self.assertEqual(data["status"], "fail")
2759+
failed = [c for c in data["checks"] if c["status"] == "fail"]
2760+
self.assertEqual([c["rule_id"] for c in failed], ["CC001"])
2761+
self.assertTrue(failed[0]["error"])
2762+
scope = main.ScopeResult(label="Commit 1/1", checks=data["checks"])
2763+
self.assertEqual(scope.status, "fail")
2764+
self.assertEqual(main.overall_status([scope]), "fail")
2765+
# The rendered report must carry the rule link the CLI supplied.
2766+
self.assertIn(
2767+
"[CC001 message](https://commit-check.com/rules/#cc001)",
2768+
main.render_report([scope]),
2769+
)

0 commit comments

Comments
 (0)