codex-python / tests /test_adapter.py
tailot's picture
Upload folder using huggingface_hub
339ddd8 verified
Raw History Blame
3.53 kB
"""Tests for PythonLanguageAdapter, StrictPythonLanguageAdapter, and codex loading."""
from __future__ import annotations
from pathlib import Path
import pytest
from lgcodex.adapter_contract import (
ADAPTER_SPEC_VERSION,
CommandLanguageAdapter,
WorkspaceBoundaryError,
check_adapter_contract,
load_adapter_from_file,
load_adapter_from_ref,
)
from lgcodex.types import ABSTAIN, CONFIRMED, REJECTED
from lgpython.adapter import PythonLanguageAdapter, StrictPythonLanguageAdapter
from lgpython.ast_graph import discover_python_files
from lgpython.gates import resolve_tool_gate_prefix
def test_python_adapter_passes_full_codex_contract(sample_pkg: Path) -> None:
adapter = PythonLanguageAdapter(sample_pkg)
files = discover_python_files(sample_pkg)
violations = check_adapter_contract(adapter, str(sample_pkg), files)
assert violations == []
def test_adapter_metadata_and_env(sample_pkg: Path) -> None:
adapter = PythonLanguageAdapter(sample_pkg)
meta = adapter.meta()
assert meta.name == "lingologos-python"
assert meta.languages == ("python",)
assert ".py" in meta.extensions
assert meta.spec_version == ADAPTER_SPEC_VERSION
env = adapter.command_env()
assert env["PYTHONDONTWRITEBYTECODE"] == "1"
assert str(sample_pkg.resolve()) in env["PYTHONPATH"]
def test_syntax_gate_rejects_broken_python_file(sample_pkg: Path) -> None:
broken = sample_pkg / "src" / "broken.py"
broken.write_text("def bad(:\n return 1\n", encoding="utf-8")
adapter = PythonLanguageAdapter(sample_pkg)
gates = adapter.run_typecheck_and_lint(str(sample_pkg), ["src/broken.py"])
assert len(gates) == 1
assert gates[0].verdict == REJECTED
assert "src/broken.py" in gates[0].failed_targets
def test_strict_adapter_abstains_when_tool_is_missing(sample_pkg: Path) -> None:
adapter = StrictPythonLanguageAdapter(sample_pkg)
gates = adapter.run_typecheck_and_lint(str(sample_pkg), ["src/mathops.py"])
assert gates[0].verdict == CONFIRMED
for tool_name, gate in zip(("mypy", "ruff"), gates[1:]):
if resolve_tool_gate_prefix(tool_name) is None:
assert gate.verdict == ABSTAIN
assert tool_name in gate.note
def test_custom_missing_tool_always_abstains(sample_pkg: Path) -> None:
adapter = PythonLanguageAdapter(sample_pkg, require_tools=("nonexistent_checker_xyz",))
gates = adapter.run_typecheck_and_lint(str(sample_pkg), ["src/mathops.py"])
assert gates[0].verdict == CONFIRMED
assert gates[1].verdict == ABSTAIN
assert "nonexistent_checker_xyz" in gates[1].note
def test_workspace_escape_is_refused(sample_pkg: Path) -> None:
adapter = PythonLanguageAdapter(sample_pkg)
with pytest.raises(WorkspaceBoundaryError):
adapter.run_tests("..", [], full_suite=True)
def test_load_adapter_via_codex_ref_and_declarative_spec(sample_pkg: Path) -> None:
python_root = Path(__file__).resolve().parent.parent
by_module = load_adapter_from_ref("lgpython.adapter:PythonLanguageAdapter", sample_pkg)
assert isinstance(by_module, PythonLanguageAdapter)
adapter_file = python_root / "lgpython" / "adapter.py"
by_file = load_adapter_from_ref(f"{adapter_file}:PythonLanguageAdapter", sample_pkg)
assert by_file.meta().name == "lingologos-python"
spec_adapter = load_adapter_from_file(python_root / "codex.python.json", sample_pkg)
assert isinstance(spec_adapter, CommandLanguageAdapter)
assert spec_adapter.meta().languages == ("python",)