"""AT-3 (release durability): the artifacts of a tag land on the Gitea release, idempotently.""" from __future__ import annotations import contextlib import importlib.util import io import json import sys import tempfile import threading import unittest from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer from pathlib import Path ROOT = Path(__file__).resolve().parents[2] SCRIPT = ROOT / "scripts" / "publish-gitea-release.py" def load_module(): spec = importlib.util.spec_from_file_location("publish_gitea_release", SCRIPT) assert spec is not None and spec.loader is not None module = importlib.util.module_from_spec(spec) sys.modules[spec.name] = module spec.loader.exec_module(module) return module class Stub: def __init__(self) -> None: self.release_assets: list[dict] = [] self.created = False self.uploaded: list[str] = [] self.created_payload: dict = {} def handler(self): stub = self class Handler(BaseHTTPRequestHandler): def log_message(self, format, *args): # noqa: A002 - base class signature return def _reply(self, status: int, payload: dict) -> None: body = json.dumps(payload).encode("utf-8") self.send_response(status) self.send_header("Content-Type", "application/json") self.send_header("Content-Length", str(len(body))) self.end_headers() self.wfile.write(body) def do_GET(self): if self.path.endswith("/releases/tags/v1.2.3") and stub.created: self._reply(200, {"id": 7, "assets": stub.release_assets}) else: self._reply(404, {"message": "not found"}) def do_POST(self): length = int(self.headers.get("Content-Length") or 0) body = self.rfile.read(length) if self.path.endswith("/releases"): stub.created = True stub.created_payload = json.loads(body) self._reply(201, {"id": 7, "assets": []}) return if "/releases/7/assets?name=" in self.path: name = self.path.rsplit("name=", 1)[1] stub.uploaded.append(name) stub.release_assets.append( {"name": name, "browser_download_url": f"http://stub/assets/{name}"} ) self._reply(201, {"name": name, "browser_download_url": f"http://stub/assets/{name}"}) return self._reply(404, {"message": "not found"}) return Handler class PublishGiteaReleaseTests(unittest.TestCase): def setUp(self) -> None: self.module = load_module() self.stub = Stub() self.server = ThreadingHTTPServer(("127.0.0.1", 0), self.stub.handler()) self.thread = threading.Thread(target=self.server.serve_forever, daemon=True) self.thread.start() self.api = f"http://127.0.0.1:{self.server.server_address[1]}" self.tmp = tempfile.TemporaryDirectory() self.dist = Path(self.tmp.name) (self.dist / "crank-community-admin-api-v1.2.3.tar.gz").write_bytes(b"admin") (self.dist / "crank-community-v1.2.3-checksums.txt").write_text("deadbeef admin.tar.gz\n") (self.dist / "nested").mkdir() (self.dist / "nested" / "admin-api").write_bytes(b"raw") self.notes = self.dist / "RELEASE-NOTES.md" self.notes.write_text("## v1.2.3\n", encoding="utf-8") def tearDown(self) -> None: self.server.shutdown() self.server.server_close() self.tmp.cleanup() def publish(self) -> tuple[int, str]: output = io.StringIO() with contextlib.redirect_stdout(output), contextlib.redirect_stderr(output): code = self.module.publish( self.api, "bsodfather/crank", "v1.2.3", "Crank Community v1.2.3", self.notes, self.dist, True, "stub-token-never-printed", ) return code, output.getvalue() def test_release_is_created_with_notes_and_every_top_level_artifact(self) -> None: code, output = self.publish() self.assertEqual(code, 0, output) self.assertEqual(self.stub.created_payload["tag_name"], "v1.2.3") self.assertTrue(self.stub.created_payload["prerelease"]) self.assertEqual(self.stub.created_payload["body"], "## v1.2.3\n") self.assertEqual( self.stub.uploaded, ["crank-community-admin-api-v1.2.3.tar.gz", "crank-community-v1.2.3-checksums.txt"], "every top-level artifact and nothing from the nested build dir", ) self.assertNotIn("stub-token-never-printed", output, "the token must never reach the log") def test_second_run_reuses_the_release_and_skips_existing_assets(self) -> None: self.assertEqual(self.publish()[0], 0) uploaded_first = list(self.stub.uploaded) code, output = self.publish() self.assertEqual(code, 0, output) self.assertEqual(self.stub.uploaded, uploaded_first, "a rerun must not upload duplicates") self.assertEqual(output.count("skip existing asset"), 2, output) if __name__ == "__main__": unittest.main()