185 lines
8.0 KiB
Python
185 lines
8.0 KiB
Python
"""Tests of the release scripts: `python3 -m unittest discover -s .gitea/scripts` (run by the `flow` job)."""
|
|
|
|
import importlib.util
|
|
import os
|
|
import sqlite3
|
|
import sys
|
|
import unittest
|
|
from datetime import datetime, timezone
|
|
|
|
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
|
import check_flow # noqa: E402
|
|
import packagist # noqa: E402
|
|
import prune_branches # noqa: E402
|
|
import semver # noqa: E402
|
|
|
|
TAGS = ["v0.1.0", "v0.2.0", "v0.10.0", "not-a-version"]
|
|
CHANGELOG = "# Changelog\n\n## v0.11.0 - 2026-12-10\n\n- A feature\n- Another\n\n## v0.10.0 - 2026-12-01\n\n- Older\n"
|
|
|
|
|
|
class Versions(unittest.TestCase):
|
|
def test_release_branch(self):
|
|
self.assertEqual(semver.release_version("release/v1.2.3"), (1, 2, 3))
|
|
for wrong in ("release/1.2.3", "release/v1.2", "feature/x", "release/v1.2.3-rc1", "", None):
|
|
self.assertIsNone(semver.release_version(wrong))
|
|
|
|
def test_latest_is_numeric_not_alphabetical(self):
|
|
self.assertEqual(semver.latest(TAGS), (0, 10, 0))
|
|
self.assertIsNone(semver.latest(["x"]))
|
|
|
|
def test_changelog_section(self):
|
|
self.assertEqual(semver.changelog_section(CHANGELOG, (0, 11, 0)), "- A feature\n- Another")
|
|
self.assertEqual(semver.changelog_section(CHANGELOG, (0, 10, 0)), "- Older")
|
|
self.assertIsNone(semver.changelog_section(CHANGELOG, (0, 1, 0)))
|
|
self.assertIsNone(semver.changelog_section(CHANGELOG, (0, 1, 1)))
|
|
|
|
|
|
class Flow(unittest.TestCase):
|
|
def check(self, base, head, tags=TAGS, changelog=CHANGELOG, composer=None):
|
|
return check_flow.problems(base, head, tags, changelog, composer or {"name": "x/y"})
|
|
|
|
def test_release_to_production(self):
|
|
self.assertEqual(self.check("production", "release/v0.11.0"), [])
|
|
|
|
def test_production_also_takes_the_nightly_renovate_branch(self):
|
|
self.assertEqual(self.check("production", "renovate/deps-20261205"), [])
|
|
self.assertTrue(self.check("production", "renovate"))
|
|
|
|
def test_production_only_from_a_release_branch(self):
|
|
self.assertTrue(self.check("production", "feature/x"))
|
|
self.assertTrue(self.check("production", "develop"))
|
|
|
|
def test_release_must_be_above_the_latest_tag(self):
|
|
self.assertTrue(self.check("production", "release/v0.10.0"))
|
|
self.assertTrue(self.check("production", "release/v0.9.0"))
|
|
|
|
def test_release_needs_its_changelog_section(self):
|
|
self.assertTrue(self.check("production", "release/v0.12.0"))
|
|
self.assertTrue(self.check("production", "release/v0.11.0", changelog=None))
|
|
|
|
def test_composer_json_has_no_version(self):
|
|
self.assertTrue(self.check("production", "release/v0.11.0", composer={"version": "0.11.0"}))
|
|
|
|
def test_features_into_an_open_release_branch(self):
|
|
self.assertEqual(self.check("release/v0.11.0", "feature/x"), [])
|
|
self.assertEqual(self.check("release/v0.11.0", "fix/y"), [])
|
|
|
|
def test_nothing_into_a_release_that_is_out(self):
|
|
self.assertTrue(self.check("release/v0.10.0", "feature/x"))
|
|
|
|
def test_a_release_branch_takes_no_long_lived_branch(self):
|
|
for head in ("production", "develop", "release/v0.12.0"):
|
|
self.assertTrue(self.check("release/v0.11.0", head))
|
|
|
|
def test_nothing_goes_to_develop_or_anything_else(self):
|
|
self.assertTrue(self.check("develop", "feature/x"))
|
|
self.assertTrue(self.check("main", "feature/x"))
|
|
|
|
def test_first_release_without_tags(self):
|
|
self.assertEqual(self.check("production", "release/v0.1.0", tags=[], changelog="## v0.1.0 - x\n\n- a\n"), [])
|
|
|
|
|
|
class Prune(unittest.TestCase):
|
|
NOW = datetime(2026, 12, 4, tzinfo=timezone.utc)
|
|
|
|
def branch(self, name, day):
|
|
return {"name": name, "commit": {"timestamp": f"{day}T10:00:00Z"}}
|
|
|
|
def names(self, branches, open_heads=()):
|
|
return [name for name, _ in prune_branches.stale(branches, set(open_heads), self.NOW)]
|
|
|
|
def test_removes_only_quiet_feature_and_fix_branches(self):
|
|
branches = [
|
|
self.branch("feature/old", "2026-10-01"), self.branch("fix/old", "2026-11-01"),
|
|
self.branch("feature/new", "2026-11-20"), self.branch("feature/edge", "2026-11-05"),
|
|
]
|
|
self.assertEqual(self.names(branches), ["feature/old", "fix/old"])
|
|
|
|
def test_never_removes_the_long_lived_branches(self):
|
|
old = "2025-01-01"
|
|
branches = [self.branch(n, old) for n in ("production", "develop", "release/v0.1.0", "renovate/x", "other")]
|
|
self.assertEqual(self.names(branches), [])
|
|
|
|
def test_keeps_a_branch_with_an_open_pull_request(self):
|
|
self.assertEqual(self.names([self.branch("feature/old", "2026-01-01")], open_heads=["feature/old"]), [])
|
|
|
|
|
|
def load_hermes():
|
|
path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "..", "dev", "token-usage-hermes.py")
|
|
spec = importlib.util.spec_from_file_location("token_usage_hermes", path)
|
|
module = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(module)
|
|
return module
|
|
|
|
|
|
class HermesTokens(unittest.TestCase):
|
|
def setUp(self):
|
|
self.hermes = load_hermes()
|
|
self.con = sqlite3.connect(":memory:")
|
|
self.con.execute("CREATE TABLE sessions (id TEXT, parent_session_id TEXT, model TEXT, api_call_count INT, output_tokens INT, input_tokens INT, cache_write_tokens INT, cache_read_tokens INT)")
|
|
self.con.executemany("INSERT INTO sessions VALUES (?,?,?,?,?,?,?,?)", [
|
|
("main", None, "model-a", 10, 1000, 20, 300, 4000),
|
|
("helper", "main", "model-b", 3, 200, 5, 0, 50),
|
|
("grandchild", "helper", "model-b", 2, 100, 1, 0, 10),
|
|
("other", None, "model-a", 99, 9, 9, 9, 9),
|
|
])
|
|
|
|
def test_children_through_every_level(self):
|
|
self.assertEqual(self.hermes.expand_children(self.con, ["main"]), ["grandchild", "helper", "main"])
|
|
|
|
def test_one_row_per_model_with_the_hours_on_the_first_only(self):
|
|
_, per_model, missing = self.hermes.totals(self.con, ["main", "helper", "grandchild", "nope"])
|
|
self.assertEqual(missing, {"nope"})
|
|
rows = self.hermes.lines(per_model, "Work", "2026-12-04", "", 1.5)
|
|
self.assertEqual(rows[0], "| 2026-12-04 | Work | | model-a | 10 | 1.000 | 20 | 300 | 4.000 | 1,50 |")
|
|
self.assertEqual(rows[1], "| 2026-12-04 | Work | | model-b | 5 | 300 | 6 | 0 | 60 | 0,00 |")
|
|
|
|
def test_hours_are_never_invented(self):
|
|
_, per_model, _ = self.hermes.totals(self.con, ["main"])
|
|
self.assertTrue(self.hermes.lines(per_model, "Work", "2026-12-04", "", None)[0].endswith("| 4.000 | |"))
|
|
|
|
|
|
class PackagistNotice(unittest.TestCase):
|
|
def run_notice(self, answers):
|
|
"""Runs notify() with fake answers per endpoint (a status number, or an HTTP error code as a string)."""
|
|
import io
|
|
import urllib.error
|
|
|
|
calls = []
|
|
|
|
def fake(endpoint, user, token, url):
|
|
calls.append(endpoint)
|
|
answer = answers[endpoint]
|
|
if isinstance(answer, str):
|
|
raise urllib.error.HTTPError("x", int(answer), "refused", {}, io.BytesIO(b"nope"))
|
|
return answer
|
|
|
|
original = packagist.call
|
|
packagist.call = fake
|
|
try:
|
|
return packagist.notify("u", "t", "https://example.org/r"), calls
|
|
finally:
|
|
packagist.call = original
|
|
|
|
def test_a_known_package_is_only_updated(self):
|
|
message, calls = self.run_notice({"update-package": 202})
|
|
self.assertEqual(calls, ["update-package"])
|
|
self.assertIn("202", message)
|
|
|
|
def test_an_unknown_package_is_submitted(self):
|
|
message, calls = self.run_notice({"update-package": "404", "create-package": 202})
|
|
self.assertEqual(calls, ["update-package", "create-package"])
|
|
self.assertIn("submitted", message)
|
|
|
|
def test_another_refusal_is_not_submitted(self):
|
|
with self.assertRaises(SystemExit):
|
|
self.run_notice({"update-package": "403", "create-package": 202})
|
|
|
|
def test_a_refused_submission_fails(self):
|
|
with self.assertRaises(SystemExit):
|
|
self.run_notice({"update-package": "404", "create-package": "400"})
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|