dart_test.dart 20.2 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
import 'package:file/memory.dart';
import 'package:flutter_tools/src/artifacts.dart';
7
import 'package:flutter_tools/src/base/file_system.dart';
8
import 'package:flutter_tools/src/base/platform.dart';
9 10 11
import 'package:flutter_tools/src/build_info.dart';
import 'package:flutter_tools/src/build_system/build_system.dart';
import 'package:flutter_tools/src/build_system/exceptions.dart';
12
import 'package:flutter_tools/src/build_system/targets/common.dart';
13
import 'package:flutter_tools/src/build_system/targets/ios.dart';
14 15
import 'package:flutter_tools/src/cache.dart';
import 'package:flutter_tools/src/compile.dart';
16
import 'package:flutter_tools/src/globals.dart' as globals;
17 18
import 'package:process/process.dart';

19
import '../../../src/common.dart';
20
import '../../../src/fake_process_manager.dart';
21
import '../../../src/testbed.dart';
22

23 24 25 26
const String kBoundaryKey = '4d2d9609-c662-4571-afde-31410f96caa6';
const String kElfAot = '--snapshot_kind=app-aot-elf';
const String kAssemblyAot = '--snapshot_kind=app-aot-assembly';

27
void main() {
28
  Testbed testbed;
29
  FakeProcessManager processManager;
30 31
  Environment androidEnvironment;
  Environment iosEnvironment;
32
  Artifacts artifacts;
33 34 35 36

  setUpAll(() {
    Cache.disableLocking();
  });
37

38 39
  setUp(() {
    testbed = Testbed(setup: () {
40 41
      androidEnvironment = Environment.test(
        globals.fs.currentDirectory,
42 43 44
        defines: <String, String>{
          kBuildMode: getNameForBuildMode(BuildMode.profile),
          kTargetPlatform: getNameForTargetPlatform(TargetPlatform.android_arm),
45
        },
46 47 48 49
        artifacts: artifacts,
        processManager: processManager,
        fileSystem: globals.fs,
        logger: globals.logger,
50
      );
51
      androidEnvironment.buildDir.createSync(recursive: true);
52 53
      iosEnvironment = Environment.test(
        globals.fs.currentDirectory,
54 55 56
        defines: <String, String>{
          kBuildMode: getNameForBuildMode(BuildMode.profile),
          kTargetPlatform: getNameForTargetPlatform(TargetPlatform.ios),
57
        },
58 59 60 61
        artifacts: artifacts,
        processManager: processManager,
        fileSystem: globals.fs,
        logger: globals.logger,
62
      );
63 64 65 66 67 68
      iosEnvironment.buildDir.createSync(recursive: true);
      artifacts = CachedArtifacts(
        cache: globals.cache,
        platform: globals.platform,
        fileSystem: globals.fs,
      );
69
    }, overrides: <Type, Generator>{
70 71 72
      Platform: () => FakePlatform(operatingSystem: 'macos', environment: <String, String>{}),
      FileSystem: () => MemoryFileSystem.test(style: FileSystemStyle.posix),
      ProcessManager: () => processManager,
73
    });
74
  });
75

76 77 78 79
  test('KernelSnapshot throws error if missing build mode', () => testbed.run(() async {
    androidEnvironment.defines.remove(kBuildMode);
    expect(
      const KernelSnapshot().build(androidEnvironment),
80
      throwsA(isA<MissingDefineException>()));
81
  }));
82

83 84 85 86 87 88
  test('KernelSnapshot handles null result from kernel compilation', () => testbed.run(() async {
    globals.fs.file('.packages').writeAsStringSync('\n');
    final String build = androidEnvironment.buildDir.path;
    processManager = FakeProcessManager.list(<FakeCommand>[
      FakeCommand(command: <String>[
        artifacts.getArtifactPath(Artifact.engineDartBinary),
89
        '--disable-dart-dev',
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
        artifacts.getArtifactPath(Artifact.frontendServerSnapshotForEngineDartSdk),
        '--sdk-root',
        artifacts.getArtifactPath(Artifact.flutterPatchedSdkPath) + '/',
        '--target=flutter',
        '-Ddart.developer.causal_async_stacks=false',
        ...buildModeOptions(BuildMode.profile),
        '--aot',
        '--tfa',
        '--packages',
        '/.packages',
        '--output-dill',
        '$build/app.dill',
        '--depfile',
        '$build/kernel_snapshot.d',
        '/lib/main.dart',
      ], exitCode: 1),
    ]);

    await expectLater(() => const KernelSnapshot().build(androidEnvironment),
      throwsA(isA<Exception>()));
    expect(processManager.hasRemainingExpectations, false);
111
  }));
112

113 114 115 116 117 118
  test('KernelSnapshot does not use track widget creation on profile builds', () => testbed.run(() async {
    globals.fs.file('.packages').writeAsStringSync('\n');
    final String build = androidEnvironment.buildDir.path;
    processManager = FakeProcessManager.list(<FakeCommand>[
      FakeCommand(command: <String>[
        artifacts.getArtifactPath(Artifact.engineDartBinary),
119
        '--disable-dart-dev',
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
        artifacts.getArtifactPath(Artifact.frontendServerSnapshotForEngineDartSdk),
        '--sdk-root',
        artifacts.getArtifactPath(Artifact.flutterPatchedSdkPath) + '/',
        '--target=flutter',
        '-Ddart.developer.causal_async_stacks=false',
        ...buildModeOptions(BuildMode.profile),
        '--aot',
        '--tfa',
        '--packages',
        '/.packages',
        '--output-dill',
        '$build/app.dill',
        '--depfile',
        '$build/kernel_snapshot.d',
        '/lib/main.dart',
      ], stdout: 'result $kBoundaryKey\n$kBoundaryKey\n$kBoundaryKey $build/app.dill 0\n'),
    ]);
137

138
    await const KernelSnapshot().build(androidEnvironment);
139 140

    expect(processManager.hasRemainingExpectations, false);
141 142
  }));

143 144 145 146 147 148
  test('KernelSnapshot correctly handles an empty string in ExtraFrontEndOptions', () => testbed.run(() async {
    globals.fs.file('.packages').writeAsStringSync('\n');
    final String build = androidEnvironment.buildDir.path;
    processManager = FakeProcessManager.list(<FakeCommand>[
      FakeCommand(command: <String>[
        artifacts.getArtifactPath(Artifact.engineDartBinary),
149
        '--disable-dart-dev',
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
        artifacts.getArtifactPath(Artifact.frontendServerSnapshotForEngineDartSdk),
        '--sdk-root',
        artifacts.getArtifactPath(Artifact.flutterPatchedSdkPath) + '/',
        '--target=flutter',
        '-Ddart.developer.causal_async_stacks=false',
        ...buildModeOptions(BuildMode.profile),
        '--aot',
        '--tfa',
        '--packages',
        '/.packages',
        '--output-dill',
        '$build/app.dill',
        '--depfile',
        '$build/kernel_snapshot.d',
        '/lib/main.dart',
      ], stdout: 'result $kBoundaryKey\n$kBoundaryKey\n$kBoundaryKey $build/app.dill 0\n'),
    ]);

    await const KernelSnapshot()
      .build(androidEnvironment..defines[kExtraFrontEndOptions] = '');

    expect(processManager.hasRemainingExpectations, false);
  }));

  test('KernelSnapshot correctly forwards ExtraFrontEndOptions', () => testbed.run(() async {
    globals.fs.file('.packages').writeAsStringSync('\n');
    final String build = androidEnvironment.buildDir.path;
    processManager = FakeProcessManager.list(<FakeCommand>[
      FakeCommand(command: <String>[
        artifacts.getArtifactPath(Artifact.engineDartBinary),
180
        '--disable-dart-dev',
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
        artifacts.getArtifactPath(Artifact.frontendServerSnapshotForEngineDartSdk),
        '--sdk-root',
        artifacts.getArtifactPath(Artifact.flutterPatchedSdkPath) + '/',
        '--target=flutter',
        '-Ddart.developer.causal_async_stacks=false',
        ...buildModeOptions(BuildMode.profile),
        '--aot',
        '--tfa',
        '--packages',
        '/.packages',
        '--output-dill',
        '$build/app.dill',
        '--depfile',
        '$build/kernel_snapshot.d',
        'foo',
        'bar',
        '/lib/main.dart',
      ], stdout: 'result $kBoundaryKey\n$kBoundaryKey\n$kBoundaryKey $build/app.dill 0\n'),
    ]);

    await const KernelSnapshot()
      .build(androidEnvironment..defines[kExtraFrontEndOptions] = 'foo,bar');

    expect(processManager.hasRemainingExpectations, false);
  }));

207 208 209 210 211 212
  test('KernelSnapshot can disable track-widget-creation on debug builds', () => testbed.run(() async {
    globals.fs.file('.packages').writeAsStringSync('\n');
    final String build = androidEnvironment.buildDir.path;
    processManager = FakeProcessManager.list(<FakeCommand>[
      FakeCommand(command: <String>[
        artifacts.getArtifactPath(Artifact.engineDartBinary),
213
        '--disable-dart-dev',
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
        artifacts.getArtifactPath(Artifact.frontendServerSnapshotForEngineDartSdk),
        '--sdk-root',
        artifacts.getArtifactPath(Artifact.flutterPatchedSdkPath) + '/',
        '--target=flutter',
        '-Ddart.developer.causal_async_stacks=true',
        ...buildModeOptions(BuildMode.debug),
        '--no-link-platform',
        '--packages',
        '/.packages',
        '--output-dill',
        '$build/app.dill',
        '--depfile',
        '$build/kernel_snapshot.d',
        '/lib/main.dart',
      ], stdout: 'result $kBoundaryKey\n$kBoundaryKey\n$kBoundaryKey $build/app.dill 0\n'),
    ]);
230

231
    await const KernelSnapshot().build(androidEnvironment
232
      ..defines[kBuildMode] = getNameForBuildMode(BuildMode.debug)
233
      ..defines[kTrackWidgetCreation] = 'false');
234 235

    expect(processManager.hasRemainingExpectations, false);
236 237
  }));

238 239 240 241 242 243
  test('KernelSnapshot forces platform linking on debug for darwin target platforms', () => testbed.run(() async {
    globals.fs.file('.packages').writeAsStringSync('\n');
    final String build = androidEnvironment.buildDir.path;
    processManager = FakeProcessManager.list(<FakeCommand>[
      FakeCommand(command: <String>[
        artifacts.getArtifactPath(Artifact.engineDartBinary),
244
        '--disable-dart-dev',
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
        artifacts.getArtifactPath(Artifact.frontendServerSnapshotForEngineDartSdk),
        '--sdk-root',
        artifacts.getArtifactPath(Artifact.flutterPatchedSdkPath) + '/',
        '--target=flutter',
        '-Ddart.developer.causal_async_stacks=true',
        ...buildModeOptions(BuildMode.debug),
        '--packages',
        '/.packages',
        '--output-dill',
        '$build/app.dill',
        '--depfile',
        '$build/kernel_snapshot.d',
        '/lib/main.dart',
      ], stdout: 'result $kBoundaryKey\n$kBoundaryKey\n$kBoundaryKey $build/app.dill 0\n'),
    ]);
260 261

    await const KernelSnapshot().build(androidEnvironment
262
      ..defines[kTargetPlatform]  = getNameForTargetPlatform(TargetPlatform.darwin_x64)
263
      ..defines[kBuildMode] = getNameForBuildMode(BuildMode.debug)
264
      ..defines[kTrackWidgetCreation] = 'false'
265 266
    );

267 268
    expect(processManager.hasRemainingExpectations, false);
  }));
269

270 271 272
  test('KernelSnapshot does use track widget creation on debug builds', () => testbed.run(() async {
    globals.fs.file('.packages').writeAsStringSync('\n');
    final Environment testEnvironment = Environment.test(
273
      globals.fs.currentDirectory,
274
      defines: <String, String>{
275
        kBuildMode: getNameForBuildMode(BuildMode.debug),
276
        kTargetPlatform: getNameForTargetPlatform(TargetPlatform.android_arm),
277
      },
278 279 280 281
      processManager: processManager,
      artifacts: artifacts,
      fileSystem: globals.fs,
      logger: globals.logger,
282 283 284 285 286
    );
    final String build = testEnvironment.buildDir.path;
    processManager = FakeProcessManager.list(<FakeCommand>[
      FakeCommand(command: <String>[
        artifacts.getArtifactPath(Artifact.engineDartBinary),
287
        '--disable-dart-dev',
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
        artifacts.getArtifactPath(Artifact.frontendServerSnapshotForEngineDartSdk),
        '--sdk-root',
        artifacts.getArtifactPath(Artifact.flutterPatchedSdkPath) + '/',
        '--target=flutter',
        '-Ddart.developer.causal_async_stacks=true',
        ...buildModeOptions(BuildMode.debug),
        '--track-widget-creation',
        '--no-link-platform',
        '--packages',
        '/.packages',
        '--output-dill',
        '$build/app.dill',
        '--depfile',
        '$build/kernel_snapshot.d',
        '/lib/main.dart',
      ], stdout: 'result $kBoundaryKey\n$kBoundaryKey\n$kBoundaryKey /build/653e11a8e6908714056a57cd6b4f602a/app.dill 0\n'),
    ]);

    await const KernelSnapshot().build(testEnvironment);

    expect(processManager.hasRemainingExpectations, false);
309 310
  }));

311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
  test('AotElfProfile Produces correct output directory', () => testbed.run(() async {
    final String build = androidEnvironment.buildDir.path;
    processManager = FakeProcessManager.list(<FakeCommand>[
      FakeCommand(command: <String>[
        artifacts.getArtifactPath(Artifact.genSnapshot, mode: BuildMode.profile),
        '--deterministic',
        kElfAot,
        '--elf=$build/app.so',
        '--strip',
        '--no-sim-use-hardfp',
        '--no-use-integer-division',
        '--no-causal-async-stacks',
        '--lazy-async-stacks',
        '$build/app.dill',
      ])
    ]);
    androidEnvironment.buildDir.childFile('app.dill').createSync(recursive: true);

329
    await const AotElfProfile(TargetPlatform.android_arm).build(androidEnvironment);
330 331

    expect(processManager.hasRemainingExpectations, false);
332
  }));
333

334 335
  test('AotElfProfile throws error if missing build mode', () => testbed.run(() async {
    androidEnvironment.defines.remove(kBuildMode);
336

337
    expect(const AotElfProfile(TargetPlatform.android_arm).build(androidEnvironment),
338
      throwsA(isA<MissingDefineException>()));
339
  }));
340

341 342
  test('AotElfProfile throws error if missing target platform', () => testbed.run(() async {
    androidEnvironment.defines.remove(kTargetPlatform);
343

344
    expect(const AotElfProfile(TargetPlatform.android_arm).build(androidEnvironment),
345
      throwsA(isA<MissingDefineException>()));
346
  }));
347

348 349
  test('AotAssemblyProfile throws error if missing build mode', () => testbed.run(() async {
    iosEnvironment.defines.remove(kBuildMode);
350

351
    expect(const AotAssemblyProfile().build(iosEnvironment),
352
      throwsA(isA<MissingDefineException>()));
353
  }));
354

355 356
  test('AotAssemblyProfile throws error if missing target platform', () => testbed.run(() async {
    iosEnvironment.defines.remove(kTargetPlatform);
357

358
    expect(const AotAssemblyProfile().build(iosEnvironment),
359
      throwsA(isA<MissingDefineException>()));
360
  }));
361

362 363
  test('AotAssemblyProfile throws error if built for non-iOS platform', () => testbed.run(() async {
    expect(const AotAssemblyProfile().build(androidEnvironment),
364
      throwsA(isA<Exception>()));
365
  }));
366

367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 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 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
  test('AotAssemblyProfile generates multiple arches and lipos together', () => testbed.run(() async {
    final String build = iosEnvironment.buildDir.path;
    processManager = FakeProcessManager.list(<FakeCommand>[
      FakeCommand(command: <String>[
        // This path is not known by the cache due to the iOS gen_snapshot split.
        'bin/cache/artifacts/engine/ios-profile/gen_snapshot_armv7',
        '--deterministic',
        kAssemblyAot,
        '--assembly=$build/armv7/snapshot_assembly.S',
        '--strip',
        '--no-sim-use-hardfp',
        '--no-use-integer-division',
        '--no-causal-async-stacks',
        '--lazy-async-stacks',
        '$build/app.dill',
      ]),
      FakeCommand(command: <String>[
        // This path is not known by the cache due to the iOS gen_snapshot split.
        'bin/cache/artifacts/engine/ios-profile/gen_snapshot_arm64',
        '--deterministic',
        kAssemblyAot,
        '--assembly=$build/arm64/snapshot_assembly.S',
        '--strip',
        '--no-causal-async-stacks',
        '--lazy-async-stacks',
        '$build/app.dill',
      ]),
      const FakeCommand(command: <String>[
        'xcrun',
        '--sdk',
        'iphoneos',
        '--show-sdk-path',
      ]),
      const FakeCommand(command: <String>[
        'xcrun',
        '--sdk',
        'iphoneos',
        '--show-sdk-path',
      ]),
      FakeCommand(command: <String>[
        'xcrun',
        'cc',
        '-arch',
        'armv7',
        '-isysroot',
        '',
        '-c',
        '$build/armv7/snapshot_assembly.S',
        '-o',
        '$build/armv7/snapshot_assembly.o',
      ]),
      FakeCommand(command: <String>[
        'xcrun',
        'cc',
        '-arch',
        'arm64',
        '-isysroot',
        '',
        '-c',
        '$build/arm64/snapshot_assembly.S',
        '-o',
        '$build/arm64/snapshot_assembly.o',
      ]),
      FakeCommand(command: <String>[
        'xcrun',
        'clang',
        '-arch',
        'armv7',
        '-miphoneos-version-min=8.0',
        '-dynamiclib',
        '-Xlinker',
        '-rpath',
        '-Xlinker',
        '@executable_path/Frameworks',
        '-Xlinker',
        '-rpath',
        '-Xlinker',
        '@loader_path/Frameworks',
        '-install_name',
        '@rpath/App.framework/App',
        '-isysroot',
        '',
        '-o',
        '$build/armv7/App.framework/App',
        '$build/armv7/snapshot_assembly.o',
      ]),
      FakeCommand(command: <String>[
        'xcrun',
        'clang',
        '-arch',
        'arm64',
        '-miphoneos-version-min=8.0',
        '-dynamiclib',
        '-Xlinker',
        '-rpath',
        '-Xlinker',
        '@executable_path/Frameworks',
        '-Xlinker',
        '-rpath',
        '-Xlinker',
        '@loader_path/Frameworks',
        '-install_name',
        '@rpath/App.framework/App',
        '-isysroot',
        '',
        '-o',
        '$build/arm64/App.framework/App',
        '$build/arm64/snapshot_assembly.o',
      ]),
      FakeCommand(command: <String>[
        'lipo',
        '$build/armv7/App.framework/App',
        '$build/arm64/App.framework/App',
        '-create',
        '-output',
        '$build/App.framework/App',
      ]),
    ]);
485
    iosEnvironment.defines[kIosArchs] ='armv7 arm64';
486

487
    await const AotAssemblyProfile().build(iosEnvironment);
488

489
    expect(processManager.hasRemainingExpectations, false);
490 491
  }));

492 493
  test('AotAssemblyProfile with bitcode sends correct argument to snapshotter (one arch)', () => testbed.run(() async {
    iosEnvironment.defines[kIosArchs] = 'arm64';
494
    iosEnvironment.defines[kBitcodeFlag] = 'true';
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560
    final String build = iosEnvironment.buildDir.path;
    processManager = FakeProcessManager.list(<FakeCommand>[
      FakeCommand(command: <String>[
        // This path is not known by the cache due to the iOS gen_snapshot split.
        'bin/cache/artifacts/engine/ios-profile/gen_snapshot_arm64',
        '--deterministic',
        kAssemblyAot,
        '--assembly=$build/arm64/snapshot_assembly.S',
        '--strip',
        '--no-causal-async-stacks',
        '--lazy-async-stacks',
        '$build/app.dill',
      ]),
      const FakeCommand(command: <String>[
        'xcrun',
        '--sdk',
        'iphoneos',
        '--show-sdk-path',
      ]),
      FakeCommand(command: <String>[
        'xcrun',
        'cc',
        '-arch',
        'arm64',
        '-isysroot',
        '',
        // Contains bitcode flag.
        '-fembed-bitcode',
        '-c',
        '$build/arm64/snapshot_assembly.S',
        '-o',
        '$build/arm64/snapshot_assembly.o',
      ]),
      FakeCommand(command: <String>[
        'xcrun',
        'clang',
        '-arch',
        'arm64',
        '-miphoneos-version-min=8.0',
        '-dynamiclib',
        '-Xlinker',
        '-rpath',
        '-Xlinker',
        '@executable_path/Frameworks',
        '-Xlinker',
        '-rpath',
        '-Xlinker',
        '@loader_path/Frameworks',
        '-install_name',
        '@rpath/App.framework/App',
        // Contains bitcode flag.
        '-fembed-bitcode',
        '-isysroot',
        '',
        '-o',
        '$build/arm64/App.framework/App',
        '$build/arm64/snapshot_assembly.o',
      ]),
      FakeCommand(command: <String>[
        'lipo',
        '$build/arm64/App.framework/App',
        '-create',
        '-output',
        '$build/App.framework/App',
      ]),
    ]);
561

562
    await const AotAssemblyProfile().build(iosEnvironment);
563

564
    expect(processManager.hasRemainingExpectations, false);
565
  }));
566

567 568
  test('kExtraGenSnapshotOptions passes values to gen_snapshot', () => testbed.run(() async {
    androidEnvironment.defines[kExtraGenSnapshotOptions] = 'foo,bar,baz=2';
569 570
    androidEnvironment.defines[kBuildMode] = getNameForBuildMode(BuildMode.profile);
    final String build = androidEnvironment.buildDir.path;
571

572 573 574 575
    processManager = FakeProcessManager.list(<FakeCommand>[
      FakeCommand(command: <String>[
        artifacts.getArtifactPath(Artifact.genSnapshot, mode: BuildMode.profile),
        '--deterministic',
576 577 578
        'foo',
        'bar',
        'baz=2',
579 580 581 582 583 584 585 586 587 588
        kElfAot,
        '--elf=$build/app.so',
        '--strip',
        '--no-sim-use-hardfp',
        '--no-use-integer-division',
        '--no-causal-async-stacks',
        '--lazy-async-stacks',
        '$build/app.dill',
      ]),
    ]);
589

590
    await const AotElfRelease(TargetPlatform.android_arm).build(androidEnvironment);
591

592 593
    expect(processManager.hasRemainingExpectations, false);
  }));
594
}