Download tests/test_cli.py from tailot/codex-python: direct link, hf CLI and curl.
- Browser
- Download file 20 kB
-
https://huggingface.co/datasets/tailot/codex-python/resolve/main/tests/test_cli.py
- Command line
-
hf download hf://datasets/tailot/codex-python/tests/test_cli.py
-
curl -L -o test_cli.py https://huggingface.co/datasets/tailot/codex-python/resolve/main/tests/test_cli.py
20 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 os | |
| import re | |
| import shutil | |
| import subprocess | |
| import sys | |
| import time | |
| 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_shows_the_edits_a_hit_would_apply_and_dumps_nothing() -> None: | |
| proc = run_cli("patterns", "scan", "--workspace", str(LAB), "--pattern", "chain-to-registry") | |
| assert proc.returncode == 0, proc.stdout + proc.stderr | |
| lines = [line.strip() for line in proc.stdout.splitlines() if line.strip()] | |
| assert lines[0] == "chain-to-registry: 1 hit(s) in this workspace" | |
| assert lines[1] == "src/registry.py::render" | |
| assert lines[2].startswith("src/registry.py") | |
| assert lines[3].startswith("tests/test_registry.py") | |
| assert any(line.startswith("why:") for line in lines) | |
| for noise in ("RuleHit", "Edit(", "evidence=", "Traceback"): | |
| assert noise not in proc.stdout | |
| def test_patterns_scan_json_carries_the_edits_a_reader_can_act_on() -> None: | |
| proc = run_cli( | |
| "patterns", | |
| "scan", | |
| "--workspace", | |
| str(LAB), | |
| "--pattern", | |
| "chain-to-registry", | |
| "--json", | |
| ) | |
| assert proc.returncode == 0, proc.stdout + proc.stderr | |
| payload: Dict[str, Any] = json.loads(proc.stdout) | |
| assert payload["pattern"] == "chain-to-registry" | |
| assert len(payload["hits"]) == 1 | |
| hit = payload["hits"][0] | |
| assert hit["target"] == "src/registry.py::render" | |
| assert hit["rule"] == "chain-to-registry" | |
| assert [edit["file"] for edit in hit["edits"]] == ["src/registry.py", "tests/test_registry.py"] | |
| for edit in hit["edits"]: | |
| assert edit["search"] and edit["replace"] and edit["label"] | |
| assert hit["evidence"] and all(hit["evidence"]) | |
| 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_apply_exits_zero_only_once_the_pair_has_landed(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", | |
| ) | |
| assert proc.returncode == 0, proc.stdout + proc.stderr | |
| assert "VERIFIED" in proc.stdout | |
| assert "committed=True" 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 | |
| def _delete_workspace(tmp_path: Path) -> Path: | |
| ws = tmp_path / "delete_pkg" | |
| shutil.copytree(SAMPLE, ws) | |
| (ws / "src" / "helper.py").write_text( | |
| '"""Extra module."""\n\nVALUE = 1\n', encoding="utf-8", newline="" | |
| ) | |
| return ws | |
| def test_delete_dry_run_verifies_the_removal_without_touching_the_disk(tmp_path: Path) -> None: | |
| ws = _delete_workspace(tmp_path) | |
| proc = run_cli("delete", "--workspace", str(ws), "--file", "src/helper.py", "--dry-run") | |
| assert proc.returncode == 0, proc.stdout + proc.stderr | |
| assert "VERIFIED" in proc.stdout | |
| assert "committed=False" in proc.stdout | |
| assert (ws / "src" / "helper.py").is_file() | |
| def test_delete_exits_zero_only_once_the_file_is_really_gone(tmp_path: Path) -> None: | |
| ws = _delete_workspace(tmp_path) | |
| proc = run_cli( | |
| "delete", | |
| "--workspace", | |
| str(ws), | |
| "--file", | |
| "src/helper.py", | |
| "--node-timeout", | |
| "60", | |
| "--total-timeout", | |
| "300", | |
| "--gate-timeout", | |
| "60", | |
| ) | |
| assert proc.returncode == 0, proc.stdout + proc.stderr | |
| assert "committed=True" in proc.stdout | |
| assert not (ws / "src" / "helper.py").exists() | |
| def test_delete_answers_a_refusal_for_a_file_that_is_not_there(tmp_path: Path) -> None: | |
| ws = tmp_path / "delete_empty" | |
| shutil.copytree(SAMPLE, ws) | |
| proc = run_cli("delete", "--workspace", str(ws), "--file", "src/absent.py") | |
| assert proc.returncode == 1 | |
| assert "REFUSED" in proc.stderr | |
| def _hold_the_lock(ws: Path) -> None: | |
| """A lock record naming this live test process, the shape a running session writes.""" | |
| (ws / ".lingologos-session.lock").write_text( | |
| "%d - %s" % (os.getpid(), "0" * 32), encoding="utf-8", newline="" | |
| ) | |
| def test_graph_shows_both_directions_and_the_reverse_index_agrees() -> None: | |
| proc = run_cli("graph", "--workspace", str(LAB)) | |
| assert proc.returncode == 0, proc.stdout + proc.stderr | |
| assert "import_edges=" in proc.stdout | |
| back = [line for line in proc.stdout.splitlines() if line.startswith(" src/registry.py <- ")] | |
| assert back and "tests/test_registry.py" in back[0] | |
| def test_graph_json_lists_the_importers_of_every_file() -> None: | |
| proc = run_cli("graph", "--workspace", str(LAB), "--json") | |
| assert proc.returncode == 0, proc.stdout + proc.stderr | |
| payload = json.loads(proc.stdout) | |
| assert set(payload) == {"files", "import_edges", "importers"} | |
| assert "tests/test_registry.py" in payload["importers"]["src/registry.py"] | |
| assert any(edge[1] == "src/registry.py" for edge in payload["import_edges"]) | |
| for source, target in payload["import_edges"]: | |
| assert source in payload["importers"][target] | |
| def test_graph_refuses_a_workspace_that_is_not_a_directory(tmp_path: Path) -> None: | |
| proc = run_cli("graph", "--workspace", str(tmp_path / "nowhere")) | |
| assert proc.returncode == 1 | |
| assert "does not exist" in proc.stderr | |
| def test_lock_status_reads_a_quiet_workspace_as_free(tmp_path: Path) -> None: | |
| proc = run_cli("lock-status", "--workspace", str(tmp_path), "--json") | |
| assert proc.returncode == 0, proc.stdout + proc.stderr | |
| payload = json.loads(proc.stdout) | |
| assert payload["present"] is False | |
| assert payload["held"] is False | |
| text = run_cli("lock-status", "--workspace", str(tmp_path)) | |
| assert text.returncode == 0 | |
| assert "free: no lock file" in text.stdout | |
| def test_lock_status_names_a_live_holder_and_says_held(tmp_path: Path) -> None: | |
| _hold_the_lock(tmp_path) | |
| proc = run_cli("lock-status", "--workspace", str(tmp_path)) | |
| assert proc.returncode == 1 | |
| assert "held: process %d" % os.getpid() in proc.stdout | |
| def test_lock_status_reads_a_leftover_lock_as_free(tmp_path: Path) -> None: | |
| (tmp_path / ".lingologos-session.lock").write_text( | |
| "999999 - %s" % ("0" * 32), encoding="utf-8", newline="" | |
| ) | |
| proc = run_cli("lock-status", "--workspace", str(tmp_path)) | |
| assert proc.returncode == 0 | |
| assert "no longer runs" in proc.stdout | |
| def test_a_held_workspace_refuses_at_once_and_a_wait_budget_is_honoured(tmp_path: Path) -> None: | |
| ws = _delete_workspace(tmp_path) | |
| _hold_the_lock(ws) | |
| started = time.monotonic() | |
| proc = run_cli( | |
| "delete", "--workspace", str(ws), "--file", "src/helper.py", | |
| "--dry-run", "--lock-wait", "0", | |
| ) | |
| at_once = time.monotonic() - started | |
| assert proc.returncode == 1 | |
| assert "SESSION_LOCKED" in proc.stdout | |
| started = time.monotonic() | |
| proc = run_cli( | |
| "delete", "--workspace", str(ws), "--file", "src/helper.py", | |
| "--dry-run", "--lock-wait", "0.6", | |
| ) | |
| waited = time.monotonic() - started | |
| assert proc.returncode == 1 | |
| assert "SESSION_LOCKED" in proc.stdout | |
| assert waited >= at_once + 0.3 | |
| def test_a_config_file_sets_the_same_knobs_and_a_flag_still_wins(tmp_path: Path) -> None: | |
| ws = _delete_workspace(tmp_path) | |
| _hold_the_lock(ws) | |
| config = tmp_path / "codex.python.json" | |
| config.write_text(json.dumps({"lock_wait": -5}), encoding="utf-8") | |
| refused = run_cli( | |
| "delete", "--workspace", str(ws), "--file", "src/helper.py", "--config", str(config) | |
| ) | |
| assert refused.returncode == 2 | |
| assert "REFUSED" in refused.stderr | |
| config.write_text(json.dumps({"lock_wait": 1.5}), encoding="utf-8") | |
| started = time.monotonic() | |
| overridden = run_cli( | |
| "delete", "--workspace", str(ws), "--file", "src/helper.py", | |
| "--config", str(config), "--lock-wait", "0", | |
| ) | |
| at_once = time.monotonic() - started | |
| assert overridden.returncode == 1 | |
| assert "SESSION_LOCKED" in overridden.stdout | |
| started = time.monotonic() | |
| followed = run_cli( | |
| "delete", "--workspace", str(ws), "--file", "src/helper.py", "--config", str(config) | |
| ) | |
| waited = time.monotonic() - started | |
| assert followed.returncode == 1 | |
| assert "SESSION_LOCKED" in followed.stdout | |
| assert waited >= at_once + 1.2 | |
| def test_a_config_file_that_makes_no_sense_refuses_with_exit_two(tmp_path: Path) -> None: | |
| ws = tmp_path / "quiet" | |
| ws.mkdir() | |
| config = tmp_path / "bad.json" | |
| config.write_text(json.dumps({"invented": 1}), encoding="utf-8") | |
| unknown = run_cli("verify", "--workspace", str(ws), "--config", str(config)) | |
| assert unknown.returncode == 2 | |
| assert "unknown key" in unknown.stderr | |
| config.write_text("{ this is not json", encoding="utf-8") | |
| broken = run_cli("verify", "--workspace", str(ws), "--config", str(config)) | |
| assert broken.returncode == 2 | |
| assert "not JSON" in broken.stderr | |
| config.write_text(json.dumps({"strict_tools": "yes"}), encoding="utf-8") | |
| mistyped = run_cli("verify", "--workspace", str(ws), "--config", str(config)) | |
| assert mistyped.returncode == 2 | |
| assert "true or false" in mistyped.stderr | |
| def test_an_audit_file_collects_one_json_line_per_session_run(tmp_path: Path) -> None: | |
| ws = _delete_workspace(tmp_path) | |
| _hold_the_lock(ws) | |
| audit = tmp_path / "ledger.jsonl" | |
| for _ in range(2): | |
| proc = run_cli( | |
| "delete", "--workspace", str(ws), "--file", "src/helper.py", | |
| "--dry-run", "--lock-wait", "0", "--audit", str(audit), | |
| ) | |
| assert proc.returncode == 1 | |
| assert "SESSION_LOCKED" in proc.stdout | |
| lines = audit.read_text(encoding="utf-8").splitlines() | |
| assert len(lines) == 2 | |
| for line in lines: | |
| row = json.loads(line) | |
| assert row["command"] == "delete" | |
| assert row["verdict"] == "REFUSED" | |
| assert row["reason"] == "SESSION_LOCKED" | |
| assert row["committed"] is False | |
| def test_the_exit_code_table_in_the_card_holds_for_the_real_commands() -> None: | |
| readme = (ROOT / "README.md").read_text(encoding="utf-8", newline="") | |
| table: Dict[str, Dict[int, str]] = {} | |
| for line in readme.splitlines(): | |
| if not line.startswith("| `"): | |
| continue | |
| cells = [cell.strip() for cell in line.strip().strip("|").split("|")] | |
| if len(cells) != 4: | |
| continue | |
| codes = {0: cells[1], 1: cells[2], 2: cells[3]} | |
| for verb in cells[0].strip("`").split("`, `"): | |
| table[verb] = codes | |
| assert table, "the card lost its exit code table" | |
| surface = re.search(r"\{([a-z][a-z, |-]+)\}", run_cli("--help").stdout) | |
| assert surface is not None, "the parser surface no longer prints in the usage line" | |
| for verb in (part.strip() for part in surface.group(1).split(",")): | |
| covered = any(name == verb or name.startswith(verb + " ") for name in table) | |
| assert covered, "the table forgot the verb " + verb | |
| probes = [ | |
| (("patterns", "show", "--pattern", "no-such-card"), "patterns show", 2), | |
| (("graph", "--workspace", str(ROOT / "no-such-workspace")), "graph", 1), | |
| (("lock-status", "--workspace", str(LAB)), "lock-status", 0), | |
| (("symbols", "--workspace", str(SAMPLE)), "symbols", 0), | |
| (("verify", "--workspace", str(SAMPLE), "--file", "src/mathops.py"), "verify", 0), | |
| ] | |
| for argv, verb, expected in probes: | |
| proc = run_cli(*argv) | |
| assert proc.returncode == expected, "%s answered %d, not %d" % ( | |
| " ".join(argv), | |
| proc.returncode, | |
| expected, | |
| ) | |
| cell = table[verb][expected] | |
| assert cell and cell != "--", "%s answers %d but the table leaves that cell empty" % ( | |
| verb, | |
| expected, | |
| ) | |
| def test_protect_adds_one_more_path_the_session_will_not_write(tmp_path: Path) -> None: | |
| ws = tmp_path / "guarded" | |
| shutil.copytree(SAMPLE, ws) | |
| before = (ws / "src" / "mathops.py").read_text(encoding="utf-8") | |
| proc = run_cli( | |
| "edit", | |
| "--workspace", | |
| str(ws), | |
| "--file", | |
| "src/mathops.py", | |
| "--search", | |
| "return left + right", | |
| "--replace", | |
| "return left - right", | |
| "--protect", | |
| "src/mathops.py", | |
| ) | |
| assert proc.returncode == 1, proc.stdout + proc.stderr | |
| assert "PROTECTED_PATH" in proc.stdout | |
| assert (ws / "src" / "mathops.py").read_text(encoding="utf-8") == before | |
| def test_print_diff_shows_the_pair_before_the_verdict(tmp_path: Path) -> None: | |
| ws = _delete_workspace(tmp_path) | |
| proc = run_cli( | |
| "edit", | |
| "--workspace", | |
| str(ws), | |
| "--file", | |
| "src/mathops.py", | |
| "--search", | |
| "return left + right", | |
| "--replace", | |
| "return left + right # the sum keeps its name", | |
| "--dry-run", | |
| "--print-diff", | |
| ) | |
| assert proc.returncode == 0, proc.stdout + proc.stderr | |
| assert "--- a/src/mathops.py" in proc.stdout | |
| assert "+++ b/src/mathops.py" in proc.stdout | |
| assert "-return left + right" in proc.stdout | |
| assert "+return left + right # the sum keeps its name" in proc.stdout | |
| def test_delete_prints_what_the_removal_takes_away(tmp_path: Path) -> None: | |
| ws = _delete_workspace(tmp_path) | |
| proc = run_cli( | |
| "delete", | |
| "--workspace", | |
| str(ws), | |
| "--file", | |
| "src/helper.py", | |
| "--dry-run", | |
| "--print-diff", | |
| ) | |
| assert proc.returncode == 0, proc.stdout + proc.stderr | |
| assert "--- a/src/helper.py" in proc.stdout | |
| assert "+++ /dev/null" in proc.stdout | |
| assert "-VALUE = 1" in proc.stdout | |
| def test_a_card_shows_its_pair_before_the_verdict(pattern_lab: Path) -> None: | |
| proc = run_cli( | |
| "patterns", | |
| "apply", | |
| "--workspace", | |
| str(pattern_lab), | |
| "--pattern", | |
| "chain-to-registry", | |
| "--target", | |
| "src/registry.py::render", | |
| "--dry-run", | |
| "--print-diff", | |
| ) | |
| assert proc.returncode == 0, proc.stdout + proc.stderr | |
| assert "--- a/src/registry.py" in proc.stdout | |
| assert "--- a/tests/test_registry.py" in proc.stdout | |