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

5 6
// @dart = 2.8

7
import 'package:file/file.dart';
8
import 'package:file/memory.dart';
9
import 'package:flutter_tools/src/artifacts.dart';
10
import 'package:flutter_tools/src/base/file_system.dart';
11
import 'package:flutter_tools/src/base/logger.dart';
12
import 'package:flutter_tools/src/base/process.dart';
13
import 'package:flutter_tools/src/build_info.dart';
14
import 'package:flutter_tools/src/cache.dart';
15
import 'package:flutter_tools/src/ios/devices.dart';
16
import 'package:flutter_tools/src/ios/mac.dart';
17
import 'package:flutter_tools/src/project.dart';
18
import 'package:flutter_tools/src/reporting/reporting.dart';
19
import 'package:test/fake.dart';
20

21
import '../../src/common.dart';
22
import '../../src/fake_process_manager.dart';
23
import '../../src/fakes.dart';
24 25

void main() {
26 27 28 29 30 31
  BufferLogger logger;

  setUp(() {
    logger = BufferLogger.test();
  });

32
  group('IMobileDevice', () {
33 34
    Artifacts artifacts;
    Cache cache;
35 36

    setUp(() {
37 38 39 40
      artifacts = Artifacts.test();
      cache = Cache.test(
        artifacts: <ArtifactSet>[
          FakeDyldEnvironmentArtifact(),
41 42 43
        ],
        processManager: FakeProcessManager.any(),
      );
44 45
    });

46
    group('screenshot', () {
47
      FakeProcessManager fakeProcessManager;
48
      File outputFile;
49 50

      setUp(() {
51
        fakeProcessManager = FakeProcessManager.empty();
52
        outputFile = MemoryFileSystem.test().file('image.png');
53 54
      });

55
      testWithoutContext('error if idevicescreenshot is not installed', () async {
56
        // Let `idevicescreenshot` fail with exit code 1.
57 58
        fakeProcessManager.addCommand(FakeCommand(
          command: <String>[
59
            'HostArtifact.idevicescreenshot',
60 61 62 63 64 65 66 67 68
            outputFile.path,
            '--udid',
            '1234',
          ],
          environment: const <String, String>{
            'DYLD_LIBRARY_PATH': '/path/to/libraries',
          },
          exitCode: 1,
        ));
69

70
        final IMobileDevice iMobileDevice = IMobileDevice(
71 72
          artifacts: artifacts,
          cache: cache,
73
          processManager: fakeProcessManager,
74 75 76
          logger: logger,
        );

77
        expect(() async => iMobileDevice.takeScreenshot(
78
          outputFile,
79 80 81
          '1234',
          IOSDeviceInterface.usb,
        ), throwsA(anything));
82
        expect(fakeProcessManager.hasRemainingExpectations, isFalse);
83 84
      });

85
      testWithoutContext('idevicescreenshot captures and returns USB screenshot', () async {
86 87
        fakeProcessManager.addCommand(FakeCommand(
          command: <String>[
88
            'HostArtifact.idevicescreenshot', outputFile.path, '--udid', '1234',
89 90 91
          ],
          environment: const <String, String>{'DYLD_LIBRARY_PATH': '/path/to/libraries'},
        ));
92

93
        final IMobileDevice iMobileDevice = IMobileDevice(
94 95
          artifacts: artifacts,
          cache: cache,
96
          processManager: fakeProcessManager,
97 98 99
          logger: logger,
        );

100
        await iMobileDevice.takeScreenshot(
101
          outputFile,
102 103 104
          '1234',
          IOSDeviceInterface.usb,
        );
105
        expect(fakeProcessManager.hasRemainingExpectations, isFalse);
106
      });
107 108

      testWithoutContext('idevicescreenshot captures and returns network screenshot', () async {
109 110
        fakeProcessManager.addCommand(FakeCommand(
          command: <String>[
111
            'HostArtifact.idevicescreenshot', outputFile.path, '--udid', '1234', '--network',
112 113 114
          ],
          environment: const <String, String>{'DYLD_LIBRARY_PATH': '/path/to/libraries'},
        ));
115 116

        final IMobileDevice iMobileDevice = IMobileDevice(
117 118
          artifacts: artifacts,
          cache: cache,
119
          processManager: fakeProcessManager,
120 121 122 123
          logger: logger,
        );

        await iMobileDevice.takeScreenshot(
124
          outputFile,
125 126 127
          '1234',
          IOSDeviceInterface.network,
        );
128
        expect(fakeProcessManager.hasRemainingExpectations, isFalse);
129
      });
130 131 132
    });
  });

133
  group('Diagnose Xcode build failure', () {
xster's avatar
xster committed
134
    Map<String, String> buildSettings;
135
    TestUsage testUsage;
136 137

    setUp(() {
xster's avatar
xster committed
138 139 140
      buildSettings = <String, String>{
        'PRODUCT_BUNDLE_IDENTIFIER': 'test.app',
      };
141
      testUsage = TestUsage();
142 143
    });

144
    testWithoutContext('Sends analytics when bitcode fails', () async {
145 146 147 148 149 150 151 152 153 154 155 156
      const List<String> buildCommands = <String>['xcrun', 'cc', 'blah'];
      final XcodeBuildResult buildResult = XcodeBuildResult(
        success: false,
        stdout: 'BITCODE_ENABLED = YES',
        xcodeBuildExecution: XcodeBuildExecution(
          buildCommands: buildCommands,
          appDirectory: '/blah/blah',
          buildForPhysicalDevice: true,
          buildSettings: buildSettings,
        ),
      );

157 158 159 160
      await diagnoseXcodeBuildFailure(buildResult, testUsage, logger);
      expect(testUsage.events, contains(
        TestUsageEvent(
          'build',
161
          'ios',
162
          label: 'xcode-bitcode-failure',
163 164 165 166
          parameters: CustomDimensions(
            buildEventCommand: buildCommands.toString(),
            buildEventSettings: buildSettings.toString(),
          ),
167 168
        ),
      ));
169 170
    });

171
    testWithoutContext('No provisioning profile shows message', () async {
172
      final XcodeBuildResult buildResult = XcodeBuildResult(
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
        success: false,
        stdout: '''
Launching lib/main.dart on iPhone in debug mode...
Signing iOS app for device deployment using developer identity: "iPhone Developer: test@flutter.io (1122334455)"
Running Xcode build...                                1.3s
Failed to build iOS app
Error output from Xcode build:

    ** BUILD FAILED **


    The following build commands failed:
    	Check dependencies
    (1 failure)
Xcode's output:

    Build settings from command line:
        ARCHS = arm64
        BUILD_DIR = /Users/blah/blah
        DEVELOPMENT_TEAM = AABBCCDDEE
        ONLY_ACTIVE_ARCH = YES
        SDKROOT = iphoneos10.3

    === CLEAN TARGET Runner OF PROJECT Runner WITH CONFIGURATION Release ===

    Check dependencies
199
    [BCEROR]No profiles for 'com.example.test' were found:  Xcode couldn't find a provisioning profile matching 'com.example.test'.
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
    [BCEROR]Code signing is required for product type 'Application' in SDK 'iOS 10.3'
    [BCEROR]Code signing is required for product type 'Application' in SDK 'iOS 10.3'
    [BCEROR]Code signing is required for product type 'Application' in SDK 'iOS 10.3'

    Create product structure
    /bin/mkdir -p /Users/blah/Runner.app

    Clean.Remove clean /Users/blah/Runner.app.dSYM
        builtin-rm -rf /Users/blah/Runner.app.dSYM

    Clean.Remove clean /Users/blah/Runner.app
        builtin-rm -rf /Users/blah/Runner.app

    Clean.Remove clean /Users/blah/Runner-dfvicjniknvzghgwsthwtgcjhtsk/Build/Intermediates/Runner.build/Release-iphoneos/Runner.build
        builtin-rm -rf /Users/blah/Runner-dfvicjniknvzghgwsthwtgcjhtsk/Build/Intermediates/Runner.build/Release-iphoneos/Runner.build

    ** CLEAN SUCCEEDED **

    === BUILD TARGET Runner OF PROJECT Runner WITH CONFIGURATION Release ===

    Check dependencies
221
    No profiles for 'com.example.test' were found:  Xcode couldn't find a provisioning profile matching 'com.example.test'.
222 223 224 225 226 227 228
    Code signing is required for product type 'Application' in SDK 'iOS 10.3'
    Code signing is required for product type 'Application' in SDK 'iOS 10.3'
    Code signing is required for product type 'Application' in SDK 'iOS 10.3'

Could not build the precompiled application for the device.

Error launching application on iPhone.''',
229
        xcodeBuildExecution: XcodeBuildExecution(
xster's avatar
xster committed
230 231 232 233
          buildCommands: <String>['xcrun', 'xcodebuild', 'blah'],
          appDirectory: '/blah/blah',
          buildForPhysicalDevice: true,
          buildSettings: buildSettings,
234 235 236
        ),
      );

237
      await diagnoseXcodeBuildFailure(buildResult, testUsage, logger);
238
      expect(
239
        logger.errorText,
240
        contains("No Provisioning Profile was found for your project's Bundle Identifier or your \ndevice."),
241
      );
242
    });
243

244
    testWithoutContext('No development team shows message', () async {
245
      final XcodeBuildResult buildResult = XcodeBuildResult(
246 247
        success: false,
        stdout: '''
248
Running "flutter pub get" in flutter_gallery...  0.6s
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
Launching lib/main.dart on x in release mode...
Running pod install...                                1.2s
Running Xcode build...                                1.4s
Failed to build iOS app
Error output from Xcode build:

    ** BUILD FAILED **


    The following build commands failed:
    	Check dependencies
    (1 failure)
Xcode's output:

    blah

    === CLEAN TARGET url_launcher OF PROJECT Pods WITH CONFIGURATION Release ===

    Check dependencies

    blah

    === CLEAN TARGET Pods-Runner OF PROJECT Pods WITH CONFIGURATION Release ===

    Check dependencies

    blah

    === CLEAN TARGET Runner OF PROJECT Runner WITH CONFIGURATION Release ===

    Check dependencies
    [BCEROR]Signing for "Runner" requires a development team. Select a development team in the project editor.
    [BCEROR]Code signing is required for product type 'Application' in SDK 'iOS 10.3'
    [BCEROR]Code signing is required for product type 'Application' in SDK 'iOS 10.3'
    [BCEROR]Code signing is required for product type 'Application' in SDK 'iOS 10.3'

    blah

    ** CLEAN SUCCEEDED **

    === BUILD TARGET url_launcher OF PROJECT Pods WITH CONFIGURATION Release ===

    Check dependencies

    blah

    === BUILD TARGET Pods-Runner OF PROJECT Pods WITH CONFIGURATION Release ===

    Check dependencies

    blah

    === BUILD TARGET Runner OF PROJECT Runner WITH CONFIGURATION Release ===

    Check dependencies
    Signing for "Runner" requires a development team. Select a development team in the project editor.
    Code signing is required for product type 'Application' in SDK 'iOS 10.3'
    Code signing is required for product type 'Application' in SDK 'iOS 10.3'
    Code signing is required for product type 'Application' in SDK 'iOS 10.3'

Could not build the precompiled application for the device.''',
310
        xcodeBuildExecution: XcodeBuildExecution(
xster's avatar
xster committed
311 312 313 314
          buildCommands: <String>['xcrun', 'xcodebuild', 'blah'],
          appDirectory: '/blah/blah',
          buildForPhysicalDevice: true,
          buildSettings: buildSettings,
315 316 317
        ),
      );

318
      await diagnoseXcodeBuildFailure(buildResult, testUsage, logger);
319
      expect(
320
        logger.errorText,
321
        contains('Building a deployable iOS app requires a selected Development Team with a \nProvisioning Profile.'),
322
      );
323
    });
324

325
    testWithoutContext('embedded and linked framework iOS mismatch shows message', () async {
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
      final XcodeBuildResult buildResult = XcodeBuildResult(
        success: false,
        stdout: '''
Launching lib/main.dart on iPhone in debug mode...
Automatically signing iOS for device deployment using specified development team in Xcode project: blah
Xcode build done. 5.7s
Failed to build iOS app
Error output from Xcode build:

** BUILD FAILED **
Xcode's output:

note: Using new build system
note: Building targets in parallel
note: Planning build
note: Constructing build description
error: Building for iOS Simulator, but the linked and embedded framework 'App.framework' was built for iOS. (in target 'Runner' from project 'Runner')
Could not build the precompiled application for the device.

Error launching application on iPhone.
Exited (sigterm)''',
        xcodeBuildExecution: XcodeBuildExecution(
          buildCommands: <String>['xcrun', 'xcodebuild', 'blah'],
          appDirectory: '/blah/blah',
          buildForPhysicalDevice: true,
          buildSettings: buildSettings,
        ),
      );

355
      await diagnoseXcodeBuildFailure(buildResult, testUsage, logger);
356
      expect(
357
        logger.errorText,
358 359
        contains('Your Xcode project requires migration.'),
      );
360
    });
361

362
    testWithoutContext('embedded and linked framework iOS simulator mismatch shows message', () async {
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
      final XcodeBuildResult buildResult = XcodeBuildResult(
        success: false,
        stdout: '''
Launching lib/main.dart on iPhone in debug mode...
Automatically signing iOS for device deployment using specified development team in Xcode project: blah
Xcode build done. 5.7s
Failed to build iOS app
Error output from Xcode build:

** BUILD FAILED **
Xcode's output:

note: Using new build system
note: Building targets in parallel
note: Planning build
note: Constructing build description
error: Building for iOS, but the linked and embedded framework 'App.framework' was built for iOS Simulator. (in target 'Runner' from project 'Runner')
Could not build the precompiled application for the device.

Error launching application on iPhone.
Exited (sigterm)''',
        xcodeBuildExecution: XcodeBuildExecution(
          buildCommands: <String>['xcrun', 'xcodebuild', 'blah'],
          appDirectory: '/blah/blah',
          buildForPhysicalDevice: true,
          buildSettings: buildSettings,
        ),
      );

392
      await diagnoseXcodeBuildFailure(buildResult, testUsage, logger);
393
      expect(
394
        logger.errorText,
395 396
        contains('Your Xcode project requires migration.'),
      );
397
    });
398
  });
399 400

  group('Upgrades project.pbxproj for old asset usage', () {
401 402 403 404 405 406 407 408 409 410 411 412 413
    const String flutterAssetPbxProjLines =
      '/* flutter_assets */\n'
      '/* App.framework\n'
      'another line';

    const String appFlxPbxProjLines =
      '/* app.flx\n'
      '/* App.framework\n'
      'another line';

    const String cleanPbxProjLines =
      '/* App.framework\n'
      'another line';
414

415
    testWithoutContext('upgradePbxProjWithFlutterAssets', () async {
416 417
      final File pbxprojFile = MemoryFileSystem.test().file('project.pbxproj')
        ..writeAsStringSync(flutterAssetPbxProjLines);
418
      final FakeIosProject project = FakeIosProject(pbxprojFile);
419

420
      bool result = upgradePbxProjWithFlutterAssets(project, logger);
421 422
      expect(result, true);
      expect(
423
        logger.statusText,
424 425
        contains('Removing obsolete reference to flutter_assets'),
      );
426
      logger.clear();
427

428
      pbxprojFile.writeAsStringSync(appFlxPbxProjLines);
429
      result = upgradePbxProjWithFlutterAssets(project, logger);
430 431
      expect(result, true);
      expect(
432
        logger.statusText,
433 434
        contains('Removing obsolete reference to app.flx'),
      );
435
      logger.clear();
436

437
      pbxprojFile.writeAsStringSync(cleanPbxProjLines);
438
      result = upgradePbxProjWithFlutterAssets(project, logger);
439 440
      expect(result, true);
      expect(
441
        logger.statusText,
442 443 444 445
        isEmpty,
      );
    });
  });
446 447

  group('remove Finder extended attributes', () {
448
    Directory projectDirectory;
449 450
    setUp(() {
      final MemoryFileSystem fs = MemoryFileSystem.test();
451
      projectDirectory = fs.directory('flutter_project');
452 453 454 455 456 457 458 459 460
    });

    testWithoutContext('removes xattr', () async {
      final FakeProcessManager processManager = FakeProcessManager.list(<FakeCommand>[
        FakeCommand(command: <String>[
          'xattr',
          '-r',
          '-d',
          'com.apple.FinderInfo',
461
          projectDirectory.path,
462 463 464
        ])
      ]);

465
      await removeFinderExtendedAttributes(projectDirectory, ProcessUtils(processManager: processManager, logger: logger), logger);
466
      expect(processManager, hasNoRemainingExpectations);
467 468 469 470 471 472 473 474 475
    });

    testWithoutContext('ignores errors', () async {
      final FakeProcessManager processManager = FakeProcessManager.list(<FakeCommand>[
        FakeCommand(command: <String>[
          'xattr',
          '-r',
          '-d',
          'com.apple.FinderInfo',
476
          projectDirectory.path,
477 478 479 480
        ], exitCode: 1,
        )
      ]);

481
      await removeFinderExtendedAttributes(projectDirectory, ProcessUtils(processManager: processManager, logger: logger), logger);
482
      expect(logger.traceText, contains('Failed to remove xattr com.apple.FinderInfo'));
483
      expect(processManager, hasNoRemainingExpectations);
484 485
    });
  });
486
}
487 488 489 490 491 492 493 494 495 496 497 498

class FakeIosProject extends Fake implements IosProject {
  FakeIosProject(this.xcodeProjectInfoFile);
  @override
  final File xcodeProjectInfoFile;

  @override
  Future<String> hostAppBundleName(BuildInfo buildInfo) async => 'UnitTestRunner.app';

  @override
  final Directory xcodeProject = null;
}