codex-python / tests /test_cli.py
tailot's picture
Upload folder using huggingface_hub
2a9dc4c verified
Raw History Blame
6.13 kB
"""The command line is a published interface, so it is run as a process here.
Nothing in this file reaches into the CLI module. Each test starts the same process a
reader of the card would start, and asserts on the exit code and on the text that comes
back, because a documented command that crashes is a broken promise even when every
library call beside it is correct.
"""
from __future__ import annotations
import hashlib
import json
import subprocess
import sys
from pathlib import Path
from typing import Any, Dict
from lingologos_python.patterns import PATTERNS
from lingologos_python.rules import RULE_IDS
ROOT = Path(__file__).resolve().parents[1]
LAB = ROOT / "fixtures" / "pattern_lab"
SAMPLE = ROOT / "fixtures" / "sample_pkg"
def run_cli(*args: str) -> subprocess.CompletedProcess:
return subprocess.run(
[sys.executable, "-B", "-m", "lingologos_python", *args],
cwd=str(ROOT),
capture_output=True,
text=True,
)
def _tree_state(root: Path) -> Dict[str, str]:
state = {}
for path in sorted(root.rglob("*")):
if path.is_file():
digest = hashlib.sha256(path.read_bytes()).hexdigest()
state[str(path.relative_to(root))] = digest
return state
def test_patterns_list_prints_every_card_once_in_a_stable_order() -> None:
proc = run_cli("patterns", "list")
assert proc.returncode == 0, proc.stdout + proc.stderr
lines = [line for line in proc.stdout.splitlines() if line.strip()]
assert len(lines) == len(PATTERNS)
assert [line.split()[0] for line in lines] == sorted(PATTERNS)
for line in lines:
assert line.split()[1] in {"contracts", "mutation", "resources", "shape", "typing"}
def test_patterns_list_json_carries_the_fields_a_reader_needs() -> None:
proc = run_cli("patterns", "list", "--json")
assert proc.returncode == 0, proc.stdout + proc.stderr
rows = json.loads(proc.stdout)["patterns"]
assert [row["pattern_id"] for row in rows] == sorted(PATTERNS)
for row in rows:
assert set(row) == {
"pattern_id",
"family",
"intent_tags",
"min_assurance",
"rule",
"companions",
}
assert row["rule"] in RULE_IDS
assert row["companions"]
assert row["min_assurance"] >= 2
def test_patterns_show_answers_for_one_card_in_text_and_in_json() -> None:
proc = run_cli("patterns", "show", "--pattern", "chain-to-registry")
assert proc.returncode == 0, proc.stdout + proc.stderr
for part in ("shape", "why", "needs", "lock", "refuses"):
assert part in proc.stdout
assert "BRANCH_SHAPE_MISMATCH" in proc.stdout
proc = run_cli("patterns", "show", "--pattern", "chain-to-registry", "--json")
assert proc.returncode == 0, proc.stdout + proc.stderr
payload: Dict[str, Any] = json.loads(proc.stdout)
assert payload["pattern_id"] == "chain-to-registry"
assert payload["lock_claims"]
def test_an_unknown_card_and_a_missing_flag_answer_before_any_work_starts() -> None:
proc = run_cli("patterns", "show", "--pattern", "no-such-card")
assert proc.returncode == 2
assert "unknown pattern" in proc.stderr
proc = run_cli("patterns", "scan", "--workspace", str(LAB))
assert proc.returncode == 2
assert "--pattern" in proc.stderr
proc = run_cli("patterns", "apply", "--pattern", "chain-to-registry")
assert proc.returncode == 2
assert "--workspace" in proc.stderr
def test_patterns_scan_names_the_functions_in_the_shape_of_the_card() -> None:
proc = run_cli("patterns", "scan", "--workspace", str(LAB), "--pattern", "chain-to-registry")
assert proc.returncode == 0, proc.stdout + proc.stderr
assert "src/registry.py::render" in proc.stdout
assert "render_pair" not in proc.stdout
def test_patterns_scan_says_so_when_nothing_in_the_workspace_qualifies() -> None:
proc = run_cli("patterns", "scan", "--workspace", str(SAMPLE), "--pattern", "chain-to-registry")
assert proc.returncode == 0, proc.stdout + proc.stderr
assert "no function in this workspace has the shape of this card" in proc.stdout
def test_apply_dry_run_verifies_the_pair_without_writing_it(pattern_lab: Path) -> None:
before = _tree_state(pattern_lab)
proc = run_cli(
"patterns",
"apply",
"--workspace",
str(pattern_lab),
"--pattern",
"chain-to-registry",
"--target",
"src/registry.py::render",
"--dry-run",
)
assert proc.returncode == 0, proc.stdout + proc.stderr
assert "VERIFIED" in proc.stdout
assert "committed=False" in proc.stdout
assert _tree_state(pattern_lab) == before
def test_a_shape_the_card_does_not_claim_exits_non_zero_and_writes_nothing(pattern_lab: Path) -> None:
before = _tree_state(pattern_lab)
proc = run_cli(
"patterns",
"apply",
"--workspace",
str(pattern_lab),
"--pattern",
"chain-to-registry",
"--target",
"src/registry.py::render_pair",
"--dry-run",
)
assert proc.returncode == 1
assert "REFUSED" in proc.stdout
assert "BRANCH_SHAPE_MISMATCH" in proc.stdout
assert "nothing was written" in proc.stdout
assert _tree_state(pattern_lab) == before
def test_symbols_names_the_functions_of_a_workspace_and_ranks_them() -> None:
proc = run_cli("symbols", "--workspace", str(LAB), "--limit", "200")
assert proc.returncode == 0, proc.stdout + proc.stderr
assert "files=" in proc.stdout
assert "src/registry.py::render" in proc.stdout
def test_select_tests_answers_with_the_tests_that_import_the_changed_file() -> None:
proc = run_cli("select-tests", "--workspace", str(LAB), "--files", "src/registry.py")
assert proc.returncode == 0, proc.stdout + proc.stderr
assert "tests/test_registry.py" in proc.stdout.split()
def test_the_adapter_answers_the_contract_of_the_kernel() -> None:
proc = run_cli("check-adapter", "--workspace", str(SAMPLE))
assert proc.returncode == 0, proc.stdout + proc.stderr
assert "ADAPTER OK" in proc.stdout