ios_project_migration_test.dart 33.7 KB
Newer Older
1 2 3 4 5 6 7
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:file/file.dart';
import 'package:file/memory.dart';
import 'package:flutter_tools/src/base/logger.dart';
8
import 'package:flutter_tools/src/base/project_migrator.dart';
9
import 'package:flutter_tools/src/ios/migrations/host_app_info_plist_migration.dart';
10
import 'package:flutter_tools/src/ios/migrations/ios_deployment_target_migration.dart';
11
import 'package:flutter_tools/src/ios/migrations/project_base_configuration_migration.dart';
12
import 'package:flutter_tools/src/ios/migrations/project_build_location_migration.dart';
13
import 'package:flutter_tools/src/ios/migrations/remove_framework_link_and_embedding_migration.dart';
14
import 'package:flutter_tools/src/ios/migrations/xcode_build_system_migration.dart';
15 16
import 'package:flutter_tools/src/migrations/xcode_project_object_version_migration.dart';
import 'package:flutter_tools/src/migrations/xcode_script_build_phase_migration.dart';
17
import 'package:flutter_tools/src/reporting/reporting.dart';
18
import 'package:flutter_tools/src/xcode_project.dart';
19
import 'package:test/fake.dart';
20 21 22 23

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

void main () {
24
  group('iOS migration', () {
25
    late TestUsage testUsage;
26

27
    setUp(() {
28
      testUsage = TestUsage();
29 30
    });

31 32
    testWithoutContext('migrators succeed', () {
      final FakeIOSMigrator fakeIOSMigrator = FakeIOSMigrator(succeeds: true);
33
      final ProjectMigration migration = ProjectMigration(<ProjectMigrator>[fakeIOSMigrator]);
34
      expect(migration.run(), isTrue);
35 36
    });

37 38
    testWithoutContext('migrators fail', () {
      final FakeIOSMigrator fakeIOSMigrator = FakeIOSMigrator(succeeds: false);
39
      final ProjectMigration migration = ProjectMigration(<ProjectMigrator>[fakeIOSMigrator]);
40
      expect(migration.run(), isFalse);
41 42
    });

43
    group('remove framework linking and embedding migration', () {
44 45 46 47
      late MemoryFileSystem memoryFileSystem;
      late BufferLogger testLogger;
      late FakeIosProject project;
      late File xcodeProjectInfoFile;
48 49

      setUp(() {
50
        memoryFileSystem = MemoryFileSystem.test();
51
        xcodeProjectInfoFile = memoryFileSystem.file('project.pbxproj');
52
        testLogger = BufferLogger.test();
53 54
        project = FakeIosProject();
        project.xcodeProjectInfoFile = xcodeProjectInfoFile;
55 56 57 58
      });

      testWithoutContext('skipped if files are missing', () {
        final RemoveFrameworkLinkAndEmbeddingMigration iosProjectMigration = RemoveFrameworkLinkAndEmbeddingMigration(
59
          project,
60
          testLogger,
61
          testUsage
62 63
        );
        expect(iosProjectMigration.migrate(), isTrue);
64
        expect(testUsage.events, isEmpty);
65 66 67

        expect(xcodeProjectInfoFile.existsSync(), isFalse);

68
        expect(testLogger.traceText, contains('Xcode project not found, skipping framework link and embedding migration'));
69 70 71 72 73 74 75 76 77
        expect(testLogger.statusText, isEmpty);
      });

      testWithoutContext('skipped if nothing to upgrade', () {
        const String contents = 'Nothing to upgrade';
        xcodeProjectInfoFile.writeAsStringSync(contents);
        final DateTime projectLastModified = xcodeProjectInfoFile.lastModifiedSync();

        final RemoveFrameworkLinkAndEmbeddingMigration iosProjectMigration = RemoveFrameworkLinkAndEmbeddingMigration(
78
          project,
79
          testLogger,
80
          testUsage,
81 82
        );
        expect(iosProjectMigration.migrate(), isTrue);
83
        expect(testUsage.events, isEmpty);
84 85 86 87 88 89 90 91

        expect(xcodeProjectInfoFile.lastModifiedSync(), projectLastModified);
        expect(xcodeProjectInfoFile.readAsStringSync(), contents);

        expect(testLogger.statusText, isEmpty);
      });

      testWithoutContext('skips migrating script with embed', () {
92 93
        const String contents = r'''
shellScript = "/bin/sh \"$FLUTTER_ROOT/packages/flutter_tools/bin/xcode_backend.sh\" embed\n/bin/sh \"$FLUTTER_ROOT/packages/flutter_tools/bin/xcode_backend.sh\" thin";
94
			''';
95 96 97
        xcodeProjectInfoFile.writeAsStringSync(contents);

        final RemoveFrameworkLinkAndEmbeddingMigration iosProjectMigration = RemoveFrameworkLinkAndEmbeddingMigration(
98
          project,
99
          testLogger,
100
          testUsage,
101 102 103 104 105 106 107
        );
        expect(iosProjectMigration.migrate(), isTrue);
        expect(xcodeProjectInfoFile.readAsStringSync(), contents);
        expect(testLogger.statusText, isEmpty);
      });

      testWithoutContext('Xcode project is migrated', () {
108
        xcodeProjectInfoFile.writeAsStringSync(r'''
109 110 111 112 113 114 115 116 117 118 119
prefix 3B80C3941E831B6300D905FE
3B80C3951E831B6300D905FE suffix
741F496821356857001E2961
keep this 1
  3B80C3931E831B6300D905FE spaces
741F496521356807001E2961
9705A1C61CF904A100538489
9705A1C71CF904A300538489
741F496221355F47001E2961
9740EEBA1CF902C7004384FC
741F495E21355F27001E2961
120
			shellScript = "/bin/sh \"$FLUTTER_ROOT/packages/flutter_tools/bin/xcode_backend.sh\" thin";
121 122 123
keep this 2
''');

124
        final RemoveFrameworkLinkAndEmbeddingMigration iosProjectMigration = RemoveFrameworkLinkAndEmbeddingMigration(
125
          project,
126
          testLogger,
127
          testUsage,
128 129
        );
        expect(iosProjectMigration.migrate(), isTrue);
130
        expect(testUsage.events, isEmpty);
131

132
        expect(xcodeProjectInfoFile.readAsStringSync(), r'''
133
keep this 1
134
			shellScript = "/bin/sh \"$FLUTTER_ROOT/packages/flutter_tools/bin/xcode_backend.sh\" embed_and_thin";
135 136
keep this 2
''');
137 138
        expect(testLogger.statusText, contains('Upgrading project.pbxproj'));
      });
139

140 141
      testWithoutContext('migration fails with leftover App.framework reference', () {
        xcodeProjectInfoFile.writeAsStringSync('''
142 143
        746232531E83B71900CC1A5E /* App.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 746232521E83B71900CC1A5E /* App.framework */; };
''');
144 145

        final RemoveFrameworkLinkAndEmbeddingMigration iosProjectMigration = RemoveFrameworkLinkAndEmbeddingMigration(
146
          project,
147
          testLogger,
148
          testUsage,
149 150
        );

151
        expect(iosProjectMigration.migrate, throwsToolExit(message: 'Your Xcode project requires migration'));
152 153 154
        expect(testUsage.events, contains(
          const TestUsageEvent('ios-migration', 'remove-frameworks', label: 'failure'),
        ));
155 156 157 158
      });

      testWithoutContext('migration fails with leftover Flutter.framework reference', () {
        xcodeProjectInfoFile.writeAsStringSync('''
159 160
      9705A1C71CF904A300538480 /* Flutter.framework in Embed Frameworks */,
''');
161 162

        final RemoveFrameworkLinkAndEmbeddingMigration iosProjectMigration = RemoveFrameworkLinkAndEmbeddingMigration(
163
          project,
164
          testLogger,
165
          testUsage,
166
        );
167
        expect(iosProjectMigration.migrate, throwsToolExit(message: 'Your Xcode project requires migration'));
168 169 170
        expect(testUsage.events, contains(
          const TestUsageEvent('ios-migration', 'remove-frameworks', label: 'failure'),
        ));
171 172 173 174
      });

      testWithoutContext('migration fails without Xcode installed', () {
        xcodeProjectInfoFile.writeAsStringSync('''
175 176
        746232531E83B71900CC1A5E /* App.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 746232521E83B71900CC1A5E /* App.framework */; };
''');
177 178

        final RemoveFrameworkLinkAndEmbeddingMigration iosProjectMigration = RemoveFrameworkLinkAndEmbeddingMigration(
179
          project,
180
          testLogger,
181
          testUsage,
182
        );
183
        expect(iosProjectMigration.migrate, throwsToolExit(message: 'Your Xcode project requires migration'));
184 185 186
        expect(testUsage.events, contains(
          const TestUsageEvent('ios-migration', 'remove-frameworks', label: 'failure'),
        ));
187
      });
188
    });
189 190

    group('new Xcode build system', () {
191 192 193 194
      late MemoryFileSystem memoryFileSystem;
      late BufferLogger testLogger;
      late FakeIosProject project;
      late File xcodeWorkspaceSharedSettings;
195 196

      setUp(() {
197
        memoryFileSystem = MemoryFileSystem.test();
198
        xcodeWorkspaceSharedSettings = memoryFileSystem.file('WorkspaceSettings.xcsettings');
199
        testLogger = BufferLogger.test();
200 201
        project = FakeIosProject();
        project.xcodeWorkspaceSharedSettings = xcodeWorkspaceSharedSettings;
202 203 204 205
      });

      testWithoutContext('skipped if files are missing', () {
        final XcodeBuildSystemMigration iosProjectMigration = XcodeBuildSystemMigration(
206
          project,
207 208 209 210 211
          testLogger,
        );
        expect(iosProjectMigration.migrate(), isTrue);
        expect(xcodeWorkspaceSharedSettings.existsSync(), isFalse);

212
        expect(testLogger.traceText, contains('Xcode workspace settings not found, skipping build system migration'));
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
        expect(testLogger.statusText, isEmpty);
      });

      testWithoutContext('skipped if nothing to upgrade', () {
        const String contents = '''
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>BuildSystemType</key>
	<string></string>
</dict>
</plist>''';
        xcodeWorkspaceSharedSettings.writeAsStringSync(contents);

        final XcodeBuildSystemMigration iosProjectMigration = XcodeBuildSystemMigration(
229
          project,
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
          testLogger,
        );
        expect(iosProjectMigration.migrate(), isTrue);
        expect(xcodeWorkspaceSharedSettings.existsSync(), isTrue);
        expect(testLogger.statusText, isEmpty);
      });

      testWithoutContext('Xcode project is migrated', () {
        const String contents = '''
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>BuildSystemType</key>
	<string>Original</string>
245 246
	<key>PreviewsEnabled</key>
	<false/>
247 248 249 250 251
</dict>
</plist>''';
        xcodeWorkspaceSharedSettings.writeAsStringSync(contents);

        final XcodeBuildSystemMigration iosProjectMigration = XcodeBuildSystemMigration(
252
          project,
253 254 255 256 257 258 259 260
          testLogger,
        );
        expect(iosProjectMigration.migrate(), isTrue);
        expect(xcodeWorkspaceSharedSettings.existsSync(), isFalse);

        expect(testLogger.statusText, contains('Legacy build system detected, removing'));
      });
    });
261

262
    group('Xcode default build location', () {
263 264 265 266
      late MemoryFileSystem memoryFileSystem;
      late BufferLogger testLogger;
      late FakeIosProject project;
      late File xcodeProjectWorkspaceData;
267 268

      setUp(() {
269 270 271
        memoryFileSystem = MemoryFileSystem();
        xcodeProjectWorkspaceData = memoryFileSystem.file('contents.xcworkspacedata');
        testLogger = BufferLogger.test();
272 273
        project = FakeIosProject();
        project.xcodeProjectWorkspaceData = xcodeProjectWorkspaceData;
274 275 276 277
      });

      testWithoutContext('skipped if files are missing', () {
        final ProjectBuildLocationMigration iosProjectMigration = ProjectBuildLocationMigration(
278
          project,
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
          testLogger,
        );
        expect(iosProjectMigration.migrate(), isTrue);
        expect(xcodeProjectWorkspaceData.existsSync(), isFalse);

        expect(testLogger.traceText, contains('Xcode project workspace data not found, skipping build location migration.'));
        expect(testLogger.statusText, isEmpty);
      });

      testWithoutContext('skipped if nothing to upgrade', () {
        const String contents = '''
 <?xml version="1.0" encoding="UTF-8"?>
 <Workspace
    version = "1.0">
    <FileRef
      location = "self:">
    </FileRef>
 </Workspace>''';
        xcodeProjectWorkspaceData.writeAsStringSync(contents);

        final ProjectBuildLocationMigration iosProjectMigration = ProjectBuildLocationMigration(
300
          project,
301 302 303 304 305 306
          testLogger,
        );
        expect(iosProjectMigration.migrate(), isTrue);
        expect(xcodeProjectWorkspaceData.existsSync(), isTrue);
        expect(testLogger.statusText, isEmpty);
      });
307

308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
      testWithoutContext('Xcode project is migrated', () {
        const String contents = '''
 <?xml version="1.0" encoding="UTF-8"?>
 <Workspace
   version = "1.0">
   <FileRef
      location = "group:Runner.xcodeproj">
   </FileRef>
   <FileRef
      location = "group:Pods/Pods.xcodeproj">
   </FileRef>
 </Workspace>
''';
        xcodeProjectWorkspaceData.writeAsStringSync(contents);

        final ProjectBuildLocationMigration iosProjectMigration = ProjectBuildLocationMigration(
324
          project,
325
          testLogger,
326
        );
327 328 329 330 331 332 333 334 335 336 337 338 339
        expect(iosProjectMigration.migrate(), isTrue);
        expect(xcodeProjectWorkspaceData.readAsStringSync(), '''
 <?xml version="1.0" encoding="UTF-8"?>
 <Workspace
   version = "1.0">
   <FileRef
      location = "self:">
   </FileRef>
 </Workspace>
''');
        expect(testLogger.statusText, contains('Upgrading contents.xcworkspacedata'));
      });
    });
340

341
    group('remove Runner project base configuration', () {
342 343 344 345
      late MemoryFileSystem memoryFileSystem;
      late BufferLogger testLogger;
      late FakeIosProject project;
      late File xcodeProjectInfoFile;
346 347 348 349 350

      setUp(() {
        memoryFileSystem = MemoryFileSystem();
        xcodeProjectInfoFile = memoryFileSystem.file('project.pbxproj');
        testLogger = BufferLogger.test();
351 352
        project = FakeIosProject();
        project.xcodeProjectInfoFile = xcodeProjectInfoFile;
353 354 355 356
      });

      testWithoutContext('skipped if files are missing', () {
        final ProjectBaseConfigurationMigration iosProjectMigration = ProjectBaseConfigurationMigration(
357
          project,
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
          testLogger,
        );
        expect(iosProjectMigration.migrate(), isTrue);
        expect(xcodeProjectInfoFile.existsSync(), isFalse);

        expect(testLogger.traceText, contains('Xcode project not found, skipping Runner project build settings and configuration migration'));
        expect(testLogger.statusText, isEmpty);
      });

      testWithoutContext('skipped if nothing to upgrade', () {
        const String contents = 'Nothing to upgrade';
        xcodeProjectInfoFile.writeAsStringSync(contents);
        final DateTime projectLastModified = xcodeProjectInfoFile.lastModifiedSync();

        final ProjectBaseConfigurationMigration iosProjectMigration = ProjectBaseConfigurationMigration(
373
          project,
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
          testLogger,
        );
        expect(iosProjectMigration.migrate(), isTrue);

        expect(xcodeProjectInfoFile.lastModifiedSync(), projectLastModified);
        expect(xcodeProjectInfoFile.readAsStringSync(), contents);

        expect(testLogger.statusText, isEmpty);
      });

      testWithoutContext('Xcode project is migrated with template identifiers', () {
        xcodeProjectInfoFile.writeAsStringSync('''
		97C147031CF9000F007C117D /* Debug */ = {
			isa = XCBuildConfiguration;
			baseConfigurationReference = 9740EEB21CF90195004384FC /* Debug.xcconfig */;
keep this 1
		249021D3217E4FDB00AE95B9 /* Profile */ = {
			isa = XCBuildConfiguration;
			baseConfigurationReference = 7AFA3C8E1D35360C0083082E /* Release.xcconfig */;
keep this 2
		97C147041CF9000F007C117D /* Release */ = {
			isa = XCBuildConfiguration;
			baseConfigurationReference = 7AFA3C8E1D35360C0083082E /* Release.xcconfig */;
keep this 3
''');

        final ProjectBaseConfigurationMigration iosProjectMigration = ProjectBaseConfigurationMigration(
401
          project,
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
          testLogger,
        );
        expect(iosProjectMigration.migrate(), isTrue);

        expect(xcodeProjectInfoFile.readAsStringSync(), '''
		97C147031CF9000F007C117D /* Debug */ = {
			isa = XCBuildConfiguration;
keep this 1
		249021D3217E4FDB00AE95B9 /* Profile */ = {
			isa = XCBuildConfiguration;
keep this 2
		97C147041CF9000F007C117D /* Release */ = {
			isa = XCBuildConfiguration;
keep this 3
''');
        expect(testLogger.statusText, contains('Project base configurations detected, removing.'));
      });

      testWithoutContext('Xcode project is migrated with custom identifiers', () {
        xcodeProjectInfoFile.writeAsStringSync('''
		97C147031CF9000F007C1171 /* Debug */ = {
			isa = XCBuildConfiguration;
			baseConfigurationReference = 9740EEB21CF90195004384FC /* Debug.xcconfig */;
		2436755321828D23008C7051 /* Profile */ = {
			isa = XCBuildConfiguration;
			baseConfigurationReference = 7AFA3C8E1D35360C0083082E /* Release.xcconfig */;
		97C147041CF9000F007C1171 /* Release */ = {
			isa = XCBuildConfiguration;
			baseConfigurationReference = 7AFA3C8E1D35360C0083082E /* Release.xcconfig */;
      /* Begin XCConfigurationList section */
      97C146E91CF9000F007C117D /* Build configuration list for PBXProject "Runner" */ = {
        isa = XCConfigurationList;
        buildConfigurations = (
          97C147031CF9000F007C1171 /* Debug */,
          97C147041CF9000F007C1171 /* Release */,
          2436755321828D23008C7051 /* Profile */,
        );
        defaultConfigurationIsVisible = 0;
        defaultConfigurationName = Release;
      };
      97C147051CF9000F007C117D /* Build configuration list for PBXNativeTarget "Runner" */ = {
        isa = XCConfigurationList;
        buildConfigurations = (
          97C147061CF9000F007C117D /* Debug */,
          97C147071CF9000F007C117D /* Release */,
          2436755421828D23008C705F /* Profile */,
        );
        defaultConfigurationIsVisible = 0;
        defaultConfigurationName = Release;
      };
/* End XCConfigurationList section */
''');

        final ProjectBaseConfigurationMigration iosProjectMigration = ProjectBaseConfigurationMigration(
456
          project,
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 485 486 487 488 489 490 491 492 493
          testLogger,
        );
        expect(iosProjectMigration.migrate(), isTrue);

        expect(xcodeProjectInfoFile.readAsStringSync(), '''
		97C147031CF9000F007C1171 /* Debug */ = {
			isa = XCBuildConfiguration;
		2436755321828D23008C7051 /* Profile */ = {
			isa = XCBuildConfiguration;
		97C147041CF9000F007C1171 /* Release */ = {
			isa = XCBuildConfiguration;
      /* Begin XCConfigurationList section */
      97C146E91CF9000F007C117D /* Build configuration list for PBXProject "Runner" */ = {
        isa = XCConfigurationList;
        buildConfigurations = (
          97C147031CF9000F007C1171 /* Debug */,
          97C147041CF9000F007C1171 /* Release */,
          2436755321828D23008C7051 /* Profile */,
        );
        defaultConfigurationIsVisible = 0;
        defaultConfigurationName = Release;
      };
      97C147051CF9000F007C117D /* Build configuration list for PBXNativeTarget "Runner" */ = {
        isa = XCConfigurationList;
        buildConfigurations = (
          97C147061CF9000F007C117D /* Debug */,
          97C147071CF9000F007C117D /* Release */,
          2436755421828D23008C705F /* Profile */,
        );
        defaultConfigurationIsVisible = 0;
        defaultConfigurationName = Release;
      };
/* End XCConfigurationList section */
''');
        expect(testLogger.statusText, contains('Project base configurations detected, removing.'));
      });
    });
494 495 496 497 498 499 500

    group('update deployment target version', () {
      late MemoryFileSystem memoryFileSystem;
      late BufferLogger testLogger;
      late FakeIosProject project;
      late File xcodeProjectInfoFile;
      late File appFrameworkInfoPlist;
501
      late File podfile;
502 503 504 505 506 507 508 509 510 511

      setUp(() {
        memoryFileSystem = MemoryFileSystem();
        testLogger = BufferLogger.test();
        project = FakeIosProject();
        xcodeProjectInfoFile = memoryFileSystem.file('project.pbxproj');
        project.xcodeProjectInfoFile = xcodeProjectInfoFile;

        appFrameworkInfoPlist = memoryFileSystem.file('AppFrameworkInfo.plist');
        project.appFrameworkInfoPlist = appFrameworkInfoPlist;
512 513 514

        podfile = memoryFileSystem.file('Podfile');
        project.podfile = podfile;
515 516 517
      });

      testWithoutContext('skipped if files are missing', () {
518
        final IOSDeploymentTargetMigration iosProjectMigration = IOSDeploymentTargetMigration(
519 520 521 522 523 524
          project,
          testLogger,
        );
        expect(iosProjectMigration.migrate(), isTrue);
        expect(xcodeProjectInfoFile.existsSync(), isFalse);
        expect(appFrameworkInfoPlist.existsSync(), isFalse);
525
        expect(podfile.existsSync(), isFalse);
526 527 528

        expect(testLogger.traceText, contains('Xcode project not found, skipping iOS deployment target version migration'));
        expect(testLogger.traceText, contains('AppFrameworkInfo.plist not found, skipping minimum OS version migration'));
529
        expect(testLogger.traceText, contains('Podfile not found, skipping global platform iOS version migration'));
530 531 532 533
        expect(testLogger.statusText, isEmpty);
      });

      testWithoutContext('skipped if nothing to upgrade', () {
534
        const String xcodeProjectInfoFileContents = 'IPHONEOS_DEPLOYMENT_TARGET = 11.0;';
535 536 537 538
        xcodeProjectInfoFile.writeAsStringSync(xcodeProjectInfoFileContents);

        const String appFrameworkInfoPlistContents = '''
  <key>MinimumOSVersion</key>
539
  <string>11.0</string>
540 541 542 543 544
''';
        appFrameworkInfoPlist.writeAsStringSync(appFrameworkInfoPlistContents);

        final DateTime projectLastModified = xcodeProjectInfoFile.lastModifiedSync();

545 546 547 548
        const String podfileFileContents = "# platform :ios, '11.0'";
        podfile.writeAsStringSync(podfileFileContents);
        final DateTime podfileLastModified = podfile.lastModifiedSync();

549
        final IOSDeploymentTargetMigration iosProjectMigration = IOSDeploymentTargetMigration(
550 551 552 553 554 555 556 557
          project,
          testLogger,
        );
        expect(iosProjectMigration.migrate(), isTrue);

        expect(xcodeProjectInfoFile.lastModifiedSync(), projectLastModified);
        expect(xcodeProjectInfoFile.readAsStringSync(), xcodeProjectInfoFileContents);
        expect(appFrameworkInfoPlist.readAsStringSync(), appFrameworkInfoPlistContents);
558 559
        expect(podfile.lastModifiedSync(), podfileLastModified);
        expect(podfile.readAsStringSync(), podfileFileContents);
560 561 562 563

        expect(testLogger.statusText, isEmpty);
      });

564
      testWithoutContext('Xcode project is migrated to 11', () {
565 566 567 568 569 570 571
        xcodeProjectInfoFile.writeAsStringSync('''
				GCC_WARN_UNUSED_VARIABLE = YES;
				IPHONEOS_DEPLOYMENT_TARGET = 8.0;
				MTL_ENABLE_DEBUG_INFO = YES;
				ONLY_ACTIVE_ARCH = YES;

				IPHONEOS_DEPLOYMENT_TARGET = 8.0;
572
				IPHONEOS_DEPLOYMENT_TARGET = 11.0;
573 574 575 576 577 578 579 580 581 582 583
''');

        appFrameworkInfoPlist.writeAsStringSync('''
  <key>CFBundleShortVersionString</key>
  <string>1.0</string>
  <key>CFBundleSignature</key>
  <string>????</string>
  <key>CFBundleVersion</key>
  <string>1.0</string>
  <key>MinimumOSVersion</key>
  <string>8.0</string>
584 585
  <key>MinimumOSVersion</key>
  <string>11.0</string>
586 587
</dict>
</plist>
588 589 590 591 592
''');

        podfile.writeAsStringSync('''
# platform :ios, '9.0'
platform :ios, '9.0'
593 594
''');

595
        final IOSDeploymentTargetMigration iosProjectMigration = IOSDeploymentTargetMigration(
596 597 598 599 600 601 602
          project,
          testLogger,
        );
        expect(iosProjectMigration.migrate(), isTrue);

        expect(xcodeProjectInfoFile.readAsStringSync(), '''
				GCC_WARN_UNUSED_VARIABLE = YES;
603
				IPHONEOS_DEPLOYMENT_TARGET = 11.0;
604 605 606
				MTL_ENABLE_DEBUG_INFO = YES;
				ONLY_ACTIVE_ARCH = YES;

607 608
				IPHONEOS_DEPLOYMENT_TARGET = 11.0;
				IPHONEOS_DEPLOYMENT_TARGET = 11.0;
609 610 611 612 613 614 615 616 617 618
''');

        expect(appFrameworkInfoPlist.readAsStringSync(), '''
  <key>CFBundleShortVersionString</key>
  <string>1.0</string>
  <key>CFBundleSignature</key>
  <string>????</string>
  <key>CFBundleVersion</key>
  <string>1.0</string>
  <key>MinimumOSVersion</key>
619 620 621
  <string>11.0</string>
  <key>MinimumOSVersion</key>
  <string>11.0</string>
622 623
</dict>
</plist>
624 625 626 627 628
''');

        expect(podfile.readAsStringSync(), '''
# platform :ios, '11.0'
platform :ios, '11.0'
629 630
''');
        // Only print once even though 2 lines were changed.
631
        expect('Updating minimum iOS deployment target to 11.0'.allMatches(testLogger.statusText).length, 1);
632 633
      });
    });
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653

    group('update Xcode project object version', () {
      late MemoryFileSystem memoryFileSystem;
      late BufferLogger testLogger;
      late FakeIosProject project;
      late File xcodeProjectInfoFile;
      late File xcodeProjectSchemeFile;

      setUp(() {
        memoryFileSystem = MemoryFileSystem();
        testLogger = BufferLogger.test();
        project = FakeIosProject();
        xcodeProjectInfoFile = memoryFileSystem.file('project.pbxproj');
        project.xcodeProjectInfoFile = xcodeProjectInfoFile;

        xcodeProjectSchemeFile = memoryFileSystem.file('Runner.xcscheme');
        project.xcodeProjectSchemeFile = xcodeProjectSchemeFile;
      });

      testWithoutContext('skipped if files are missing', () {
654
        final XcodeProjectObjectVersionMigration iosProjectMigration = XcodeProjectObjectVersionMigration(
655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670
          project,
          testLogger,
        );
        expect(iosProjectMigration.migrate(), isTrue);
        expect(xcodeProjectInfoFile.existsSync(), isFalse);
        expect(xcodeProjectSchemeFile.existsSync(), isFalse);

        expect(testLogger.traceText, contains('Xcode project not found, skipping Xcode compatibility migration'));
        expect(testLogger.traceText, contains('Runner scheme not found, skipping Xcode compatibility migration'));
        expect(testLogger.statusText, isEmpty);
      });

      testWithoutContext('skipped if nothing to upgrade', () {
        const String xcodeProjectInfoFileContents = '''
	classes = {
	};
671
	objectVersion = 54;
672 673 674 675 676 677 678 679 680 681 682 683 684 685
	objects = {
			attributes = {
				LastUpgradeCheck = 1300;
				ORGANIZATIONNAME = "";
      ''';
        xcodeProjectInfoFile.writeAsStringSync(xcodeProjectInfoFileContents);

        const String xcodeProjectSchemeFileContents = '''
   LastUpgradeVersion = "1300"
''';
        xcodeProjectSchemeFile.writeAsStringSync(xcodeProjectSchemeFileContents);

        final DateTime projectLastModified = xcodeProjectInfoFile.lastModifiedSync();

686
        final XcodeProjectObjectVersionMigration iosProjectMigration = XcodeProjectObjectVersionMigration(
687 688 689 690 691 692 693 694 695 696 697 698
          project,
          testLogger,
        );
        expect(iosProjectMigration.migrate(), isTrue);

        expect(xcodeProjectInfoFile.lastModifiedSync(), projectLastModified);
        expect(xcodeProjectInfoFile.readAsStringSync(), xcodeProjectInfoFileContents);
        expect(xcodeProjectSchemeFile.readAsStringSync(), xcodeProjectSchemeFileContents);

        expect(testLogger.statusText, isEmpty);
      });

699
      testWithoutContext('Xcode project is migrated to newest objectVersion', () {
700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715
        xcodeProjectInfoFile.writeAsStringSync('''
	classes = {
	};
	objectVersion = 46;
	objects = {
			attributes = {
				LastUpgradeCheck = 1020;
				ORGANIZATIONNAME = "";
''');

        xcodeProjectSchemeFile.writeAsStringSync('''
<Scheme
   LastUpgradeVersion = "1020"
   version = "1.3">
''');

716
        final XcodeProjectObjectVersionMigration iosProjectMigration = XcodeProjectObjectVersionMigration(
717 718 719 720 721 722 723 724
          project,
          testLogger,
        );
        expect(iosProjectMigration.migrate(), isTrue);

        expect(xcodeProjectInfoFile.readAsStringSync(), '''
	classes = {
	};
725
	objectVersion = 54;
726 727 728 729 730 731 732 733 734 735 736 737 738 739 740
	objects = {
			attributes = {
				LastUpgradeCheck = 1300;
				ORGANIZATIONNAME = "";
''');

        expect(xcodeProjectSchemeFile.readAsStringSync(), '''
<Scheme
   LastUpgradeVersion = "1300"
   version = "1.3">
''');
        // Only print once even though 3 lines were changed.
        expect('Updating project for Xcode compatibility'.allMatches(testLogger.statusText).length, 1);
      });
    });
741

742
    group('update info.plist migration', () {
743 744 745 746 747 748 749 750 751 752 753 754 755 756
      late MemoryFileSystem memoryFileSystem;
      late BufferLogger testLogger;
      late FakeIosProject project;
      late File infoPlistFile;

      setUp(() {
        memoryFileSystem = MemoryFileSystem();
        testLogger = BufferLogger.test();
        project = FakeIosProject();
        infoPlistFile = memoryFileSystem.file('info.plist');
        project.defaultHostInfoPlist = infoPlistFile;
      });

      testWithoutContext('skipped if files are missing', () {
757
        final HostAppInfoPlistMigration iosProjectMigration = HostAppInfoPlistMigration(
758 759 760 761 762 763
          project,
          testLogger,
        );
        expect(iosProjectMigration.migrate(), isTrue);
        expect(infoPlistFile.existsSync(), isFalse);

764
        expect(testLogger.traceText, contains('Info.plist not found, skipping host app Info.plist migration.'));
765 766 767 768 769 770 771 772 773 774 775
        expect(testLogger.statusText, isEmpty);
      });

      testWithoutContext('skipped if nothing to upgrade', () {
        const String infoPlistFileContent = '''
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>CADisableMinimumFrameDurationOnPhone</key>
	<true/>
776 777
	<key>UIApplicationSupportsIndirectInputEvents</key>
	<true/>
778 779 780 781 782
</dict>
</plist>
''';
        infoPlistFile.writeAsStringSync(infoPlistFileContent);

783
        final HostAppInfoPlistMigration iosProjectMigration = HostAppInfoPlistMigration(
784 785 786 787 788 789 790 791 792 793
          project,
          testLogger,
        );
        final DateTime infoPlistFileLastModified = infoPlistFile.lastModifiedSync();
        expect(iosProjectMigration.migrate(), isTrue);

        expect(infoPlistFile.lastModifiedSync(), infoPlistFileLastModified);
        expect(testLogger.statusText, isEmpty);
      });

794
      testWithoutContext('info.plist is migrated', () {
795 796 797 798 799 800 801 802 803 804
        const String infoPlistFileContent = '''
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
</dict>
</plist>
''';
        infoPlistFile.writeAsStringSync(infoPlistFileContent);

805
        final HostAppInfoPlistMigration iosProjectMigration = HostAppInfoPlistMigration(
806 807 808 809 810 811 812 813 814 815 816
          project,
          testLogger,
        );
        expect(iosProjectMigration.migrate(), isTrue);
        expect(infoPlistFile.readAsStringSync(), equals('''
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>CADisableMinimumFrameDurationOnPhone</key>
	<true/>
817 818
	<key>UIApplicationSupportsIndirectInputEvents</key>
	<true/>
819 820 821 822 823
</dict>
</plist>
'''));
      });
    });
824
  });
825

826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925
  group('update Xcode script build phase', () {
    late MemoryFileSystem memoryFileSystem;
    late BufferLogger testLogger;
    late FakeIosProject project;
    late File xcodeProjectInfoFile;

    setUp(() {
      memoryFileSystem = MemoryFileSystem();
      testLogger = BufferLogger.test();
      project = FakeIosProject();
      xcodeProjectInfoFile = memoryFileSystem.file('project.pbxproj');
      project.xcodeProjectInfoFile = xcodeProjectInfoFile;
    });

    testWithoutContext('skipped if files are missing', () {
      final XcodeScriptBuildPhaseMigration iosProjectMigration = XcodeScriptBuildPhaseMigration(
        project,
        testLogger,
      );
      expect(iosProjectMigration.migrate(), isTrue);
      expect(xcodeProjectInfoFile.existsSync(), isFalse);

      expect(testLogger.traceText, contains('Xcode project not found, skipping script build phase dependency analysis removal'));
      expect(testLogger.statusText, isEmpty);
    });

    testWithoutContext('skipped if nothing to upgrade', () {
      const String xcodeProjectInfoFileContents = '''
/* Begin PBXShellScriptBuildPhase section */
		3B06AD1E1E4923F5004D2608 /* Thin Binary */ = {
			isa = PBXShellScriptBuildPhase;
			alwaysOutOfDate = 1;
			buildActionMask = 2147483647;
			files = (
			);
			inputPaths = (
      ''';
      xcodeProjectInfoFile.writeAsStringSync(xcodeProjectInfoFileContents);

      final DateTime projectLastModified = xcodeProjectInfoFile.lastModifiedSync();

      final XcodeScriptBuildPhaseMigration iosProjectMigration = XcodeScriptBuildPhaseMigration(
        project,
        testLogger,
      );
      expect(iosProjectMigration.migrate(), isTrue);

      expect(xcodeProjectInfoFile.lastModifiedSync(), projectLastModified);
      expect(xcodeProjectInfoFile.readAsStringSync(), xcodeProjectInfoFileContents);

      expect(testLogger.statusText, isEmpty);
    });

    testWithoutContext('alwaysOutOfDate is migrated', () {
      xcodeProjectInfoFile.writeAsStringSync('''
/* Begin PBXShellScriptBuildPhase section */
		3B06AD1E1E4923F5004D2608 /* Thin Binary */ = {
			isa = PBXShellScriptBuildPhase;
			buildActionMask = 2147483647;
			files = (
			);
			inputPaths = (

		9740EEB61CF901F6004384FC /* Run Script */ = {
			isa = PBXShellScriptBuildPhase;
			buildActionMask = 2147483647;
			files = (
			);
			inputPaths = (
			);
''');

      final XcodeScriptBuildPhaseMigration iosProjectMigration = XcodeScriptBuildPhaseMigration(
        project,
        testLogger,
      );
      expect(iosProjectMigration.migrate(), isTrue);

      expect(xcodeProjectInfoFile.readAsStringSync(), '''
/* Begin PBXShellScriptBuildPhase section */
		3B06AD1E1E4923F5004D2608 /* Thin Binary */ = {
			isa = PBXShellScriptBuildPhase;
			alwaysOutOfDate = 1;
			buildActionMask = 2147483647;
			files = (
			);
			inputPaths = (

		9740EEB61CF901F6004384FC /* Run Script */ = {
			isa = PBXShellScriptBuildPhase;
			alwaysOutOfDate = 1;
			buildActionMask = 2147483647;
			files = (
			);
			inputPaths = (
			);
''');
      expect(testLogger.statusText, contains('Removing script build phase dependency analysis'));
    });
  });
926 927
}

