Files
packages/script/tool/test/common/xcode_test.dart
stuartmorgan-g cc3dca67f2 [all] Omit obvious local types (#10511)
Makes the analysis options changes described in
https://github.com/flutter/flutter/issues/178827:
- Adding
[omit_obvious_local_variable_types](https://dart.dev/tools/linter-rules/omit_obvious_local_variable_types)
- Adding
[specify_nonobvious_local_variable_types](https://dart.dev/tools/linter-rules/specify_nonobvious_local_variable_types)
- Adding
[specify_nonobvious_property_types](https://dart.dev/tools/linter-rules/specify_nonobvious_property_types)
- Adding
[type_annotate_public_apis](https://dart.dev/tools/linter-rules/type_annotate_public_apis)
- Removing
[always_specify_types](https://dart.dev/tools/linter-rules/always_specify_types)

After those changes, makes the following repo-wide changes:
- `dart fix --apply` in all packages and in script/tool/
- `dart format` in all packages and in script/tool/
- `update-excerpts` repo tooling command to update excerpts based on the
changes to their sources

Also updates the min Flutter/Dart SDK version to 3.35/3.9 for the
following packages, to avoid `analyze` failures in the `N-2` legacy
analysis run due to what appears to be a 3.9 change in what the Dart
analyzer continues to be an obvious local type in loop iterations:
- go_router
- google_fonts
- google_identity_services_web
- google_maps_flutter_web
- local_auth_platform_interface
- metrics_center
- multicast_dns
- pigeon
- rfw
- shared_preferences
- two_dimensional_scrollables
- vector_graphics_compiler
- mustache_template
- path_parsing

Because this is causing a significant amount of format churn already, I
took this opportunity to update the repository tooling to a min Dart SDK
of 3.8 (the N-2 stable version, so the earliest version we need the
tooling to support) to pick up the new format style, so the amount of
automated formatter change is higher in script/tool/ than in the
packages.

This does contain two manual changes (other than the repo tooling min
version):
-
d700b45c7d
changes `dynamic` to `Object?` in a few places where `dynamic` caused
analyzer warnings under the new rule set.
- Updates the repo tooling to ignore `.dart_tool/` when looking for
unexpected local `analysis_options.yaml` files, to fix issues running
the repo tool's `analyze` command locally based on recent changes in
`dart` behavior.

This does not include any CHANGELOG or version updates; even though we
normally version any changes to production code, mass automated changes
like this aren't worth the churn of releasing. This includes changes to
lib/example/main.dart and to README.md excerpts; while the style changes
will be user-visible on pub.dev, it's fine for those changes to wait for
the next release of each package.

Part of https://github.com/flutter/flutter/issues/178827
2025-11-24 18:26:06 -05:00

447 lines
14 KiB
Dart

// Copyright 2013 The Flutter Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:convert';
import 'package:file/file.dart';
import 'package:file/local.dart';
import 'package:file/memory.dart';
import 'package:flutter_plugin_tools/src/common/xcode.dart';
import 'package:test/test.dart';
import '../mocks.dart';
import '../util.dart';
void main() {
late RecordingProcessRunner processRunner;
late Xcode xcode;
setUp(() {
processRunner = RecordingProcessRunner();
xcode = Xcode(processRunner: processRunner);
});
group('findBestAvailableIphoneSimulator', () {
test('finds the newest device', () async {
const expectedDeviceId = '1E76A0FD-38AC-4537-A989-EA639D7D012A';
// Note: This uses `dynamic` deliberately, and should not be updated to
// Object, in order to ensure that the code correctly handles this return
// type from JSON decoding.
final devices = <String, dynamic>{
'runtimes': <Map<String, dynamic>>[
<String, dynamic>{
'bundlePath':
'/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS 13.0.simruntime',
'buildversion': '17A577',
'runtimeRoot':
'/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS 13.0.simruntime/Contents/Resources/RuntimeRoot',
'identifier': 'com.apple.CoreSimulator.SimRuntime.iOS-13-0',
'version': '13.0',
'isAvailable': true,
'name': 'iOS 13.0',
},
<String, dynamic>{
'bundlePath':
'/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS 13.4.simruntime',
'buildversion': '17L255',
'runtimeRoot':
'/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS 13.4.simruntime/Contents/Resources/RuntimeRoot',
'identifier': 'com.apple.CoreSimulator.SimRuntime.iOS-13-4',
'version': '13.4',
'isAvailable': true,
'name': 'iOS 13.4',
},
<String, dynamic>{
'bundlePath':
'/Applications/Xcode_11_7.app/Contents/Developer/Platforms/WatchOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/watchOS.simruntime',
'buildversion': '17T531',
'runtimeRoot':
'/Applications/Xcode_11_7.app/Contents/Developer/Platforms/WatchOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/watchOS.simruntime/Contents/Resources/RuntimeRoot',
'identifier': 'com.apple.CoreSimulator.SimRuntime.watchOS-6-2',
'version': '6.2.1',
'isAvailable': true,
'name': 'watchOS 6.2',
},
],
'devices': <String, dynamic>{
'com.apple.CoreSimulator.SimRuntime.iOS-13-4': <Map<String, dynamic>>[
<String, dynamic>{
'dataPath':
'/Users/xxx/Library/Developer/CoreSimulator/Devices/2706BBEB-1E01-403E-A8E9-70E8E5A24774/data',
'logPath':
'/Users/xxx/Library/Logs/CoreSimulator/2706BBEB-1E01-403E-A8E9-70E8E5A24774',
'udid': '2706BBEB-1E01-403E-A8E9-70E8E5A24774',
'isAvailable': true,
'deviceTypeIdentifier':
'com.apple.CoreSimulator.SimDeviceType.iPhone-8',
'state': 'Shutdown',
'name': 'iPhone 8',
},
<String, dynamic>{
'dataPath':
'/Users/xxx/Library/Developer/CoreSimulator/Devices/1E76A0FD-38AC-4537-A989-EA639D7D012A/data',
'logPath':
'/Users/xxx/Library/Logs/CoreSimulator/1E76A0FD-38AC-4537-A989-EA639D7D012A',
'udid': expectedDeviceId,
'isAvailable': true,
'deviceTypeIdentifier':
'com.apple.CoreSimulator.SimDeviceType.iPhone-8-Plus',
'state': 'Shutdown',
'name': 'iPhone 8 Plus',
},
],
},
};
processRunner.mockProcessesForExecutable['xcrun'] = <FakeProcessInfo>[
FakeProcessInfo(MockProcess(stdout: jsonEncode(devices)), <String>[
'simctl',
'list',
]),
];
expect(await xcode.findBestAvailableIphoneSimulator(), expectedDeviceId);
});
test('ignores non-iOS runtimes', () async {
// Note: This uses `dynamic` deliberately, and should not be updated to
// Object, in order to ensure that the code correctly handles this return
// type from JSON decoding.
final devices = <String, dynamic>{
'runtimes': <Map<String, dynamic>>[
<String, dynamic>{
'bundlePath':
'/Applications/Xcode_11_7.app/Contents/Developer/Platforms/WatchOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/watchOS.simruntime',
'buildversion': '17T531',
'runtimeRoot':
'/Applications/Xcode_11_7.app/Contents/Developer/Platforms/WatchOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/watchOS.simruntime/Contents/Resources/RuntimeRoot',
'identifier': 'com.apple.CoreSimulator.SimRuntime.watchOS-6-2',
'version': '6.2.1',
'isAvailable': true,
'name': 'watchOS 6.2',
},
],
'devices': <String, dynamic>{
'com.apple.CoreSimulator.SimRuntime.watchOS-6-2': <Map<String, dynamic>>[
<String, dynamic>{
'dataPath':
'/Users/xxx/Library/Developer/CoreSimulator/Devices/1E76A0FD-38AC-4537-A989-EA639D7D012A/data',
'logPath':
'/Users/xxx/Library/Logs/CoreSimulator/1E76A0FD-38AC-4537-A989-EA639D7D012A',
'udid': '1E76A0FD-38AC-4537-A989-EA639D7D012A',
'isAvailable': true,
'deviceTypeIdentifier':
'com.apple.CoreSimulator.SimDeviceType.Apple-Watch-38mm',
'state': 'Shutdown',
'name': 'Apple Watch',
},
],
},
};
processRunner.mockProcessesForExecutable['xcrun'] = <FakeProcessInfo>[
FakeProcessInfo(MockProcess(stdout: jsonEncode(devices)), <String>[
'simctl',
'list',
]),
];
expect(await xcode.findBestAvailableIphoneSimulator(), null);
});
test('returns null if simctl fails', () async {
processRunner.mockProcessesForExecutable['xcrun'] = <FakeProcessInfo>[
FakeProcessInfo(MockProcess(exitCode: 1), <String>['simctl', 'list']),
];
expect(await xcode.findBestAvailableIphoneSimulator(), null);
});
});
group('runXcodeBuild', () {
test('handles minimal arguments', () async {
final Directory directory = const LocalFileSystem().currentDirectory;
final int exitCode = await xcode.runXcodeBuild(
directory,
'ios',
workspace: 'A.xcworkspace',
scheme: 'AScheme',
hostPlatform: MockPlatform(),
);
expect(exitCode, 0);
expect(
processRunner.recordedCalls,
orderedEquals(<ProcessCall>[
ProcessCall('xcrun', const <String>[
'xcodebuild',
'build',
'-workspace',
'A.xcworkspace',
'-scheme',
'AScheme',
], directory.path),
]),
);
});
test('handles all arguments', () async {
final Directory directory = const LocalFileSystem().currentDirectory;
final int exitCode = await xcode.runXcodeBuild(
directory,
'ios',
actions: <String>['action1', 'action2'],
workspace: 'A.xcworkspace',
scheme: 'AScheme',
configuration: 'Debug',
hostPlatform: MockPlatform(),
extraFlags: <String>['-a', '-b', 'c=d'],
);
expect(exitCode, 0);
expect(
processRunner.recordedCalls,
orderedEquals(<ProcessCall>[
ProcessCall('xcrun', const <String>[
'xcodebuild',
'action1',
'action2',
'-workspace',
'A.xcworkspace',
'-scheme',
'AScheme',
'-configuration',
'Debug',
'-a',
'-b',
'c=d',
], directory.path),
]),
);
});
test('returns error codes', () async {
processRunner.mockProcessesForExecutable['xcrun'] = <FakeProcessInfo>[
FakeProcessInfo(MockProcess(exitCode: 1), <String>['xcodebuild']),
];
final Directory directory = const LocalFileSystem().currentDirectory;
final int exitCode = await xcode.runXcodeBuild(
directory,
'ios',
workspace: 'A.xcworkspace',
scheme: 'AScheme',
hostPlatform: MockPlatform(),
);
expect(exitCode, 1);
expect(
processRunner.recordedCalls,
orderedEquals(<ProcessCall>[
ProcessCall('xcrun', const <String>[
'xcodebuild',
'build',
'-workspace',
'A.xcworkspace',
'-scheme',
'AScheme',
], directory.path),
]),
);
});
test('sets CODE_SIGN_ENTITLEMENTS for macos tests', () async {
final FileSystem fileSystem = MemoryFileSystem();
final Directory directory = fileSystem.currentDirectory;
directory
.childDirectory('macos')
.childDirectory('Runner')
.childFile('DebugProfile.entitlements')
.createSync(recursive: true);
final int exitCode = await xcode.runXcodeBuild(
directory,
'macos',
workspace: 'A.xcworkspace',
scheme: 'AScheme',
hostPlatform: MockPlatform(),
actions: <String>['test'],
);
expect(exitCode, 0);
expect(
processRunner.recordedCalls,
orderedEquals(<ProcessCall>[
ProcessCall('xcrun', const <String>[
'xcodebuild',
'test',
'-workspace',
'A.xcworkspace',
'-scheme',
'AScheme',
'CODE_SIGN_ENTITLEMENTS=/.tmp_rand0/flutter_disable_sandbox_entitlement.rand0/DebugProfileWithDisabledSandboxing.entitlements',
], directory.path),
]),
);
});
});
group('projectHasTarget', () {
test('returns true when present', () async {
const stdout = '''
{
"project" : {
"configurations" : [
"Debug",
"Release"
],
"name" : "Runner",
"schemes" : [
"Runner"
],
"targets" : [
"Runner",
"RunnerTests",
"RunnerUITests"
]
}
}''';
processRunner.mockProcessesForExecutable['xcrun'] = <FakeProcessInfo>[
FakeProcessInfo(MockProcess(stdout: stdout), <String>['xcodebuild']),
];
final Directory project = const LocalFileSystem().directory(
'/foo.xcodeproj',
);
expect(await xcode.projectHasTarget(project, 'RunnerTests'), true);
expect(
processRunner.recordedCalls,
orderedEquals(<ProcessCall>[
ProcessCall('xcrun', <String>[
'xcodebuild',
'-list',
'-json',
'-project',
project.path,
], null),
]),
);
});
test('returns false when not present', () async {
const stdout = '''
{
"project" : {
"configurations" : [
"Debug",
"Release"
],
"name" : "Runner",
"schemes" : [
"Runner"
],
"targets" : [
"Runner",
"RunnerUITests"
]
}
}''';
processRunner.mockProcessesForExecutable['xcrun'] = <FakeProcessInfo>[
FakeProcessInfo(MockProcess(stdout: stdout), <String>['xcodebuild']),
];
final Directory project = const LocalFileSystem().directory(
'/foo.xcodeproj',
);
expect(await xcode.projectHasTarget(project, 'RunnerTests'), false);
expect(
processRunner.recordedCalls,
orderedEquals(<ProcessCall>[
ProcessCall('xcrun', <String>[
'xcodebuild',
'-list',
'-json',
'-project',
project.path,
], null),
]),
);
});
test('returns null for unexpected output', () async {
processRunner.mockProcessesForExecutable['xcrun'] = <FakeProcessInfo>[
FakeProcessInfo(MockProcess(stdout: '{}'), <String>['xcodebuild']),
];
final Directory project = const LocalFileSystem().directory(
'/foo.xcodeproj',
);
expect(await xcode.projectHasTarget(project, 'RunnerTests'), null);
expect(
processRunner.recordedCalls,
orderedEquals(<ProcessCall>[
ProcessCall('xcrun', <String>[
'xcodebuild',
'-list',
'-json',
'-project',
project.path,
], null),
]),
);
});
test('returns null for invalid output', () async {
processRunner.mockProcessesForExecutable['xcrun'] = <FakeProcessInfo>[
FakeProcessInfo(MockProcess(stdout: ':)'), <String>['xcodebuild']),
];
final Directory project = const LocalFileSystem().directory(
'/foo.xcodeproj',
);
expect(await xcode.projectHasTarget(project, 'RunnerTests'), null);
expect(
processRunner.recordedCalls,
orderedEquals(<ProcessCall>[
ProcessCall('xcrun', <String>[
'xcodebuild',
'-list',
'-json',
'-project',
project.path,
], null),
]),
);
});
test('returns null for failure', () async {
processRunner.mockProcessesForExecutable['xcrun'] = <FakeProcessInfo>[
FakeProcessInfo(MockProcess(exitCode: 1), <String>[
'xcodebuild',
'-list',
]),
];
final Directory project = const LocalFileSystem().directory(
'/foo.xcodeproj',
);
expect(await xcode.projectHasTarget(project, 'RunnerTests'), null);
expect(
processRunner.recordedCalls,
orderedEquals(<ProcessCall>[
ProcessCall('xcrun', <String>[
'xcodebuild',
'-list',
'-json',
'-project',
project.path,
], null),
]),
);
});
});
}