diff --git a/scripts/app_build.py b/scripts/app_build.py index f80e3208..5958e031 100644 --- a/scripts/app_build.py +++ b/scripts/app_build.py @@ -129,8 +129,16 @@ def build(kind, runner=subprocess.run, app=APP, cache=None, arguments=(), log=pr "-v", "pipelines-flutter-gradle:/home/builder/.gradle", "-v", "pipelines-android-sdk:/opt/android-sdk", "-v", f"{signing_dir()}:/home/builder/.android"] - docker(runner, *command, tag, "pipelines-app-build", kind, *arguments) - (work / ".complete").write_text(json.dumps({"key": key, "built": int(time.time())}) + "\n") + built = int(time.time()) + stamp = () + if kind == "apk": + # Android installs only a higher build number over the last one, so + # each build is stamped when it is made (minutes since the epoch); + # the stamp is not part of the reuse key. + code = built // 60 + stamp = (f"--build-number={code}", f"--build-name=0.1.{code}") + docker(runner, *command, tag, "pipelines-app-build", kind, *arguments, *stamp) + (work / ".complete").write_text(json.dumps({"key": key, "built": built}) + "\n") if entry.exists(): shutil.rmtree(entry) work.rename(entry) diff --git a/scripts/test_app_build.py b/scripts/test_app_build.py new file mode 100644 index 00000000..8d3e48c1 --- /dev/null +++ b/scripts/test_app_build.py @@ -0,0 +1,60 @@ +import importlib.util +import json +from pathlib import Path +import subprocess +import tempfile +import unittest +from unittest.mock import patch + +spec = importlib.util.spec_from_file_location("app_build", Path(__file__).with_name("app_build.py")) +app_build = importlib.util.module_from_spec(spec) +spec.loader.exec_module(app_build) + + +class Runner: + def __init__(self): + self.calls = [] + + def __call__(self, command, **kwargs): + self.calls.append(list(command)) + if command[:2] == ["docker", "run"]: + out = Path(command[command.index("-v", command.index("-v") + 1) + 1].split(":")[0]) + (out / "pipelines.apk").write_bytes(b"apk") + return subprocess.CompletedProcess(command, 0, stdout="", stderr="") + + +class AppBuildTests(unittest.TestCase): + def setUp(self): + self.home = tempfile.TemporaryDirectory() + self.addCleanup(self.home.cleanup) + patcher = patch.object(app_build.Path, "home", return_value=Path(self.home.name)) + patcher.start() + self.addCleanup(patcher.stop) + + def test_apk_is_stamped_and_reused_while_sources_are_unchanged(self): + runner = Runner() + cache = Path(self.home.name) / "cache" + first = app_build.build("apk", runner=runner, cache=cache, arguments=("--dart-define=X=1",), log=lambda *_: None) + run = next(call for call in runner.calls if call[:2] == ["docker", "run"]) + built = json.loads((first / ".complete").read_text())["built"] + self.assertIn(f"--build-number={built // 60}", run) + self.assertIn("--dart-define=X=1", run) + self.assertIn("linux/amd64", run) + runner.calls.clear() + again = app_build.build("apk", runner=runner, cache=cache, arguments=("--dart-define=X=1",), log=lambda *_: None) + self.assertEqual(again, first) + self.assertEqual(runner.calls, []) + # Different build arguments are a different build. + other = app_build.build("apk", runner=runner, cache=cache, arguments=("--dart-define=X=2",), log=lambda *_: None) + self.assertNotEqual(other, first) + + def test_web_build_has_no_build_number(self): + runner = Runner() + app_build.build("web", runner=runner, cache=Path(self.home.name) / "cache", log=lambda *_: None) + run = next(call for call in runner.calls if call[:2] == ["docker", "run"]) + self.assertFalse(any(part.startswith("--build-number") for part in run)) + self.assertNotIn("linux/amd64", run) + + +if __name__ == "__main__": + unittest.main()