928 929
class FakeIosProject extends Fake implements IosProject {
  @override
930
  File xcodeProjectWorkspaceData = MemoryFileSystem.test().file('xcodeProjectWorkspaceData');
931 932

  @override
933
  File xcodeWorkspaceSharedSettings = MemoryFileSystem.test().file('xcodeWorkspaceSharedSettings');
934 935

  @override
936
  File xcodeProjectInfoFile = MemoryFileSystem.test().file('xcodeProjectInfoFile');
937

938 939 940
  @override
  File xcodeProjectSchemeFile = MemoryFileSystem.test().file('xcodeProjectSchemeFile');

941 942
  @override
  File appFrameworkInfoPlist = MemoryFileSystem.test().file('appFrameworkInfoPlist');
943 944 945

  @override
  File defaultHostInfoPlist = MemoryFileSystem.test().file('defaultHostInfoPlist');
946 947 948

  @override
  File podfile = MemoryFileSystem.test().file('Podfile');
949
}
950

951
class FakeIOSMigrator extends ProjectMigrator {
952 953
  FakeIOSMigrator({required this.succeeds})
    : super(BufferLogger.test());
954 955 956 957 958 959 960 961 962 963 964 965 966

  final bool succeeds;

  @override
  bool migrate() {
    return succeeds;
  }

  @override
  String migrateLine(String line) {
    return line;
  }
}