Skip to content

Commit 7315edf

Browse files
authored
fix: print the display name in compact output, not the config key (#529)
* fix: print the display name in compact output, not the config key The same failing check is named two ways depending on the format: $ commit-check -m --subject-imperative=true --no-banner CC003 subject-imperative check failed ==> docs: revamped the profile $ commit-check -m --subject-imperative=true --compact [FAIL] CC003 subject_imperative: docs: revamped the profile Both are text written for a person, so they should agree. The kebab-case form is the one that agrees with the rules reference, whose headings read `subject-imperative (CC003)` — so a name printed to a terminal can be searched for there verbatim, which is why the default output already converts. Every other human-facing surface in the ecosystem does the same: commit-check-action converts the JSON `check` field before showing it in step logs, job summaries and pull request comments. Compact was the only one left printing the raw key. The JSON output is deliberately unchanged. Its `check` field carries the snake_case key so a consumer can map a failure back to the `cchk.toml` option that controls it, which is what the action relies on. Both call sites now go through one helper rather than one of them open-coding the conversion, which is how they drifted apart to begin with. Refs #528 * refactor: keep one implementation of the check display name Review pointed out that RuleCatalogEntry.name already did the same snake-to-kebab conversion, so the helper added in the previous commit was a second copy and its docstring claim that every text surface goes through it was false. Two copies of the rule this change exists to enforce is the wrong number. The helper moves to rules_catalog rather than the catalog importing it from util. rules_catalog is a pure data module — dataclasses and nothing else — and commit-check-mcp imports it directly, so it should not gain a dependency on a module that pulls in os, sys and subprocess. Pointing the arrow the other way keeps the catalog light and puts the formatter beside the entry whose name property defines the concept. Verified there is now one implementation, that both call sites reach it, and that importing util, rules_catalog, engine and main together raises no cycle.
1 parent 1072b87 commit 7315edf

3 files changed

Lines changed: 50 additions & 5 deletions

File tree

‎commit_check/rules_catalog.py‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,22 @@
2424
RULES_DOCS_URL = "https://commit-check.com/rules/"
2525

2626

27+
def display_name(check: str) -> str:
28+
"""Human-readable form of a check name, e.g. ``subject-imperative``.
29+
30+
Config files and the JSON output carry the snake_case key, because that is
31+
what a reader sets in ``cchk.toml`` and what a consumer maps back to an
32+
option. Text written for a person uses the kebab-case form instead: it is
33+
how the rules reference titles each rule, so a name printed to a terminal
34+
can be searched for there verbatim.
35+
36+
Every text surface goes through here so the two forms cannot drift apart
37+
again — the compact output once printed the config key while the default
38+
output printed this one.
39+
"""
40+
return check.replace("_", "-")
41+
42+
2743
@dataclass(frozen=True)
2844
class RuleCatalogEntry:
2945
check: str
@@ -35,7 +51,7 @@ class RuleCatalogEntry:
3551
@property
3652
def name(self) -> str:
3753
"""Human-readable rule name, e.g. ``subject-imperative``."""
38-
return self.check.replace("_", "-")
54+
return display_name(self.check)
3955

4056
@property
4157
def docs_url(self) -> str | None:

‎commit_check/util.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import sys
1212
from subprocess import CalledProcessError
1313
from commit_check import RED, GREEN, YELLOW, RESET_COLOR
14+
from commit_check.rules_catalog import display_name
1415

1516

1617
def _print_failure(
@@ -23,7 +24,8 @@ def _print_failure(
2324
rule_id = check.get("rule_id", "")
2425
if compact:
2526
compact_value = actual.splitlines()[0] if actual else actual
26-
label = f"{rule_id} {check['check']}" if rule_id else check["check"]
27+
name = display_name(check["check"])
28+
label = f"{rule_id} {name}" if rule_id else name
2729
print(f"[FAIL] {label}: {compact_value}")
2830
return
2931
if not no_banner and not print_error_header.has_been_called:
@@ -364,9 +366,7 @@ def print_error_message(
364366
365367
:returns: Give error messages to user
366368
"""
367-
# The kebab-case form is what the rules reference uses as its headings, so
368-
# the name printed here can be searched for there verbatim.
369-
name = check_type.replace("_", "-")
369+
name = display_name(check_type)
370370
label = rule_id
371371
if rule_id and docs_url and supports_hyperlinks():
372372
label = hyperlink(rule_id, docs_url)

‎tests/main_test.py‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,35 @@ def test_compact_shows_one_line_per_failure(self, mocker, capsys, monkeypatch):
728728
assert all(line.startswith("[FAIL]") for line in lines)
729729
assert len(lines) >= 1
730730

731+
@pytest.mark.benchmark
732+
def test_compact_names_checks_the_way_the_default_output_does(
733+
self, mocker, capsys, monkeypatch
734+
):
735+
"""--compact prints the kebab-case name, not the config key.
736+
737+
Both are text written for a person, so they have to agree. This
738+
assertion is the one the suite was missing: --compact shipped
739+
printing ``subject_imperative`` while the default output printed
740+
``subject-imperative``, and nothing here noticed.
741+
"""
742+
mocker.patch("sys.stdin.isatty", return_value=False)
743+
mocker.patch("sys.stdin.read", return_value="docs: revamped the profile\n")
744+
mocker.patch("commit_check.engine.get_commit_info", return_value="test-author")
745+
746+
# A check whose name contains an underscore, so the two forms differ.
747+
monkeypatch.setattr(
748+
"sys.argv", [CMD, "-m", "--compact", "--subject-imperative=true"]
749+
)
750+
main()
751+
752+
out, _ = capsys.readouterr()
753+
assert "CC003 subject-imperative:" in out, (
754+
f"--compact should print the display name: {out!r}"
755+
)
756+
assert "subject_imperative" not in out, (
757+
f"--compact printed the config key: {out!r}"
758+
)
759+
731760
@pytest.mark.benchmark
732761
def test_compact_no_suggestions(self, mocker, capsys, monkeypatch):
733762
"""--compact output must not include 'Suggest:' lines."""

0 commit comments

Comments
 (0)