import "dart:io"; import "package:flutter_test/flutter_test.dart"; import "package:garage_updates/core.dart"; import "support/fake_install.dart"; import "support/fake_server.dart"; // the rule: a copy the updater didnt put there is left completly alone. no // check, no download, nothing swapped, one line on the console. void main() { group("detecting", () { test("this very test run is not an install", () { // flutter_tester off the jit, exactly the dev case final found = detectInstall(); expect(found.installer, isNull); expect(found.reason, isNotNull); }); test("a debug build is never an install, wherever it sits", () { final fake = FakeWindowsInstall.create(); addTearDown(fake.dispose); final found = detectInstall(executable: fake.exe.path, platform: "windows", product: false); expect(found.installer, isNull); expect(found.reason, contains("release build")); // the same place as a release build is one expect(detectInstall(executable: fake.exe.path, platform: "windows", product: true).installer, isNotNull); }); test("flutter run on windows", () { final found = detectInstall( executable: r"C:\src\app\build\windows\x64\runner\Debug\app.exe", platform: "windows", product: true, ); expect(found.installer, isNull); expect(found.reason, contains("app-")); }); test("a release exe run out of the build folder on windows", () { final tmp = Directory.systemTemp.createTempSync("gu_dev_"); addTearDown(() => tmp.deleteSync(recursive: true)); final release = Directory("${tmp.path}/build/windows/x64/runner/Release")..createSync(recursive: true); final found = detectInstall(executable: "${release.path}/app.exe", platform: "windows", product: true); expect(found.installer, isNull); }); test("dart run / a bare binary on the mac", () { final found = detectInstall( executable: "/opt/homebrew/Cellar/dart/3.8.1/libexec/bin/dart", platform: "macos", product: true, environment: const {"HOME": "/Users/someone"}, ); expect(found.installer, isNull); expect(found.reason, contains("app bundle")); }); test("flutter run on the mac", () { final found = detectInstall( executable: "/Users/someone/app/build/macos/Build/Products/Debug/App.app/Contents/MacOS/App", platform: "macos", product: true, environment: const {"HOME": "/Users/someone"}, ); expect(found.installer, isNull); expect(found.reason, contains("build folder")); }); test("a platform core has no installer for", () { final found = detectInstall(executable: "/usr/bin/app", platform: "linux", product: true); expect(found.installer, isNull); expect(found.reason, contains("linux")); }); }); group("an engine with no install", () { late FakeServer server; late UpdateEngine engine; final exits = []; setUp(() async { exits.clear(); server = await FakeServer.start(noise(1024)); server.updateBody = {"status": "required", "minimum_build": 99, "latest": server.release().toJson()}; engine = UpdateEngine( project: "proj_1", build: 12, notInstalledReason: "this isn't a release build", client: UpdateClient(project: "proj_1", apiBase: server.base, httpClient: HttpClient()), downloader: Downloader(httpClient: HttpClient(), retries: 0), arch: "arm64", exitProcess: exits.add, ); }); tearDown(() async { engine.close(); await server.close(); }); test("says why once, and never asks the server", () async { expect(engine.installed, isFalse); expect(engine.notInstalledReason, "this isn't a release build"); final lines = await printsOf(() async { expect((await engine.check()).status, UpdateStatus.unknown); expect((await engine.check()).hasUpdate, isFalse); expect(engine.reportIfNotInstalled(), isFalse); }); expect(lines, ["[garage_updates] not updating this copy: this isn't a release build"]); expect(server.log, isEmpty); }); test("wont download, install or restart", () async { final release = server.release(); final notInstalled = throwsA(isA().having((e) => e.code, "code", "not_installed")); await expectLater(engine.download(release), notInstalled); await expectLater(engine.install(release), notInstalled); await expectLater(engine.restart(), notInstalled); expect(server.log, isEmpty); expect(exits, isEmpty); }); test("counting starts and markHealthy are quiet no-ops", () async { expect(await engine.noteStart(), isFalse); await engine.markHealthy(); expect(await engine.isDownloaded(server.release()), isFalse); }); test("detect() off a test run gives exactly this engine", () { final e = UpdateEngine.detect(project: "proj_1", build: 12); addTearDown(e.close); expect(e.installed, isFalse); expect(e.installer, isNull); }); }); }