| 
 | 1 | +// Copyright 2014 The Flutter Authors. All rights reserved.  | 
 | 2 | +// Use of this source code is governed by a BSD-style license that can be  | 
 | 3 | +// found in the LICENSE file.  | 
 | 4 | + | 
 | 5 | +// @dart = 2.8  | 
 | 6 | + | 
 | 7 | +import 'package:file/file.dart';  | 
 | 8 | +import 'package:file/memory.dart';  | 
 | 9 | +import 'package:flutter_tools/src/base/file_system.dart';  | 
 | 10 | +import 'package:flutter_tools/src/base/logger.dart';  | 
 | 11 | +import 'package:flutter_tools/src/base/os.dart';  | 
 | 12 | +import 'package:flutter_tools/src/windows/application_package.dart';  | 
 | 13 | +import 'package:test/fake.dart';  | 
 | 14 | + | 
 | 15 | +import '../../src/common.dart';  | 
 | 16 | +import '../../src/context.dart';  | 
 | 17 | + | 
 | 18 | +void main() {  | 
 | 19 | +  group('PrebuiltWindowsApp', () {  | 
 | 20 | +    FakeOperatingSystemUtils os;  | 
 | 21 | +    FileSystem fileSystem;  | 
 | 22 | +    BufferLogger logger;  | 
 | 23 | + | 
 | 24 | +    final Map<Type, Generator> overrides = <Type, Generator>{  | 
 | 25 | +      FileSystem: () => fileSystem,  | 
 | 26 | +      ProcessManager: () => FakeProcessManager.any(),  | 
 | 27 | +      OperatingSystemUtils: () => os,  | 
 | 28 | +      Logger: () => logger,  | 
 | 29 | +    };  | 
 | 30 | + | 
 | 31 | +    setUp(() {  | 
 | 32 | +      fileSystem = MemoryFileSystem.test();  | 
 | 33 | +      os = FakeOperatingSystemUtils();  | 
 | 34 | +      logger = BufferLogger.test();  | 
 | 35 | +    });  | 
 | 36 | + | 
 | 37 | +    testUsingContext('Error on non-existing exe file', () {  | 
 | 38 | +      final PrebuiltWindowsApp windowsApp = WindowsApp.fromPrebuiltApp(fileSystem.file('not_existing.exe')) as PrebuiltWindowsApp;  | 
 | 39 | + | 
 | 40 | +      expect(windowsApp, isNull);  | 
 | 41 | +      expect(logger.errorText, contains('File "not_existing.exe" does not exist.'));  | 
 | 42 | +    }, overrides: overrides);  | 
 | 43 | + | 
 | 44 | +    testUsingContext('Success on exe file', () {  | 
 | 45 | +      fileSystem.file('file.exe').createSync();  | 
 | 46 | +      final PrebuiltWindowsApp windowsApp = WindowsApp.fromPrebuiltApp(fileSystem.file('file.exe')) as PrebuiltWindowsApp;  | 
 | 47 | + | 
 | 48 | +      expect(windowsApp.name, 'file.exe');  | 
 | 49 | +    }, overrides: overrides);  | 
 | 50 | + | 
 | 51 | +    testUsingContext('Error on non-existing zip file', () {  | 
 | 52 | +      final PrebuiltWindowsApp windowsApp = WindowsApp.fromPrebuiltApp(fileSystem.file('not_existing.zip')) as PrebuiltWindowsApp;  | 
 | 53 | + | 
 | 54 | +      expect(windowsApp, isNull);  | 
 | 55 | +      expect(logger.errorText, contains('File "not_existing.zip" does not exist.'));  | 
 | 56 | +    }, overrides: overrides);  | 
 | 57 | + | 
 | 58 | +    testUsingContext('Bad zipped app, no payload dir', () {  | 
 | 59 | +      fileSystem.file('app.zip').createSync();  | 
 | 60 | +      final PrebuiltWindowsApp windowsApp = WindowsApp.fromPrebuiltApp(fileSystem.file('app.zip')) as PrebuiltWindowsApp;  | 
 | 61 | + | 
 | 62 | +      expect(windowsApp, isNull);  | 
 | 63 | +      expect(logger.errorText, contains('Cannot find .exe files in the zip archive.'));  | 
 | 64 | +    }, overrides: overrides);  | 
 | 65 | + | 
 | 66 | +    testUsingContext('Bad zipped app, two .exe files', () {  | 
 | 67 | +      fileSystem.file('app.zip').createSync();  | 
 | 68 | +      os.unzipOverride = (File zipFile, Directory targetDirectory) {  | 
 | 69 | +        if (zipFile.path != 'app.zip') {  | 
 | 70 | +          return;  | 
 | 71 | +        }  | 
 | 72 | +        final String exePath1 = fileSystem.path.join(targetDirectory.path, 'app1.exe');  | 
 | 73 | +        final String exePath2 = fileSystem.path.join(targetDirectory.path, 'app2.exe');  | 
 | 74 | +        fileSystem.directory(exePath1).createSync(recursive: true);  | 
 | 75 | +        fileSystem.directory(exePath2).createSync(recursive: true);  | 
 | 76 | +      };  | 
 | 77 | +      final PrebuiltWindowsApp windowsApp = WindowsApp.fromPrebuiltApp(fileSystem.file('app.zip')) as PrebuiltWindowsApp;  | 
 | 78 | + | 
 | 79 | +      expect(windowsApp, isNull);  | 
 | 80 | +      expect(logger.errorText, contains('Archive "app.zip" contains more than one .exe files.'));  | 
 | 81 | +    }, overrides: overrides);  | 
 | 82 | + | 
 | 83 | +    testUsingContext('Success with zipped app', () {  | 
 | 84 | +      fileSystem.file('app.zip').createSync();  | 
 | 85 | +      String exePath;  | 
 | 86 | +      os.unzipOverride = (File zipFile, Directory targetDirectory) {  | 
 | 87 | +        if (zipFile.path != 'app.zip') {  | 
 | 88 | +          return;  | 
 | 89 | +        }  | 
 | 90 | +        exePath = fileSystem.path.join(targetDirectory.path, 'app.exe');  | 
 | 91 | +        fileSystem.directory(exePath).createSync(recursive: true);  | 
 | 92 | +      };  | 
 | 93 | +      final PrebuiltWindowsApp windowsApp = WindowsApp.fromPrebuiltApp(fileSystem.file('app.zip')) as PrebuiltWindowsApp;  | 
 | 94 | + | 
 | 95 | +      expect(logger.errorText, isEmpty);  | 
 | 96 | +      expect(windowsApp.name, exePath);  | 
 | 97 | +      expect(windowsApp.applicationPackage.path, 'app.zip');  | 
 | 98 | +    }, overrides: overrides);  | 
 | 99 | + | 
 | 100 | +    testUsingContext('Error on unknown file type', () {  | 
 | 101 | +      fileSystem.file('not_existing.app').createSync();  | 
 | 102 | +      final PrebuiltWindowsApp windowsApp = WindowsApp.fromPrebuiltApp(fileSystem.file('not_existing.app')) as PrebuiltWindowsApp;  | 
 | 103 | + | 
 | 104 | +      expect(windowsApp, isNull);  | 
 | 105 | +      expect(logger.errorText, contains('Unknown windows application type.'));  | 
 | 106 | +    }, overrides: overrides);  | 
 | 107 | +  });  | 
 | 108 | +}  | 
 | 109 | + | 
 | 110 | +class FakeOperatingSystemUtils extends Fake implements OperatingSystemUtils {  | 
 | 111 | +  FakeOperatingSystemUtils();  | 
 | 112 | + | 
 | 113 | +  void Function(File, Directory) unzipOverride;  | 
 | 114 | + | 
 | 115 | +  @override  | 
 | 116 | +  void unzip(File file, Directory targetDirectory) {  | 
 | 117 | +    unzipOverride?.call(file, targetDirectory);  | 
 | 118 | +  }  | 
 | 119 | +}  | 
0 commit comments