diff --git a/testing/tests/test_publish_test_metrics.py b/testing/tests/test_publish_test_metrics.py index e0aa8bbb..ff1b9ca7 100644 --- a/testing/tests/test_publish_test_metrics.py +++ b/testing/tests/test_publish_test_metrics.py @@ -58,6 +58,18 @@ def test_parse_junit_handles_testsuites_and_invalid_counts(tmp_path: Path): } +def test_parse_junit_handles_unknown_root(tmp_path: Path): + junit_path = tmp_path / "suite.xml" + junit_path.write_text("", encoding="utf-8") + + assert publish_test_metrics._parse_junit(str(junit_path)) == { + "tests": 0, + "failures": 0, + "errors": 0, + "skipped": 0, + } + + def test_read_exit_code_and_summary_fallbacks(tmp_path: Path): rc_path = tmp_path / "rc.txt" rc_path.write_text("0\n", encoding="utf-8") @@ -70,6 +82,34 @@ def test_read_exit_code_and_summary_fallbacks(tmp_path: Path): assert publish_test_metrics._load_summary(str(tmp_path / "missing.json")) == {} +def test_summary_extractors_handle_invalid_shapes_and_values(): + assert publish_test_metrics._summary_coverage_percent(None) == 0.0 + assert publish_test_metrics._summary_coverage_percent({"results": "not-a-list"}) == 0.0 + assert publish_test_metrics._summary_coverage_percent({"results": [42, {"name": "coverage", "status": "failed"}]}) == 0.0 + assert publish_test_metrics._summary_coverage_percent({"results": [{"name": "coverage", "status": "ok"}]}) == 95.0 + + assert publish_test_metrics._summary_source_lines_over_500(None) == 0 + assert publish_test_metrics._summary_source_lines_over_500({"results": "not-a-list"}) == 0 + assert publish_test_metrics._summary_source_lines_over_500({"results": [42, {"name": "hygiene", "issues": "bad"}]}) == 0 + assert ( + publish_test_metrics._summary_source_lines_over_500( + { + "results": [ + { + "name": "hygiene", + "issues": [ + "services/foo.py has 501 lines (max 500)", + "a different issue", + "scripts/bar.sh has 777 lines > 500", + ], + } + ] + } + ) + == 2 + ) + + def test_read_text_post_text_and_fetch_existing_counter(monkeypatch): class _FakeResponse: def __init__(self, payload: str, status: int = 200): @@ -163,6 +203,26 @@ def test_post_text_raises_and_counter_handles_bad_metric_lines(monkeypatch): == 0.0 ) + monkeypatch.setattr( + publish_test_metrics, + "_read_text", + lambda url: "\n".join( + [ + 'different_metric{job="platform-quality-ci",suite="titan-iac",status="ok"} 8', + 'platform_quality_gate_runs_total{job="platform-quality-ci",suite="other",status="ok"} 8', + 'platform_quality_gate_runs_total{job="platform-quality-ci",suite="titan-iac",status="ok"} 9', + ] + ), + ) + assert ( + publish_test_metrics._fetch_existing_counter( + "http://push.invalid", + "platform_quality_gate_runs_total", + {"job": "platform-quality-ci", "suite": "titan-iac", "status": "ok"}, + ) + == 9.0 + ) + def test_build_payload_includes_summary_metrics(): payload = publish_test_metrics._build_payload(