61 lines
2.5 KiB
Python
61 lines
2.5 KiB
Python
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()
|