utils/repo_contract/tests/test_sources.py
Krzysztof kuhy Rudnicki 6c71d531bb repo contract + coverage_gaps gates, test_changed, dependency bumps
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-10-04 16:27:28 +02:00

64 lines
1.9 KiB
Python

"""Command sources: doc, Makefile, package.json, CI."""
from __future__ import annotations
from pathlib import Path
from repo_contract.sources import (
ci_commands,
doc_commands,
first_match,
make_targets,
package_scripts,
read_text,
)
from .conftest import write
def test_read_text_missing(tmp_path: Path) -> None:
assert read_text(tmp_path / "x") == ""
(tmp_path / "d").mkdir()
assert read_text(tmp_path / "d") == ""
def test_doc_commands() -> None:
assert doc_commands("use `pytest -q` and `scripts/x.sh` and `~/a b` and `one`") == [
"pytest -q"
]
def test_make_targets(tmp_path: Path) -> None:
write(tmp_path, "Makefile", "CC := gcc\ntest:\n\tx\nlint-all: y\n")
assert make_targets(tmp_path) == {"test", "lint-all"}
def test_package_scripts(tmp_path: Path) -> None:
assert package_scripts(tmp_path) == {}
write(tmp_path, "package.json", "{bad")
assert package_scripts(tmp_path) == {}
write(tmp_path, "package.json", "[1]")
assert package_scripts(tmp_path) == {}
write(tmp_path, "package.json", '{"scripts": 3}')
assert package_scripts(tmp_path) == {}
write(tmp_path, "package.json", '{"scripts": {"test": "jest"}}')
assert package_scripts(tmp_path) == {"test": "jest"}
def test_ci_commands(tmp_path: Path) -> None:
write(
tmp_path,
".github/workflows/ci.yml",
"steps:\n - run: pytest -q\n - run: |\n multi\n - run: echo ${{ x }}\n run: ruff check .\n",
)
assert ci_commands(tmp_path) == ["pytest -q", "ruff check ."]
def test_first_match() -> None:
cmds = ["flutter test --coverage", "flutter test", "ruff check ."]
assert first_match("test", cmds) == "flutter test"
assert first_match("coverage", cmds) == "flutter test --coverage"
assert first_match("lint", cmds) == "ruff check ."
assert first_match("run", cmds) is None
assert first_match("lint", ["x"]) is None