Files
Garage-SDKs/garage_updates/test/mac_installer_test.dart
T
ImBenjiandClaude Opus 5.5 d3b232f0ee feat(garage_updates): an auto updater for garage apps
Asks Garage for the newest release of a project, downloads it with resume,
checks the sha256, installs it beside the old one and restarts into it.
Splash on garage_ui, a native stub and a first time installer for windows,
bundle swap on mac, the system installer on android. No-op on web.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KHotisA4hV2xmuZsHTksFX
2026-10-12 06:37:02 +01:00

297 lines
11 KiB
Dart

@TestOn("mac-os")
library;
import "dart:io";
import "package:flutter_test/flutter_test.dart";
import "package:garage_updates/core.dart";
import "support/fake_install.dart";
UpdateRelease _release(int build, {String format = "zip"}) => UpdateRelease(
label: "v$build",
build: build,
asset: UpdateAsset(url: "http://x/app.$format", filename: "app.$format", format: format, size: 3, sha256: "aa"),
);
void main() {
late Directory tmp;
late Directory apps;
late Directory bundle;
late MacInstaller mac;
setUp(() {
// resolved, /var is a symlink to /private/var and the paths get compared
tmp = Directory(Directory.systemTemp.createTempSync("gu_mac_").resolveSymbolicLinksSync());
apps = Directory("${tmp.path}/Applications")..createSync();
bundle = makeApp(apps, marker: "build 12");
mac = MacInstaller(
bundle: bundle,
bundleId: "net.imbenji.foo",
support: Directory("${tmp.path}/Library/Application Support/net.imbenji.foo/garage_updates"),
);
});
tearDown(() {
Process.runSync("/bin/chmod", ["-R", "u+w", tmp.path]);
tmp.deleteSync(recursive: true);
});
// a zip of build [build], sitting in staging the way the downloader leaves it
File stagedZip(int build, {String id = "net.imbenji.foo", String name = "Foo.app"}) {
final src = Directory("${tmp.path}/src-$build")..createSync();
final app = makeApp(src, marker: "build $build", id: id, name: name);
mac.staging.createSync(recursive: true);
return zipApp(app, File("${mac.staging.path}/update-$build.zip"));
}
group("finding the install", () {
test("an exe inside a bundle finds it, its id and where the state goes", () {
final found = MacInstaller.locate("${bundle.path}/Contents/MacOS/Foo", environment: {"HOME": tmp.path});
expect(found.reason, isNull);
final m = found.installer as MacInstaller;
expect(m.bundle.path, bundle.path);
expect(m.bundleId, "net.imbenji.foo");
expect(m.support.path, "${tmp.path}/Library/Application Support/net.imbenji.foo/garage_updates");
expect(m.state.file.path, endsWith("garage_updates/state.json"));
expect(m.previousApp.path, endsWith("garage_updates/previous.app"));
});
test("a helper nested in the bundle belongs to the outer app", () {
final helper = "${bundle.path}/Contents/Library/LoginItems/Helper.app/Contents/MacOS/Helper";
final found = MacInstaller.locate(helper, environment: {"HOME": tmp.path});
expect((found.installer as MacInstaller).bundle.path, bundle.path);
});
test("a bare executable is not an install", () {
final found = MacInstaller.locate("${tmp.path}/bin/dart", environment: {"HOME": tmp.path});
expect(found.installer, isNull);
expect(found.reason, contains("app bundle"));
});
test("a translocated bundle is not an install", () {
final found = MacInstaller.locate(
"/private/var/folders/xx/T/AppTranslocation/ABCD-1234/d/Foo.app/Contents/MacOS/Foo",
environment: {"HOME": tmp.path},
);
expect(found.installer, isNull);
expect(found.reason, contains("translocated"));
});
test("a build folder is not an install", () {
final found = MacInstaller.locate(
"${tmp.path}/proj/build/macos/Build/Products/Release/Foo.app/Contents/MacOS/Foo",
environment: {"HOME": tmp.path},
);
expect(found.installer, isNull);
expect(found.reason, contains("build folder"));
});
test("a bundle in a folder we cant write to is not an install", () {
Process.runSync("/bin/chmod", ["555", apps.path]);
final found = MacInstaller.locate("${bundle.path}/Contents/MacOS/Foo", environment: {"HOME": tmp.path});
expect(found.installer, isNull);
expect(found.reason, contains("can't be replaced"));
});
test("reads the bundle id out of a binary plist too", () {
final plist = "${bundle.path}/Contents/Info.plist";
final res = Process.runSync("/usr/bin/plutil", ["-convert", "binary1", plist]);
expect(res.exitCode, 0, reason: "${res.stderr}");
expect(readBundleId(bundle), "net.imbenji.foo");
});
});
group("the swap", () {
test("unpacks with ditto, keeps the old bundle as previous.app, puts the new one in place", () async {
final zip = stagedZip(13);
await mac.noteStart(12);
await mac.apply(zip, _release(13), runningBuild: 12);
expect(markerOf(bundle), "build 13");
expect(markerOf(mac.previousApp), "build 12");
// still a bundle someone could run
expect(File("${bundle.path}/Contents/MacOS/Foo").existsSync(), isTrue);
expect(readBundleId(bundle), "net.imbenji.foo");
final s = mac.state.read()!;
expect(s.current, 13);
expect(s.previous, 12);
expect(s.failedStarts, 0);
expect(zip.existsSync(), isFalse);
expect(Directory("${mac.support.path}/unpack").existsSync(), isFalse);
// and nothing else turned up beside the app
expect(apps.listSync().map((e) => e.uri.pathSegments.where((s) => s.isNotEmpty).last), ["Foo.app"]);
});
test("a second update replaces previous.app", () async {
await mac.apply(stagedZip(13), _release(13), runningBuild: 12);
await mac.apply(stagedZip(14), _release(14), runningBuild: 13);
expect(markerOf(bundle), "build 14");
expect(markerOf(mac.previousApp), "build 13");
expect(mac.state.read()!.previous, 13);
});
test("an update for some other app is refused and nothing moves", () async {
final zip = stagedZip(13, id: "com.someone.else");
await expectLater(
mac.apply(zip, _release(13), runningBuild: 12),
throwsA(isA<UpdateError>().having((e) => e.code, "code", "wrong_app")),
);
expect(markerOf(bundle), "build 12");
expect(mac.previousApp.existsSync(), isFalse);
});
test("a zip with no app in it is refused and nothing moves", () async {
final junk = Directory("${tmp.path}/junk")..createSync();
File("${junk.path}/readme.txt").writeAsStringSync("hello");
mac.staging.createSync(recursive: true);
final zip = File("${mac.staging.path}/update-13.zip");
Process.runSync("/usr/bin/ditto", ["-c", "-k", junk.path, zip.path]);
await expectLater(
mac.apply(zip, _release(13), runningBuild: 12),
throwsA(isA<UpdateError>().having((e) => e.code, "code", "no_app")),
);
expect(markerOf(bundle), "build 12");
});
test("a file that isnt a zip at all fails cleanly", () async {
mac.staging.createSync(recursive: true);
final zip = File("${mac.staging.path}/update-13.zip")..writeAsStringSync("not a zip");
await expectLater(
mac.apply(zip, _release(13), runningBuild: 12),
throwsA(isA<UpdateError>()),
);
expect(markerOf(bundle), "build 12");
});
test("the app inside can be one folder down, and named differently", () async {
final src = Directory("${tmp.path}/src/Foo 13")..createSync(recursive: true);
makeApp(src, marker: "build 13", name: "Foo Renamed.app");
mac.staging.createSync(recursive: true);
final zip = File("${mac.staging.path}/update-13.zip");
Process.runSync("/usr/bin/ditto", ["-c", "-k", "--keepParent", src.path, zip.path]);
await mac.apply(zip, _release(13), runningBuild: 12);
// it goes where the running bundle was, under the running bundles name
expect(markerOf(bundle), "build 13");
});
test("a dmg is mounted, copied out of and detached", () async {
final src = Directory("${tmp.path}/dmgsrc")..createSync();
makeApp(src, marker: "build 13");
mac.staging.createSync(recursive: true);
final dmg = File("${mac.staging.path}/update-13.dmg");
final made = Process.runSync(
"/usr/bin/hdiutil",
["create", "-srcfolder", src.path, "-volname", "Foo", "-format", "UDZO", "-ov", dmg.path],
);
expect(made.exitCode, 0, reason: "${made.stderr}");
await mac.apply(dmg, _release(13, format: "dmg"), runningBuild: 12);
expect(markerOf(bundle), "build 13");
expect(markerOf(mac.previousApp), "build 12");
expect(Directory("${mac.support.path}/mount").existsSync(), isFalse);
});
});
group("the fallback", () {
test("three starts with no markHealthy and the fourth goes back", () async {
await mac.noteStart(12);
await mac.markHealthy();
await mac.apply(stagedZip(13), _release(13), runningBuild: 12);
expect(await mac.noteStart(13), isFalse);
expect(await mac.noteStart(13), isFalse);
expect(await mac.noteStart(13), isFalse);
expect(mac.state.read()!.failedStarts, 3);
expect(markerOf(bundle), "build 13");
expect(await mac.noteStart(13), isTrue);
expect(markerOf(bundle), "build 12");
expect(mac.previousApp.existsSync(), isFalse);
expect(Directory("${mac.support.path}/failed.app").existsSync(), isFalse);
final s = mac.state.read()!;
expect(s.current, 12);
expect(s.previous, isNull);
expect(s.failedStarts, 0);
expect(s.blocked, [13]);
expect(mac.blockedBuilds(), [13]);
});
test("markHealthy stops the count", () async {
await mac.apply(stagedZip(13), _release(13), runningBuild: 12);
for (var i = 0; i < 6; i++) {
expect(await mac.noteStart(13), isFalse);
await mac.markHealthy();
}
expect(markerOf(bundle), "build 13");
expect(mac.state.read()!.failedStarts, 0);
expect(mac.state.read()!.blocked, isEmpty);
});
test("with no previous.app theres nowhere to go back to", () async {
for (var i = 0; i < 6; i++) {
expect(await mac.noteStart(12), isFalse);
}
expect(markerOf(bundle), "build 12");
expect(mac.state.read()!.blocked, isEmpty);
});
test("a build put there by hand starts its count from nought", () async {
mac.state.write(UpdateState(current: 12, previous: 9, failedStarts: 3));
// 13 was dragged into Applications, the file knows nothing about it
expect(await mac.noteStart(13), isFalse);
final s = mac.state.read()!;
expect(s.current, 13);
expect(s.failedStarts, 1);
// no previous.app on disk, so no previous
expect(s.previous, isNull);
});
test("a damaged state file is started again rather than trusted", () async {
mac.support.createSync(recursive: true);
mac.state.file.writeAsStringSync('{ "current": 1');
expect(await mac.noteStart(12), isFalse);
expect(mac.state.read()!.current, 12);
expect(mac.state.read()!.failedStarts, 1);
});
});
test("relaunch is open -n on the bundle, arguments after --args", () async {
final calls = <List<String>>[];
final m = MacInstaller(
bundle: bundle,
bundleId: "net.imbenji.foo",
support: mac.support,
run: (exe, args) async {
calls.add([exe, ...args]);
return ProcessResult(1, 0, "", "");
},
);
await m.relaunch(const []);
await m.relaunch(["--route=/settings"]);
expect(calls, [
["/usr/bin/open", "-n", bundle.path],
["/usr/bin/open", "-n", bundle.path, "--args", "--route=/settings"],
]);
});
}