74 lines
3.0 KiB
Python
74 lines
3.0 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
|
|
import os
|
|
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)
|
|
environment = {
|
|
"GITHUB_EVENT_NAME": "workflow_run",
|
|
"GITHUB_REF": "refs/heads/main",
|
|
"GITHUB_REPOSITORY": "owner/repo",
|
|
"GITHUB_API_URL": "https://example.invalid/api/v1",
|
|
"GITEA_TOKEN": "test-token",
|
|
}
|
|
with patch.dict(os.environ, environment), \
|
|
patch.object(module, "latest_completed", side_effect=[(201, "success"),
|
|
(202, "failure")]) as latest, \
|
|
patch.object(module, "publish") as published:
|
|
module.main()
|
|
assert latest.call_count == 2
|
|
assert [call.args[2] for call in published.call_args_list] == ["main", "next"]
|
|
with patch.dict(os.environ, {**environment, "GITHUB_EVENT_NAME": "workflow_dispatch"}), \
|
|
patch.object(module, "latest_completed", side_effect=[(201, "success"),
|
|
(202, "failure")]), \
|
|
patch.object(module, "publish") as published:
|
|
module.main()
|
|
assert published.call_count == 2
|
|
with patch.dict(os.environ, {**environment, "GITHUB_EVENT_NAME": "schedule"}):
|
|
try:
|
|
module.main()
|
|
except RuntimeError as error:
|
|
assert "CI completion or manual dispatch" in str(error)
|
|
else:
|
|
raise AssertionError("scheduled event entered badge publisher")
|
|
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")
|