application_package_test.dart 8.22 KB
Newer Older
1 2 3 4 5 6 7 8 9
// 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 'dart:convert';

import 'package:file/file.dart';
import 'package:file/memory.dart';
import 'package:flutter_tools/src/base/file_system.dart';
10
import 'package:flutter_tools/src/base/logger.dart';
11 12
import 'package:flutter_tools/src/base/os.dart';
import 'package:flutter_tools/src/base/utils.dart';
13
import 'package:flutter_tools/src/globals.dart' as globals;
14
import 'package:flutter_tools/src/ios/plist_parser.dart';
15
import 'package:flutter_tools/src/macos/application_package.dart';
16
import 'package:flutter_tools/src/project.dart';
17
import 'package:test/fake.dart';
18 19 20 21 22

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

void main() {
23
group('PrebuiltMacOSApp', () {
24 25 26
    late FakeOperatingSystemUtils os;
    late FileSystem fileSystem;
    late BufferLogger logger;
27

28
    final Map<Type, Generator> overrides = <Type, Generator>{
29
      FileSystem: () => fileSystem,
30
      ProcessManager: () => FakeProcessManager.any(),
31
      PlistParser: () => FakePlistUtils(fileSystem),
32
      OperatingSystemUtils: () => os,
33
      Logger: () => logger,
34 35 36
    };

    setUp(() {
37 38 39
      fileSystem = MemoryFileSystem.test();
      os = FakeOperatingSystemUtils();
      logger = BufferLogger.test();
40 41 42
    });

    testUsingContext('Error on non-existing file', () {
43
      final PrebuiltMacOSApp? macosApp = MacOSApp.fromPrebuiltApp(fileSystem.file('not_existing.app')) as PrebuiltMacOSApp?;
44

45
      expect(macosApp, isNull);
46
      expect(logger.errorText, contains('File "not_existing.app" does not exist.'));
47 48 49
    }, overrides: overrides);

    testUsingContext('Error on non-app-bundle folder', () {
50
      fileSystem.directory('regular_folder').createSync();
51
      final PrebuiltMacOSApp? macosApp = MacOSApp.fromPrebuiltApp(fileSystem.file('regular_folder')) as PrebuiltMacOSApp?;
52

53
      expect(macosApp, isNull);
54
      expect(logger.errorText, contains('Folder "regular_folder" is not an app bundle.'));
55 56 57
    }, overrides: overrides);

    testUsingContext('Error on no info.plist', () {
58
      fileSystem.directory('bundle.app').createSync();
59
      final PrebuiltMacOSApp? macosApp = MacOSApp.fromPrebuiltApp(fileSystem.file('bundle.app')) as PrebuiltMacOSApp?;
60

61
      expect(macosApp, isNull);
62
      expect(logger.errorText, contains('Invalid prebuilt macOS app. Does not contain Info.plist.'));
63 64 65
    }, overrides: overrides);

    testUsingContext('Error on info.plist missing bundle identifier', () {
66 67
      final String contentsDirectory = fileSystem.path.join('bundle.app', 'Contents');
      fileSystem.directory(contentsDirectory).createSync(recursive: true);
68
      fileSystem.file(fileSystem.path.join('bundle.app', 'Contents', 'Info.plist'))
69
        .writeAsStringSync(badPlistData);
70
      final PrebuiltMacOSApp? macosApp = MacOSApp.fromPrebuiltApp(fileSystem.file('bundle.app')) as PrebuiltMacOSApp?;
71

72
      expect(macosApp, isNull);
73
      expect(logger.errorText, contains('Invalid prebuilt macOS app. Info.plist does not contain bundle identifier'));
74 75 76
    }, overrides: overrides);

    testUsingContext('Error on info.plist missing executable', () {
77 78
      final String contentsDirectory = fileSystem.path.join('bundle.app', 'Contents');
      fileSystem.directory(contentsDirectory).createSync(recursive: true);
79
      fileSystem.file(fileSystem.path.join('bundle.app', 'Contents', 'Info.plist'))
80
        .writeAsStringSync(badPlistDataNoExecutable);
81
      final PrebuiltMacOSApp? macosApp = MacOSApp.fromPrebuiltApp(fileSystem.file('bundle.app')) as PrebuiltMacOSApp?;
82

83
      expect(macosApp, isNull);
84
      expect(logger.errorText, contains('Invalid prebuilt macOS app. Info.plist does not contain bundle executable'));
85 86 87
    }, overrides: overrides);

    testUsingContext('Success with app bundle', () {
88 89
      final String appDirectory = fileSystem.path.join('bundle.app', 'Contents', 'MacOS');
      fileSystem.directory(appDirectory).createSync(recursive: true);
90
      fileSystem.file(fileSystem.path.join('bundle.app', 'Contents', 'Info.plist'))
91
        .writeAsStringSync(plistData);
92
      fileSystem.file(fileSystem.path.join(appDirectory, executableName))
93
        .createSync();
94
      final PrebuiltMacOSApp macosApp = MacOSApp.fromPrebuiltApp(fileSystem.file('bundle.app'))! as PrebuiltMacOSApp;
95 96

      expect(logger.errorText, isEmpty);
97
      expect(macosApp.uncompressedBundle.path, 'bundle.app');
98 99 100 101 102
      expect(macosApp.id, 'fooBundleId');
      expect(macosApp.bundleName, 'bundle.app');
    }, overrides: overrides);

    testUsingContext('Bad zipped app, no payload dir', () {
103
      fileSystem.file('app.zip').createSync();
104
      final PrebuiltMacOSApp? macosApp = MacOSApp.fromPrebuiltApp(fileSystem.file('app.zip')) as PrebuiltMacOSApp?;
105

106
      expect(macosApp, isNull);
107
      expect(logger.errorText, contains('Archive "app.zip" does not contain a single app bundle.'));
108 109 110
    }, overrides: overrides);

    testUsingContext('Bad zipped app, two app bundles', () {
111 112
      fileSystem.file('app.zip').createSync();
      os.unzipOverride = (File zipFile, Directory targetDirectory) {
113 114 115
        if (zipFile.path != 'app.zip') {
          return;
        }
116 117 118 119 120
        final String bundlePath1 = fileSystem.path.join(targetDirectory.path, 'bundle1.app');
        final String bundlePath2 = fileSystem.path.join(targetDirectory.path, 'bundle2.app');
        fileSystem.directory(bundlePath1).createSync(recursive: true);
        fileSystem.directory(bundlePath2).createSync(recursive: true);
      };
121
      final PrebuiltMacOSApp? macosApp = MacOSApp.fromPrebuiltApp(fileSystem.file('app.zip')) as PrebuiltMacOSApp?;
122

123
      expect(macosApp, isNull);
124
      expect(logger.errorText, contains('Archive "app.zip" does not contain a single app bundle.'));
125 126 127
    }, overrides: overrides);

    testUsingContext('Success with zipped app', () {
128 129
      fileSystem.file('app.zip').createSync();
      os.unzipOverride = (File zipFile, Directory targetDirectory) {
130 131 132
        if (zipFile.path != 'app.zip') {
          return;
        }
133
        final Directory bundleAppContentsDir = fileSystem.directory(fileSystem.path.join(targetDirectory.path, 'bundle.app', 'Contents'));
134
        bundleAppContentsDir.createSync(recursive: true);
135
        fileSystem.file(fileSystem.path.join(bundleAppContentsDir.path, 'Info.plist'))
136
          .writeAsStringSync(plistData);
137
        fileSystem.directory(fileSystem.path.join(bundleAppContentsDir.path, 'MacOS'))
138
          .createSync();
139
        fileSystem.file(fileSystem.path
140 141 142
          .join(bundleAppContentsDir.path, 'MacOS', executableName))
          .createSync();
      };
143
      final PrebuiltMacOSApp macosApp = MacOSApp.fromPrebuiltApp(fileSystem.file('app.zip'))! as PrebuiltMacOSApp;
144 145

      expect(logger.errorText, isEmpty);
146
      expect(macosApp.uncompressedBundle.path, endsWith('bundle.app'));
147 148 149
      expect(macosApp.id, 'fooBundleId');
      expect(macosApp.bundleName, endsWith('bundle.app'));
    }, overrides: overrides);
150 151 152 153 154 155 156 157

    testUsingContext('Success with project', () {
      final MacOSApp macosApp = MacOSApp.fromMacOSProject(FlutterProject.fromDirectory(globals.fs.currentDirectory).macos);

      expect(logger.errorText, isEmpty);
      expect(macosApp.id, 'com.example.placeholder');
      expect(macosApp.name, 'macOS');
    }, overrides: overrides);
158 159 160
  });
}

161 162 163
class FakeOperatingSystemUtils extends Fake implements OperatingSystemUtils {
  FakeOperatingSystemUtils();

164
  void Function(File, Directory)? unzipOverride;
165

166 167 168 169 170
  @override
  void unzip(File file, Directory targetDirectory) {
    unzipOverride?.call(file, targetDirectory);
  }
}
171

172
class FakePlistUtils extends Fake implements PlistParser {
173 174
  FakePlistUtils(this.fileSystem);

175
  final FileSystem? fileSystem;
176

177
  @override
178 179
  Map<String, Object> parseFile(String plistFilePath) {
    final File file = fileSystem!.file(plistFilePath);
180
    if (!file.existsSync()) {
181
      return <String, Object>{};
182
    }
183
    return castStringKeyedMap(json.decode(file.readAsStringSync()))!.cast();
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
  }
}

// Contains no bundle identifier.
const String badPlistData = '''
{}
''';

// Contains no bundle executable.
const String badPlistDataNoExecutable = '''
{"CFBundleIdentifier": "fooBundleId"}
''';

const String executableName = 'foo';

const String plistData = '''
{"CFBundleIdentifier": "fooBundleId", "CFBundleExecutable": "$executableName"}
''';