application_package_test.dart 8.98 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/build_info.dart';
14
import 'package:flutter_tools/src/globals.dart' as globals;
15
import 'package:flutter_tools/src/ios/plist_parser.dart';
16
import 'package:flutter_tools/src/macos/application_package.dart';
17
import 'package:flutter_tools/src/project.dart';
18
import 'package:test/fake.dart';
19 20 21 22 23

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    testUsingContext('Bad zipped app, two app bundles', () {
112 113
      fileSystem.file('app.zip').createSync();
      os.unzipOverride = (File zipFile, Directory targetDirectory) {
114 115 116
        if (zipFile.path != 'app.zip') {
          return;
        }
117 118 119 120 121
        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);
      };
122
      final PrebuiltMacOSApp? macosApp = MacOSApp.fromPrebuiltApp(fileSystem.file('app.zip')) as PrebuiltMacOSApp?;
123

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

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

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

    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);
159 160 161 162 163 164 165 166 167 168 169

    testUsingContext('Chooses the correct directory for application.', () {
      final MacOSProject project = FlutterProject.fromDirectory(globals.fs.currentDirectory).macos;
      final BuildableMacOSApp macosApp = MacOSApp.fromMacOSProject(project) as BuildableMacOSApp;

      const BuildInfo vanillaApp = BuildInfo(BuildMode.debug, null, treeShakeIcons: false);
      String? applicationBundle = macosApp.bundleDirectory(vanillaApp);
      expect(applicationBundle, 'Debug');

      const BuildInfo flavoredApp = BuildInfo(BuildMode.release, 'flavor', treeShakeIcons: false);
      applicationBundle = macosApp.bundleDirectory(flavoredApp);
170
      expect(applicationBundle, 'Release-flavor');
171 172

    }, overrides: overrides);
173 174 175
  });
}

176 177 178
class FakeOperatingSystemUtils extends Fake implements OperatingSystemUtils {
  FakeOperatingSystemUtils();

179
  void Function(File, Directory)? unzipOverride;
180

181 182 183 184 185
  @override
  void unzip(File file, Directory targetDirectory) {
    unzipOverride?.call(file, targetDirectory);
  }
}
186

187
class FakePlistUtils extends Fake implements PlistParser {
188 189
  FakePlistUtils(this.fileSystem);

190
  final FileSystem? fileSystem;
191

192
  @override
193 194
  Map<String, Object> parseFile(String plistFilePath) {
    final File file = fileSystem!.file(plistFilePath);
195
    if (!file.existsSync()) {
196
      return <String, Object>{};
197
    }
198
    return castStringKeyedMap(json.decode(file.readAsStringSync()))!.cast();
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
  }
}

// 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"}
''';