46 lines
1.8 KiB
Python
46 lines
1.8 KiB
Python
#!/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
|
|
|
|
from badge_store import publish_file
|
|
|
|
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",
|
|
"main.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</text>" in svg
|
|
assert b"#d1242f" in svg
|
|
assert b">pass</text>" in module.badge("next", "success", 108)
|
|
for invalid in ("", "/main.svg", "../main.svg", "badges//main.svg"):
|
|
try:
|
|
publish_file("https://example.invalid/api/v1", "owner/repo", invalid,
|
|
b"<svg/>", "token", "test")
|
|
except ValueError:
|
|
pass
|
|
else:
|
|
raise AssertionError(f"unsafe badge path accepted: {invalid}")
|
|
print("last completed CI badge: queued run ignored, pass/fail SVGs valid")
|