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

5
import 'package:file/memory.dart';
6
import 'package:flutter_tools/src/artifacts.dart';
7 8
import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/base/io.dart';
9
import 'package:flutter_tools/src/base/logger.dart';
10
import 'package:flutter_tools/src/base/platform.dart';
11
import 'package:flutter_tools/src/base/version.dart';
12
import 'package:flutter_tools/src/build_info.dart';
13
import 'package:flutter_tools/src/ios/xcode_build_settings.dart';
14
import 'package:flutter_tools/src/ios/xcodeproj.dart';
15
import 'package:flutter_tools/src/project.dart';
16
import 'package:flutter_tools/src/reporting/reporting.dart';
17

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

const String xcodebuild = '/usr/bin/xcodebuild';
23 24

void main() {
25 26
  group('MockProcessManager', () {
    setUp(() {
27
      final FileSystem fileSystem = MemoryFileSystem.test();
28 29 30 31
      fileSystem.file(xcodebuild).createSync(recursive: true);
    });
  });

32 33 34 35 36 37 38
  const FakeCommand kWhichSysctlCommand = FakeCommand(
    command: <String>[
      'which',
      'sysctl',
    ],
  );

39 40
  // x64 host.
  const FakeCommand kx64CheckCommand = FakeCommand(
41 42 43 44 45 46 47
    command: <String>[
      'sysctl',
      'hw.optional.arm64',
    ],
    exitCode: 1,
  );

48 49 50 51 52 53 54 55 56
  // ARM host.
  const FakeCommand kARMCheckCommand = FakeCommand(
    command: <String>[
      'sysctl',
      'hw.optional.arm64',
    ],
    stdout: 'hw.optional.arm64: 1',
  );

57 58 59 60 61
  late FakeProcessManager fakeProcessManager;
  late XcodeProjectInterpreter xcodeProjectInterpreter;
  late FakePlatform platform;
  late FileSystem fileSystem;
  late BufferLogger logger;
62 63

  setUp(() {
64
    fakeProcessManager = FakeProcessManager.empty();
65
    platform = FakePlatform(operatingSystem: 'macos');
66
    fileSystem = MemoryFileSystem.test();
67
    fileSystem.file(xcodebuild).createSync(recursive: true);
68
    logger = BufferLogger.test();
69 70 71 72
    xcodeProjectInterpreter = XcodeProjectInterpreter(
      logger: logger,
      fileSystem: fileSystem,
      platform: platform,
73
      processManager: fakeProcessManager,
74
      usage: TestUsage(),
75 76
    );
  });
77

78
  testWithoutContext('xcodebuild versionText returns null when xcodebuild is not fully installed', () {
79
    fakeProcessManager.addCommands(const <FakeCommand>[
80
      kWhichSysctlCommand,
81
      kx64CheckCommand,
82 83 84 85 86 87 88 89
      FakeCommand(
        command: <String>['xcrun', 'xcodebuild', '-version'],
        stdout: "xcode-select: error: tool 'xcodebuild' requires Xcode, "
            "but active developer directory '/Library/Developer/CommandLineTools' "
            'is a command line tools instance',
        exitCode: 1,
      ),
    ]);
90

91
    expect(xcodeProjectInterpreter.versionText, isNull);
92
    expect(fakeProcessManager, hasNoRemainingExpectations);
93
  });
94

95 96 97
  testWithoutContext('xcodebuild versionText returns null when xcodebuild is not installed', () {
    fakeProcessManager.addCommands(const <FakeCommand>[
      kWhichSysctlCommand,
98
      kx64CheckCommand,
99 100 101 102 103 104 105 106 107
      FakeCommand(
        command: <String>['xcrun', 'xcodebuild', '-version'],
        exception: ProcessException(xcodebuild, <String>['-version']),
      ),
    ]);

    expect(xcodeProjectInterpreter.versionText, isNull);
  });

108
  testWithoutContext('xcodebuild versionText returns formatted version text', () {
109
    fakeProcessManager.addCommands(const <FakeCommand>[
110
      kWhichSysctlCommand,
111
      kx64CheckCommand,
112 113 114 115 116
      FakeCommand(
        command: <String>['xcrun', 'xcodebuild', '-version'],
        stdout: 'Xcode 8.3.3\nBuild version 8E3004b',
      ),
    ]);
117

118
    expect(xcodeProjectInterpreter.versionText, 'Xcode 8.3.3, Build version 8E3004b');
119
    expect(fakeProcessManager, hasNoRemainingExpectations);
120
  });
121

122
  testWithoutContext('xcodebuild versionText handles Xcode version string with unexpected format', () {
123
    fakeProcessManager.addCommands(const <FakeCommand>[
124
      kWhichSysctlCommand,
125
      kx64CheckCommand,
126 127 128 129 130
      FakeCommand(
        command: <String>['xcrun', 'xcodebuild', '-version'],
        stdout: 'Xcode Ultra5000\nBuild version 8E3004b',
      ),
    ]);
131

132
    expect(xcodeProjectInterpreter.versionText, 'Xcode Ultra5000, Build version 8E3004b');
133
    expect(fakeProcessManager, hasNoRemainingExpectations);
134
  });
135

136
  testWithoutContext('xcodebuild version parts can be parsed', () {
137
    fakeProcessManager.addCommands(const <FakeCommand>[
138
      kWhichSysctlCommand,
139
      kx64CheckCommand,
140 141 142 143 144
      FakeCommand(
        command: <String>['xcrun', 'xcodebuild', '-version'],
        stdout: 'Xcode 11.4.1\nBuild version 11N111s',
      ),
    ]);
145

146
    expect(xcodeProjectInterpreter.version, Version(11, 4, 1));
147
    expect(xcodeProjectInterpreter.build, '11N111s');
148
    expect(fakeProcessManager, hasNoRemainingExpectations);
149
  });
150

151
  testWithoutContext('xcodebuild minor and patch version default to 0', () {
152
    fakeProcessManager.addCommands(const <FakeCommand>[
153
      kWhichSysctlCommand,
154
      kx64CheckCommand,
155 156 157 158 159
      FakeCommand(
        command: <String>['xcrun', 'xcodebuild', '-version'],
        stdout: 'Xcode 11\nBuild version 11N111s',
      ),
    ]);
160

161
    expect(xcodeProjectInterpreter.version, Version(11, 0, 0));
162
    expect(fakeProcessManager, hasNoRemainingExpectations);
163
  });
164

165
  testWithoutContext('xcodebuild version parts is null when version has unexpected format', () {
166
    fakeProcessManager.addCommands(const <FakeCommand>[
167
      kWhichSysctlCommand,
168
      kx64CheckCommand,
169 170 171 172 173
      FakeCommand(
        command: <String>['xcrun', 'xcodebuild', '-version'],
        stdout: 'Xcode Ultra5000\nBuild version 8E3004b',
      ),
    ]);
174
    expect(xcodeProjectInterpreter.version, isNull);
175
    expect(xcodeProjectInterpreter.build, isNull);
176
    expect(fakeProcessManager, hasNoRemainingExpectations);
177
  });
178

179
  testWithoutContext('xcodebuild isInstalled is false when not on MacOS', () {
180
    final Platform platform = FakePlatform(operatingSystem: 'notMacOS');
181 182 183 184
    xcodeProjectInterpreter = XcodeProjectInterpreter(
      logger: logger,
      fileSystem: fileSystem,
      platform: platform,
185
      processManager: fakeProcessManager,
186
      usage: TestUsage(),
187 188 189 190
    );
    fileSystem.file(xcodebuild).deleteSync();

    expect(xcodeProjectInterpreter.isInstalled, isFalse);
191
    expect(fakeProcessManager, hasNoRemainingExpectations);
192 193 194 195 196 197
  });

  testWithoutContext('xcodebuild isInstalled is false when xcodebuild does not exist', () {
    fileSystem.file(xcodebuild).deleteSync();

    expect(xcodeProjectInterpreter.isInstalled, isFalse);
198
    expect(fakeProcessManager, hasNoRemainingExpectations);
199 200
  });

201 202 203
  testWithoutContext(
      'xcodebuild isInstalled is false when Xcode is not fully installed', () {
    fakeProcessManager.addCommands(const <FakeCommand>[
204
      kWhichSysctlCommand,
205
      kx64CheckCommand,
206 207 208 209 210 211 212 213
      FakeCommand(
        command: <String>['xcrun', 'xcodebuild', '-version'],
        stdout: "xcode-select: error: tool 'xcodebuild' requires Xcode, "
            "but active developer directory '/Library/Developer/CommandLineTools' "
            'is a command line tools instance',
        exitCode: 1,
      ),
    ]);
214

215
    expect(xcodeProjectInterpreter.isInstalled, isFalse);
216
    expect(fakeProcessManager, hasNoRemainingExpectations);
217
  });
218

219
  testWithoutContext('xcodebuild isInstalled is false when version has unexpected format', () {
220
    fakeProcessManager.addCommands(const <FakeCommand>[
221
      kWhichSysctlCommand,
222
      kx64CheckCommand,
223 224 225 226 227
      FakeCommand(
        command: <String>['xcrun', 'xcodebuild', '-version'],
        stdout: 'Xcode Ultra5000\nBuild version 8E3004b',
      ),
    ]);
228

229
    expect(xcodeProjectInterpreter.isInstalled, isFalse);
230
    expect(fakeProcessManager, hasNoRemainingExpectations);
231
  });
232

233
  testWithoutContext('xcodebuild isInstalled is true when version has expected format', () {
234
    fakeProcessManager.addCommands(const <FakeCommand>[
235
      kWhichSysctlCommand,
236
      kx64CheckCommand,
237 238 239 240 241
      FakeCommand(
        command: <String>['xcrun', 'xcodebuild', '-version'],
        stdout: 'Xcode 8.3.3\nBuild version 8E3004b',
      ),
    ]);
242

243
    expect(xcodeProjectInterpreter.isInstalled, isTrue);
244
    expect(fakeProcessManager, hasNoRemainingExpectations);
245
  });
246

247 248
  testWithoutContext('xcrun runs natively on arm64', () {
    fakeProcessManager.addCommands(const <FakeCommand>[
249
      kWhichSysctlCommand,
250
      kARMCheckCommand,
251 252 253 254 255 256 257
    ]);

    expect(xcodeProjectInterpreter.xcrunCommand(), <String>[
      '/usr/bin/arch',
      '-arm64e',
      'xcrun',
    ]);
258
    expect(fakeProcessManager, hasNoRemainingExpectations);
259 260
  });

261
  testUsingContext('xcodebuild build settings is empty when xcodebuild failed to get the build settings', () async {
262
    platform.environment = const <String, String>{};
263

264
    fakeProcessManager.addCommands(<FakeCommand>[
265
      kWhichSysctlCommand,
266
      const FakeCommand(
267 268 269 270 271 272 273 274 275 276 277 278 279 280
        command: <String>[
          'sysctl',
          'hw.optional.arm64',
        ],
        exitCode: 1,
      ),
      FakeCommand(
        command: <String>[
          'xcrun',
          'xcodebuild',
          '-project',
          '/',
          '-scheme',
          'Free',
281 282
          '-destination',
          'id=123',
283 284
          '-showBuildSettings',
          'BUILD_DIR=${fileSystem.path.absolute('build', 'ios')}',
285 286 287 288
        ],
        exitCode: 1,
      ),
    ]);
289

290
    expect(
291
        await xcodeProjectInterpreter.getBuildSettings('', buildContext: const XcodeProjectBuildContext(deviceId: '123', scheme: 'Free')),
292
        const <String, String>{});
293
    expect(fakeProcessManager, hasNoRemainingExpectations);
294 295 296
  }, overrides: <Type, Generator>{
    FileSystem: () => fileSystem,
    ProcessManager: () => FakeProcessManager.any(),
297 298
  });

299
  testUsingContext('build settings passes in the simulator SDK', () async {
300 301
    platform.environment = const <String, String>{};

302 303
    fakeProcessManager.addCommands(<FakeCommand>[
      kWhichSysctlCommand,
304
      kx64CheckCommand,
305 306 307 308 309 310 311 312
      FakeCommand(
        command: <String>[
          'xcrun',
          'xcodebuild',
          '-project',
          '/',
          '-sdk',
          'iphonesimulator',
313 314
          '-destination',
          'generic/platform=iOS Simulator',
315 316 317 318 319 320 321 322 323 324 325 326 327 328
          '-showBuildSettings',
          'BUILD_DIR=${fileSystem.path.absolute('build', 'ios')}',
        ],
        exitCode: 1,
      ),
    ]);

    expect(
      await xcodeProjectInterpreter.getBuildSettings(
        '',
        buildContext: const XcodeProjectBuildContext(environmentType: EnvironmentType.simulator),
      ),
      const <String, String>{},
    );
329
    expect(fakeProcessManager, hasNoRemainingExpectations);
330 331 332 333 334 335 336 337 338
  }, overrides: <Type, Generator>{
    FileSystem: () => fileSystem,
    ProcessManager: () => FakeProcessManager.any(),
  });

  testUsingContext('build settings accepts an empty scheme', () async {
    platform.environment = const <String, String>{};

    fakeProcessManager.addCommands(<FakeCommand>[
339
      kWhichSysctlCommand,
340
      kx64CheckCommand,
341 342 343 344 345 346
      FakeCommand(
        command: <String>[
          'xcrun',
          'xcodebuild',
          '-project',
          '/',
347 348
          '-destination',
          'generic/platform=iOS',
349 350
          '-showBuildSettings',
          'BUILD_DIR=${fileSystem.path.absolute('build', 'ios')}',
351 352 353 354
        ],
        exitCode: 1,
      ),
    ]);
355

356 357
    expect(await xcodeProjectInterpreter.getBuildSettings('', buildContext: const XcodeProjectBuildContext()),
        const <String, String>{});
358
    expect(fakeProcessManager, hasNoRemainingExpectations);
359 360 361
  }, overrides: <Type, Generator>{
    FileSystem: () => fileSystem,
    ProcessManager: () => FakeProcessManager.any(),
362 363
  });

364
  testUsingContext('xcodebuild build settings contains Flutter Xcode environment variables', () async {
365
    platform.environment = const <String, String>{
366
      'FLUTTER_XCODE_CODE_SIGN_STYLE': 'Manual',
367
      'FLUTTER_XCODE_ARCHS': 'arm64',
368
    };
369
    fakeProcessManager.addCommands(<FakeCommand>[
370
      kWhichSysctlCommand,
371
      kx64CheckCommand,
372 373 374 375 376 377 378 379
      FakeCommand(
        command: <String>[
          'xcrun',
          'xcodebuild',
          '-project',
          fileSystem.path.separator,
          '-scheme',
          'Free',
380 381
          '-destination',
          'generic/platform=iOS',
382
          '-showBuildSettings',
383
          'BUILD_DIR=${fileSystem.path.absolute('build', 'ios')}',
384
          'CODE_SIGN_STYLE=Manual',
385
          'ARCHS=arm64',
386 387 388
        ],
      ),
    ]);
389 390 391
    expect(
        await xcodeProjectInterpreter.getBuildSettings('', buildContext: const XcodeProjectBuildContext(scheme: 'Free')),
        const <String, String>{});
392
    expect(fakeProcessManager, hasNoRemainingExpectations);
393 394 395
  }, overrides: <Type, Generator>{
    FileSystem: () => fileSystem,
    ProcessManager: () => FakeProcessManager.any(),
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
  testUsingContext('build settings uses watch destination if isWatch is true', () async {
    platform.environment = const <String, String>{};

    fakeProcessManager.addCommands(<FakeCommand>[
      kWhichSysctlCommand,
      kx64CheckCommand,
      FakeCommand(
        command: <String>[
          'xcrun',
          'xcodebuild',
          '-project',
          '/',
          '-destination',
          'generic/platform=watchOS',
          '-showBuildSettings',
          'BUILD_DIR=${fileSystem.path.absolute('build', 'ios')}',
        ],
        exitCode: 1,
      ),
    ]);

    expect(
      await xcodeProjectInterpreter.getBuildSettings(
        '',
        buildContext: const XcodeProjectBuildContext(isWatch: true),
      ),
      const <String, String>{},
    );
    expect(fakeProcessManager, hasNoRemainingExpectations);
  }, overrides: <Type, Generator>{
    FileSystem: () => fileSystem,
    ProcessManager: () => FakeProcessManager.any(),
  });

  testUsingContext('build settings uses watch simulator destination if isWatch is true and environment type is simulator', () async {
    platform.environment = const <String, String>{};

    fakeProcessManager.addCommands(<FakeCommand>[
      kWhichSysctlCommand,
      kx64CheckCommand,
      FakeCommand(
        command: <String>[
          'xcrun',
          'xcodebuild',
          '-project',
          '/',
          '-sdk',
          'iphonesimulator',
          '-destination',
          'generic/platform=watchOS Simulator',
          '-showBuildSettings',
          'BUILD_DIR=${fileSystem.path.absolute('build', 'ios')}',
        ],
        exitCode: 1,
      ),
    ]);

    expect(
      await xcodeProjectInterpreter.getBuildSettings(
        '',
        buildContext: const XcodeProjectBuildContext(environmentType: EnvironmentType.simulator, isWatch: true),
      ),
      const <String, String>{},
    );
    expect(fakeProcessManager, hasNoRemainingExpectations);
  }, overrides: <Type, Generator>{
    FileSystem: () => fileSystem,
    ProcessManager: () => FakeProcessManager.any(),
  });

468
  testWithoutContext('xcodebuild clean contains Flutter Xcode environment variables', () async {
469
    platform.environment = const <String, String>{
470
      'FLUTTER_XCODE_CODE_SIGN_STYLE': 'Manual',
471
      'FLUTTER_XCODE_ARCHS': 'arm64',
472 473
    };

474
    fakeProcessManager.addCommands(const <FakeCommand>[
475
      kWhichSysctlCommand,
476
      kx64CheckCommand,
477 478 479 480 481 482 483 484 485 486 487
      FakeCommand(
        command: <String>[
          'xcrun',
          'xcodebuild',
          '-workspace',
          'workspace_path',
          '-scheme',
          'Free',
          '-quiet',
          'clean',
          'CODE_SIGN_STYLE=Manual',
488
          'ARCHS=arm64',
489 490 491
        ],
      ),
    ]);
492

493
    await xcodeProjectInterpreter.cleanWorkspace('workspace_path', 'Free');
494
    expect(fakeProcessManager, hasNoRemainingExpectations);
495 496 497 498
  });

  testWithoutContext('xcodebuild -list getInfo returns something when xcodebuild -list succeeds', () async {
    const String workingDirectory = '/';
499
    fakeProcessManager.addCommands(const <FakeCommand>[
500
      kWhichSysctlCommand,
501
      kx64CheckCommand,
502 503 504 505
      FakeCommand(
        command: <String>['xcrun', 'xcodebuild', '-list'],
      ),
    ]);
506

507 508 509 510
    final XcodeProjectInterpreter xcodeProjectInterpreter = XcodeProjectInterpreter(
      logger: logger,
      fileSystem: fileSystem,
      platform: platform,
511
      processManager: fakeProcessManager,
512
      usage: TestUsage(),
513 514 515
    );

    expect(await xcodeProjectInterpreter.getInfo(workingDirectory), isNotNull);
516
    expect(fakeProcessManager, hasNoRemainingExpectations);
517 518 519 520 521
  });

  testWithoutContext('xcodebuild -list getInfo throws a tool exit when it is unable to find a project', () async {
    const String workingDirectory = '/';
    const String stderr = 'Useful Xcode failure message about missing project.';
522

523
    fakeProcessManager.addCommands(const <FakeCommand>[
524
      kWhichSysctlCommand,
525
      kx64CheckCommand,
526 527 528 529 530 531
      FakeCommand(
        command: <String>['xcrun', 'xcodebuild', '-list'],
        exitCode: 66,
        stderr: stderr,
      ),
    ]);
532

533 534 535 536
    final XcodeProjectInterpreter xcodeProjectInterpreter = XcodeProjectInterpreter(
      logger: logger,
      fileSystem: fileSystem,
      platform: platform,
537
      processManager: fakeProcessManager,
538
      usage: TestUsage(),
539 540
    );

541 542 543 544 545 546 547 548 549 550
    expect(() => xcodeProjectInterpreter.getInfo(workingDirectory), throwsToolExit(message: stderr));
    expect(fakeProcessManager, hasNoRemainingExpectations);
  });

  testWithoutContext('xcodebuild -list getInfo throws a tool exit when project is corrupted', () async {
    const String workingDirectory = '/';
    const String stderr = 'Useful Xcode failure message about corrupted project.';

    fakeProcessManager.addCommands(const <FakeCommand>[
      kWhichSysctlCommand,
551
      kx64CheckCommand,
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568
      FakeCommand(
        command: <String>['xcrun', 'xcodebuild', '-list'],
        exitCode: 74,
        stderr: stderr,
      ),
    ]);

    final XcodeProjectInterpreter xcodeProjectInterpreter = XcodeProjectInterpreter(
      logger: logger,
      fileSystem: fileSystem,
      platform: platform,
      processManager: fakeProcessManager,
      usage: TestUsage(),
    );

    expect(() => xcodeProjectInterpreter.getInfo(workingDirectory), throwsToolExit(message: stderr));
    expect(fakeProcessManager, hasNoRemainingExpectations);
569 570 571 572
  });

  testWithoutContext('Xcode project properties from default project can be parsed', () {
    const String output = '''
573 574 575 576 577 578 579 580 581 582 583 584 585 586
Information about project "Runner":
    Targets:
        Runner

    Build Configurations:
        Debug
        Release

    If no build configuration is specified and -scheme is not passed then "Release" is used.

    Schemes:
        Runner

''';
587
    final XcodeProjectInfo info = XcodeProjectInfo.fromXcodeBuildOutput(output, logger);
588 589 590 591 592 593 594
    expect(info.targets, <String>['Runner']);
    expect(info.schemes, <String>['Runner']);
    expect(info.buildConfigurations, <String>['Debug', 'Release']);
  });

  testWithoutContext('Xcode project properties from project with custom schemes can be parsed', () {
    const String output = '''
595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611
Information about project "Runner":
    Targets:
        Runner

    Build Configurations:
        Debug (Free)
        Debug (Paid)
        Release (Free)
        Release (Paid)

    If no build configuration is specified and -scheme is not passed then "Release (Free)" is used.

    Schemes:
        Free
        Paid

''';
612
    final XcodeProjectInfo info = XcodeProjectInfo.fromXcodeBuildOutput(output, logger);
613 614 615 616 617 618 619 620 621 622 623 624 625 626 627
    expect(info.targets, <String>['Runner']);
    expect(info.schemes, <String>['Free', 'Paid']);
    expect(info.buildConfigurations, <String>['Debug (Free)', 'Debug (Paid)', 'Release (Free)', 'Release (Paid)']);
  });

  testWithoutContext('expected scheme for non-flavored build is Runner', () {
    expect(XcodeProjectInfo.expectedSchemeFor(BuildInfo.debug), 'Runner');
    expect(XcodeProjectInfo.expectedSchemeFor(BuildInfo.profile), 'Runner');
    expect(XcodeProjectInfo.expectedSchemeFor(BuildInfo.release), 'Runner');
  });

  testWithoutContext('expected build configuration for non-flavored build is derived from BuildMode', () {
    expect(XcodeProjectInfo.expectedBuildConfigurationFor(BuildInfo.debug, 'Runner'), 'Debug');
    expect(XcodeProjectInfo.expectedBuildConfigurationFor(BuildInfo.profile, 'Runner'), 'Profile');
    expect(XcodeProjectInfo.expectedBuildConfigurationFor(BuildInfo.release, 'Runner'), 'Release');
628
  });
629

630
  testWithoutContext('expected scheme for flavored build is the title-cased flavor', () {
631 632 633
    expect(XcodeProjectInfo.expectedSchemeFor(const BuildInfo(BuildMode.debug, 'hello', treeShakeIcons: false)), 'Hello');
    expect(XcodeProjectInfo.expectedSchemeFor(const BuildInfo(BuildMode.profile, 'HELLO', treeShakeIcons: false)), 'HELLO');
    expect(XcodeProjectInfo.expectedSchemeFor(const BuildInfo(BuildMode.release, 'Hello', treeShakeIcons: false)), 'Hello');
634 635
  });
  testWithoutContext('expected build configuration for flavored build is Mode-Flavor', () {
636 637 638
    expect(XcodeProjectInfo.expectedBuildConfigurationFor(const BuildInfo(BuildMode.debug, 'hello', treeShakeIcons: false), 'Hello'), 'Debug-Hello');
    expect(XcodeProjectInfo.expectedBuildConfigurationFor(const BuildInfo(BuildMode.profile, 'HELLO', treeShakeIcons: false), 'Hello'), 'Profile-Hello');
    expect(XcodeProjectInfo.expectedBuildConfigurationFor(const BuildInfo(BuildMode.release, 'Hello', treeShakeIcons: false), 'Hello'), 'Release-Hello');
639 640 641
  });

  testWithoutContext('scheme for default project is Runner', () {
642
    final XcodeProjectInfo info = XcodeProjectInfo(<String>['Runner'], <String>['Debug', 'Release'], <String>['Runner'], logger);
643 644 645 646

    expect(info.schemeFor(BuildInfo.debug), 'Runner');
    expect(info.schemeFor(BuildInfo.profile), 'Runner');
    expect(info.schemeFor(BuildInfo.release), 'Runner');
647
    expect(info.schemeFor(const BuildInfo(BuildMode.debug, 'unknown', treeShakeIcons: false)), isNull);
648 649 650
  });

  testWithoutContext('build configuration for default project is matched against BuildMode', () {
651
    final XcodeProjectInfo info = XcodeProjectInfo(<String>['Runner'], <String>['Debug', 'Profile', 'Release'], <String>['Runner'], logger);
652 653 654 655 656 657 658 659 660 661 662

    expect(info.buildConfigurationFor(BuildInfo.debug, 'Runner'), 'Debug');
    expect(info.buildConfigurationFor(BuildInfo.profile, 'Runner'), 'Profile');
    expect(info.buildConfigurationFor(BuildInfo.release, 'Runner'), 'Release');
  });

  testWithoutContext('scheme for project with custom schemes is matched against flavor', () {
    final XcodeProjectInfo info = XcodeProjectInfo(
      <String>['Runner'],
      <String>['Debug (Free)', 'Debug (Paid)', 'Release (Free)', 'Release (Paid)'],
      <String>['Free', 'Paid'],
663
      logger,
664 665
    );

666 667 668
    expect(info.schemeFor(const BuildInfo(BuildMode.debug, 'free', treeShakeIcons: false)), 'Free');
    expect(info.schemeFor(const BuildInfo(BuildMode.profile, 'Free', treeShakeIcons: false)), 'Free');
    expect(info.schemeFor(const BuildInfo(BuildMode.release, 'paid', treeShakeIcons: false)), 'Paid');
669
    expect(info.schemeFor(BuildInfo.debug), isNull);
670
    expect(info.schemeFor(const BuildInfo(BuildMode.debug, 'unknown', treeShakeIcons: false)), isNull);
671 672
  });

673 674 675 676 677 678 679 680 681
  testWithoutContext('reports default scheme error and exit', () {
    final XcodeProjectInfo defaultInfo = XcodeProjectInfo(
      <String>[],
      <String>[],
      <String>['Runner'],
      logger,
    );

    expect(
682
      defaultInfo.reportFlavorNotFoundAndExit,
683 684 685 686 687 688 689 690 691 692 693 694 695 696 697
      throwsToolExit(
        message: 'The Xcode project does not define custom schemes. You cannot use the --flavor option.'
      ),
    );
  });

  testWithoutContext('reports custom scheme error and exit', () {
    final XcodeProjectInfo info = XcodeProjectInfo(
      <String>[],
      <String>[],
      <String>['Free', 'Paid'],
      logger,
    );

    expect(
698
      info.reportFlavorNotFoundAndExit,
699 700 701 702 703 704
      throwsToolExit(
        message: 'You must specify a --flavor option to select one of the available schemes.'
      ),
    );
  });

705 706 707 708 709
  testWithoutContext('build configuration for project with custom schemes is matched against BuildMode and flavor', () {
    final XcodeProjectInfo info = XcodeProjectInfo(
      <String>['Runner'],
      <String>['debug (free)', 'Debug paid', 'profile - Free', 'Profile-Paid', 'release - Free', 'Release-Paid'],
      <String>['Free', 'Paid'],
710
      logger,
711 712
    );

713 714 715 716
    expect(info.buildConfigurationFor(const BuildInfo(BuildMode.debug, 'free', treeShakeIcons: false), 'Free'), 'debug (free)');
    expect(info.buildConfigurationFor(const BuildInfo(BuildMode.debug, 'Paid', treeShakeIcons: false), 'Paid'), 'Debug paid');
    expect(info.buildConfigurationFor(const BuildInfo(BuildMode.profile, 'FREE', treeShakeIcons: false), 'Free'), 'profile - Free');
    expect(info.buildConfigurationFor(const BuildInfo(BuildMode.release, 'paid', treeShakeIcons: false), 'Paid'), 'Release-Paid');
717 718 719 720 721 722 723
  });

  testWithoutContext('build configuration for project with inconsistent naming is null', () {
    final XcodeProjectInfo info = XcodeProjectInfo(
      <String>['Runner'],
      <String>['Debug-F', 'Dbg Paid', 'Rel Free', 'Release Full'],
      <String>['Free', 'Paid'],
724
      logger,
725
    );
726 727 728
    expect(info.buildConfigurationFor(const BuildInfo(BuildMode.debug, 'Free', treeShakeIcons: false), 'Free'), null);
    expect(info.buildConfigurationFor(const BuildInfo(BuildMode.profile, 'Free', treeShakeIcons: false), 'Free'), null);
    expect(info.buildConfigurationFor(const BuildInfo(BuildMode.release, 'Paid', treeShakeIcons: false), 'Paid'), null);
729 730
  });
 group('environmentVariablesAsXcodeBuildSettings', () {
731
    late FakePlatform platform;
732 733

    setUp(() {
734
      platform = FakePlatform();
735 736
    });

737
    testWithoutContext('environment variables as Xcode build settings', () {
738
      platform.environment = const <String, String>{
739 740 741
        'Ignored': 'Bogus',
        'FLUTTER_NOT_XCODE': 'Bogus',
        'FLUTTER_XCODE_CODE_SIGN_STYLE': 'Manual',
742
        'FLUTTER_XCODE_ARCHS': 'arm64',
743
      };
744
      final List<String> environmentVariablesAsBuildSettings = environmentVariablesAsXcodeBuildSettings(platform);
745 746 747 748
      expect(environmentVariablesAsBuildSettings, <String>['CODE_SIGN_STYLE=Manual', 'ARCHS=arm64']);
    });
  });

749
  group('updateGeneratedXcodeProperties', () {
750 751 752
    late Artifacts localIosArtifacts;
    late FakePlatform macOS;
    late FileSystem fs;
753 754

    setUp(() {
755
      fs = MemoryFileSystem.test();
756
      localIosArtifacts = Artifacts.test(localEngine: 'out/ios_profile_arm64');
757
      macOS = FakePlatform(operatingSystem: 'macos');
758
      fs.file(xcodebuild).createSync(recursive: true);
759 760
    });

761
    group('arm simulator', () {
762 763
      late FakeProcessManager fakeProcessManager;
      late XcodeProjectInterpreter xcodeProjectInterpreter;
764 765 766 767 768 769 770 771 772 773 774 775

      setUp(() {
        fakeProcessManager = FakeProcessManager.empty();
        xcodeProjectInterpreter = XcodeProjectInterpreter.test(processManager: fakeProcessManager);
      });

      testUsingContext('does not exclude arm64 simulator when supported by all plugins', () async {
        const BuildInfo buildInfo = BuildInfo.debug;
        final FlutterProject project = FlutterProject.fromDirectoryTest(fs.directory('path/to/project'));
        final Directory podXcodeProject = project.ios.hostAppRoot.childDirectory('Pods').childDirectory('Pods.xcodeproj')
          ..createSync(recursive: true);

776
        final String buildDirectory = fileSystem.path.absolute('build', 'ios');
777 778 779 780 781 782 783 784 785 786 787 788 789 790 791
        fakeProcessManager.addCommands(<FakeCommand>[
          kWhichSysctlCommand,
          kARMCheckCommand,
          FakeCommand(
            command: <String>[
              '/usr/bin/arch',
              '-arm64e',
              'xcrun',
              'xcodebuild',
              '-alltargets',
              '-sdk',
              'iphonesimulator',
              '-project',
              podXcodeProject.path,
              '-showBuildSettings',
792 793
              'BUILD_DIR=$buildDirectory',
              'OBJROOT=$buildDirectory',
794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816
            ],
            stdout: '''
Build settings for action build and target plugin1:
    ENABLE_BITCODE = NO;
    EXCLUDED_ARCHS = i386;
    INFOPLIST_FILE = Runner/Info.plist;
    UNRELATED_BUILD_SETTING = arm64;

Build settings for action build and target plugin2:
    ENABLE_BITCODE = NO;
    EXCLUDED_ARCHS = i386;
    INFOPLIST_FILE = Runner/Info.plist;
    UNRELATED_BUILD_SETTING = arm64;
				'''
          ),
        ]);
        await updateGeneratedXcodeProperties(
          project: project,
          buildInfo: buildInfo,
        );

        final File config = fs.file('path/to/project/ios/Flutter/Generated.xcconfig');
        expect(config.readAsStringSync(), contains('EXCLUDED_ARCHS[sdk=iphonesimulator*]=i386\n'));
817
        expect(config.readAsStringSync(), contains('EXCLUDED_ARCHS[sdk=iphoneos*]=armv7\n'));
818 819
        expect(fakeProcessManager, hasNoRemainingExpectations);
      }, overrides: <Type, Generator>{
820
        Artifacts: () => localIosArtifacts,
821 822 823 824 825 826 827 828 829 830 831 832
        Platform: () => macOS,
        FileSystem: () => fs,
        ProcessManager: () => fakeProcessManager,
        XcodeProjectInterpreter: () => xcodeProjectInterpreter,
      });

      testUsingContext('excludes arm64 simulator when build setting fetch fails', () async {
        const BuildInfo buildInfo = BuildInfo.debug;
        final FlutterProject project = FlutterProject.fromDirectoryTest(fs.directory('path/to/project'));
        final Directory podXcodeProject = project.ios.hostAppRoot.childDirectory('Pods').childDirectory('Pods.xcodeproj')
          ..createSync(recursive: true);

833
        final String buildDirectory = fileSystem.path.absolute('build', 'ios');
834 835 836 837 838 839 840 841 842 843 844 845 846 847 848
        fakeProcessManager.addCommands(<FakeCommand>[
          kWhichSysctlCommand,
          kARMCheckCommand,
          FakeCommand(
              command: <String>[
                '/usr/bin/arch',
                '-arm64e',
                'xcrun',
                'xcodebuild',
                '-alltargets',
                '-sdk',
                'iphonesimulator',
                '-project',
                podXcodeProject.path,
                '-showBuildSettings',
849 850
                'BUILD_DIR=$buildDirectory',
                'OBJROOT=$buildDirectory',
851 852 853 854 855 856 857 858 859 860 861
              ],
              exitCode: 1,
          ),
        ]);
        await updateGeneratedXcodeProperties(
          project: project,
          buildInfo: buildInfo,
        );

        final File config = fs.file('path/to/project/ios/Flutter/Generated.xcconfig');
        expect(config.readAsStringSync(), contains('EXCLUDED_ARCHS[sdk=iphonesimulator*]=i386 arm64\n'));
862
        expect(config.readAsStringSync(), contains('EXCLUDED_ARCHS[sdk=iphoneos*]=armv7\n'));
863 864
        expect(fakeProcessManager, hasNoRemainingExpectations);
      }, overrides: <Type, Generator>{
865
        Artifacts: () => localIosArtifacts,
866 867 868 869 870 871 872 873 874 875 876 877
        Platform: () => macOS,
        FileSystem: () => fs,
        ProcessManager: () => fakeProcessManager,
        XcodeProjectInterpreter: () => xcodeProjectInterpreter,
      });

      testUsingContext('excludes arm64 simulator when unsupported by plugins', () async {
        const BuildInfo buildInfo = BuildInfo.debug;
        final FlutterProject project = FlutterProject.fromDirectoryTest(fs.directory('path/to/project'));
        final Directory podXcodeProject = project.ios.hostAppRoot.childDirectory('Pods').childDirectory('Pods.xcodeproj')
          ..createSync(recursive: true);

878
        final String buildDirectory = fileSystem.path.absolute('build', 'ios');
879 880 881 882 883 884 885 886 887 888 889 890 891 892 893
        fakeProcessManager.addCommands(<FakeCommand>[
          kWhichSysctlCommand,
          kARMCheckCommand,
          FakeCommand(
              command: <String>[
                '/usr/bin/arch',
                '-arm64e',
                'xcrun',
                'xcodebuild',
                '-alltargets',
                '-sdk',
                'iphonesimulator',
                '-project',
                podXcodeProject.path,
                '-showBuildSettings',
894 895
                'BUILD_DIR=$buildDirectory',
                'OBJROOT=$buildDirectory',
896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918
              ],
              stdout: '''
Build settings for action build and target plugin1:
    ENABLE_BITCODE = NO;
    EXCLUDED_ARCHS = i386;
    INFOPLIST_FILE = Runner/Info.plist;
    UNRELATED_BUILD_SETTING = arm64;

Build settings for action build and target plugin2:
    ENABLE_BITCODE = NO;
    EXCLUDED_ARCHS = i386 arm64;
    INFOPLIST_FILE = Runner/Info.plist;
    UNRELATED_BUILD_SETTING = arm64;
				'''
          ),
        ]);
        await updateGeneratedXcodeProperties(
          project: project,
          buildInfo: buildInfo,
        );

        final File config = fs.file('path/to/project/ios/Flutter/Generated.xcconfig');
        expect(config.readAsStringSync(), contains('EXCLUDED_ARCHS[sdk=iphonesimulator*]=i386 arm64\n'));
919
        expect(config.readAsStringSync(), contains('EXCLUDED_ARCHS[sdk=iphoneos*]=armv7\n'));
920 921
        expect(fakeProcessManager, hasNoRemainingExpectations);
      }, overrides: <Type, Generator>{
922
        Artifacts: () => localIosArtifacts,
923 924 925 926 927 928 929
        Platform: () => macOS,
        FileSystem: () => fs,
        ProcessManager: () => fakeProcessManager,
        XcodeProjectInterpreter: () => xcodeProjectInterpreter,
      });
    });

930
    void testUsingOsxContext(String description, dynamic Function() testMethod) {
931
      testUsingContext(description, testMethod, overrides: <Type, Generator>{
932
        Artifacts: () => localIosArtifacts,
933 934
        Platform: () => macOS,
        FileSystem: () => fs,
935
        ProcessManager: () => FakeProcessManager.any(),
936 937 938
      });
    }

939 940
    testUsingOsxContext('exits when armv7 local engine is set', () async {
      localIosArtifacts = Artifacts.test(localEngine: 'out/ios_profile_arm');
941
      const BuildInfo buildInfo = BuildInfo.debug;
942
      final FlutterProject project = FlutterProject.fromDirectoryTest(fs.directory('path/to/project'));
943 944 945 946 947 948
      await expectLater(() =>
        updateGeneratedXcodeProperties(
          project: project,
          buildInfo: buildInfo,
        ),
        throwsToolExit(message: '32-bit iOS local engine binaries are not supported.'),
949 950 951
      );
    });

952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005
    testUsingContext('sets ARCHS=arm64 when arm64 local host engine is set', () async {
      const BuildInfo buildInfo = BuildInfo.debug;
      final FlutterProject project = FlutterProject.fromDirectoryTest(fs.directory('path/to/project'));
      await updateGeneratedXcodeProperties(
        project: project,
        buildInfo: buildInfo,
        useMacOSConfig: true,
      );

      final File config = fs.file('path/to/project/macos/Flutter/ephemeral/Flutter-Generated.xcconfig');
      expect(config.existsSync(), isTrue);

      final String contents = config.readAsStringSync();
      expect(contents.contains('ARCHS=arm64\n'), isTrue);

      final File buildPhaseScript = fs.file('path/to/project/macos/Flutter/ephemeral/flutter_export_environment.sh');
      expect(buildPhaseScript.existsSync(), isTrue);

      final String buildPhaseScriptContents = buildPhaseScript.readAsStringSync();
      expect(buildPhaseScriptContents.contains('export "ARCHS=arm64"'), isTrue);
    }, overrides: <Type, Generator>{
      Artifacts: () => Artifacts.test(localEngine: 'out/host_profile_arm64'),
      Platform: () => macOS,
      FileSystem: () => fs,
      ProcessManager: () => FakeProcessManager.any(),
    });

    testUsingContext('sets ARCHS=x86_64 when x64 local host engine is set', () async {
      const BuildInfo buildInfo = BuildInfo.debug;
      final FlutterProject project = FlutterProject.fromDirectoryTest(fs.directory('path/to/project'));
      await updateGeneratedXcodeProperties(
        project: project,
        buildInfo: buildInfo,
        useMacOSConfig: true,
      );

      final File config = fs.file('path/to/project/macos/Flutter/ephemeral/Flutter-Generated.xcconfig');
      expect(config.existsSync(), isTrue);

      final String contents = config.readAsStringSync();
      expect(contents.contains('ARCHS=x86_64\n'), isTrue);

      final File buildPhaseScript = fs.file('path/to/project/macos/Flutter/ephemeral/flutter_export_environment.sh');
      expect(buildPhaseScript.existsSync(), isTrue);

      final String buildPhaseScriptContents = buildPhaseScript.readAsStringSync();
      expect(buildPhaseScriptContents.contains('export "ARCHS=x86_64"'), isTrue);
    }, overrides: <Type, Generator>{
      Artifacts: () => Artifacts.test(localEngine: 'out/host_profile'),
      Platform: () => macOS,
      FileSystem: () => fs,
      ProcessManager: () => FakeProcessManager.any(),
    });

1006
    testUsingOsxContext('does not exclude arm64 simulator when there are no plugins', () async {
1007 1008 1009 1010 1011 1012 1013 1014 1015
      const BuildInfo buildInfo = BuildInfo.debug;
      final FlutterProject project = FlutterProject.fromDirectoryTest(fs.directory('path/to/project'));
      await updateGeneratedXcodeProperties(
        project: project,
        buildInfo: buildInfo,
      );

      final File config = fs.file('path/to/project/ios/Flutter/Generated.xcconfig');
      expect(config.readAsStringSync(), contains('EXCLUDED_ARCHS[sdk=iphonesimulator*]=i386\n'));
1016
      expect(config.readAsStringSync(), contains('EXCLUDED_ARCHS[sdk=iphoneos*]=armv7\n'));
1017 1018 1019 1020 1021

      final File buildPhaseScript = fs.file('path/to/project/ios/Flutter/flutter_export_environment.sh');
      expect(buildPhaseScript.readAsStringSync(), isNot(contains('EXCLUDED_ARCHS')));
    });

1022
    testUsingOsxContext('sets TRACK_WIDGET_CREATION=true when trackWidgetCreation is true', () async {
1023
      const BuildInfo buildInfo = BuildInfo.debug;
1024
      final FlutterProject project = FlutterProject.fromDirectoryTest(fs.directory('path/to/project'));
1025 1026
      await updateGeneratedXcodeProperties(
        project: project,
1027 1028 1029
        buildInfo: buildInfo,
      );

1030
      final File config = fs.file('path/to/project/ios/Flutter/Generated.xcconfig');
1031 1032 1033 1034
      expect(config.existsSync(), isTrue);

      final String contents = config.readAsStringSync();
      expect(contents.contains('TRACK_WIDGET_CREATION=true'), isTrue);
1035

1036
      final File buildPhaseScript = fs.file('path/to/project/ios/Flutter/flutter_export_environment.sh');
1037 1038 1039 1040
      expect(buildPhaseScript.existsSync(), isTrue);

      final String buildPhaseScriptContents = buildPhaseScript.readAsStringSync();
      expect(buildPhaseScriptContents.contains('TRACK_WIDGET_CREATION=true'), isTrue);
1041 1042 1043
    });

    testUsingOsxContext('does not set TRACK_WIDGET_CREATION when trackWidgetCreation is false', () async {
1044
      const BuildInfo buildInfo = BuildInfo(BuildMode.debug, null, treeShakeIcons: false);
1045
      final FlutterProject project = FlutterProject.fromDirectoryTest(fs.directory('path/to/project'));
1046 1047
      await updateGeneratedXcodeProperties(
        project: project,
1048 1049 1050
        buildInfo: buildInfo,
      );

1051
      final File config = fs.file('path/to/project/ios/Flutter/Generated.xcconfig');
1052 1053 1054 1055
      expect(config.existsSync(), isTrue);

      final String contents = config.readAsStringSync();
      expect(contents.contains('TRACK_WIDGET_CREATION=true'), isFalse);
1056

1057
      final File buildPhaseScript = fs.file('path/to/project/ios/Flutter/flutter_export_environment.sh');
1058 1059 1060 1061
      expect(buildPhaseScript.existsSync(), isTrue);

      final String buildPhaseScriptContents = buildPhaseScript.readAsStringSync();
      expect(buildPhaseScriptContents.contains('TRACK_WIDGET_CREATION=true'), isFalse);
1062 1063
    });

1064
    group('sim local engine', () {
1065 1066 1067 1068 1069 1070 1071
      testUsingContext('sets ARCHS=x86_64 when x86 sim local engine is set', () async {
        const BuildInfo buildInfo = BuildInfo.debug;
        final FlutterProject project = FlutterProject.fromDirectoryTest(fs.directory('path/to/project'));
        await updateGeneratedXcodeProperties(
          project: project,
          buildInfo: buildInfo,
        );
1072

1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088
        final File config = fs.file('path/to/project/ios/Flutter/Generated.xcconfig');
        expect(config.existsSync(), isTrue);

        final String contents = config.readAsStringSync();
        expect(contents.contains('ARCHS=x86_64'), isTrue);

        final File buildPhaseScript = fs.file('path/to/project/ios/Flutter/flutter_export_environment.sh');
        expect(buildPhaseScript.existsSync(), isTrue);

        final String buildPhaseScriptContents = buildPhaseScript.readAsStringSync();
        expect(buildPhaseScriptContents.contains('ARCHS=x86_64'), isTrue);
      }, overrides: <Type, Generator>{
        Artifacts: () => Artifacts.test(localEngine: 'out/ios_debug_sim_unopt'),
        Platform: () => macOS,
        FileSystem: () => fs,
        ProcessManager: () => FakeProcessManager.any(),
1089
      });
1090

1091
      testUsingContext('sets ARCHS=arm64 when arm64 sim local engine is set', () async {
1092 1093 1094 1095 1096 1097
        const BuildInfo buildInfo = BuildInfo.debug;
        final FlutterProject project = FlutterProject.fromDirectoryTest(fs.directory('path/to/project'));
        await updateGeneratedXcodeProperties(
          project: project,
          buildInfo: buildInfo,
        );
1098

1099 1100 1101 1102
        final File config = fs.file('path/to/project/ios/Flutter/Generated.xcconfig');
        expect(config.existsSync(), isTrue);

        final String contents = config.readAsStringSync();
1103
        expect(contents.contains('ARCHS=arm64'), isTrue);
1104 1105 1106 1107 1108

        final File buildPhaseScript = fs.file('path/to/project/ios/Flutter/flutter_export_environment.sh');
        expect(buildPhaseScript.existsSync(), isTrue);

        final String buildPhaseScriptContents = buildPhaseScript.readAsStringSync();
1109
        expect(buildPhaseScriptContents.contains('ARCHS=arm64'), isTrue);
1110
      }, overrides: <Type, Generator>{
1111
        Artifacts: () => Artifacts.test(localEngine: 'out/ios_debug_sim_arm64'),
1112 1113 1114 1115 1116 1117
        Platform: () => macOS,
        FileSystem: () => fs,
        ProcessManager: () => FakeProcessManager.any(),
      });
    });

1118
    String? propertyFor(String key, File file) {
1119 1120 1121 1122 1123 1124 1125 1126 1127
      final List<String> properties = file
          .readAsLinesSync()
          .where((String line) => line.startsWith('$key='))
          .map((String line) => line.split('=')[1])
          .toList();
      return properties.isEmpty ? null : properties.first;
    }

    Future<void> checkBuildVersion({
1128 1129 1130 1131
      required String manifestString,
      required BuildInfo buildInfo,
      String? expectedBuildName,
      String? expectedBuildNumber,
1132
    }) async {
1133
      final File manifestFile = fs.file('path/to/project/pubspec.yaml');
1134 1135 1136
      manifestFile.createSync(recursive: true);
      manifestFile.writeAsStringSync(manifestString);

1137
      await updateGeneratedXcodeProperties(
1138
        project: FlutterProject.fromDirectoryTest(fs.directory('path/to/project')),
1139 1140 1141
        buildInfo: buildInfo,
      );

1142
      final File localPropertiesFile = fs.file('path/to/project/ios/Flutter/Generated.xcconfig');
1143 1144
      expect(propertyFor('FLUTTER_BUILD_NAME', localPropertiesFile), expectedBuildName);
      expect(propertyFor('FLUTTER_BUILD_NUMBER', localPropertiesFile), expectedBuildNumber);
1145
      expect(propertyFor('FLUTTER_BUILD_NUMBER', localPropertiesFile), isNotNull);
1146 1147
    }

1148
    testUsingOsxContext('extract build name and number from pubspec.yaml', () async {
1149 1150 1151 1152 1153 1154 1155 1156 1157
      const String manifest = '''
name: test
version: 1.0.0+1
dependencies:
  flutter:
    sdk: flutter
flutter:
''';

1158
      const BuildInfo buildInfo = BuildInfo(BuildMode.release, null, treeShakeIcons: false);
1159
      await checkBuildVersion(
1160
        manifestString: manifest,
1161 1162 1163 1164 1165 1166
        buildInfo: buildInfo,
        expectedBuildName: '1.0.0',
        expectedBuildNumber: '1',
      );
    });

1167
    testUsingOsxContext('extract build name from pubspec.yaml', () async {
1168 1169 1170 1171 1172 1173 1174 1175
      const String manifest = '''
name: test
version: 1.0.0
dependencies:
  flutter:
    sdk: flutter
flutter:
''';
1176
      const BuildInfo buildInfo = BuildInfo(BuildMode.release, null, treeShakeIcons: false);
1177
      await checkBuildVersion(
1178
        manifestString: manifest,
1179 1180
        buildInfo: buildInfo,
        expectedBuildName: '1.0.0',
1181
        expectedBuildNumber: '1.0.0',
1182 1183 1184
      );
    });

1185
    testUsingOsxContext('allow build info to override build name', () async {
1186 1187 1188 1189 1190 1191 1192 1193
      const String manifest = '''
name: test
version: 1.0.0+1
dependencies:
  flutter:
    sdk: flutter
flutter:
''';
1194
      const BuildInfo buildInfo = BuildInfo(BuildMode.release, null, buildName: '1.0.2', treeShakeIcons: false);
1195
      await checkBuildVersion(
1196
        manifestString: manifest,
1197 1198 1199 1200 1201 1202
        buildInfo: buildInfo,
        expectedBuildName: '1.0.2',
        expectedBuildNumber: '1',
      );
    });

1203 1204 1205 1206 1207 1208 1209 1210 1211
    testUsingOsxContext('allow build info to override build name with build number fallback', () async {
      const String manifest = '''
name: test
version: 1.0.0
dependencies:
  flutter:
    sdk: flutter
flutter:
''';
1212
      const BuildInfo buildInfo = BuildInfo(BuildMode.release, null, buildName: '1.0.2', treeShakeIcons: false);
1213 1214 1215 1216 1217 1218 1219 1220
      await checkBuildVersion(
        manifestString: manifest,
        buildInfo: buildInfo,
        expectedBuildName: '1.0.2',
        expectedBuildNumber: '1.0.2',
      );
    });

1221
    testUsingOsxContext('allow build info to override build number', () async {
1222 1223 1224 1225 1226 1227 1228 1229
      const String manifest = '''
name: test
version: 1.0.0+1
dependencies:
  flutter:
    sdk: flutter
flutter:
''';
1230
      const BuildInfo buildInfo = BuildInfo(BuildMode.release, null, buildNumber: '3', treeShakeIcons: false);
1231
      await checkBuildVersion(
1232
        manifestString: manifest,
1233 1234 1235 1236 1237 1238
        buildInfo: buildInfo,
        expectedBuildName: '1.0.0',
        expectedBuildNumber: '3',
      );
    });

1239
    testUsingOsxContext('allow build info to override build name and number', () async {
1240 1241 1242 1243 1244 1245 1246 1247
      const String manifest = '''
name: test
version: 1.0.0+1
dependencies:
  flutter:
    sdk: flutter
flutter:
''';
1248
      const BuildInfo buildInfo = BuildInfo(BuildMode.release, null, buildName: '1.0.2', buildNumber: '3', treeShakeIcons: false);
1249
      await checkBuildVersion(
1250
        manifestString: manifest,
1251 1252 1253 1254 1255 1256
        buildInfo: buildInfo,
        expectedBuildName: '1.0.2',
        expectedBuildNumber: '3',
      );
    });

1257
    testUsingOsxContext('allow build info to override build name and set number', () async {
1258 1259 1260 1261 1262 1263 1264 1265
      const String manifest = '''
name: test
version: 1.0.0
dependencies:
  flutter:
    sdk: flutter
flutter:
''';
1266
      const BuildInfo buildInfo = BuildInfo(BuildMode.release, null, buildName: '1.0.2', buildNumber: '3', treeShakeIcons: false);
1267
      await checkBuildVersion(
1268
        manifestString: manifest,
1269 1270 1271 1272 1273 1274
        buildInfo: buildInfo,
        expectedBuildName: '1.0.2',
        expectedBuildNumber: '3',
      );
    });

1275
    testUsingOsxContext('allow build info to set build name and number', () async {
1276 1277 1278 1279 1280 1281 1282
      const String manifest = '''
name: test
dependencies:
  flutter:
    sdk: flutter
flutter:
''';
1283
      const BuildInfo buildInfo = BuildInfo(BuildMode.release, null, buildName: '1.0.2', buildNumber: '3', treeShakeIcons: false);
1284
      await checkBuildVersion(
1285
        manifestString: manifest,
1286 1287 1288 1289 1290
        buildInfo: buildInfo,
        expectedBuildName: '1.0.2',
        expectedBuildNumber: '3',
      );
    });
1291 1292 1293 1294 1295 1296 1297 1298 1299

    testUsingOsxContext('default build name and number when version is missing', () async {
      const String manifest = '''
name: test
dependencies:
  flutter:
    sdk: flutter
flutter:
''';
1300
      const BuildInfo buildInfo = BuildInfo(BuildMode.release, null, treeShakeIcons: false);
1301 1302 1303 1304 1305 1306 1307
      await checkBuildVersion(
        manifestString: manifest,
        buildInfo: buildInfo,
        expectedBuildName: '1.0.0',
        expectedBuildNumber: '1',
      );
    });
1308
  });
1309
}