Files
pipelines/scripts/test_app_desktop_build.py
T
alan 8138cdc3d9 Linux desktop: optimized release build, installer, Linux gaps
Build: split Dart debug info out of libapp.so (13.96 -> 11.27 MB, symbols
kept in the cache) and fix icon tree shaking, which Flutter 3.44's Linux
CMake backend silently disables by quoting the define; the build image
patches its SDK copy (MaterialIcons 1.6 MB -> 25 KB). Impeller cannot be
enabled in a Linux release build and was slower in a profile-build bench.

Install: `./install --linux --install` installs the bundle into
<prefix>/lib/app-linux with com.pipelines.app.desktop (the GTK/Wayland
app id), its icon and a ~/.local/bin/pipelines-app link, through
scripts/install-linux-app.sh.

Linux gaps: notifications through the freedesktop daemon; HTML artifacts
explain the missing web view instead of throwing; no Share for files;
a GTK clipboard reader for pasting files and screenshots (Ctrl+V and
Omarchy's Super+V); GTK header bar only on GNOME; window icon.

Bench: native fixture frame log, wheel/mark steps and
tool/perf/linux_desktop_bench.py (private Xvfb); results in
app/notes/linux-desktop-optimized.md.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-03 12:54:18 +00:00

246 lines
13 KiB
Python

import importlib.util
import json
from pathlib import Path
import subprocess
import tarfile
import tempfile
import unittest
from unittest.mock import patch
def load(name):
spec = importlib.util.spec_from_file_location(name, Path(__file__).with_name(name + ".py"))
result = importlib.util.module_from_spec(spec)
spec.loader.exec_module(result)
return result
core = load("app_build")
desktop = load("app_desktop_build")
class Runner:
def __init__(self):
self.calls = []
self.native_env = None
self.fail_native = False
self.version = "3.44.6"
def __call__(self, command, **kwargs):
self.calls.append(command)
stdout = ""
code = 0
if "--machine" in command:
stdout = json.dumps({"frameworkVersion": self.version})
elif command[:2] == ["xcodebuild", "-version"]:
stdout = "Xcode 27.0\nBuild version 27A266a"
elif command[:2] == ["docker", "run"]:
output = Path(next(arg[:-5] for arg in command if arg.endswith(":/out")))
(output / "pipelines-app").write_bytes(b"linux-client")
(output / "pipelines-app").chmod(0o755)
(output / "data/flutter_assets").mkdir(parents=True)
(output / "data/flutter_assets/font.ttf").write_bytes(b"font")
(output / "lib").mkdir()
(output / "lib/engine.so").write_bytes(b"engine")
elif command[1:3] == ["build", "macos"]:
self.native_env = kwargs.get("env")
if self.fail_native:
code = 1
else:
app = Path(kwargs["cwd"]) / "build/macos/Build/Products/Release/Pipelines.app"
(app / "Contents/MacOS").mkdir(parents=True)
(app / "Contents/MacOS/Pipelines").write_bytes(b"macos-client")
(app / "Contents/MacOS/Pipelines").chmod(0o755)
framework = app / "Contents/Frameworks/Fixture.framework"
(framework / "Versions/A").mkdir(parents=True)
(framework / "Versions/A/Fixture").write_bytes(b"framework")
(framework / "Versions/Current").symlink_to("A")
(framework / "Fixture").symlink_to("Versions/Current/Fixture")
elif command[:2] == ["lipo", "-archs"]:
stdout = "x86_64 arm64"
elif command[0] == "ditto":
Path(command[-1]).write_bytes(b"archive")
return subprocess.CompletedProcess(command, code, stdout=stdout, stderr="")
class DesktopBuildTests(unittest.TestCase):
def setUp(self):
self.temporary = tempfile.TemporaryDirectory()
self.addCleanup(self.temporary.cleanup)
self.root = Path(self.temporary.name)
self.app = self.root / "app"
for directory in ("lib", "fonts", "linux", "macos", "docker"):
(self.app / directory).mkdir(parents=True, exist_ok=True)
(self.app / "lib/main.dart").write_text("main() {}")
(self.app / ".flutter-version").write_text("3.44.6\n")
self.runner = Runner()
def build(self, kind, **kwargs):
return desktop.build_desktop(kind, app=self.app, cache=self.root / "cache",
runner=self.runner, source_digest=core.digest, docker_build=core.build,
docker_ready=lambda *_: True, log=lambda *_: None, **kwargs)
def test_linux_is_x64_and_packages_full_bundle_without_installation(self):
bundle, archive = self.build("linux", output=self.root / "artifacts")
builds = [c for c in self.runner.calls if c[:2] == ["docker", "build"]]
self.assertEqual(builds[0][builds[0].index("--target") + 1], "linux")
for command in [builds[0], next(c for c in self.runner.calls if c[:2] == ["docker", "run"])]:
self.assertEqual(command[command.index("--platform") + 1], "linux/amd64")
with tarfile.open(archive) as package:
self.assertTrue({"pipelines-linux-x64/pipelines-app", "pipelines-linux-x64/lib/engine.so",
"pipelines-linux-x64/data/flutter_assets/font.ttf"}.issubset(package.getnames()))
self.assertEqual(package.getmember("pipelines-linux-x64/pipelines-app").mode & 0o111, 0o111)
self.assertTrue(bundle.is_dir())
before = len(self.runner.calls)
self.build("linux")
self.assertEqual(len(self.runner.calls), before)
def test_font_and_target_sources_invalidate_correct_caches(self):
keys = {kind: core.digest(kind, self.app) for kind in core.SOURCES}
(self.app / "fonts/font.ttf").write_bytes(b"new font")
for kind in core.SOURCES:
self.assertNotEqual(keys[kind], core.digest(kind, self.app), kind)
linux = core.digest("linux", self.app)
macos = core.digest("macos", self.app)
(self.app / "linux/CMakeLists.txt").write_text("changed native runner")
self.assertNotEqual(linux, core.digest("linux", self.app))
self.assertEqual(macos, core.digest("macos", self.app))
generated = self.app / "macos/Flutter/ephemeral"
generated.mkdir(parents=True)
(generated / "local-path").write_text("machine-specific generated value")
self.assertEqual(macos, core.digest("macos", self.app))
def test_pub_get_generated_linux_registries_do_not_change_source_key(self):
before = core.digest("linux", self.app)
directory = self.app / "linux/flutter"
directory.mkdir()
for name in ("generated_plugin_registrant.cc", "generated_plugin_registrant.h", "generated_plugins.cmake"):
(directory / name).write_text("generated from locked dependencies")
self.assertEqual(before, core.digest("linux", self.app))
(self.app / "pubspec.lock").write_text("changed dependency lock")
self.assertNotEqual(before, core.digest("linux", self.app))
@patch.object(desktop.platform, "system", return_value="Darwin")
def test_native_macos_preserves_framework_symlinks_and_reuses_build(self, _):
bundle, archive = self.build("macos", flutter="/fixture/flutter")
self.assertTrue((bundle / "Contents/Frameworks/Fixture.framework/Versions/Current").is_symlink())
self.assertEqual((bundle / "Contents/MacOS/Pipelines").stat().st_mode & 0o111, 0o111)
self.assertTrue(archive.is_file())
self.build("macos", flutter="/fixture/flutter")
self.assertEqual(sum(c[1:3] == ["build", "macos"] for c in self.runner.calls), 1)
self.assertFalse(any(c[0] == "docker" for c in self.runner.calls))
self.assertFalse((self.app / "build").exists())
expected = {"aarch64": "arm64", "x86_64": "x86_64"}.get(desktop.platform.machine(), desktop.platform.machine())
self.assertEqual(self.runner.native_env["FLUTTER_XCODE_ARCHS"], expected)
@patch.object(desktop.platform, "system", return_value="Darwin")
def test_failed_native_build_preserves_previous_artifact(self, _):
bundle, archive = self.build("macos", flutter="/fixture/flutter")
previous = archive.read_bytes()
(self.app / "lib/main.dart").write_text("changed application")
self.runner.fail_native = True
with self.assertRaisesRegex(RuntimeError, "failed"):
self.build("macos", flutter="/fixture/flutter")
self.assertEqual(archive.read_bytes(), previous)
self.assertTrue((bundle / "Contents/MacOS/Pipelines").is_file())
@patch.object(desktop.platform, "system", return_value="Darwin")
def test_wrong_sdk_fails_before_building_or_changing_any_sdk(self, _):
self.runner.version = "3.47.0"
with self.assertRaisesRegex(ValueError, "3.44.6 is required"):
self.build("macos", flutter="/fixture/flutter")
self.assertEqual(len(self.runner.calls), 1)
@patch.object(desktop.platform, "system", return_value="Linux")
def test_macos_refuses_non_macos_host_without_docker_fallback(self, _):
with self.assertRaisesRegex(ValueError, "natively on macOS"):
self.build("macos")
self.assertEqual(self.runner.calls, [])
def test_linux_publication_has_matching_hash_and_rejects_downgrade(self):
bundle, archive = self.build("linux")
updates = self.root / "updates"
signed = []
signer = lambda data: signed.append(data) or b"signature document\n"
metadata = desktop.publish_desktop("linux", bundle, archive, updates, signer=signer, log=lambda *_: None)
target = updates / "linux-x64"
self.assertEqual(json.loads((target / "version.json").read_text()), metadata)
# The detached signature covers the exact published feed bytes.
self.assertEqual(signed, [(target / "version.json").read_bytes()])
self.assertEqual((target / "version.json.sig").read_bytes(), b"signature document\n")
self.assertEqual(metadata["architectures"], ["x64"])
self.assertEqual(metadata["sha256"], desktop.hashlib.sha256((target / metadata["file"]).read_bytes()).hexdigest())
metadata["versionCode"] += 1
(target / "version.json").write_text(json.dumps(metadata))
with self.assertRaisesRegex(ValueError, "older build"):
desktop.publish_desktop("linux", bundle, archive, updates, signer=signer)
@patch.object(desktop.platform, "system", return_value="Darwin")
def test_macos_feed_uses_actual_architectures_and_native_build_stamp(self, _):
bundle, archive = self.build("macos", flutter="/fixture/flutter")
metadata = desktop.publish_desktop("macos", bundle, archive, self.root / "updates",
signer=lambda data: b"{}\n", log=lambda *_: None)
self.assertTrue((self.root / "updates/macos/version.json.sig").is_file())
self.assertEqual(metadata["architectures"], ["arm64", "x64"])
command = next(c for c in self.runner.calls if c[1:3] == ["build", "macos"])
self.assertIn(f"--dart-define=PIPELINES_NATIVE_BUILD_CODE={metadata['versionCode']}", command)
def test_desktop_builds_embed_the_trusted_update_keys(self):
self.build("linux", update_keys=("--dart-define=PIPELINES_UPDATE_PUBLIC_KEY=abc",))
run = next(c for c in self.runner.calls if c[:2] == ["docker", "run"])
self.assertIn("--dart-define=PIPELINES_UPDATE_PUBLIC_KEY=abc", run)
def test_linux_archive_leaves_split_debug_symbols_in_the_cache(self):
bundle, archive = self.build("linux")
(bundle / ".debug-info").mkdir()
(bundle / ".debug-info/app.linux-x64.symbols").write_bytes(b"symbols")
archive.unlink()
archive = desktop._linux_archive(bundle)
with tarfile.open(archive) as package:
self.assertFalse([n for n in package.getnames() if ".debug-info" in n])
self.assertIn("pipelines-linux-x64/pipelines-app", package.getnames())
def test_linux_install_runs_the_user_installer_with_the_prefix(self):
_, archive = self.build("linux", install=True, prefix=self.root / "prefix")
command = self.runner.calls[-1]
self.assertEqual(command[0], "sh")
self.assertTrue(command[1].endswith("install-linux-app.sh"))
self.assertEqual(command[2], str(archive))
with self.assertRaisesRegex(ValueError, "Linux client only"):
self.build("macos", install=True)
def test_linux_installer_installs_bundle_icon_and_desktop_entry(self):
_, archive = self.build("linux")
home = self.root / "home"
icon = self.root / "icon-bundle"
# A bundle with its icon, as the Linux CMake install lays it out.
with tarfile.open(archive) as package:
package.extractall(icon, filter="data")
(icon / "pipelines-linux-x64/data/app_icon.png").write_bytes(b"png")
with tarfile.open(archive, "w:gz") as package:
package.add(icon / "pipelines-linux-x64", arcname="pipelines-linux-x64")
environment = {"HOME": str(home), "XDG_DATA_HOME": str(home / ".local/share"),
"PATH": "/usr/bin:/bin"}
with patch.dict(desktop.os.environ, environment, clear=True):
desktop.install_linux(archive, runner=subprocess.run, prefix=home / "prefix")
desktop.install_linux(archive, runner=subprocess.run, prefix=home / "prefix")
target = home / "prefix/lib/app-linux"
self.assertEqual((target / "pipelines-app").read_bytes(), b"linux-client")
self.assertTrue((home / "prefix/lib/app-linux.previous/pipelines-app").is_file())
entry = (home / ".local/share/applications/com.pipelines.app.desktop").read_text()
self.assertIn(f"Exec={target}/pipelines-app\n", entry)
self.assertIn("Icon=com.pipelines.app\n", entry)
self.assertIn("StartupWMClass=com.pipelines.app\n", entry)
self.assertTrue((home / ".local/share/icons/hicolor/512x512/apps/com.pipelines.app.png").is_file())
self.assertEqual((home / ".local/bin/pipelines-app").resolve(), (target / "pipelines-app").resolve())
self.assertFalse(list((home / "prefix/lib").glob(".app-linux-install.*")))
def test_desktop_server_prefill_rejects_embedded_credentials(self):
with self.assertRaisesRegex(ValueError, "embedded credentials"):
self.build("linux", server_url="wss://secret@example.com/connect")
self.assertEqual(self.runner.calls, [])
if __name__ == "__main__":
unittest.main()