Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Commit 986eb8e

Browse files
[flutter_plugin_tools] Migrate xctest command to NNBD (#4024)
1 parent 04f8ef7 commit 986eb8e

File tree

3 files changed

+34
-22
lines changed

3 files changed

+34
-22
lines changed

script/tool/lib/src/xctest_command.dart

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5-
// @dart=2.9
6-
75
import 'dart:async';
86
import 'dart:convert';
97
import 'dart:io' as io;
@@ -48,7 +46,7 @@ class XCTestCommand extends PluginCommand {
4846
Future<void> run() async {
4947
String destination = getStringArg(_kiOSDestination);
5048
if (destination.isEmpty) {
51-
final String simulatorId = await _findAvailableIphoneSimulator();
49+
final String? simulatorId = await _findAvailableIphoneSimulator();
5250
if (simulatorId == null) {
5351
print(_kFoundNoSimulatorsMessage);
5452
throw ToolExit(1);
@@ -119,7 +117,7 @@ class XCTestCommand extends PluginCommand {
119117
workingDir: example, exitOnError: false);
120118
}
121119

122-
Future<String> _findAvailableIphoneSimulator() async {
120+
Future<String?> _findAvailableIphoneSimulator() async {
123121
// Find the first available destination if not specified.
124122
final List<String> findSimulatorsArguments = <String>[
125123
'simctl',
@@ -143,30 +141,40 @@ class XCTestCommand extends PluginCommand {
143141
final List<Map<String, dynamic>> runtimes =
144142
(simulatorListJson['runtimes'] as List<dynamic>)
145143
.cast<Map<String, dynamic>>();
146-
final Map<String, dynamic> devices =
147-
simulatorListJson['devices'] as Map<String, dynamic>;
144+
final Map<String, Object> devices =
145+
(simulatorListJson['devices'] as Map<String, dynamic>)
146+
.cast<String, Object>();
148147
if (runtimes.isEmpty || devices.isEmpty) {
149148
return null;
150149
}
151-
String id;
150+
String? id;
152151
// Looking for runtimes, trying to find one with highest OS version.
153-
for (final Map<String, dynamic> runtimeMap in runtimes.reversed) {
154-
if (!(runtimeMap['name'] as String).contains('iOS')) {
152+
for (final Map<String, dynamic> rawRuntimeMap in runtimes.reversed) {
153+
final Map<String, Object> runtimeMap =
154+
rawRuntimeMap.cast<String, Object>();
155+
if ((runtimeMap['name'] as String?)?.contains('iOS') != true) {
156+
continue;
157+
}
158+
final String? runtimeID = runtimeMap['identifier'] as String?;
159+
if (runtimeID == null) {
155160
continue;
156161
}
157-
final String runtimeID = runtimeMap['identifier'] as String;
158-
final List<Map<String, dynamic>> devicesForRuntime =
159-
(devices[runtimeID] as List<dynamic>).cast<Map<String, dynamic>>();
160-
if (devicesForRuntime.isEmpty) {
162+
final List<Map<String, dynamic>>? devicesForRuntime =
163+
(devices[runtimeID] as List<dynamic>?)?.cast<Map<String, dynamic>>();
164+
if (devicesForRuntime == null || devicesForRuntime.isEmpty) {
161165
continue;
162166
}
163167
// Looking for runtimes, trying to find latest version of device.
164-
for (final Map<String, dynamic> device in devicesForRuntime.reversed) {
168+
for (final Map<String, dynamic> rawDevice in devicesForRuntime.reversed) {
169+
final Map<String, Object> device = rawDevice.cast<String, Object>();
165170
if (device['availabilityError'] != null ||
166-
(device['isAvailable'] as bool == false)) {
171+
(device['isAvailable'] as bool?) == false) {
172+
continue;
173+
}
174+
id = device['udid'] as String?;
175+
if (id == null) {
167176
continue;
168177
}
169-
id = device['udid'] as String;
170178
print('device selected: $device');
171179
return id;
172180
}

script/tool/test/mocks.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ class MockProcess extends Mock implements io.Process {
1616
StreamController<List<int>>();
1717
final MockIOSink stdinMock = MockIOSink();
1818

19+
@override
20+
int get pid => 99;
21+
1922
@override
2023
Future<int> get exitCode => exitCodeCompleter.future;
2124

script/tool/test/xctest_command_test.dart

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5-
// @dart=2.9
6-
75
import 'dart:convert';
86

97
import 'package:args/command_runner.dart';
@@ -15,6 +13,9 @@ import 'package:test/test.dart';
1513
import 'mocks.dart';
1614
import 'util.dart';
1715

16+
// Note: This uses `dynamic` deliberately, and should not be updated to Object,
17+
// in order to ensure that the code correctly handles this return type from
18+
// JSON decoding.
1819
final Map<String, dynamic> _kDeviceListMap = <String, dynamic>{
1920
'runtimes': <Map<String, dynamic>>[
2021
<String, dynamic>{
@@ -85,10 +86,10 @@ void main() {
8586
const String _kDestination = '--ios-destination';
8687

8788
group('test xctest_command', () {
88-
FileSystem fileSystem;
89-
Directory packagesDir;
90-
CommandRunner<void> runner;
91-
RecordingProcessRunner processRunner;
89+
late FileSystem fileSystem;
90+
late Directory packagesDir;
91+
late CommandRunner<void> runner;
92+
late RecordingProcessRunner processRunner;
9293

9394
setUp(() {
9495
fileSystem = MemoryFileSystem();

0 commit comments

Comments
 (0)