macos_test.dart 15 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
// @dart = 2.8

7
import 'package:file/memory.dart';
8
import 'package:file_testing/file_testing.dart';
9
import 'package:flutter_tools/src/artifacts.dart';
10
import 'package:flutter_tools/src/base/file_system.dart';
11 12
import 'package:flutter_tools/src/base/logger.dart';
import 'package:flutter_tools/src/build_info.dart';
13 14
import 'package:flutter_tools/src/build_system/build_system.dart';
import 'package:flutter_tools/src/build_system/targets/macos.dart';
15
import 'package:flutter_tools/src/convert.dart';
16

17
import '../../../src/common.dart';
18
import '../../../src/context.dart';
19
import '../../../src/fake_process_manager.dart';
20

21
void main() {
22
  Environment environment;
23 24 25
  FileSystem fileSystem;
  Artifacts artifacts;
  FakeProcessManager processManager;
26 27 28 29 30 31
  File binary;
  BufferLogger logger;
  FakeCommand copyFrameworkCommand;
  FakeCommand lipoInfoNonFatCommand;
  FakeCommand lipoInfoFatCommand;
  FakeCommand lipoVerifyX86_64Command;
32

33
  setUp(() {
34
    processManager = FakeProcessManager.empty();
35 36
    artifacts = Artifacts.test();
    fileSystem = MemoryFileSystem.test();
37
    logger = BufferLogger.test();
38 39 40 41
    environment = Environment.test(
      fileSystem.currentDirectory,
      defines: <String, String>{
        kBuildMode: 'debug',
42 43
        kTargetPlatform: 'darwin',
        kDarwinArchs: 'x86_64',
44 45 46 47
      },
      inputs: <String, String>{},
      artifacts: artifacts,
      processManager: processManager,
48
      logger: logger,
49 50 51
      fileSystem: fileSystem,
      engineVersion: '2'
    );
52 53

    binary = environment.outputDir
54 55 56 57
      .childDirectory('FlutterMacOS.framework')
      .childDirectory('Versions')
      .childDirectory('A')
      .childFile('FlutterMacOS');
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88

    copyFrameworkCommand = FakeCommand(
      command: <String>[
        'rsync',
        '-av',
        '--delete',
        '--filter',
        '- .DS_Store/',
        'Artifact.flutterMacOSFramework.debug',
        environment.outputDir.path,
      ],
    );

    lipoInfoNonFatCommand = FakeCommand(command: <String>[
      'lipo',
      '-info',
      binary.path,
    ], stdout: 'Non-fat file:');

    lipoInfoFatCommand = FakeCommand(command: <String>[
      'lipo',
      '-info',
      binary.path,
    ], stdout: 'Architectures in the fat file:');

    lipoVerifyX86_64Command = FakeCommand(command: <String>[
      'lipo',
      binary.path,
      '-verify_arch',
      'x86_64',
    ]);
89
  });
90

91
  testUsingContext('Copies files to correct cache directory', () async {
92 93 94 95 96
    binary.createSync(recursive: true);
    processManager.addCommands(<FakeCommand>[
      copyFrameworkCommand,
      lipoInfoNonFatCommand,
      lipoVerifyX86_64Command,
97 98
    ]);

99
    await const DebugUnpackMacOS().build(environment);
100

101
    expect(processManager, hasNoRemainingExpectations);
102 103 104 105
  }, overrides: <Type, Generator>{
    FileSystem: () => fileSystem,
    ProcessManager: () => processManager,
  });
106

107 108 109
  testUsingContext('thinning fails when framework missing', () async {
    processManager.addCommand(copyFrameworkCommand);
    await expectLater(
110 111 112 113
      const DebugUnpackMacOS().build(environment),
      throwsA(isException.having(
        (Exception exception) => exception.toString(),
        'description',
114
        contains('FlutterMacOS.framework/Versions/A/FlutterMacOS does not exist, cannot thin'),
115 116
      )),
    );
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
  }, overrides: <Type, Generator>{
    FileSystem: () => fileSystem,
    ProcessManager: () => processManager,
  });

  testUsingContext('lipo fails when arch missing from framework', () async {
    environment.defines[kDarwinArchs] = 'arm64 x86_64';
    binary.createSync(recursive: true);
    processManager.addCommands(<FakeCommand>[
      copyFrameworkCommand,
      lipoInfoFatCommand,
      FakeCommand(command: <String>[
        'lipo',
        binary.path,
        '-verify_arch',
        'arm64',
        'x86_64',
      ], exitCode: 1),
    ]);

    await expectLater(
138 139 140 141 142 143 144
      const DebugUnpackMacOS().build(environment),
      throwsA(isException.having(
        (Exception exception) => exception.toString(),
        'description',
        contains('does not contain arm64 x86_64. Running lipo -info:\nArchitectures in the fat file:'),
      )),
    );
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
  }, overrides: <Type, Generator>{
    FileSystem: () => fileSystem,
    ProcessManager: () => processManager,
  });

  testUsingContext('skips thins framework', () async {
    binary.createSync(recursive: true);
    processManager.addCommands(<FakeCommand>[
      copyFrameworkCommand,
      lipoInfoNonFatCommand,
      lipoVerifyX86_64Command,
    ]);

    await const DebugUnpackMacOS().build(environment);

160
    expect(logger.traceText, contains('Skipping lipo for non-fat file /FlutterMacOS.framework/Versions/A/FlutterMacOS'));
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
  });

  testUsingContext('thins fat framework', () async {
    binary.createSync(recursive: true);
    processManager.addCommands(<FakeCommand>[
      copyFrameworkCommand,
      lipoInfoFatCommand,
      lipoVerifyX86_64Command,
      FakeCommand(command: <String>[
          'lipo',
          '-output',
          binary.path,
          '-extract',
          'x86_64',
          binary.path,
      ]),
    ]);

    await const DebugUnpackMacOS().build(environment);

    expect(processManager, hasNoRemainingExpectations);
  });

184 185 186 187 188 189 190 191 192
  testUsingContext('debug macOS application fails if App.framework missing', () async {
    fileSystem.directory(
      artifacts.getArtifactPath(
        Artifact.flutterMacOSFramework,
        mode: BuildMode.debug,
      ))
      .createSync();
    final String inputKernel = fileSystem.path.join(environment.buildDir.path, 'app.dill');
    fileSystem.file(inputKernel)
193 194 195
      ..createSync(recursive: true)
      ..writeAsStringSync('testing');

196
    expect(() async => const DebugMacOSBundleFlutterAssets().build(environment),
Dan Field's avatar
Dan Field committed
197
        throwsException);
198 199 200 201
  }, overrides: <Type, Generator>{
    FileSystem: () => fileSystem,
    ProcessManager: () => processManager,
  });
202

203 204 205 206 207 208 209
  testUsingContext('debug macOS application creates correctly structured framework', () async {
    fileSystem.directory(
      artifacts.getArtifactPath(
        Artifact.flutterMacOSFramework,
        mode: BuildMode.debug,
      ))
      .createSync();
210
    environment.defines[kBundleSkSLPath] = 'bundle.sksl';
211 212 213
    fileSystem.file(
      artifacts.getArtifactPath(
        Artifact.vmSnapshotData,
214
        platform: TargetPlatform.darwin,
215 216 217 218 219
        mode: BuildMode.debug,
      )).createSync(recursive: true);
    fileSystem.file(
      artifacts.getArtifactPath(
        Artifact.isolateSnapshotData,
220
        platform: TargetPlatform.darwin,
221 222 223
        mode: BuildMode.debug,
      )).createSync(recursive: true);
    fileSystem.file('${environment.buildDir.path}/App.framework/App')
224 225
      .createSync(recursive: true);
    // sksl bundle
226
    fileSystem.file('bundle.sksl').writeAsStringSync(json.encode(
227 228 229 230 231 232 233 234
      <String, Object>{
        'engineRevision': '2',
        'platform': 'ios',
        'data': <String, Object>{
          'A': 'B',
        }
      }
    ));
235

236
    final String inputKernel = '${environment.buildDir.path}/app.dill';
237
    fileSystem.file(inputKernel)
238 239 240
      ..createSync(recursive: true)
      ..writeAsStringSync('testing');

241
    await const DebugMacOSBundleFlutterAssets().build(environment);
242

243
    expect(fileSystem.file(
244 245 246
      'App.framework/Versions/A/Resources/flutter_assets/kernel_blob.bin').readAsStringSync(),
      'testing',
    );
247
    expect(fileSystem.file(
248 249 250
      'App.framework/Versions/A/Resources/Info.plist').readAsStringSync(),
      contains('io.flutter.flutter.app'),
    );
251
    expect(fileSystem.file(
252 253 254
      'App.framework/Versions/A/Resources/flutter_assets/vm_snapshot_data'),
      exists,
    );
255
    expect(fileSystem.file(
256 257 258 259
      'App.framework/Versions/A/Resources/flutter_assets/isolate_snapshot_data'),
      exists,
    );

260
    final File skslFile = fileSystem.file('App.framework/Versions/A/Resources/flutter_assets/io.flutter.shaders.json');
261 262 263

    expect(skslFile, exists);
    expect(skslFile.readAsStringSync(), '{"data":{"A":"B"}}');
264 265 266 267
  }, overrides: <Type, Generator>{
    FileSystem: () => fileSystem,
    ProcessManager: () => processManager,
  });
268

269 270
  testUsingContext('release/profile macOS application has no blob or precompiled runtime', () async {
    fileSystem.file('bin/cache/artifacts/engine/darwin-x64/vm_isolate_snapshot.bin')
271
      .createSync(recursive: true);
272
    fileSystem.file('bin/cache/artifacts/engine/darwin-x64/isolate_snapshot.bin')
273
      .createSync(recursive: true);
274
    fileSystem.file('${environment.buildDir.path}/App.framework/App')
275 276
      .createSync(recursive: true);

277
    await const ProfileMacOSBundleFlutterAssets().build(environment..defines[kBuildMode] = 'profile');
278

279
    expect(fileSystem.file(
280 281 282
      'App.framework/Versions/A/Resources/flutter_assets/kernel_blob.bin'),
      isNot(exists),
    );
283
    expect(fileSystem.file(
284 285 286
      'App.framework/Versions/A/Resources/flutter_assets/vm_snapshot_data'),
      isNot(exists),
    );
287
    expect(fileSystem.file(
288 289 290
      'App.framework/Versions/A/Resources/flutter_assets/isolate_snapshot_data'),
      isNot(exists),
    );
291 292 293 294
  }, overrides: <Type, Generator>{
    FileSystem: () => fileSystem,
    ProcessManager: () => processManager,
  });
295

296 297
  testUsingContext('release/profile macOS application updates when App.framework updates', () async {
    fileSystem.file('bin/cache/artifacts/engine/darwin-x64/vm_isolate_snapshot.bin')
298
      .createSync(recursive: true);
299
    fileSystem.file('bin/cache/artifacts/engine/darwin-x64/isolate_snapshot.bin')
300
      .createSync(recursive: true);
301
    final File inputFramework = fileSystem.file(fileSystem.path.join(environment.buildDir.path, 'App.framework', 'App'))
302 303
      ..createSync(recursive: true)
      ..writeAsStringSync('ABC');
304 305

    await const ProfileMacOSBundleFlutterAssets().build(environment..defines[kBuildMode] = 'profile');
306
    final File outputFramework = fileSystem.file(fileSystem.path.join(environment.outputDir.path, 'App.framework', 'App'));
307 308 309 310 311 312 313

    expect(outputFramework.readAsStringSync(), 'ABC');

    inputFramework.writeAsStringSync('DEF');
    await const ProfileMacOSBundleFlutterAssets().build(environment..defines[kBuildMode] = 'profile');

    expect(outputFramework.readAsStringSync(), 'DEF');
314 315 316 317
  }, overrides: <Type, Generator>{
    FileSystem: () => fileSystem,
    ProcessManager: () => processManager,
  });
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386

  testUsingContext('DebugMacOSFramework creates expected binary with arm64 only arch', () async {
    environment.defines[kDarwinArchs] = 'arm64';
    processManager.addCommand(
      FakeCommand(command: <String>[
        'xcrun',
        'clang',
        '-x',
        'c',
        environment.buildDir.childFile('debug_app.cc').path,
        '-arch',
        'arm64',
        '-dynamiclib',
        '-Xlinker', '-rpath', '-Xlinker', '@executable_path/Frameworks',
        '-Xlinker', '-rpath', '-Xlinker', '@loader_path/Frameworks',
        '-install_name', '@rpath/App.framework/App',
        '-o',
        environment.buildDir
            .childDirectory('App.framework')
            .childFile('App')
            .path,
      ]),
    );

    await const DebugMacOSFramework().build(environment);
    expect(processManager.hasRemainingExpectations, isFalse);
  }, overrides: <Type, Generator>{
    FileSystem: () => fileSystem,
    ProcessManager: () => processManager,
  });

  testUsingContext('DebugMacOSFramework creates universal binary', () async {
    environment.defines[kDarwinArchs] = 'arm64 x86_64';
    processManager.addCommand(
      FakeCommand(command: <String>[
        'xcrun',
        'clang',
        '-x',
        'c',
        environment.buildDir.childFile('debug_app.cc').path,
        '-arch',
        'arm64',
        '-arch',
        'x86_64',
        '-dynamiclib',
        '-Xlinker', '-rpath', '-Xlinker', '@executable_path/Frameworks',
        '-Xlinker', '-rpath', '-Xlinker', '@loader_path/Frameworks',
        '-install_name', '@rpath/App.framework/App',
        '-o',
        environment.buildDir
            .childDirectory('App.framework')
            .childFile('App')
            .path,
      ]),
    );

    await const DebugMacOSFramework().build(environment);
    expect(processManager.hasRemainingExpectations, isFalse);
  }, overrides: <Type, Generator>{
    FileSystem: () => fileSystem,
    ProcessManager: () => processManager,
  });

  testUsingContext('CompileMacOSFramework creates universal binary', () async {
    environment.defines[kDarwinArchs] = 'arm64 x86_64';
    environment.defines[kBuildMode] = 'release';

    processManager.addCommands(<FakeCommand>[
      FakeCommand(command: <String>[
387
        'Artifact.genSnapshot.TargetPlatform.darwin.release_arm64',
388 389 390 391 392 393 394
        '--deterministic',
        '--snapshot_kind=app-aot-assembly',
        '--assembly=${environment.buildDir.childFile('arm64/snapshot_assembly.S').path}',
        '--strip',
        environment.buildDir.childFile('app.dill').path
      ]),
      FakeCommand(command: <String>[
395
        'Artifact.genSnapshot.TargetPlatform.darwin.release_x64',
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
        '--deterministic',
        '--snapshot_kind=app-aot-assembly',
        '--assembly=${environment.buildDir.childFile('x86_64/snapshot_assembly.S').path}',
        '--strip',
        environment.buildDir.childFile('app.dill').path
      ]),
      FakeCommand(command: <String>[
        'xcrun', 'cc',  '-arch', 'arm64',
        '-c', environment.buildDir.childFile('arm64/snapshot_assembly.S').path,
        '-o', environment.buildDir.childFile('arm64/snapshot_assembly.o').path
      ]),
      FakeCommand(command: <String>[
        'xcrun', 'cc',  '-arch', 'x86_64',
        '-c', environment.buildDir.childFile('x86_64/snapshot_assembly.S').path,
        '-o', environment.buildDir.childFile('x86_64/snapshot_assembly.o').path
      ]),
      FakeCommand(command: <String>[
        'xcrun', 'clang', '-arch', 'arm64', '-dynamiclib', '-Xlinker', '-rpath',
        '-Xlinker', '@executable_path/Frameworks', '-Xlinker', '-rpath',
        '-Xlinker', '@loader_path/Frameworks',
        '-install_name', '@rpath/App.framework/App',
        '-o', environment.buildDir.childFile('arm64/App.framework/App').path,
        environment.buildDir.childFile('arm64/snapshot_assembly.o').path
      ]),
      FakeCommand(command: <String>[
        'xcrun', 'clang', '-arch', 'x86_64', '-dynamiclib', '-Xlinker', '-rpath',
        '-Xlinker', '@executable_path/Frameworks', '-Xlinker', '-rpath',
        '-Xlinker', '@loader_path/Frameworks',
        '-install_name', '@rpath/App.framework/App',
        '-o', environment.buildDir.childFile('x86_64/App.framework/App').path,
        environment.buildDir.childFile('x86_64/snapshot_assembly.o').path
      ]),
      FakeCommand(command: <String>[
        'lipo',
        environment.buildDir.childFile('arm64/App.framework/App').path,
        environment.buildDir.childFile('x86_64/App.framework/App').path,
        '-create',
        '-output',
        environment.buildDir.childFile('App.framework/App').path,
      ]),
    ]);

    await const CompileMacOSFramework().build(environment);
    expect(processManager.hasRemainingExpectations, isFalse);

  }, overrides: <Type, Generator>{
    FileSystem: () => fileSystem,
    ProcessManager: () => processManager,
  });
445
}