application_package_test.dart 4.58 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:file/file.dart';
import 'package:file/memory.dart';
import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/base/logger.dart';
import 'package:flutter_tools/src/base/os.dart';
import 'package:flutter_tools/src/windows/application_package.dart';
import 'package:test/fake.dart';

import '../../src/common.dart';
import '../../src/context.dart';

void main() {
  group('PrebuiltWindowsApp', () {
18 19 20
    late FakeOperatingSystemUtils os;
    late FileSystem fileSystem;
    late BufferLogger logger;
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35

    final Map<Type, Generator> overrides = <Type, Generator>{
      FileSystem: () => fileSystem,
      ProcessManager: () => FakeProcessManager.any(),
      OperatingSystemUtils: () => os,
      Logger: () => logger,
    };

    setUp(() {
      fileSystem = MemoryFileSystem.test();
      os = FakeOperatingSystemUtils();
      logger = BufferLogger.test();
    });

    testUsingContext('Error on non-existing exe file', () {
36
      final PrebuiltWindowsApp? windowsApp = WindowsApp.fromPrebuiltApp(fileSystem.file('not_existing.exe')) as PrebuiltWindowsApp?;
37 38 39 40 41 42 43

      expect(windowsApp, isNull);
      expect(logger.errorText, contains('File "not_existing.exe" does not exist.'));
    }, overrides: overrides);

    testUsingContext('Success on exe file', () {
      fileSystem.file('file.exe').createSync();
44
      final PrebuiltWindowsApp windowsApp = WindowsApp.fromPrebuiltApp(fileSystem.file('file.exe'))! as PrebuiltWindowsApp;
45 46 47 48 49

      expect(windowsApp.name, 'file.exe');
    }, overrides: overrides);

    testUsingContext('Error on non-existing zip file', () {
50
      final PrebuiltWindowsApp? windowsApp = WindowsApp.fromPrebuiltApp(fileSystem.file('not_existing.zip')) as PrebuiltWindowsApp?;
51 52 53 54 55 56 57

      expect(windowsApp, isNull);
      expect(logger.errorText, contains('File "not_existing.zip" does not exist.'));
    }, overrides: overrides);

    testUsingContext('Bad zipped app, no payload dir', () {
      fileSystem.file('app.zip').createSync();
58
      final PrebuiltWindowsApp? windowsApp = WindowsApp.fromPrebuiltApp(fileSystem.file('app.zip')) as PrebuiltWindowsApp?;
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74

      expect(windowsApp, isNull);
      expect(logger.errorText, contains('Cannot find .exe files in the zip archive.'));
    }, overrides: overrides);

    testUsingContext('Bad zipped app, two .exe files', () {
      fileSystem.file('app.zip').createSync();
      os.unzipOverride = (File zipFile, Directory targetDirectory) {
        if (zipFile.path != 'app.zip') {
          return;
        }
        final String exePath1 = fileSystem.path.join(targetDirectory.path, 'app1.exe');
        final String exePath2 = fileSystem.path.join(targetDirectory.path, 'app2.exe');
        fileSystem.directory(exePath1).createSync(recursive: true);
        fileSystem.directory(exePath2).createSync(recursive: true);
      };
75
      final PrebuiltWindowsApp? windowsApp = WindowsApp.fromPrebuiltApp(fileSystem.file('app.zip')) as PrebuiltWindowsApp?;
76 77 78 79 80 81 82

      expect(windowsApp, isNull);
      expect(logger.errorText, contains('Archive "app.zip" contains more than one .exe files.'));
    }, overrides: overrides);

    testUsingContext('Success with zipped app', () {
      fileSystem.file('app.zip').createSync();
83
      String? exePath;
84 85 86 87 88 89 90
      os.unzipOverride = (File zipFile, Directory targetDirectory) {
        if (zipFile.path != 'app.zip') {
          return;
        }
        exePath = fileSystem.path.join(targetDirectory.path, 'app.exe');
        fileSystem.directory(exePath).createSync(recursive: true);
      };
91
      final PrebuiltWindowsApp windowsApp = WindowsApp.fromPrebuiltApp(fileSystem.file('app.zip'))! as PrebuiltWindowsApp;
92 93 94 95 96 97 98 99

      expect(logger.errorText, isEmpty);
      expect(windowsApp.name, exePath);
      expect(windowsApp.applicationPackage.path, 'app.zip');
    }, overrides: overrides);

    testUsingContext('Error on unknown file type', () {
      fileSystem.file('not_existing.app').createSync();
100
      final PrebuiltWindowsApp? windowsApp = WindowsApp.fromPrebuiltApp(fileSystem.file('not_existing.app')) as PrebuiltWindowsApp?;
101 102 103 104 105 106 107 108 109 110

      expect(windowsApp, isNull);
      expect(logger.errorText, contains('Unknown windows application type.'));
    }, overrides: overrides);
  });
}

class FakeOperatingSystemUtils extends Fake implements OperatingSystemUtils {
  FakeOperatingSystemUtils();

111
  void Function(File, Directory)? unzipOverride;
112 113 114 115 116 117

  @override
  void unzip(File file, Directory targetDirectory) {
    unzipOverride?.call(file, targetDirectory);
  }
}