306 lines
12 KiB
Python
306 lines
12 KiB
Python
from __future__ import annotations
|
|
|
|
import os
|
|
import re
|
|
import shlex
|
|
import shutil
|
|
import stat
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
from unittest import mock
|
|
|
|
import check_release_notes as notes
|
|
|
|
|
|
ROOT = Path(__file__).resolve().parents[2]
|
|
WORKFLOW_PATH = ROOT / ".github" / "workflows" / "release.yml"
|
|
WORKFLOW = WORKFLOW_PATH.read_text(encoding="utf-8")
|
|
FIXTURES = Path(__file__).resolve().parent / "fixtures" / "release-notes"
|
|
RELEASE_SCRIPT = ROOT / "androidApp" / "scripts" / "release.sh"
|
|
|
|
|
|
def job_block(name):
|
|
match = re.search(rf"^ {re.escape(name)}:\n", WORKFLOW, re.MULTILINE)
|
|
if match is None:
|
|
raise AssertionError(f"workflow job {name} is missing")
|
|
next_job = re.search(r"^ [a-z][a-z0-9_-]*:\n", WORKFLOW[match.end() :], re.MULTILINE)
|
|
end = match.end() + next_job.start() if next_job else len(WORKFLOW)
|
|
return WORKFLOW[match.start() : end]
|
|
|
|
|
|
def publish_allowed(results, event="tag"):
|
|
"""Local truth-table model of the explicitly checked workflow expression."""
|
|
return (
|
|
event == "tag"
|
|
and all(
|
|
results[job] == "success"
|
|
for job in ("version", "linux", "flatpak", "windows", "macos")
|
|
)
|
|
and results["android"] in ("success", "skipped")
|
|
)
|
|
|
|
|
|
class ReleaseWorkflowStaticTest(unittest.TestCase):
|
|
def test_version_job_has_all_outputs_and_python_312(self):
|
|
version = job_block("version")
|
|
for output in ("version", "version_code", "android", "intent"):
|
|
self.assertIn(f" {output}: ", version)
|
|
self.assertIn("actions/setup-python@v6", version)
|
|
self.assertIn('python-version: "3.12"', version)
|
|
self.assertIn("ci-intent --repo-root .", version)
|
|
self.assertIn('GITHUB_REF_NAME}" != "v${version}', version)
|
|
|
|
def test_manual_dispatch_requires_current_before_classification(self):
|
|
version = job_block("version")
|
|
require = version.index("require-current --repo-root .")
|
|
classify = version.index("ci-intent --repo-root .")
|
|
self.assertLess(require, classify)
|
|
self.assertIn('GITHUB_REF_TYPE}" != "tag"', version)
|
|
self.assertNotIn("intent=desktop-only", version)
|
|
|
|
def test_android_job_is_conditional_as_a_whole(self):
|
|
android = job_block("android")
|
|
self.assertIn("needs: version", android)
|
|
self.assertIn("if: needs.version.outputs.android == 'true'", android)
|
|
self.assertLess(android.index("if:"), android.index("steps:"))
|
|
for operation in (
|
|
"sdkmanager",
|
|
"ANDROID_KEYSTORE_BASE64",
|
|
"androidApp/scripts/release.sh",
|
|
"name: android",
|
|
"name: rustore",
|
|
):
|
|
self.assertIn(operation, android)
|
|
self.assertNotIn("placeholder", android.lower())
|
|
|
|
def test_desktop_jobs_do_not_require_android_numeric_code(self):
|
|
for name in ("linux", "flatpak", "windows", "macos", "publish"):
|
|
with self.subTest(job=name):
|
|
block = job_block(name)
|
|
self.assertNotIn("version_code", block)
|
|
if name != "publish":
|
|
self.assertIn("needs: version", block)
|
|
self.assertNotIn("needs: [version", block)
|
|
|
|
def test_publish_keeps_all_needs_and_explicit_always_allowlist(self):
|
|
publish = job_block("publish")
|
|
self.assertIn("needs: [version, linux, flatpak, windows, macos, android]", publish)
|
|
self.assertIn("${{ always() && github.ref_type == 'tag'", publish)
|
|
for name in ("version", "linux", "flatpak", "windows", "macos"):
|
|
self.assertIn(f"needs.{name}.result == 'success'", publish)
|
|
self.assertIn("needs.android.result == 'success'", publish)
|
|
self.assertIn("needs.android.result == 'skipped'", publish)
|
|
self.assertNotIn("needs.android.result == 'failure' ||", publish)
|
|
self.assertNotIn("needs.android.result == 'cancelled' ||", publish)
|
|
|
|
def test_publish_has_no_mandatory_android_artifact_or_placeholder(self):
|
|
publish = job_block("publish")
|
|
self.assertIn("actions/download-artifact@v8", publish)
|
|
self.assertIn("merge-multiple: true", publish)
|
|
self.assertFalse(any(line.strip() == "name: android" for line in publish.splitlines()))
|
|
self.assertFalse(any(line.strip() == "name: rustore" for line in publish.splitlines()))
|
|
self.assertIn("rm -f dist/*-rustore.apk", publish)
|
|
self.assertIn("sha256sum * > SHA256SUMS.txt", publish)
|
|
self.assertIn("files: dist/*", publish)
|
|
self.assertNotIn("placeholder", publish.lower())
|
|
|
|
def test_publish_truth_table_allows_only_success_or_intentional_skip(self):
|
|
successful = {
|
|
"version": "success",
|
|
"linux": "success",
|
|
"flatpak": "success",
|
|
"windows": "success",
|
|
"macos": "success",
|
|
"android": "success",
|
|
}
|
|
for android, expected in (
|
|
("success", True),
|
|
("skipped", True),
|
|
("failure", False),
|
|
("cancelled", False),
|
|
):
|
|
with self.subTest(android=android):
|
|
results = successful | {"android": android}
|
|
self.assertEqual(publish_allowed(results), expected)
|
|
|
|
for failed_job in ("version", "linux", "flatpak", "windows", "macos"):
|
|
for result in ("failure", "cancelled"):
|
|
with self.subTest(job=failed_job, result=result):
|
|
results = successful | {failed_job: result, "android": "skipped"}
|
|
self.assertFalse(publish_allowed(results))
|
|
self.assertFalse(publish_allowed(successful, event="workflow_dispatch"))
|
|
|
|
def test_release_gate_is_immediately_after_cd(self):
|
|
lines = RELEASE_SCRIPT.read_text(encoding="utf-8").splitlines()
|
|
cd = next(index for index, line in enumerate(lines) if line.startswith("cd "))
|
|
self.assertEqual(
|
|
lines[cd + 1],
|
|
"python3 scripts/check_release_notes.py require-current --repo-root ..",
|
|
)
|
|
self.assertLess(cd + 1, lines.index('env_file="${CP_SIGNING_ENV:-$HOME/.config/chesspenguin/android/.env}"'))
|
|
self.assertLess(cd + 1, next(index for index, line in enumerate(lines) if "./gradlew" in line))
|
|
|
|
|
|
class ReleaseWorkflowIntentRegressionTest(unittest.TestCase):
|
|
def load(self, fixture):
|
|
return notes.load_manifest(FIXTURES / fixture / "android-release-notes.json")
|
|
|
|
def test_beta_numeric_base_has_no_code_and_allows_desktop_publish(self):
|
|
history = self.load("prerelease-numeric-base")
|
|
with mock.patch.object(
|
|
notes, "_numeric_version", side_effect=AssertionError("numeric parse")
|
|
):
|
|
result = notes.classify_ci_intent(history, "v0.2.0-beta.1")
|
|
self.assertEqual(result["version"], "0.2.0-beta.1")
|
|
self.assertEqual(result["intent"], "desktop-only")
|
|
self.assertFalse(result["android"])
|
|
self.assertNotIn("version_code", result)
|
|
results = {
|
|
"version": "success",
|
|
"linux": "success",
|
|
"flatpak": "success",
|
|
"windows": "success",
|
|
"macos": "success",
|
|
"android": "skipped",
|
|
}
|
|
self.assertTrue(publish_allowed(results))
|
|
|
|
def test_numeric_name_only_and_code_only_conflicts_fail(self):
|
|
for fixture in ("numeric-code-mismatch", "numeric-code-only-match"):
|
|
with self.subTest(fixture=fixture):
|
|
with self.assertRaises(notes.ReleaseNotesError):
|
|
self.load(fixture)
|
|
results = {
|
|
"version": "failure",
|
|
"linux": "skipped",
|
|
"flatpak": "skipped",
|
|
"windows": "skipped",
|
|
"macos": "skipped",
|
|
"android": "skipped",
|
|
}
|
|
self.assertFalse(publish_allowed(results))
|
|
|
|
def test_invalid_prerelease_and_malformed_manifest_fail_closed(self):
|
|
for fixture in ("invalid-android-prerelease", "malformed"):
|
|
with self.subTest(fixture=fixture):
|
|
with self.assertRaises(notes.ReleaseNotesError):
|
|
self.load(fixture)
|
|
|
|
def test_numeric_exact_and_neither_match_route_differently(self):
|
|
exact = notes.classify_ci_intent(self.load("valid"), "v2.5.2")
|
|
neither = notes.classify_ci_intent(self.load("valid"), "v2.5.3")
|
|
self.assertEqual(exact["intent"], "android")
|
|
self.assertEqual(exact["version_code"], 20502)
|
|
self.assertEqual(neither["intent"], "desktop-only")
|
|
self.assertNotIn("version_code", neither)
|
|
|
|
|
|
class ReleaseScriptShellTest(unittest.TestCase):
|
|
"""Execute the shell gate with stubs; never sign or assemble a real APK."""
|
|
|
|
def make_sandbox(self, fixture):
|
|
temporary = tempfile.TemporaryDirectory()
|
|
root = Path(temporary.name)
|
|
scripts = root / "androidApp" / "scripts"
|
|
scripts.mkdir(parents=True)
|
|
shutil.copyfile(RELEASE_SCRIPT, scripts / "release.sh")
|
|
shutil.copyfile(
|
|
ROOT / "androidApp" / "scripts" / "check_release_notes.py",
|
|
scripts / "check_release_notes.py",
|
|
)
|
|
source = FIXTURES / fixture
|
|
manifest_path = root / notes.MANIFEST_PATH
|
|
manifest_path.parent.mkdir(parents=True)
|
|
shutil.copyfile(source / "android-release-notes.json", manifest_path)
|
|
shutil.copyfile(source / "Cargo.toml", root / "Cargo.toml")
|
|
|
|
log = root / "calls.log"
|
|
log.write_text("", encoding="utf-8")
|
|
bin_dir = root / "bin"
|
|
bin_dir.mkdir()
|
|
python_stub = bin_dir / "python3"
|
|
python_stub.write_text(
|
|
"#!/bin/sh\n"
|
|
'printf \'gate\\n\' >> "$CP_TEST_LOG"\n'
|
|
f"exec {shlex.quote(sys.executable)} \"$@\"\n",
|
|
encoding="utf-8",
|
|
)
|
|
python_stub.chmod(python_stub.stat().st_mode | stat.S_IXUSR)
|
|
|
|
gradlew = root / "androidApp" / "gradlew"
|
|
gradlew.write_text(
|
|
"#!/bin/sh\n"
|
|
'printf \'gradle\\n\' >> "$CP_TEST_LOG"\n'
|
|
"exit 42\n",
|
|
encoding="utf-8",
|
|
)
|
|
gradlew.chmod(gradlew.stat().st_mode | stat.S_IXUSR)
|
|
|
|
signing = root / "signing.env"
|
|
signing.write_text(
|
|
'printf \'key\\n\' >> "$CP_TEST_LOG"\n'
|
|
'CP_KEYSTORE="/not/a/real/release.jks"\n'
|
|
'CP_KEYSTORE_PASSWORD="test-only"\n'
|
|
'CP_KEY_PASSWORD="test-only"\n',
|
|
encoding="utf-8",
|
|
)
|
|
env = os.environ.copy()
|
|
env.pop("CP_KEYSTORE", None)
|
|
env.update(
|
|
PATH=f"{bin_dir}{os.pathsep}{env['PATH']}",
|
|
CP_SIGNING_ENV=str(signing),
|
|
CP_TEST_LOG=str(log),
|
|
HOME=str(root),
|
|
)
|
|
return temporary, root, log, env
|
|
|
|
def run_release(self, fixture, extra_env=None):
|
|
temporary, root, log, env = self.make_sandbox(fixture)
|
|
self.addCleanup(temporary.cleanup)
|
|
if extra_env:
|
|
env.update(extra_env)
|
|
result = subprocess.run(
|
|
["bash", "androidApp/scripts/release.sh"],
|
|
cwd=root,
|
|
env=env,
|
|
text=True,
|
|
capture_output=True,
|
|
check=False,
|
|
)
|
|
return result, log.read_text(encoding="utf-8").splitlines()
|
|
|
|
def test_missing_current_stops_before_key_and_gradle(self):
|
|
result, calls = self.run_release("current-missing")
|
|
self.assertNotEqual(result.returncode, 0, result.stdout)
|
|
self.assertIn("current Android release is missing", result.stderr)
|
|
self.assertEqual(calls, ["gate"])
|
|
|
|
def test_prerelease_desktop_env_cannot_bypass_direct_release_gate(self):
|
|
result, calls = self.run_release(
|
|
"prerelease-numeric-base",
|
|
{
|
|
"GITHUB_REF_TYPE": "tag",
|
|
"GITHUB_REF_NAME": "v0.2.0-beta.1",
|
|
"ANDROID": "false",
|
|
"INTENT": "desktop-only",
|
|
},
|
|
)
|
|
self.assertNotEqual(result.returncode, 0, result.stdout)
|
|
self.assertIn("must be numeric major.minor.patch", result.stderr)
|
|
self.assertEqual(calls, ["gate"])
|
|
|
|
def test_valid_and_explicit_empty_current_reach_stub_gradle_in_order(self):
|
|
for fixture in ("valid", "empty"):
|
|
with self.subTest(fixture=fixture):
|
|
result, calls = self.run_release(fixture)
|
|
self.assertEqual(result.returncode, 42, result.stderr)
|
|
self.assertIn("OK: current Android release", result.stdout)
|
|
self.assertEqual(calls, ["gate", "key", "gradle"])
|
|
self.assertNotIn("assembleFullRelease", result.stdout)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|