import 'dart:io'; import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/features/updates/linux_self_update.dart'; /// Builds a release-shaped archive: pipelines-linux-x64/{pipelines-app,data/flutter_assets,lib}. Future _archive( Directory root, String marker, { bool complete = true, }) async { final src = Directory('${root.path}/src-$marker/pipelines-linux-x64'); await Directory('${src.path}/data/flutter_assets').create(recursive: true); if (complete) await Directory('${src.path}/lib').create(); await File('${src.path}/pipelines-app').writeAsString(marker); final out = File('${root.path}/$marker.tar.gz'); final result = await Process.run('tar', [ '-czf', out.path, '-C', src.parent.path, 'pipelines-linux-x64', ]); expect(result.exitCode, 0, reason: '${result.stderr}'); return out; } void main() { late Directory root; late Directory install; setUp(() async { root = await Directory.systemTemp.createTemp('linux-self-update'); install = Directory('${root.path}/apps/pipelines'); await Directory( '${install.path}/data/flutter_assets', ).create(recursive: true); await Directory('${install.path}/lib').create(); await File('${install.path}/pipelines-app').writeAsString('old'); }); tearDown(() => root.delete(recursive: true)); test('swaps in the new bundle and keeps the previous one', () async { final updater = LinuxSelfUpdate(installDir: install); expect(await updater.problem(), isNull); final exe = await updater.install( await _archive(root, 'new'), versionCode: 7, ); expect(exe, '${install.path}/pipelines-app'); expect(await File(exe).readAsString(), 'new'); expect( await File( '${root.path}/apps/pipelines.previous/pipelines-app', ).readAsString(), 'old', ); // No staging folder is left behind. expect( Directory('${root.path}/apps').listSync().map( (e) => e.uri.pathSegments.where((s) => s.isNotEmpty).last, ), unorderedEquals(['pipelines', 'pipelines.previous']), ); // A second update replaces the kept previous bundle. await updater.install(await _archive(root, 'newer'), versionCode: 8); expect(await File(exe).readAsString(), 'newer'); expect( await File( '${root.path}/apps/pipelines.previous/pipelines-app', ).readAsString(), 'new', ); }); test('an incomplete archive leaves the installed app untouched', () async { final updater = LinuxSelfUpdate(installDir: install); await expectLater( updater.install( await _archive(root, 'broken', complete: false), versionCode: 9, ), throwsA(isA()), ); expect(await File('${install.path}/pipelines-app').readAsString(), 'old'); expect( Directory('${root.path}/apps/pipelines.previous').existsSync(), isFalse, ); }); test('a corrupt archive is reported, not installed', () async { final updater = LinuxSelfUpdate(installDir: install); final junk = File('${root.path}/junk.tar.gz')..writeAsStringSync('nope'); await expectLater( updater.install(junk, versionCode: 10), throwsA( isA().having( (e) => e.message, 'message', contains('could not be extracted'), ), ), ); expect(await File('${install.path}/pipelines-app').readAsString(), 'old'); }); test('a read-only install location explains how to fix it', () async { final apps = Directory('${root.path}/apps'); await Process.run('chmod', ['555', apps.path]); addTearDown(() => Process.run('chmod', ['755', apps.path])); final reason = await LinuxSelfUpdate(installDir: install).problem(); expect(reason, contains('Reinstall it in your home folder')); }); }