"""Tests of the release scripts: `python3 -m unittest discover -s .gitea/scripts` (run by the `flow` job).""" import os 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 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_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"]), []) if __name__ == "__main__": unittest.main()