#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Check that queued CI runs do not displace the last completed result.""" import importlib.util from pathlib import Path from unittest.mock import patch import xml.etree.ElementTree as ET path = Path(__file__).with_name("last-attempt-badge.py") spec = importlib.util.spec_from_file_location("last_attempt_badge", path) module = importlib.util.module_from_spec(spec) spec.loader.exec_module(module) def runs(_url, _token): return {"workflow_runs": [ {"id": 105, "head_branch": "main", "status": "completed", "conclusion": "cancelled"}, {"id": 104, "head_branch": "main", "status": "queued", "conclusion": None}, {"id": 103, "head_branch": "main", "status": "completed", "conclusion": "failure"}, {"id": 102, "head_branch": "main", "status": "completed", "conclusion": "success"}, ]} with patch.object(module, "request_json", runs): assert module.latest_completed("https://example.invalid/api/v1", "owner/repo", "release.yml", "main", "token") == (103, "failure") svg = module.badge("main", "failure", 103) root = ET.fromstring(svg) assert root.attrib["aria-label"] == "CI main: last completed attempt fail (run 103)" assert b">fail" in svg assert b"#d1242f" in svg assert b">pass" in module.badge("next", "success", 108) print("last completed CI badge: queued run ignored, pass/fail SVGs valid")