[flutter_plugin_tools] Migrate xctest command to NNBD (#4024)

This commit is contained in:
stuartmorgan
2021-06-07 11:44:05 -07:00
committed by GitHub
parent d491b95e69
commit 181fe18e27
3 changed files with 34 additions and 22 deletions

View File

@ -2,8 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
// @dart=2.9
import 'dart:async'; import 'dart:async';
import 'dart:convert'; import 'dart:convert';
import 'dart:io' as io; import 'dart:io' as io;
@ -48,7 +46,7 @@ class XCTestCommand extends PluginCommand {
Future<void> run() async { Future<void> run() async {
String destination = getStringArg(_kiOSDestination); String destination = getStringArg(_kiOSDestination);
if (destination.isEmpty) { if (destination.isEmpty) {
final String simulatorId = await _findAvailableIphoneSimulator(); final String? simulatorId = await _findAvailableIphoneSimulator();
if (simulatorId == null) { if (simulatorId == null) {
print(_kFoundNoSimulatorsMessage); print(_kFoundNoSimulatorsMessage);
throw ToolExit(1); throw ToolExit(1);
@ -119,7 +117,7 @@ class XCTestCommand extends PluginCommand {
workingDir: example, exitOnError: false); workingDir: example, exitOnError: false);
} }
Future<String> _findAvailableIphoneSimulator() async { Future<String?> _findAvailableIphoneSimulator() async {
// Find the first available destination if not specified. // Find the first available destination if not specified.
final List<String> findSimulatorsArguments = <String>[ final List<String> findSimulatorsArguments = <String>[
'simctl', 'simctl',
@ -143,30 +141,40 @@ class XCTestCommand extends PluginCommand {
final List<Map<String, dynamic>> runtimes = final List<Map<String, dynamic>> runtimes =
(simulatorListJson['runtimes'] as List<dynamic>) (simulatorListJson['runtimes'] as List<dynamic>)
.cast<Map<String, dynamic>>(); .cast<Map<String, dynamic>>();
final Map<String, dynamic> devices = final Map<String, Object> devices =
simulatorListJson['devices'] as Map<String, dynamic>; (simulatorListJson['devices'] as Map<String, dynamic>)
.cast<String, Object>();
if (runtimes.isEmpty || devices.isEmpty) { if (runtimes.isEmpty || devices.isEmpty) {
return null; return null;
} }
String id; String? id;
// Looking for runtimes, trying to find one with highest OS version. // Looking for runtimes, trying to find one with highest OS version.
for (final Map<String, dynamic> runtimeMap in runtimes.reversed) { for (final Map<String, dynamic> rawRuntimeMap in runtimes.reversed) {
if (!(runtimeMap['name'] as String).contains('iOS')) { final Map<String, Object> runtimeMap =
rawRuntimeMap.cast<String, Object>();
if ((runtimeMap['name'] as String?)?.contains('iOS') != true) {
continue; continue;
} }
final String runtimeID = runtimeMap['identifier'] as String; final String? runtimeID = runtimeMap['identifier'] as String?;
final List<Map<String, dynamic>> devicesForRuntime = if (runtimeID == null) {
(devices[runtimeID] as List<dynamic>).cast<Map<String, dynamic>>(); continue;
if (devicesForRuntime.isEmpty) { }
final List<Map<String, dynamic>>? devicesForRuntime =
(devices[runtimeID] as List<dynamic>?)?.cast<Map<String, dynamic>>();
if (devicesForRuntime == null || devicesForRuntime.isEmpty) {
continue; continue;
} }
// Looking for runtimes, trying to find latest version of device. // Looking for runtimes, trying to find latest version of device.
for (final Map<String, dynamic> device in devicesForRuntime.reversed) { for (final Map<String, dynamic> rawDevice in devicesForRuntime.reversed) {
final Map<String, Object> device = rawDevice.cast<String, Object>();
if (device['availabilityError'] != null || if (device['availabilityError'] != null ||
(device['isAvailable'] as bool == false)) { (device['isAvailable'] as bool?) == false) {
continue;
}
id = device['udid'] as String?;
if (id == null) {
continue; continue;
} }
id = device['udid'] as String;
print('device selected: $device'); print('device selected: $device');
return id; return id;
} }

View File

@ -16,6 +16,9 @@ class MockProcess extends Mock implements io.Process {
StreamController<List<int>>(); StreamController<List<int>>();
final MockIOSink stdinMock = MockIOSink(); final MockIOSink stdinMock = MockIOSink();
@override
int get pid => 99;
@override @override
Future<int> get exitCode => exitCodeCompleter.future; Future<int> get exitCode => exitCodeCompleter.future;

View File

@ -2,8 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
// @dart=2.9
import 'dart:convert'; import 'dart:convert';
import 'package:args/command_runner.dart'; import 'package:args/command_runner.dart';
@ -15,6 +13,9 @@ import 'package:test/test.dart';
import 'mocks.dart'; import 'mocks.dart';
import 'util.dart'; import 'util.dart';
// 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 Map<String, dynamic> _kDeviceListMap = <String, dynamic>{ final Map<String, dynamic> _kDeviceListMap = <String, dynamic>{
'runtimes': <Map<String, dynamic>>[ 'runtimes': <Map<String, dynamic>>[
<String, dynamic>{ <String, dynamic>{
@ -85,10 +86,10 @@ void main() {
const String _kDestination = '--ios-destination'; const String _kDestination = '--ios-destination';
group('test xctest_command', () { group('test xctest_command', () {
FileSystem fileSystem; late FileSystem fileSystem;
Directory packagesDir; late Directory packagesDir;
CommandRunner<void> runner; late CommandRunner<void> runner;
RecordingProcessRunner processRunner; late RecordingProcessRunner processRunner;
setUp(() { setUp(() {
fileSystem = MemoryFileSystem(); fileSystem = MemoryFileSystem();