Debian Codegraph @

tests/unit_tests/test_documented_flags.py

raw
1 """Every long flag the packaged README and the skill type is a flag hsql has.
2
3 The README is the first thing a person reads about hsql and the skill is the
4 first thing an agent reads; a command line in either is one that gets run
5 verbatim. Nothing else in this repo reads them: the CLI is tested thoroughly and
6 its documentation is not, so a flag that was renamed, or one that never existed,
7 sits in a fenced code block until someone pastes it and gets `No such option`
8 and exit 2.
9
10 So: pull every `--long-flag` out of the prose and the fences alike, and check
11 each one against the document hsql writes about itself. Long spellings only --
12 `-tAc` is a bundle of three, and taking single-dash tokens apart is guesswork
13 this does not need to do. What it cannot check is a flag spelled correctly that
14 the prose then describes wrongly; that stays a review problem.
15 """
16
17 from __future__ import annotations
18
19 import json
20 import re
21 from io import BytesIO
22 from pathlib import Path
23
24 import pytest
25
26 from harlequin.hsql.modes import spec
27
28 REPO_ROOT = Path(__file__).resolve().parents[2]
29 HSQL_README = REPO_ROOT / "packaging" / "hsql" / "README.md"
30 SKILL_DIR = REPO_ROOT / "src" / "harlequin" / "hsql" / "skill"
31
32 DOCUMENTS = [
33 HSQL_README,
34 SKILL_DIR / "SKILL.md",
35 *sorted(SKILL_DIR.glob("references/*.md")),
36 ]
37 """Every file in this repo that types an hsql command line at a reader."""
38
39 LONG_FLAG = re.compile(r"--[a-zA-Z][a-zA-Z0-9_-]*")
40 """A long option as a command line spells one, underscores included, since an
41 adapter may declare `--md_token`. Stops at `=`, so `--format=csv` is `--format`."""
42
43 OTHER_PROGRAMS = {
44 "--with": "uv, in the README's install lines",
45 "--pset": "psql, in the README's differences table",
46 }
47 """Flags the README types that belong to another program. Named one at a time,
48 with the reason, so that excusing a flag is a decision someone made rather than
49 a pattern that quietly swallowed the next typo."""
50
51
52 def documented_flags(text: str) -> set[str]:
53 """Every long flag a document types, minus the ones another program owns."""
54 return set(LONG_FLAG.findall(text)) - set(OTHER_PROGRAMS)
55
56
57 def hsql_flags() -> set[str]:
58 """Every long flag `hsql --spec` reports: hsql's own, and each adapter's.
59
60 Read out of the document rather than off the click command, so that this
61 checks the surface hsql advertises -- an adapter option whose spelling
62 collides with one of hsql's is dropped from both.
63 """
64 document = BytesIO()
65 spec.report(document, adapter=None, format_name=spec.JSON, format_chosen=False)
66 reported = json.loads(document.getvalue())
67 declared = [
68 *reported["options"],
69 *(
70 option
71 for adapter in reported["adapters"].values()
72 for option in adapter["options"] or []
73 ),
74 ]
75 return {
76 decl for option in declared for decl in option["decls"] if decl.startswith("--")
77 }
78
79
80 @pytest.mark.parametrize("document", DOCUMENTS, ids=lambda path: path.name)
81 def test_every_flag_a_document_types_exists(document: Path) -> None:
82 """The one that fails when a documented command line would exit 2."""
83 missing = documented_flags(document.read_text(encoding="utf-8")) - hsql_flags()
84 assert not missing, (
85 f"{document.name} documents flags hsql does not have: "
86 f"{', '.join(sorted(missing))}. Run `hsql --spec` for the real spellings."
87 )
88
89
90 def test_the_skill_is_covered() -> None:
91 """The parametrization reads the skill off the disk, so an empty glob would
92 have passed silently -- and the references are where the flags are."""
93 names = {path.name for path in DOCUMENTS}
94 assert "SKILL.md" in names
95 assert len(DOCUMENTS) >= 4
96
97
98 def test_the_allowlist_names_only_flags_hsql_does_not_have() -> None:
99 """An entry that shadowed a real flag would hide the next rename of it."""
100 assert not set(OTHER_PROGRAMS) & hsql_flags()
101
102
103 def test_the_allowlist_names_only_flags_a_document_still_types() -> None:
104 """So an entry outlives the line it was written for by one PR at most."""
105 typed = {
106 flag
107 for document in DOCUMENTS
108 for flag in LONG_FLAG.findall(document.read_text(encoding="utf-8"))
109 }
110 assert set(OTHER_PROGRAMS) <= typed
111
112
113 def test_the_extractor_reads_flags_out_of_prose_and_out_of_fences() -> None:
114 assert documented_flags(
115 "Pass `--read-only`, or `--limit -1`.\n"
116 "```bash\n"
117 "$ hsql -tAc 'select 1' --format=csv --md_token TOKEN -o out.csv\n"
118 "```\n"
119 "A negative number -- like -1 -- is unlimited.\n"
120 ) == {"--read-only", "--limit", "--format", "--md_token"}