ios_test.dart 28.8 KB
Newer Older
1 2 3 4
// 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.

5
import 'package:file/memory.dart';
6 7 8
import 'package:file_testing/file_testing.dart';
import 'package:flutter_tools/src/artifacts.dart';
import 'package:flutter_tools/src/base/file_system.dart';
9
import 'package:flutter_tools/src/base/logger.dart';
10
import 'package:flutter_tools/src/base/platform.dart';
11 12 13
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/targets/ios.dart';
14
import 'package:flutter_tools/src/convert.dart';
15
import 'package:flutter_tools/src/reporting/reporting.dart';
16 17

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

final Platform macPlatform = FakePlatform(operatingSystem: 'macos', environment: <String, String>{});
22 23 24

const List<String> _kSharedConfig = <String>[
  '-dynamiclib',
25
  '-miphoneos-version-min=11.0',
26 27 28 29 30 31 32 33 34 35 36
  '-Xlinker',
  '-rpath',
  '-Xlinker',
  '@executable_path/Frameworks',
  '-Xlinker',
  '-rpath',
  '-Xlinker',
  '@loader_path/Frameworks',
  '-install_name',
  '@rpath/App.framework/App',
  '-isysroot',
37
  'path/to/iPhoneOS.sdk',
38 39 40
];

