ariadne/tests/test_quality_gate.py

118 lines
3.4 KiB
Python

from __future__ import annotations
import json
from pathlib import Path
import sys
import scripts.check_quality_gate as quality_gate
def _write_quality_config(path: Path) -> None:
path.write_text(
"\n".join(
[
"[files]",
'roots = ["pkg"]',
"max_lines = 10",
"",
"[docstrings]",
'roots = ["pkg"]',
"non_trivial_min_lines = 4",
"",
"[coverage]",
'roots = ["pkg"]',
"threshold = 95.0",
'targets = ["pkg/good.py"]',
"",
"[legacy.line_count]",
'"pkg/legacy.py" = 12',
"",
"[legacy.docstrings]",
'"pkg/legacy.py" = 1',
]
)
+ "\n",
encoding="utf-8",
)
def test_build_report_accepts_legacy_exceptions(tmp_path: Path) -> None:
pkg = tmp_path / "pkg"
pkg.mkdir()
(pkg / "good.py").write_text(
'\n'.join(
[
'def documented(value: int) -> int:',
' """Return a simple incremented value.',
"",
" Inputs: an integer to increment.",
" Outputs: the incremented integer so the module stays fully documented.",
' """',
" return value + 1",
]
)
+ "\n",
encoding="utf-8",
)
(pkg / "legacy.py").write_text(
"\n".join(
[
"class Legacy:",
" pass",
]
+ ["# filler"] * 10
)
+ "\n",
encoding="utf-8",
)
coverage_path = tmp_path / "coverage.json"
coverage_path.write_text(
json.dumps({"files": {"pkg/good.py": {"summary": {"percent_covered": 99.0}}}}),
encoding="utf-8",
)
config_path = tmp_path / "quality_gate.toml"
_write_quality_config(config_path)
config = quality_gate._load_config(config_path)
report = quality_gate._build_report(
tmp_path,
config,
{"pkg/good.py": {"adjusted_percent": 99.0, "raw_percent": 99.0, "excluded_lines": []}},
True,
)
assert report["status"] == "ok"
assert report["summary"]["legacy_line_count_files"] == 1
assert report["files"]["pkg/good.py"]["coverage_enforced"] is True
def test_main_fails_when_new_violations_appear(monkeypatch, tmp_path: Path) -> None:
pkg = tmp_path / "pkg"
pkg.mkdir()
(pkg / "good.py").write_text(
"\n".join(
[
"def undocumented(value):",
" total = value + 1",
" total += 1",
" total += 1",
" return total",
]
)
+ "\n",
encoding="utf-8",
)
config_path = tmp_path / "quality_gate.toml"
_write_quality_config(config_path)
monkeypatch.chdir(tmp_path)
monkeypatch.setattr(
sys,
"argv",
["check_quality_gate.py", "--config", "quality_gate.toml", "--coverage-json", "missing.json", "--output", "out.json"],
)
assert quality_gate.main() == 1
report = json.loads((tmp_path / "out.json").read_text(encoding="utf-8"))
assert report["summary"]["docstring_violations"] >= 1
assert report["summary"]["coverage_violations"] >= 1