import 'dart:io'; import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/features/updates/macos_self_update.dart'; /// Builds a release-shaped archive holding one Pipelines.app bundle. Future _archive( Directory root, String marker, { bool complete = true, }) async { final src = Directory('${root.path}/src-$marker'); final contents = Directory('${src.path}/Pipelines.app/Contents'); await Directory('${contents.path}/MacOS').create(recursive: true); if (complete) { await File('${contents.path}/Info.plist').writeAsString(''); } await File('${contents.path}/MacOS/Pipelines').writeAsString(marker); final out = File('${root.path}/$marker.zip'); final result = await Process.run('/usr/bin/ditto', [ '-c', '-k', '--keepParent', '${src.path}/Pipelines.app', out.path, ]); expect(result.exitCode, 0, reason: '${result.stderr}'); return out; } void main() { late Directory root; late Directory bundle; setUp(() async { root = await Directory.systemTemp.createTemp('macos-self-update'); bundle = Directory('${root.path}/Applications/Pipelines.app'); await Directory('${bundle.path}/Contents/MacOS').create(recursive: true); await File('${bundle.path}/Contents/Info.plist').writeAsString(''); await File('${bundle.path}/Contents/MacOS/Pipelines').writeAsString('old'); }); tearDown(() => root.delete(recursive: true)); String installed() => File('${bundle.path}/Contents/MacOS/Pipelines').readAsStringSync(); test('swaps in the new bundle and keeps the previous one', () async { final updater = MacSelfUpdate(bundle: bundle); expect(await updater.problem(), isNull); final app = await updater.install( await _archive(root, 'new'), versionCode: 2, ); expect(app, bundle.path); expect(installed(), 'new'); final previous = File( '${root.path}/Applications/Pipelines.app.previous/Contents/MacOS/Pipelines', ); expect(previous.readAsStringSync(), 'old'); // No staging folder is left behind. expect( Directory('${root.path}/Applications').listSync().map((e) => e.path), unorderedEquals([bundle.path, '${bundle.path}.previous']), ); }); test('a second update replaces the kept previous bundle', () async { final updater = MacSelfUpdate(bundle: bundle); await updater.install(await _archive(root, 'two'), versionCode: 2); await updater.install(await _archive(root, 'three'), versionCode: 3); expect(installed(), 'three'); expect( File( '${bundle.path}.previous/Contents/MacOS/Pipelines', ).readAsStringSync(), 'two', ); }); test('an incomplete archive leaves the installed app untouched', () async { final updater = MacSelfUpdate(bundle: bundle); await expectLater( updater.install( await _archive(root, 'broken', complete: false), versionCode: 2, ), throwsA(isA()), ); expect(installed(), 'old'); expect(Directory('${bundle.path}.previous').existsSync(), isFalse); }); test('a failed extraction reports why and changes nothing', () async { final updater = MacSelfUpdate( bundle: bundle, run: (exe, args) async => ProcessResult(0, 1, '', 'bad archive'), ); await expectLater( updater.install(File('${root.path}/missing.zip'), versionCode: 2), throwsA( isA().having( (e) => e.message, 'message', contains('bad archive'), ), ), ); expect(installed(), 'old'); }); test('outside an app bundle there is nothing to update', () async { final updater = MacSelfUpdate(bundle: Directory('${root.path}/plain')); expect(await updater.problem(), contains('not running from an app')); }); }