|
| 1 | +# ******************************************************************************* |
| 2 | +# Copyright (c) 2026 Contributors to the Eclipse Foundation |
| 3 | +# |
| 4 | +# See the NOTICE file(s) distributed with this work for additional |
| 5 | +# information regarding copyright ownership. |
| 6 | +# |
| 7 | +# This program and the accompanying materials are made available under the |
| 8 | +# terms of the Apache License Version 2.0 which is available at |
| 9 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# SPDX-License-Identifier: Apache-2.0 |
| 12 | +# ******************************************************************************* |
| 13 | + |
| 14 | +# ╓ ╖ |
| 15 | +# ║ Some portions generated by Github Copilot ║ |
| 16 | +# ╙ ╜ |
| 17 | + |
| 18 | +"""Tests for traceability_gate.py.""" |
| 19 | + |
| 20 | +import json |
| 21 | +import subprocess |
| 22 | +import sys |
| 23 | +from pathlib import Path |
| 24 | + |
| 25 | +_MY_PATH = Path(__file__).parent |
| 26 | + |
| 27 | +_GATE_SCRIPT = _MY_PATH.parent / "traceability_gate.py" |
| 28 | + |
| 29 | + |
| 30 | +def _write_metrics_json(tmp_path: Path, metrics_by_type: dict | None = None) -> Path: |
| 31 | + """Write a schema-v1 metrics JSON and return its path.""" |
| 32 | + if metrics_by_type is None: |
| 33 | + metrics_by_type = { |
| 34 | + "tool_req": { |
| 35 | + "include_not_implemented": False, |
| 36 | + "requirements": { |
| 37 | + "total": 4, |
| 38 | + "with_code_link": 3, |
| 39 | + "with_test_link": 2, |
| 40 | + "fully_linked": 2, |
| 41 | + "with_code_link_pct": 75.0, |
| 42 | + "with_test_link_pct": 50.0, |
| 43 | + "fully_linked_pct": 50.0, |
| 44 | + "missing_code_link_ids": ["REQ_4"], |
| 45 | + "missing_test_link_ids": ["REQ_3", "REQ_4"], |
| 46 | + "not_fully_linked_ids": ["REQ_3", "REQ_4"], |
| 47 | + }, |
| 48 | + "tests": { |
| 49 | + "total": 3, |
| 50 | + "filtered_test_types": [], |
| 51 | + "linked_to_requirements": 2, |
| 52 | + "linked_to_requirements_pct": 66.67, |
| 53 | + "broken_references": [], |
| 54 | + }, |
| 55 | + } |
| 56 | + } |
| 57 | + payload = { |
| 58 | + "schema_version": "1", |
| 59 | + "generated_by": "traceability_coverage", |
| 60 | + "needs_json": "fake/needs.json", |
| 61 | + "metrics_by_type": metrics_by_type, |
| 62 | + } |
| 63 | + out = tmp_path / "metrics.json" |
| 64 | + out.write_text(json.dumps(payload), encoding="utf-8") |
| 65 | + return out |
| 66 | + |
| 67 | + |
| 68 | +def _run_gate(metrics_json: Path, extra_args: list[str]) -> subprocess.CompletedProcess: |
| 69 | + return subprocess.run( |
| 70 | + [sys.executable, _GATE_SCRIPT, "--metrics-json", str(metrics_json)] |
| 71 | + + extra_args, |
| 72 | + capture_output=True, |
| 73 | + text=True, |
| 74 | + ) |
| 75 | + |
| 76 | + |
| 77 | +def test_gate_passes_when_thresholds_met(tmp_path: Path) -> None: |
| 78 | + metrics_json = _write_metrics_json(tmp_path) |
| 79 | + |
| 80 | + result = _run_gate( |
| 81 | + metrics_json, |
| 82 | + ["--min-req-code", "70", "--min-req-test", "50", "--min-tests-linked", "60"], |
| 83 | + ) |
| 84 | + |
| 85 | + assert result.returncode == 0 |
| 86 | + assert "Threshold check passed." in result.stdout |
| 87 | + |
| 88 | + |
| 89 | +def test_gate_fails_when_threshold_not_met(tmp_path: Path) -> None: |
| 90 | + metrics_json = _write_metrics_json(tmp_path) |
| 91 | + |
| 92 | + result = _run_gate( |
| 93 | + metrics_json, |
| 94 | + ["--min-req-code", "100"], |
| 95 | + ) |
| 96 | + |
| 97 | + assert result.returncode == 2 |
| 98 | + assert "Threshold check failed:" in result.stdout |
| 99 | + assert "[tool_req] requirements with code links" in result.stdout |
| 100 | + |
| 101 | + |
| 102 | +def test_gate_require_all_links_fails(tmp_path: Path) -> None: |
| 103 | + metrics_json = _write_metrics_json(tmp_path) |
| 104 | + |
| 105 | + result = _run_gate(metrics_json, ["--require-all-links"]) |
| 106 | + |
| 107 | + assert result.returncode == 2 |
| 108 | + assert "Threshold check failed:" in result.stdout |
| 109 | + |
| 110 | + |
| 111 | +def test_gate_fail_on_broken_refs(tmp_path: Path) -> None: |
| 112 | + metrics_by_type = { |
| 113 | + "tool_req": { |
| 114 | + "include_not_implemented": False, |
| 115 | + "requirements": { |
| 116 | + "total": 1, |
| 117 | + "with_code_link": 1, |
| 118 | + "with_test_link": 1, |
| 119 | + "fully_linked": 1, |
| 120 | + "with_code_link_pct": 100.0, |
| 121 | + "with_test_link_pct": 100.0, |
| 122 | + "fully_linked_pct": 100.0, |
| 123 | + "missing_code_link_ids": [], |
| 124 | + "missing_test_link_ids": [], |
| 125 | + "not_fully_linked_ids": [], |
| 126 | + }, |
| 127 | + "tests": { |
| 128 | + "total": 2, |
| 129 | + "filtered_test_types": [], |
| 130 | + "linked_to_requirements": 2, |
| 131 | + "linked_to_requirements_pct": 100.0, |
| 132 | + "broken_references": [ |
| 133 | + {"testcase": "TC_X", "missing_need": "REQ_UNKNOWN"} |
| 134 | + ], |
| 135 | + }, |
| 136 | + } |
| 137 | + } |
| 138 | + metrics_json = _write_metrics_json(tmp_path, metrics_by_type) |
| 139 | + |
| 140 | + result = _run_gate(metrics_json, ["--fail-on-broken-test-refs"]) |
| 141 | + |
| 142 | + assert result.returncode == 2 |
| 143 | + assert "broken testcase references found:" in result.stdout |
| 144 | + |
| 145 | + |
| 146 | +def test_gate_specific_need_type(tmp_path: Path) -> None: |
| 147 | + metrics_by_type = { |
| 148 | + "tool_req": { |
| 149 | + "include_not_implemented": False, |
| 150 | + "requirements": { |
| 151 | + "total": 2, |
| 152 | + "with_code_link": 2, |
| 153 | + "with_test_link": 2, |
| 154 | + "fully_linked": 2, |
| 155 | + "with_code_link_pct": 100.0, |
| 156 | + "with_test_link_pct": 100.0, |
| 157 | + "fully_linked_pct": 100.0, |
| 158 | + "missing_code_link_ids": [], |
| 159 | + "missing_test_link_ids": [], |
| 160 | + "not_fully_linked_ids": [], |
| 161 | + }, |
| 162 | + "tests": { |
| 163 | + "total": 1, |
| 164 | + "filtered_test_types": [], |
| 165 | + "linked_to_requirements": 1, |
| 166 | + "linked_to_requirements_pct": 100.0, |
| 167 | + "broken_references": [], |
| 168 | + }, |
| 169 | + }, |
| 170 | + "comp_req": { |
| 171 | + "include_not_implemented": False, |
| 172 | + "requirements": { |
| 173 | + "total": 5, |
| 174 | + "with_code_link": 0, |
| 175 | + "with_test_link": 0, |
| 176 | + "fully_linked": 0, |
| 177 | + "with_code_link_pct": 0.0, |
| 178 | + "with_test_link_pct": 0.0, |
| 179 | + "fully_linked_pct": 0.0, |
| 180 | + "missing_code_link_ids": ["C1", "C2", "C3", "C4", "C5"], |
| 181 | + "missing_test_link_ids": ["C1", "C2", "C3", "C4", "C5"], |
| 182 | + "not_fully_linked_ids": ["C1", "C2", "C3", "C4", "C5"], |
| 183 | + }, |
| 184 | + "tests": { |
| 185 | + "total": 0, |
| 186 | + "filtered_test_types": [], |
| 187 | + "linked_to_requirements": 0, |
| 188 | + "linked_to_requirements_pct": 100.0, |
| 189 | + "broken_references": [], |
| 190 | + }, |
| 191 | + }, |
| 192 | + } |
| 193 | + metrics_json = _write_metrics_json(tmp_path, metrics_by_type) |
| 194 | + |
| 195 | + # Gate only on tool_req (which is fully linked) — comp_req failures are ignored |
| 196 | + result = _run_gate( |
| 197 | + metrics_json, |
| 198 | + ["--need-type", "tool_req", "--require-all-links"], |
| 199 | + ) |
| 200 | + |
| 201 | + assert result.returncode == 0 |
| 202 | + assert "[tool_req]" in result.stdout |
| 203 | + assert "[comp_req]" not in result.stdout |
| 204 | + |
| 205 | + |
| 206 | +def test_gate_unknown_need_type_fails(tmp_path: Path) -> None: |
| 207 | + metrics_json = _write_metrics_json(tmp_path) |
| 208 | + |
| 209 | + result = _run_gate(metrics_json, ["--need-type", "nonexistent_req"]) |
| 210 | + |
| 211 | + assert result.returncode == 2 |
| 212 | + assert "not found in metrics JSON" in result.stdout |
| 213 | + |
| 214 | + |
| 215 | +def test_gate_unsupported_schema_version(tmp_path: Path) -> None: |
| 216 | + bad = tmp_path / "bad.json" |
| 217 | + bad.write_text( |
| 218 | + json.dumps({"schema_version": "99", "metrics_by_type": {}}), encoding="utf-8" |
| 219 | + ) |
| 220 | + |
| 221 | + result = _run_gate(bad, []) |
| 222 | + |
| 223 | + assert result.returncode == 1 |
| 224 | + assert "unsupported schema_version" in result.stderr |
| 225 | + |
| 226 | + |
| 227 | +def test_gate_missing_file_returns_error(tmp_path: Path) -> None: |
| 228 | + result = _run_gate(tmp_path / "does_not_exist.json", []) |
| 229 | + |
| 230 | + assert result.returncode == 1 |
| 231 | + assert "not found" in result.stderr |
0 commit comments