void main() {
41 42 43 44 45
  late Environment environment;
  late FileSystem fileSystem;
  late FakeProcessManager processManager;
  late Artifacts artifacts;
  late BufferLogger logger;
46
  late TestUsage usage;
47 48

  setUp(() {
49
    fileSystem = MemoryFileSystem.test();
50
    processManager = FakeProcessManager.empty();
51 52
    logger = BufferLogger.test();
    artifacts = Artifacts.test();
53
    usage = TestUsage();
54 55 56 57 58 59 60 61 62 63 64
    environment = Environment.test(
      fileSystem.currentDirectory,
      defines: <String, String>{
        kTargetPlatform: 'ios',
      },
      inputs: <String, String>{},
      processManager: processManager,
      artifacts: artifacts,
      logger: logger,
      fileSystem: fileSystem,
      engineVersion: '2',
65
      usage: usage,
66
    );
67 68
  });

69
  testWithoutContext('iOS AOT targets has analyticsName', () {
70 71 72 73
    expect(const AotAssemblyRelease().analyticsName, 'ios_aot');
    expect(const AotAssemblyProfile().analyticsName, 'ios_aot');
  });

74
  testUsingContext('DebugUniversalFramework creates simulator binary', () async {
75 76
    environment.defines[kIosArchs] = 'x86_64';
    environment.defines[kSdkRoot] = 'path/to/iPhoneSimulator.sdk';
77 78
    final String appFrameworkPath = environment.buildDir.childDirectory('App.framework').childFile('App').path;
    processManager.addCommands(<FakeCommand>[
79 80 81 82 83 84 85 86 87 88
      FakeCommand(command: <String>[
        'xcrun',
        'clang',
        '-x',
        'c',
        '-arch',
        'x86_64',
        fileSystem.path.absolute(fileSystem.path.join(
            '.tmp_rand0', 'flutter_tools_stub_source.rand0', 'debug_app.cc')),
        '-dynamiclib',
89
        '-miphonesimulator-version-min=11.0',
90 91 92 93 94 95 96 97 98 99 100 101 102
        '-Xlinker',
        '-rpath',
        '-Xlinker',
        '@executable_path/Frameworks',
        '-Xlinker',
        '-rpath',
        '-Xlinker',
        '@loader_path/Frameworks',
        '-install_name',
        '@rpath/App.framework/App',
        '-isysroot',
        'path/to/iPhoneSimulator.sdk',
        '-o',
103
        appFrameworkPath,
104
      ]),
105 106 107 108 109 110 111 112 113
      FakeCommand(command: <String>[
        'codesign',
        '--force',
        '--sign',
        '-',
        '--timestamp=none',
        appFrameworkPath,
      ]),
    ]);
114 115

    await const DebugUniversalFramework().build(environment);
116
    expect(processManager, hasNoRemainingExpectations);
117 118 119 120 121 122
  }, overrides: <Type, Generator>{
    FileSystem: () => fileSystem,
    ProcessManager: () => processManager,
    Platform: () => macPlatform,
  });

123
  testUsingContext('DebugUniversalFramework creates expected binary with arm64 only arch', () async {
124
    environment.defines[kIosArchs] = 'arm64';
125
    environment.defines[kSdkRoot] = 'path/to/iPhoneOS.sdk';
126 127
    final String appFrameworkPath = environment.buildDir.childDirectory('App.framework').childFile('App').path;
    processManager.addCommands(<FakeCommand>[
128 129 130 131 132
      FakeCommand(command: <String>[
        'xcrun',
        'clang',
        '-x',
        'c',
133
        // iphone only gets 64 bit arch based on kIosArchs
134 135
        '-arch',
        'arm64',
136 137
        fileSystem.path.absolute(fileSystem.path.join(
            '.tmp_rand0', 'flutter_tools_stub_source.rand0', 'debug_app.cc')),
138 139
        ..._kSharedConfig,
        '-o',
140
        appFrameworkPath,
141
      ]),
142 143 144 145 146 147 148 149 150
      FakeCommand(command: <String>[
        'codesign',
        '--force',
        '--sign',
        '-',
        '--timestamp=none',
        appFrameworkPath,
      ]),
    ]);
151

152
    await const DebugUniversalFramework().build(environment);
153
    expect(processManager, hasNoRemainingExpectations);
154
  }, overrides: <Type, Generator>{
155
    FileSystem: () => fileSystem,
156
    ProcessManager: () => processManager,
157 158
    Platform: () => macPlatform,
  });
159

160
  testUsingContext('DebugIosApplicationBundle', () async {
161
    environment.defines[kBundleSkSLPath] = 'bundle.sksl';
162
    environment.defines[kBuildMode] = 'debug';
163
    environment.defines[kCodesignIdentity] = 'ABC123';
164
    // Precompiled dart data
165 166 167 168 169

    fileSystem.file(artifacts.getArtifactPath(Artifact.vmSnapshotData, mode: BuildMode.debug))
      .createSync();
    fileSystem.file(artifacts.getArtifactPath(Artifact.isolateSnapshotData, mode: BuildMode.debug))
      .createSync();
170
    // Project info
171 172
    fileSystem.file('pubspec.yaml').writeAsStringSync('name: hello');
    fileSystem.file('.packages').writeAsStringSync('\n');
173
    // Plist file
174
    fileSystem.file(fileSystem.path.join('ios', 'Flutter', 'AppFrameworkInfo.plist'))
175
      .createSync(recursive: true);
176 177 178
    // App kernel
    environment.buildDir.childFile('app.dill').createSync(recursive: true);
    // Stub framework
179
    environment.buildDir
180
        .childDirectory('App.framework')
181 182
      .childFile('App')
      .createSync(recursive: true);
183
    // sksl bundle
184
    fileSystem.file('bundle.sksl').writeAsStringSync(json.encode(
185 186 187 188 189
      <String, Object>{
        'engineRevision': '2',
        'platform': 'ios',
        'data': <String, Object>{
          'A': 'B',
190 191
        },
      },
192
    ));
193

194 195
    final Directory frameworkDirectory = environment.outputDir.childDirectory('App.framework');
    final File frameworkDirectoryBinary = frameworkDirectory.childFile('App');
196
    processManager.addCommand(
197 198 199 200 201 202 203 204
      FakeCommand(command: <String>[
        'codesign',
        '--force',
        '--sign',
        'ABC123',
        '--timestamp=none',
        frameworkDirectoryBinary.path,
      ]),
205
    );
206

207
    await const DebugIosApplicationBundle().build(environment);
208
    expect(processManager, hasNoRemainingExpectations);
209

210
    expect(frameworkDirectoryBinary, exists);
211 212 213 214 215 216 217
    expect(frameworkDirectory.childFile('Info.plist'), exists);

    final Directory assetDirectory = frameworkDirectory.childDirectory('flutter_assets');
    expect(assetDirectory.childFile('kernel_blob.bin'), exists);
    expect(assetDirectory.childFile('AssetManifest.json'), exists);
    expect(assetDirectory.childFile('vm_snapshot_data'), exists);
    expect(assetDirectory.childFile('isolate_snapshot_data'), exists);
218 219
    expect(assetDirectory.childFile('io.flutter.shaders.json'), exists);
    expect(assetDirectory.childFile('io.flutter.shaders.json').readAsStringSync(), '{"data":{"A":"B"}}');
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
  }, overrides: <Type, Generator>{
    FileSystem: () => fileSystem,
    ProcessManager: () => processManager,
    Platform: () => macPlatform,
  });

  testUsingContext('DebugIosApplicationBundle with impeller and shader compilation', () async {
    // Create impellerc to work around fallback detection logic.
    fileSystem.file(artifacts.getHostArtifact(HostArtifact.impellerc)).createSync(recursive: true);

    environment.defines[kBuildMode] = 'debug';
    environment.defines[kCodesignIdentity] = 'ABC123';
    // Precompiled dart data

    fileSystem.file(artifacts.getArtifactPath(Artifact.vmSnapshotData, mode: BuildMode.debug))
      .createSync();
    fileSystem.file(artifacts.getArtifactPath(Artifact.isolateSnapshotData, mode: BuildMode.debug))
      .createSync();
    // Project info
    fileSystem.file('pubspec.yaml').writeAsStringSync('name: hello\nflutter:\n  shaders:\n    - shader.glsl');
    fileSystem.file('.packages').writeAsStringSync('\n');
    // Plist file
    fileSystem.file(fileSystem.path.join('ios', 'Flutter', 'AppFrameworkInfo.plist'))
      .createSync(recursive: true);
    // Shader file
    fileSystem.file('shader.glsl').writeAsStringSync('test');
    // App kernel
    environment.buildDir.childFile('app.dill').createSync(recursive: true);
    // Stub framework
    environment.buildDir
        .childDirectory('App.framework')
      .childFile('App')
      .createSync(recursive: true);

    final Directory frameworkDirectory = environment.outputDir.childDirectory('App.framework');
    final File frameworkDirectoryBinary = frameworkDirectory.childFile('App');
    processManager.addCommands(<FakeCommand>[
      const FakeCommand(command: <String>[
        'HostArtifact.impellerc',
        '--runtime-stage-metal',
        '--iplr',
        '--sl=/App.framework/flutter_assets/shader.glsl',
        '--spirv=/App.framework/flutter_assets/shader.glsl.spirv',
        '--input=/shader.glsl',
        '--input-type=frag',
265
        '--include=/',
266
        '--include=/./shader_lib',
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
      ]),
      FakeCommand(command: <String>[
        'codesign',
        '--force',
        '--sign',
        'ABC123',
        '--timestamp=none',
        frameworkDirectoryBinary.path,
      ]),
    ]);

    await const DebugIosApplicationBundle().build(environment);
    expect(processManager, hasNoRemainingExpectations);

    expect(frameworkDirectoryBinary, exists);
    expect(frameworkDirectory.childFile('Info.plist'), exists);

    final Directory assetDirectory = frameworkDirectory.childDirectory('flutter_assets');
    expect(assetDirectory.childFile('kernel_blob.bin'), exists);
    expect(assetDirectory.childFile('AssetManifest.json'), exists);
    expect(assetDirectory.childFile('vm_snapshot_data'), exists);
    expect(assetDirectory.childFile('isolate_snapshot_data'), exists);
  }, overrides: <Type, Generator>{
    FileSystem: () => fileSystem,
    ProcessManager: () => processManager,
    Platform: () => macPlatform,
293
  });
294

295
  testUsingContext('ReleaseIosApplicationBundle build', () async {
296
    environment.defines[kBuildMode] = 'release';
297
    environment.defines[kCodesignIdentity] = 'ABC123';
298
    environment.defines[kXcodeAction] = 'build';
299 300

    // Project info
301 302
    fileSystem.file('pubspec.yaml').writeAsStringSync('name: hello');
    fileSystem.file('.packages').writeAsStringSync('\n');
303
    // Plist file
304
    fileSystem.file(fileSystem.path.join('ios', 'Flutter', 'AppFrameworkInfo.plist'))
305
      .createSync(recursive: true);
306 307 308 309 310 311 312

    // Real framework
    environment.buildDir
      .childDirectory('App.framework')
      .childFile('App')
      .createSync(recursive: true);

313 314 315 316 317 318 319 320
    // Input dSYM
    environment.buildDir
      .childDirectory('App.framework.dSYM')
      .childDirectory('Contents')
      .childDirectory('Resources')
      .childDirectory('DWARF')
      .childFile('App')
      .createSync(recursive: true);
321 322

    final Directory frameworkDirectory = environment.outputDir.childDirectory('App.framework');
323
    final File frameworkDirectoryBinary = frameworkDirectory.childFile('App');
324
    processManager.addCommand(
325 326 327 328 329 330 331
      FakeCommand(command: <String>[
        'codesign',
        '--force',
        '--sign',
        'ABC123',
        frameworkDirectoryBinary.path,
      ]),
332
    );
333 334

    await const ReleaseIosApplicationBundle().build(environment);
335
    expect(processManager, hasNoRemainingExpectations);
336 337

    expect(frameworkDirectoryBinary, exists);
338
    expect(frameworkDirectory.childFile('Info.plist'), exists);
339 340 341 342 343 344
    expect(environment.outputDir
      .childDirectory('App.framework.dSYM')
      .childDirectory('Contents')
      .childDirectory('Resources')
      .childDirectory('DWARF')
      .childFile('App'), exists);
345 346 347 348 349 350

    final Directory assetDirectory = frameworkDirectory.childDirectory('flutter_assets');
    expect(assetDirectory.childFile('kernel_blob.bin'), isNot(exists));
    expect(assetDirectory.childFile('AssetManifest.json'), exists);
    expect(assetDirectory.childFile('vm_snapshot_data'), isNot(exists));
    expect(assetDirectory.childFile('isolate_snapshot_data'), isNot(exists));
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 387 388 389 390 391 392 393 394 395 396 397
    expect(usage.events, isEmpty);
  }, overrides: <Type, Generator>{
    FileSystem: () => fileSystem,
    ProcessManager: () => processManager,
    Platform: () => macPlatform,
  });

  testUsingContext('ReleaseIosApplicationBundle sends archive success event', () async {
    environment.defines[kBuildMode] = 'release';
    environment.defines[kXcodeAction] = 'install';

    fileSystem.file(fileSystem.path.join('ios', 'Flutter', 'AppFrameworkInfo.plist'))
        .createSync(recursive: true);

    environment.buildDir
        .childDirectory('App.framework')
        .childFile('App')
        .createSync(recursive: true);

    final Directory frameworkDirectory = environment.outputDir.childDirectory('App.framework');
    final File frameworkDirectoryBinary = frameworkDirectory.childFile('App');
    processManager.addCommand(
      FakeCommand(command: <String>[
        'codesign',
        '--force',
        '--sign',
        '-',
        frameworkDirectoryBinary.path,
      ]),
    );

    await const ReleaseIosApplicationBundle().build(environment);
    expect(usage.events, contains(const TestUsageEvent('assemble', 'ios-archive', label: 'success')));
  }, overrides: <Type, Generator>{
    FileSystem: () => fileSystem,
    ProcessManager: () => processManager,
    Platform: () => macPlatform,
  });

  testUsingContext('ReleaseIosApplicationBundle sends archive fail event', () async {
    environment.defines[kBuildMode] = 'release';
    environment.defines[kXcodeAction] = 'install';

    // Throws because the project files are not set up.
    await expectLater(() => const ReleaseIosApplicationBundle().build(environment),
        throwsA(const TypeMatcher<FileSystemException>()));
    expect(usage.events, contains(const TestUsageEvent('assemble', 'ios-archive', label: 'fail')));
398 399 400 401 402
  }, overrides: <Type, Generator>{
    FileSystem: () => fileSystem,
    ProcessManager: () => processManager,
    Platform: () => macPlatform,
  });
403

404
  testUsingContext('AotAssemblyRelease throws exception if asked to build for simulator', () async {
405 406 407 408 409
    final FileSystem fileSystem = MemoryFileSystem.test();
    final Environment environment = Environment.test(
      fileSystem.currentDirectory,
      defines: <String, String>{
        kTargetPlatform: 'ios',
410 411 412
        kSdkRoot: 'path/to/iPhoneSimulator.sdk',
        kBuildMode: 'release',
        kIosArchs: 'x86_64',
413 414
      },
      processManager: processManager,
415 416
      artifacts: artifacts,
      logger: logger,
417 418 419
      fileSystem: fileSystem,
    );

420
    expect(const AotAssemblyRelease().build(environment), throwsA(isException
421 422 423 424 425 426
      .having(
        (Exception exception) => exception.toString(),
        'description',
        contains('release/profile builds are only supported for physical devices.'),
      )
    ));
427
    expect(processManager, hasNoRemainingExpectations);
428 429 430 431 432
  }, overrides: <Type, Generator>{
    FileSystem: () => fileSystem,
    ProcessManager: () => processManager,
    Platform: () => macPlatform,
  });
433

434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
  testUsingContext('AotAssemblyRelease throws exception if sdk root is missing', () async {
    final FileSystem fileSystem = MemoryFileSystem.test();
    final Environment environment = Environment.test(
      fileSystem.currentDirectory,
      defines: <String, String>{
        kTargetPlatform: 'ios',
      },
      processManager: processManager,
      artifacts: artifacts,
      logger: logger,
      fileSystem: fileSystem,
    );
    environment.defines[kBuildMode] = 'release';
    environment.defines[kIosArchs] = 'x86_64';

449 450
    expect(const AotAssemblyRelease().build(environment), throwsA(isException.having(
      (Exception exception) => exception.toString(),
451 452
      'description',
      contains('required define SdkRoot but it was not provided'),
453
    )));
454
    expect(processManager, hasNoRemainingExpectations);
455 456 457 458 459 460
  }, overrides: <Type, Generator>{
    FileSystem: () => fileSystem,
    ProcessManager: () => processManager,
    Platform: () => macPlatform,
  });

461
  group('copies Flutter.framework', () {
462 463 464 465 466 467
    late Directory outputDir;
    late File binary;
    late FakeCommand copyPhysicalFrameworkCommand;
    late FakeCommand lipoCommandNonFatResult;
    late FakeCommand lipoVerifyArm64Command;
    late FakeCommand adHocCodesignCommand;
468 469

    setUp(() {
470
      final FileSystem fileSystem = MemoryFileSystem.test();
471
      outputDir = fileSystem.directory('output');
472
      binary = outputDir.childDirectory('Flutter.framework').childFile('Flutter');
473 474 475 476 477 478 479 480 481
      copyPhysicalFrameworkCommand = FakeCommand(command: <String>[
        'rsync',
        '-av',
        '--delete',
        '--filter',
        '- .DS_Store/',
        'Artifact.flutterFramework.TargetPlatform.ios.debug.EnvironmentType.physical',
        outputDir.path,
      ]);
482 483 484 485 486 487 488 489 490 491 492 493 494 495

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

      lipoVerifyArm64Command = FakeCommand(command: <String>[
        'lipo',
        binary.path,
        '-verify_arch',
        'arm64',
      ]);

496 497 498 499 500 501 502 503
      adHocCodesignCommand = FakeCommand(command: <String>[
        'codesign',
        '--force',
        '--sign',
        '-',
        '--timestamp=none',
        binary.path,
      ]);
504 505 506
    });

    testWithoutContext('iphonesimulator', () async {
507 508 509 510 511 512 513 514
      final Environment environment = Environment.test(
        fileSystem.currentDirectory,
        processManager: processManager,
        artifacts: artifacts,
        logger: logger,
        fileSystem: fileSystem,
        outputDir: outputDir,
        defines: <String, String>{
515
          kIosArchs: 'x86_64',
516 517 518
          kSdkRoot: 'path/to/iPhoneSimulator.sdk',
        },
      );
519

520
      processManager.addCommands(<FakeCommand>[
521 522 523 524 525 526 527 528
        FakeCommand(command: <String>[
          'rsync',
          '-av',
          '--delete',
          '--filter',
          '- .DS_Store/',
          'Artifact.flutterFramework.TargetPlatform.ios.debug.EnvironmentType.simulator',
          outputDir.path,
529 530 531
          ],
          onRun: () => binary.createSync(recursive: true),
        ),
532
        lipoCommandNonFatResult,
533
        FakeCommand(command: <String>[
534 535 536 537
          'lipo',
          binary.path,
          '-verify_arch',
          'x86_64',
538
        ]),
539
        adHocCodesignCommand,
540
      ]);
541
      await const DebugUnpackIOS().build(environment);
542 543

      expect(logger.traceText, contains('Skipping lipo for non-fat file output/Flutter.framework/Flutter'));
544
      expect(processManager, hasNoRemainingExpectations);
545
    });
546

547
    testWithoutContext('fails when frameworks missing', () async {
548 549 550 551 552 553 554 555 556
      final Environment environment = Environment.test(
        fileSystem.currentDirectory,
        processManager: processManager,
        artifacts: artifacts,
        logger: logger,
        fileSystem: fileSystem,
        outputDir: outputDir,
        defines: <String, String>{
          kIosArchs: 'arm64',
557
          kSdkRoot: 'path/to/iPhoneOS.sdk',
558 559
        },
      );
560
      processManager.addCommand(copyPhysicalFrameworkCommand);
561
      await expectLater(
562
        const DebugUnpackIOS().build(environment),
563
        throwsA(isException.having(
564 565
          (Exception exception) => exception.toString(),
          'description',
566
          contains('Flutter.framework/Flutter does not exist, cannot thin'),
567 568 569
        )));
    });

570
    testWithoutContext('fails when requested archs missing from framework', () async {
571
      binary.createSync(recursive: true);
572 573 574 575 576 577 578 579 580 581

      final Environment environment = Environment.test(
        fileSystem.currentDirectory,
        processManager: processManager,
        artifacts: artifacts,
        logger: logger,
        fileSystem: fileSystem,
        outputDir: outputDir,
        defines: <String, String>{
          kIosArchs: 'arm64 armv7',
582
          kSdkRoot: 'path/to/iPhoneOS.sdk',
583 584 585
        },
      );

586 587
      processManager.addCommands(<FakeCommand>[
        copyPhysicalFrameworkCommand,
588 589 590
        FakeCommand(command: <String>[
          'lipo',
          '-info',
591
          binary.path,
592 593 594
        ], stdout: 'Architectures in the fat file:'),
        FakeCommand(command: <String>[
          'lipo',
595
          binary.path,
596 597 598 599
          '-verify_arch',
          'arm64',
          'armv7',
        ], exitCode: 1),
600
      ]);
601

602
      await expectLater(
603 604 605 606 607 608 609
        const DebugUnpackIOS().build(environment),
        throwsA(isException.having(
          (Exception exception) => exception.toString(),
          'description',
          contains('does not contain arm64 armv7. Running lipo -info:\nArchitectures in the fat file:'),
        )),
      );
610 611
    });

612
    testWithoutContext('fails when lipo extract fails', () async {
613
      binary.createSync(recursive: true);
614 615 616 617 618 619 620 621 622 623

      final Environment environment = Environment.test(
        fileSystem.currentDirectory,
        processManager: processManager,
        artifacts: artifacts,
        logger: logger,
        fileSystem: fileSystem,
        outputDir: outputDir,
        defines: <String, String>{
          kIosArchs: 'arm64 armv7',
624
          kSdkRoot: 'path/to/iPhoneOS.sdk',
625 626 627
        },
      );

628 629
      processManager.addCommands(<FakeCommand>[
        copyPhysicalFrameworkCommand,
630 631 632
        FakeCommand(command: <String>[
          'lipo',
          '-info',
633
          binary.path,
634 635 636
        ], stdout: 'Architectures in the fat file:'),
        FakeCommand(command: <String>[
          'lipo',
637
          binary.path,
638 639 640 641 642 643 644
          '-verify_arch',
          'arm64',
          'armv7',
        ]),
        FakeCommand(command: <String>[
          'lipo',
          '-output',
645
          binary.path,
646 647 648 649
          '-extract',
          'arm64',
          '-extract',
          'armv7',
650
          binary.path,
651 652
        ], exitCode: 1,
        stderr: 'lipo error'),
653
      ]);
654

655
      await expectLater(
656
        const DebugUnpackIOS().build(environment),
657 658
        throwsA(isException.having(
          (Exception exception) => exception.toString(),
659
          'description',
660
          contains('Failed to extract arm64 armv7 for output/Flutter.framework/Flutter.\nlipo error\nRunning lipo -info:\nArchitectures in the fat file:'),
661 662
        )),
      );
663 664
    });

665
    testWithoutContext('skips thin framework', () async {
666
      binary.createSync(recursive: true);
667 668 669 670 671 672 673 674 675 676

      final Environment environment = Environment.test(
        fileSystem.currentDirectory,
        processManager: processManager,
        artifacts: artifacts,
        logger: logger,
        fileSystem: fileSystem,
        outputDir: outputDir,
        defines: <String, String>{
          kIosArchs: 'arm64',
677
          kSdkRoot: 'path/to/iPhoneOS.sdk',
678 679 680
        },
      );

681 682 683 684
      processManager.addCommands(<FakeCommand>[
        copyPhysicalFrameworkCommand,
        lipoCommandNonFatResult,
        lipoVerifyArm64Command,
685
        adHocCodesignCommand,
686
      ]);
687
      await const DebugUnpackIOS().build(environment);
688

689
      expect(logger.traceText, contains('Skipping lipo for non-fat file output/Flutter.framework/Flutter'));
690

691
      expect(processManager, hasNoRemainingExpectations);
692 693
    });

694
    testWithoutContext('thins fat framework', () async {
695
      binary.createSync(recursive: true);
696 697 698 699 700 701 702 703 704 705

      final Environment environment = Environment.test(
        fileSystem.currentDirectory,
        processManager: processManager,
        artifacts: artifacts,
        logger: logger,
        fileSystem: fileSystem,
        outputDir: outputDir,
        defines: <String, String>{
          kIosArchs: 'arm64 armv7',
706
          kSdkRoot: 'path/to/iPhoneOS.sdk',
707 708 709
        },
      );

710 711
      processManager.addCommands(<FakeCommand>[
        copyPhysicalFrameworkCommand,
712 713 714
        FakeCommand(command: <String>[
          'lipo',
          '-info',
715
          binary.path,
716 717 718
        ], stdout: 'Architectures in the fat file:'),
        FakeCommand(command: <String>[
          'lipo',
719
          binary.path,
720 721 722 723 724 725 726
          '-verify_arch',
          'arm64',
          'armv7',
        ]),
        FakeCommand(command: <String>[
          'lipo',
          '-output',
727
          binary.path,
728 729 730 731
          '-extract',
          'arm64',
          '-extract',
          'armv7',
732
          binary.path,
733
        ]),
734
        adHocCodesignCommand,
735
      ]);
736

737
      await const DebugUnpackIOS().build(environment);
738
      expect(processManager, hasNoRemainingExpectations);
739
    });
740 741

    testWithoutContext('fails when bitcode strip fails', () async {
742
      binary.createSync(recursive: true);
743 744 745 746 747 748 749 750 751 752 753 754 755 756 757

      final Environment environment = Environment.test(
        fileSystem.currentDirectory,
        processManager: processManager,
        artifacts: artifacts,
        logger: logger,
        fileSystem: fileSystem,
        outputDir: outputDir,
        defines: <String, String>{
          kIosArchs: 'arm64',
          kSdkRoot: 'path/to/iPhoneOS.sdk',
        },
      );

      processManager.addCommands(<FakeCommand>[
758 759 760 761 762 763 764 765 766
        FakeCommand(command: <String>[
          'rsync',
          '-av',
          '--delete',
          '--filter',
          '- .DS_Store/',
          'Artifact.flutterFramework.TargetPlatform.ios.release.EnvironmentType.physical',
          outputDir.path,
        ]),
767 768
        lipoCommandNonFatResult,
        lipoVerifyArm64Command,
769 770 771 772
        FakeCommand(command: <String>[
          'xcrun',
          'bitcode_strip',
          binary.path,
773
          '-r',
774 775 776 777 778 779
          '-o',
          binary.path,
        ], exitCode: 1, stderr: 'bitcode_strip error'),
      ]);

      await expectLater(
780
        const ReleaseUnpackIOS().build(environment),
781 782 783 784 785 786
        throwsA(isException.having(
          (Exception exception) => exception.toString(),
          'description',
          contains('Failed to strip bitcode for output/Flutter.framework/Flutter.\nbitcode_strip error'),
        )),
      );
787

788
      expect(processManager, hasNoRemainingExpectations);
789 790 791
    });

    testWithoutContext('strips framework', () async {
792
      binary.createSync(recursive: true);
793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808

      final Environment environment = Environment.test(
        fileSystem.currentDirectory,
        processManager: processManager,
        artifacts: artifacts,
        logger: logger,
        fileSystem: fileSystem,
        outputDir: outputDir,
        defines: <String, String>{
          kIosArchs: 'arm64',
          kSdkRoot: 'path/to/iPhoneOS.sdk',
        },
      );

      processManager.addCommands(<FakeCommand>[
        copyPhysicalFrameworkCommand,
809 810
        lipoCommandNonFatResult,
        lipoVerifyArm64Command,
811
        adHocCodesignCommand,
812 813 814
      ]);
      await const DebugUnpackIOS().build(environment);

815
      expect(processManager, hasNoRemainingExpectations);
816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838
    });

    testWithoutContext('fails when codesign fails', () async {
      binary.createSync(recursive: true);

      final Environment environment = Environment.test(
        fileSystem.currentDirectory,
        processManager: processManager,
        artifacts: artifacts,
        logger: logger,
        fileSystem: fileSystem,
        outputDir: outputDir,
        defines: <String, String>{
          kIosArchs: 'arm64',
          kSdkRoot: 'path/to/iPhoneOS.sdk',
          kCodesignIdentity: 'ABC123',
        },
      );

      processManager.addCommands(<FakeCommand>[
        copyPhysicalFrameworkCommand,
        lipoCommandNonFatResult,
        lipoVerifyArm64Command,
839
        FakeCommand(command: <String>[
840 841 842 843 844 845 846 847 848 849 850
            'codesign',
            '--force',
            '--sign',
            'ABC123',
            '--timestamp=none',
            binary.path,
          ],
          exitCode: 1,
          stderr: 'codesign error',
          stdout: 'codesign info',
        ),
851 852 853
      ]);

      await expectLater(
854 855 856 857
        const DebugUnpackIOS().build(environment),
        throwsA(isException.having(
          (Exception exception) => exception.toString(),
          'description',
858
          contains('Failed to codesign output/Flutter.framework/Flutter with identity ABC123.\ncodesign info\ncodesign error'),
859 860
        )),
      );
861

862
      expect(processManager, hasNoRemainingExpectations);
863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885
    });

    testWithoutContext('codesigns framework', () async {
      binary.createSync(recursive: true);

      final Environment environment = Environment.test(
        fileSystem.currentDirectory,
        processManager: processManager,
        artifacts: artifacts,
        logger: logger,
        fileSystem: fileSystem,
        outputDir: outputDir,
        defines: <String, String>{
          kIosArchs: 'arm64',
          kSdkRoot: 'path/to/iPhoneOS.sdk',
          kCodesignIdentity: 'ABC123',
        },
      );

      processManager.addCommands(<FakeCommand>[
        copyPhysicalFrameworkCommand,
        lipoCommandNonFatResult,
        lipoVerifyArm64Command,
886
        FakeCommand(command: <String>[
887 888 889 890 891
          'codesign',
          '--force',
          '--sign',
          'ABC123',
          '--timestamp=none',
892 893 894 895 896
          binary.path,
        ]),
      ]);
      await const DebugUnpackIOS().build(environment);

897
      expect(processManager, hasNoRemainingExpectations);
898
    });
899
  });
900
}