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

5
import 'package:file/memory.dart';
6
import 'package:flutter_tools/src/artifacts.dart';
7
import 'package:flutter_tools/src/base/io.dart' show ProcessException, ProcessResult;
8
import 'package:flutter_tools/src/base/logger.dart';
9
import 'package:flutter_tools/src/base/platform.dart';
10
import 'package:flutter_tools/src/build_info.dart';
11
import 'package:flutter_tools/src/cache.dart';
12
import 'package:flutter_tools/src/ios/devices.dart';
13 14 15 16 17
import 'package:flutter_tools/src/ios/xcodeproj.dart';
import 'package:flutter_tools/src/macos/xcode.dart';
import 'package:mockito/mockito.dart';
import 'package:process/process.dart';

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

void main() {
22 23 24
  Logger logger;

  setUp(() {
25
    logger = BufferLogger.test();
26 27
  });

28 29 30 31
  // Group exists to work around https://github.com/flutter/flutter/issues/56415.
  // Do not add more `MockProcessManager` tests.
  group('MockProcessManager', () {
    ProcessManager processManager;
32 33

    setUp(() {
34
      processManager = MockProcessManager();
35 36
    });

37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
    group('Xcode', () {
      Xcode xcode;

      setUp(() {
        xcode = Xcode(
          logger: logger,
          platform: MockPlatform(),
          fileSystem: MemoryFileSystem.test(),
          processManager: processManager,
          xcodeProjectInterpreter: MockXcodeProjectInterpreter(),
        );
      });

      testWithoutContext('xcodeSelectPath returns null when xcode-select is not installed', () {
        when(processManager.runSync(<String>['/usr/bin/xcode-select', '--print-path']))
52
          .thenThrow(const ProcessException('/usr/bin/xcode-select', <String>['--print-path']));
53 54
        expect(xcode.xcodeSelectPath, isNull);
        when(processManager.runSync(<String>['/usr/bin/xcode-select', '--print-path']))
55 56
          .thenThrow(ArgumentError('Invalid argument(s): Cannot find executable for /usr/bin/xcode-select'));

57 58
        expect(xcode.xcodeSelectPath, isNull);
      });
59

60 61 62
      testWithoutContext('eulaSigned is false when clang is not installed', () {
        when(processManager.runSync(<String>['/usr/bin/xcrun', 'clang']))
          .thenThrow(const ProcessException('/usr/bin/xcrun', <String>['clang']));
63

64 65
        expect(xcode.eulaSigned, isFalse);
      });
66 67
    });

68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
    group('xcdevice', () {
      XCDevice xcdevice;
      MockXcode mockXcode;

      setUp(() {
        mockXcode = MockXcode();
        xcdevice = XCDevice(
          processManager: processManager,
          logger: logger,
          xcode: mockXcode,
          platform: null,
          artifacts: MockArtifacts(),
          cache: MockCache(),
        );
      });
83

84 85
      testWithoutContext("xcrun can't find xcdevice", () {
        when(mockXcode.isInstalledAndMeetsVersionCheck).thenReturn(true);
86

87 88 89 90 91
        when(processManager.runSync(<String>['xcrun', '--find', 'xcdevice']))
          .thenThrow(const ProcessException('xcrun', <String>['--find', 'xcdevice']));
        expect(xcdevice.isInstalled, false);
        verify(processManager.runSync(any)).called(1);
      });
92

93 94
      testWithoutContext('available devices xcdevice fails', () async {
        when(mockXcode.isInstalledAndMeetsVersionCheck).thenReturn(true);
95

96 97
        when(processManager.runSync(<String>['xcrun', '--find', 'xcdevice']))
          .thenReturn(ProcessResult(1, 0, '/path/to/xcdevice', ''));
98

99 100
        when(processManager.run(<String>['xcrun', 'xcdevice', 'list', '--timeout', '2']))
          .thenThrow(const ProcessException('xcrun', <String>['xcdevice', 'list', '--timeout', '2']));
101

102 103
        expect(await xcdevice.getAvailableTetheredIOSDevices(), isEmpty);
      });
104

105 106
      testWithoutContext('diagnostics xcdevice fails', () async {
        when(mockXcode.isInstalledAndMeetsVersionCheck).thenReturn(true);
107

108 109
        when(processManager.runSync(<String>['xcrun', '--find', 'xcdevice']))
          .thenReturn(ProcessResult(1, 0, '/path/to/xcdevice', ''));
110

111 112
        when(processManager.run(<String>['xcrun', 'xcdevice', 'list', '--timeout', '2']))
          .thenThrow(const ProcessException('xcrun', <String>['xcdevice', 'list', '--timeout', '2']));
113

114 115
        expect(await xcdevice.getDiagnostics(), isEmpty);
      });
116
    });
117
  });
118

119 120
  group('FakeProcessManager', () {
    FakeProcessManager fakeProcessManager;
121

122 123
    setUp(() {
      fakeProcessManager = FakeProcessManager.list(<FakeCommand>[]);
124 125
    });

126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
    group('Xcode', () {
      Xcode xcode;
      MockXcodeProjectInterpreter mockXcodeProjectInterpreter;
      MockPlatform platform;

      setUp(() {
        mockXcodeProjectInterpreter = MockXcodeProjectInterpreter();
        platform = MockPlatform();
        xcode = Xcode(
          logger: logger,
          platform: platform,
          fileSystem: MemoryFileSystem.test(),
          processManager: fakeProcessManager,
          xcodeProjectInterpreter: mockXcodeProjectInterpreter,
        );
      });
142

143 144 145 146 147 148
      testWithoutContext('xcodeSelectPath returns path when xcode-select is installed', () {
        const String xcodePath = '/Applications/Xcode8.0.app/Contents/Developer';
        fakeProcessManager.addCommand(const FakeCommand(
          command: <String>['/usr/bin/xcode-select', '--print-path'],
          stdout: xcodePath,
        ));
149

150 151 152
        expect(xcode.xcodeSelectPath, xcodePath);
        expect(fakeProcessManager.hasRemainingExpectations, isFalse);
      });
153

154 155 156 157
      testWithoutContext('xcodeVersionSatisfactory is false when version is less than minimum', () {
        when(mockXcodeProjectInterpreter.isInstalled).thenReturn(true);
        when(mockXcodeProjectInterpreter.majorVersion).thenReturn(9);
        when(mockXcodeProjectInterpreter.minorVersion).thenReturn(0);
158

159 160
        expect(xcode.isVersionSatisfactory, isFalse);
      });
161

162 163
      testWithoutContext('xcodeVersionSatisfactory is false when xcodebuild tools are not installed', () {
        when(mockXcodeProjectInterpreter.isInstalled).thenReturn(false);
164

165 166
        expect(xcode.isVersionSatisfactory, isFalse);
      });
167

168 169 170 171
      testWithoutContext('xcodeVersionSatisfactory is true when version meets minimum', () {
        when(mockXcodeProjectInterpreter.isInstalled).thenReturn(true);
        when(mockXcodeProjectInterpreter.majorVersion).thenReturn(11);
        when(mockXcodeProjectInterpreter.minorVersion).thenReturn(0);
172

173 174
        expect(xcode.isVersionSatisfactory, isTrue);
      });
175

176 177 178 179
      testWithoutContext('xcodeVersionSatisfactory is true when major version exceeds minimum', () {
        when(mockXcodeProjectInterpreter.isInstalled).thenReturn(true);
        when(mockXcodeProjectInterpreter.majorVersion).thenReturn(12);
        when(mockXcodeProjectInterpreter.minorVersion).thenReturn(0);
180

181 182
        expect(xcode.isVersionSatisfactory, isTrue);
      });
183

184 185 186 187
      testWithoutContext('xcodeVersionSatisfactory is true when minor version exceeds minimum', () {
        when(mockXcodeProjectInterpreter.isInstalled).thenReturn(true);
        when(mockXcodeProjectInterpreter.majorVersion).thenReturn(11);
        when(mockXcodeProjectInterpreter.minorVersion).thenReturn(3);
188

189 190
        expect(xcode.isVersionSatisfactory, isTrue);
      });
191

192 193
      testWithoutContext('isInstalledAndMeetsVersionCheck is false when not macOS', () {
        when(platform.isMacOS).thenReturn(false);
194

195
        expect(xcode.isInstalledAndMeetsVersionCheck, isFalse);
196 197
      });

198 199 200 201 202 203 204
      testWithoutContext('isInstalledAndMeetsVersionCheck is false when not installed', () {
        when(platform.isMacOS).thenReturn(true);
        fakeProcessManager.addCommand(const FakeCommand(
          command: <String>['/usr/bin/xcode-select', '--print-path'],
          stdout: '/Applications/Xcode8.0.app/Contents/Developer',
        ));
        when(mockXcodeProjectInterpreter.isInstalled).thenReturn(false);
205

206 207
        expect(xcode.isInstalledAndMeetsVersionCheck, isFalse);
        expect(fakeProcessManager.hasRemainingExpectations, isFalse);
208 209
      });

210 211 212 213 214 215 216 217 218 219 220 221 222 223
      testWithoutContext('isInstalledAndMeetsVersionCheck is false when no xcode-select', () {
        when(platform.isMacOS).thenReturn(true);
        fakeProcessManager.addCommand(const FakeCommand(
          command: <String>['/usr/bin/xcode-select', '--print-path'],
          exitCode: 127,
          stderr: 'ERROR',
        ));
        when(mockXcodeProjectInterpreter.isInstalled).thenReturn(true);
        when(mockXcodeProjectInterpreter.majorVersion).thenReturn(11);
        when(mockXcodeProjectInterpreter.minorVersion).thenReturn(0);

        expect(xcode.isInstalledAndMeetsVersionCheck, isFalse);
        expect(fakeProcessManager.hasRemainingExpectations, isFalse);
      });
224

225 226 227 228 229 230 231 232 233 234 235 236
      testWithoutContext('isInstalledAndMeetsVersionCheck is false when version not satisfied', () {
        when(platform.isMacOS).thenReturn(true);
        fakeProcessManager.addCommand(const FakeCommand(
          command: <String>['/usr/bin/xcode-select', '--print-path'],
          stdout: '/Applications/Xcode8.0.app/Contents/Developer',
        ));
        when(mockXcodeProjectInterpreter.isInstalled).thenReturn(true);
        when(mockXcodeProjectInterpreter.majorVersion).thenReturn(10);
        when(mockXcodeProjectInterpreter.minorVersion).thenReturn(2);

        expect(xcode.isInstalledAndMeetsVersionCheck, isFalse);
        expect(fakeProcessManager.hasRemainingExpectations, isFalse);
237
      });
238

239 240 241 242 243 244 245 246 247 248 249 250 251
      testWithoutContext('isInstalledAndMeetsVersionCheck is true when macOS and installed and version is satisfied', () {
        when(platform.isMacOS).thenReturn(true);
        fakeProcessManager.addCommand(const FakeCommand(
          command: <String>['/usr/bin/xcode-select', '--print-path'],
          stdout: '/Applications/Xcode8.0.app/Contents/Developer',
        ));
        when(mockXcodeProjectInterpreter.isInstalled).thenReturn(true);
        when(mockXcodeProjectInterpreter.majorVersion).thenReturn(11);
        when(mockXcodeProjectInterpreter.minorVersion).thenReturn(0);

        expect(xcode.isInstalledAndMeetsVersionCheck, isTrue);
        expect(fakeProcessManager.hasRemainingExpectations, isFalse);
      });
252

253 254 255 256 257 258
      testWithoutContext('eulaSigned is false when clang output indicates EULA not yet accepted', () {
        fakeProcessManager.addCommand(const FakeCommand(
          command: <String>['/usr/bin/xcrun', 'clang'],
          exitCode: 1,
          stderr: 'Xcode EULA has not been accepted.\nLaunch Xcode and accept the license.',
        ));
259

260 261
        expect(xcode.eulaSigned, isFalse);
        expect(fakeProcessManager.hasRemainingExpectations, isFalse);
262 263
      });

264 265 266 267 268 269
      testWithoutContext('eulaSigned is true when clang output indicates EULA has been accepted', () {
        fakeProcessManager.addCommand(const FakeCommand(
          command: <String>['/usr/bin/xcrun', 'clang'],
          exitCode: 1,
          stderr: 'clang: error: no input files',
        ));
270

271 272
        expect(xcode.eulaSigned, isTrue);
        expect(fakeProcessManager.hasRemainingExpectations, isFalse);
273 274
      });

275 276 277 278 279
      testWithoutContext('SDK name', () {
        expect(getNameForSdk(SdkType.iPhone), 'iphoneos');
        expect(getNameForSdk(SdkType.iPhoneSimulator), 'iphonesimulator');
        expect(getNameForSdk(SdkType.macOS), 'macosx');
      });
280

281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
      group('SDK location', () {
        const String sdkroot = 'Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS13.2.sdk';

        testWithoutContext('--show-sdk-path iphoneos', () async {
          fakeProcessManager.addCommand(const FakeCommand(
            command: <String>['xcrun', '--sdk', 'iphoneos', '--show-sdk-path'],
            stdout: sdkroot,
          ));

          expect(await xcode.sdkLocation(SdkType.iPhone), sdkroot);
          expect(fakeProcessManager.hasRemainingExpectations, isFalse);
        });

        testWithoutContext('--show-sdk-path macosx', () async {
          fakeProcessManager.addCommand(const FakeCommand(
            command: <String>['xcrun', '--sdk', 'macosx', '--show-sdk-path'],
            stdout: sdkroot,
          ));

          expect(await xcode.sdkLocation(SdkType.macOS), sdkroot);
          expect(fakeProcessManager.hasRemainingExpectations, isFalse);
        });

        testWithoutContext('--show-sdk-path fails', () async {
          fakeProcessManager.addCommand(const FakeCommand(
            command: <String>['xcrun', '--sdk', 'iphoneos', '--show-sdk-path'],
            exitCode: 1,
            stderr: 'xcrun: error:',
          ));

          expect(() async => await xcode.sdkLocation(SdkType.iPhone),
            throwsToolExit(message: 'Could not find SDK location'));
          expect(fakeProcessManager.hasRemainingExpectations, isFalse);
        });
315 316 317
      });
    });

318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
    group('xcdevice', () {
      XCDevice xcdevice;
      MockXcode mockXcode;
      MockArtifacts mockArtifacts;
      MockCache mockCache;

      setUp(() {
        mockXcode = MockXcode();
        mockArtifacts = MockArtifacts();
        mockCache = MockCache();
        xcdevice = XCDevice(
          processManager: fakeProcessManager,
          logger: logger,
          xcode: mockXcode,
          platform: null,
          artifacts: mockArtifacts,
          cache: mockCache,
        );
336 337
      });

338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
      group('installed', () {
        testWithoutContext('Xcode not installed', () {
          when(mockXcode.isInstalledAndMeetsVersionCheck).thenReturn(false);
          expect(xcdevice.isInstalled, false);
        });

        testWithoutContext('is installed', () {
          when(mockXcode.isInstalledAndMeetsVersionCheck).thenReturn(true);
          fakeProcessManager.addCommand(const FakeCommand(
            command: <String>['xcrun', '--find', 'xcdevice'],
            stdout: '/path/to/xcdevice',
          ));

          expect(xcdevice.isInstalled, true);
          expect(fakeProcessManager.hasRemainingExpectations, isFalse);
        });
      });
355

356 357
      group('available devices', () {
        final FakePlatform macPlatform = FakePlatform(operatingSystem: 'macos');
358

359 360
        testWithoutContext('Xcode not installed', () async {
          when(mockXcode.isInstalledAndMeetsVersionCheck).thenReturn(false);
361

362 363
          expect(await xcdevice.getAvailableTetheredIOSDevices(), isEmpty);
        });
364

365 366 367 368 369 370
        testUsingContext('returns devices', () async {
          when(mockXcode.isInstalledAndMeetsVersionCheck).thenReturn(true);
          fakeProcessManager.addCommand(const FakeCommand(
            command: <String>['xcrun', '--find', 'xcdevice'],
            stdout: '/path/to/xcdevice',
          ));
371

372
          const String devicesOutput = '''
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
[
  {
    "simulator" : true,
    "operatingSystemVersion" : "13.3 (17K446)",
    "available" : true,
    "platform" : "com.apple.platform.appletvsimulator",
    "modelCode" : "AppleTV5,3",
    "identifier" : "CBB5E1ED-2172-446E-B4E7-F2B5823DBBA6",
    "architecture" : "x86_64",
    "modelName" : "Apple TV",
    "name" : "Apple TV"
  },
  {
    "simulator" : false,
    "operatingSystemVersion" : "13.3 (17C54)",
    "interface" : "usb",
    "available" : true,
    "platform" : "com.apple.platform.iphoneos",
    "modelCode" : "iPhone8,1",
    "identifier" : "d83d5bc53967baa0ee18626ba87b6254b2ab5418",
    "architecture" : "arm64",
    "modelName" : "iPhone 6s",
    "name" : "An iPhone (Space Gray)"
  },
  {
    "simulator" : false,
    "operatingSystemVersion" : "10.1 (14C54)",
    "interface" : "usb",
    "available" : true,
    "platform" : "com.apple.platform.iphoneos",
    "modelCode" : "iPad11,4",
    "identifier" : "98206e7a4afd4aedaff06e687594e089dede3c44",
    "architecture" : "armv7",
    "modelName" : "iPad Air 3rd Gen",
    "name" : "iPad 1"
  },
  {
    "simulator" : false,
    "operatingSystemVersion" : "10.1 (14C54)",
    "interface" : "network",
    "available" : true,
    "platform" : "com.apple.platform.iphoneos",
    "modelCode" : "iPad11,4",
    "identifier" : "234234234234234234345445687594e089dede3c44",
    "architecture" : "arm64",
    "modelName" : "iPad Air 3rd Gen",
    "name" : "A networked iPad"
  },
  {
    "simulator" : false,
    "operatingSystemVersion" : "10.1 (14C54)",
    "interface" : "usb",
    "available" : true,
    "platform" : "com.apple.platform.iphoneos",
    "modelCode" : "iPad11,4",
    "identifier" : "f577a7903cc54959be2e34bc4f7f80b7009efcf4",
    "architecture" : "BOGUS",
    "modelName" : "iPad Air 3rd Gen",
    "name" : "iPad 2"
  },
  {
    "simulator" : true,
    "operatingSystemVersion" : "6.1.1 (17S445)",
    "available" : true,
    "platform" : "com.apple.platform.watchsimulator",
    "modelCode" : "Watch5,4",
    "identifier" : "2D74FB11-88A0-44D0-B81E-C0C142B1C94A",
    "architecture" : "i386",
    "modelName" : "Apple Watch Series 5 - 44mm",
    "name" : "Apple Watch Series 5 - 44mm"
  },
  {
    "simulator" : false,
    "operatingSystemVersion" : "13.3 (17C54)",
    "interface" : "usb",
    "available" : false,
    "platform" : "com.apple.platform.iphoneos",
    "modelCode" : "iPhone8,1",
    "identifier" : "c4ca6f7a53027d1b7e4972e28478e7a28e2faee2",
    "architecture" : "arm64",
    "modelName" : "iPhone 6s",
    "name" : "iPhone",
    "error" : {
      "code" : -9,
      "failureReason" : "",
      "description" : "iPhone is not paired with your computer.",
      "domain" : "com.apple.platform.iphoneos"
    }
  }
]
''';

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 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
          fakeProcessManager.addCommand(const FakeCommand(
            command: <String>['xcrun', 'xcdevice', 'list', '--timeout', '2'],
            stdout: devicesOutput,
          ));
          final List<IOSDevice> devices = await xcdevice.getAvailableTetheredIOSDevices();
          expect(devices, hasLength(3));
          expect(devices[0].id, 'd83d5bc53967baa0ee18626ba87b6254b2ab5418');
          expect(devices[0].name, 'An iPhone (Space Gray)');
          expect(await devices[0].sdkNameAndVersion, 'iOS 13.3');
          expect(devices[0].cpuArchitecture, DarwinArch.arm64);
          expect(devices[1].id, '98206e7a4afd4aedaff06e687594e089dede3c44');
          expect(devices[1].name, 'iPad 1');
          expect(await devices[1].sdkNameAndVersion, 'iOS 10.1');
          expect(devices[1].cpuArchitecture, DarwinArch.armv7);
          expect(devices[2].id, 'f577a7903cc54959be2e34bc4f7f80b7009efcf4');
          expect(devices[2].name, 'iPad 2');
          expect(await devices[2].sdkNameAndVersion, 'iOS 10.1');
          expect(devices[2].cpuArchitecture, DarwinArch.arm64); // Defaults to arm64 for unknown architecture.
          expect(fakeProcessManager.hasRemainingExpectations, isFalse);
        }, overrides: <Type, Generator>{
          Platform: () => macPlatform,
        });

        testWithoutContext('uses timeout', () async {
          when(mockXcode.isInstalledAndMeetsVersionCheck).thenReturn(true);
          fakeProcessManager.addCommand(const FakeCommand(
            command: <String>['xcrun', '--find', 'xcdevice'],
            stdout: '/path/to/xcdevice',
          ));

          fakeProcessManager.addCommand(const FakeCommand(
            command: <String>['xcrun', 'xcdevice', 'list', '--timeout', '20'],
            stdout: '[]',
          ));
          await xcdevice.getAvailableTetheredIOSDevices(timeout: const Duration(seconds: 20));
          expect(fakeProcessManager.hasRemainingExpectations, isFalse);
        });

        testUsingContext('ignores "Preparing debugger support for iPhone" error', () async {
          when(mockXcode.isInstalledAndMeetsVersionCheck).thenReturn(true);
          fakeProcessManager.addCommand(const FakeCommand(
            command: <String>['xcrun', '--find', 'xcdevice'],
            stdout: '/path/to/xcdevice',
          ));

          const String devicesOutput = '''
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
[
  {
    "simulator" : false,
    "operatingSystemVersion" : "13.3 (17C54)",
    "interface" : "usb",
    "available" : false,
    "platform" : "com.apple.platform.iphoneos",
    "modelCode" : "iPhone8,1",
    "identifier" : "43ad2fda7991b34fe1acbda82f9e2fd3d6ddc9f7",
    "architecture" : "arm64",
    "modelName" : "iPhone 6s",
    "name" : "iPhone",
    "error" : {
      "code" : -10,
      "failureReason" : "",
      "description" : "iPhone is busy: Preparing debugger support for iPhone",
      "recoverySuggestion" : "Xcode will continue when iPhone is finished.",
      "domain" : "com.apple.platform.iphoneos"
    }
  }
]
''';

534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
          fakeProcessManager.addCommand(const FakeCommand(
            command: <String>['xcrun', 'xcdevice', 'list', '--timeout', '2'],
            stdout: devicesOutput,
          ));
          final List<IOSDevice> devices = await xcdevice.getAvailableTetheredIOSDevices();
          expect(devices, hasLength(1));
          expect(devices[0].id, '43ad2fda7991b34fe1acbda82f9e2fd3d6ddc9f7');
          expect(fakeProcessManager.hasRemainingExpectations, isFalse);
        }, overrides: <Type, Generator>{
          Platform: () => macPlatform,
        });

        testUsingContext('handles unknown architectures', () async {
          when(mockXcode.isInstalledAndMeetsVersionCheck).thenReturn(true);
          fakeProcessManager.addCommand(const FakeCommand(
            command: <String>['xcrun', '--find', 'xcdevice'],
            stdout: '/path/to/xcdevice',
          ));

          const String devicesOutput = '''
554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581
[
  {
    "simulator" : false,
    "operatingSystemVersion" : "13.3 (17C54)",
    "interface" : "usb",
    "available" : true,
    "platform" : "com.apple.platform.iphoneos",
    "modelCode" : "iPhone8,1",
    "identifier" : "d83d5bc53967baa0ee18626ba87b6254b2ab5418",
    "architecture" : "armv7x",
    "modelName" : "iPad 3 BOGUS",
    "name" : "iPad"
  },
  {
    "simulator" : false,
    "operatingSystemVersion" : "13.3 (17C54)",
    "interface" : "usb",
    "available" : true,
    "platform" : "com.apple.platform.iphoneos",
    "modelCode" : "iPhone8,1",
    "identifier" : "d83d5bc53967baa0ee18626ba87b6254b2ab5418",
    "architecture" : "BOGUS",
    "modelName" : "Future iPad",
    "name" : "iPad"
  }
]
''';

582 583 584 585 586 587 588 589 590 591 592
          fakeProcessManager.addCommand(const FakeCommand(
            command: <String>['xcrun', 'xcdevice', 'list', '--timeout', '2'],
            stdout: devicesOutput,
          ));
          final List<IOSDevice> devices = await xcdevice.getAvailableTetheredIOSDevices();
          expect(devices[0].cpuArchitecture, DarwinArch.armv7);
          expect(devices[1].cpuArchitecture, DarwinArch.arm64);
          expect(fakeProcessManager.hasRemainingExpectations, isFalse);
        }, overrides: <Type, Generator>{
          Platform: () => macPlatform,
        });
593
      });
594

595 596
      group('diagnostics', () {
        final FakePlatform macPlatform = FakePlatform(operatingSystem: 'macos');
597

598 599
        testWithoutContext('Xcode not installed', () async {
          when(mockXcode.isInstalledAndMeetsVersionCheck).thenReturn(false);
600

601 602
          expect(await xcdevice.getDiagnostics(), isEmpty);
        });
603

604 605 606 607 608 609
        testUsingContext('uses cache', () async {
          when(mockXcode.isInstalledAndMeetsVersionCheck).thenReturn(true);
          fakeProcessManager.addCommand(const FakeCommand(
            command: <String>['xcrun', '--find', 'xcdevice'],
            stdout: '/path/to/xcdevice',
          ));
610

611
          const String devicesOutput = '''
612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631
[
  {
    "simulator" : false,
    "operatingSystemVersion" : "13.3 (17C54)",
    "interface" : "network",
    "available" : false,
    "platform" : "com.apple.platform.iphoneos",
    "modelCode" : "iPhone8,1",
    "identifier" : "d83d5bc53967baa0ee18626ba87b6254b2ab5418",
    "architecture" : "arm64",
    "modelName" : "iPhone 6s",
    "error" : {
      "code" : -13,
      "failureReason" : "",
      "domain" : "com.apple.platform.iphoneos"
    }
  }
]
''';

632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652
          fakeProcessManager.addCommand(const FakeCommand(
            command: <String>['xcrun', 'xcdevice', 'list', '--timeout', '2'],
            stdout: devicesOutput,
          ));

          await xcdevice.getAvailableTetheredIOSDevices();
          final List<String> errors = await xcdevice.getDiagnostics();
          expect(errors, hasLength(1));
          expect(fakeProcessManager.hasRemainingExpectations, isFalse);
        }, overrides: <Type, Generator>{
          Platform: () => macPlatform,
        });

        testUsingContext('returns error message', () async {
          when(mockXcode.isInstalledAndMeetsVersionCheck).thenReturn(true);
          fakeProcessManager.addCommand(const FakeCommand(
            command: <String>['xcrun', '--find', 'xcdevice'],
            stdout: '/path/to/xcdevice',
          ));

          const String devicesOutput = '''
653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736
[
   {
    "simulator" : false,
    "operatingSystemVersion" : "13.3 (17C54)",
    "interface" : "usb",
    "available" : false,
    "platform" : "com.apple.platform.iphoneos",
    "modelCode" : "iPhone8,1",
    "identifier" : "98206e7a4afd4aedaff06e687594e089dede3c44",
    "architecture" : "arm64",
    "modelName" : "iPhone 6s",
    "name" : "An iPhone (Space Gray)",
    "error" : {
      "code" : -9,
      "failureReason" : "",
      "underlyingErrors" : [
        {
          "code" : 5,
          "failureReason" : "allowsSecureServices: 1. isConnected: 0. Platform: <DVTPlatform:0x7f804ce32880:'com.apple.platform.iphoneos':<DVTFilePath:0x7f804ce32800:'\/Users\/Applications\/Xcode.app\/Contents\/Developer\/Platforms\/iPhoneOS.platform'>>. DTDKDeviceIdentifierIsIDID: 0",
          "description" : "📱<DVTiOSDevice (0x7f801f190450), iPhone, iPhone, 13.3 (17C54), d83d5bc53967baa0ee18626ba87b6254b2ab5418> -- Failed _shouldMakeReadyForDevelopment check even though device is not locked by passcode.",
          "recoverySuggestion" : "",
          "domain" : "com.apple.platform.iphoneos"
        }
      ],
      "description" : "iPhone is not paired with your computer.",
      "recoverySuggestion" : "To use iPhone with Xcode, unlock it and choose to trust this computer when prompted.",
      "domain" : "com.apple.platform.iphoneos"
    }
  },
  {
    "simulator" : false,
    "operatingSystemVersion" : "13.3 (17C54)",
    "interface" : "usb",
    "available" : false,
    "platform" : "com.apple.platform.iphoneos",
    "modelCode" : "iPhone8,1",
    "identifier" : "d83d5bc53967baa0ee18626ba87b6254b2ab5418",
    "architecture" : "arm64",
    "modelName" : "iPhone 6s",
    "name" : "iPhone",
    "error" : {
      "failureReason" : "",
      "description" : "iPhone is not paired with your computer",
      "domain" : "com.apple.platform.iphoneos"
    }
  },
  {
    "simulator" : false,
    "operatingSystemVersion" : "13.3 (17C54)",
    "interface" : "network",
    "available" : false,
    "platform" : "com.apple.platform.iphoneos",
    "modelCode" : "iPhone8,1",
    "identifier" : "d83d5bc53967baa0ee18626ba87b6254b2ab5418",
    "architecture" : "arm64",
    "modelName" : "iPhone 6s",
    "error" : {
      "code" : -13,
      "failureReason" : "",
      "domain" : "com.apple.platform.iphoneos"
    }
  },
  {
    "simulator" : false,
    "operatingSystemVersion" : "13.3 (17C54)",
    "interface" : "usb",
    "available" : false,
    "platform" : "com.apple.platform.iphoneos",
    "modelCode" : "iPhone8,1",
    "identifier" : "43ad2fda7991b34fe1acbda82f9e2fd3d6ddc9f7",
    "architecture" : "arm64",
    "modelName" : "iPhone 6s",
    "name" : "iPhone",
    "error" : {
      "code" : -10,
      "failureReason" : "",
      "description" : "iPhone is busy: Preparing debugger support for iPhone",
      "recoverySuggestion" : "Xcode will continue when iPhone is finished.",
      "domain" : "com.apple.platform.iphoneos"
    }
  }
]
''';

737 738 739 740 741 742 743 744 745 746 747 748 749 750 751
          fakeProcessManager.addCommand(const FakeCommand(
            command: <String>['xcrun', 'xcdevice', 'list', '--timeout', '2'],
            stdout: devicesOutput,
          ));

          final List<String> errors = await xcdevice.getDiagnostics();
          expect(errors, hasLength(4));
          expect(errors[0], 'Error: iPhone is not paired with your computer. To use iPhone with Xcode, unlock it and choose to trust this computer when prompted. (code -9)');
          expect(errors[1], 'Error: iPhone is not paired with your computer.');
          expect(errors[2], 'Error: Xcode pairing error. (code -13)');
          expect(errors[3], 'Error: iPhone is busy: Preparing debugger support for iPhone. Xcode will continue when iPhone is finished. (code -10)');
          expect(fakeProcessManager.hasRemainingExpectations, isFalse);
        }, overrides: <Type, Generator>{
          Platform: () => macPlatform,
        });
752 753
      });
    });
754 755
  });
}
756

757
class MockXcode extends Mock implements Xcode {}
758 759 760
class MockProcessManager extends Mock implements ProcessManager {}
class MockXcodeProjectInterpreter extends Mock implements XcodeProjectInterpreter {}
class MockPlatform extends Mock implements Platform {}
761 762
class MockArtifacts extends Mock implements Artifacts {}
class MockCache extends Mock implements Cache {}