from __future__ import annotations import json from pathlib import Path import urllib.request import scripts.publish_test_metrics as metrics_script class DummyHTTPResponse: def __init__(self, body: str, status: int = 200): self._body = body self.status = status def __enter__(self): return self def __exit__(self, exc_type, exc, tb): return False def read(self) -> bytes: return self._body.encode("utf-8") def test_build_payload_includes_quality_gate_metrics(monkeypatch, tmp_path: Path) -> None: coverage_path = tmp_path / "coverage.json" coverage_path.write_text(json.dumps({"summary": {"percent_covered": 97.5}}), encoding="utf-8") junit_path = tmp_path / "junit.xml" junit_path.write_text('', encoding="utf-8") quality_gate_path = tmp_path / "quality-gate.json" quality_gate_path.write_text( json.dumps( { "status": "ok", "summary": { "line_count_violations": 0, "docstring_violations": 1, "coverage_violations": 0, "legacy_line_count_files": 2, "legacy_docstring_files": 3, "coverage_exemptions": 4, }, "files": { "ariadne/auth/keycloak.py": { "lines": 120, "missing_docstrings": 0, "coverage_percent": 99.1, } }, } ), encoding="utf-8", ) monkeypatch.setenv("COVERAGE_JSON", str(coverage_path)) monkeypatch.setenv("JUNIT_XML", str(junit_path)) monkeypatch.setenv("QUALITY_GATE_JSON", str(quality_gate_path)) monkeypatch.setenv("PUSHGATEWAY_URL", "http://pushgateway.example") monkeypatch.setenv("SUITE_NAME", "ariadne") monkeypatch.setenv("QUALITY_STATUS", "ok") monkeypatch.setenv("BUILD_NUMBER", "42") monkeypatch.setenv("BRANCH_NAME", "main") monkeypatch.setenv("GIT_COMMIT", "abc123") monkeypatch.setattr( metrics_script, "_read_http", lambda _url: 'platform_quality_gate_runs_total{job="platform-quality-ci",suite="ariadne",status="ok"} 5\n', ) payload, result = metrics_script._build_payload() assert result["tests_total"] == 5 assert result["tests_passed"] == 2 assert result["coverage_percent"] == 97.5 assert result["ok_counter"] == 6.0 assert 'ariadne_quality_gate_artifact_present{suite="ariadne",artifact="quality_gate_json"} 1' in payload assert 'ariadne_quality_gate_violation_total{suite="ariadne",check="docstrings"} 1' in payload assert 'ariadne_quality_gate_file_coverage_percent{suite="ariadne",file="ariadne/auth/keycloak.py"} 99.1' in payload def test_main_posts_payload_for_failed_build_with_missing_artifacts(monkeypatch, capsys) -> None: requests: list[tuple[str, str]] = [] missing_base = Path("/tmp/ariadne-metrics-missing") def fake_urlopen(target, timeout=10): if isinstance(target, urllib.request.Request): requests.append((target.full_url, target.data.decode("utf-8"))) return DummyHTTPResponse("", status=200) return DummyHTTPResponse("", status=200) monkeypatch.setenv("PUSHGATEWAY_URL", "http://pushgateway.example") monkeypatch.setenv("SUITE_NAME", "ariadne") monkeypatch.setenv("QUALITY_STATUS", "failed") monkeypatch.setenv("COVERAGE_JSON", str(missing_base / "coverage.json")) monkeypatch.setenv("JUNIT_XML", str(missing_base / "junit.xml")) monkeypatch.setenv("QUALITY_GATE_JSON", str(missing_base / "quality-gate.json")) monkeypatch.setattr(metrics_script.urllib.request, "urlopen", fake_urlopen) assert metrics_script.main() == 0 assert requests assert 'ariadne_quality_gate_artifact_present{suite="ariadne",artifact="coverage_json"} 0' in requests[0][1] assert 'ariadne_quality_gate_status{suite="ariadne"} 0' in requests[0][1] output = capsys.readouterr().out assert '"quality_gate_status": "missing"' in output