From ef79e7a1c07b4a350d020886a955b05c3449c8a6 Mon Sep 17 00:00:00 2001 From: Dan Rubel Date: Thu, 1 Sep 2016 12:37:58 -0400 Subject: [PATCH] improve device matching error message (#5693) * improve device matching error message fixes https://github.com/flutter/flutter/issues/5675 --- .../flutter_tools/lib/src/commands/drive.dart | 14 +++++++-- packages/flutter_tools/lib/src/device.dart | 21 +++++--------- .../lib/src/runner/flutter_command.dart | 14 ++++++--- packages/flutter_tools/test/device_test.dart | 29 +++++++++++++------ packages/flutter_tools/test/src/context.dart | 8 ++--- 5 files changed, 53 insertions(+), 33 deletions(-) diff --git a/packages/flutter_tools/lib/src/commands/drive.dart b/packages/flutter_tools/lib/src/commands/drive.dart index 213e9f1ca0..514c3db48a 100644 --- a/packages/flutter_tools/lib/src/commands/drive.dart +++ b/packages/flutter_tools/lib/src/commands/drive.dart @@ -205,11 +205,21 @@ void restoreTargetDeviceFinder() { } Future findTargetDevice() async { + List devices = await deviceManager.getDevices(); + if (deviceManager.hasSpecifiedDeviceId) { - return deviceManager.getDeviceById(deviceManager.specifiedDeviceId); + if (devices.isEmpty) { + printStatus("No devices found with name or id matching '${deviceManager.specifiedDeviceId}'"); + return null; + } + if (devices.length > 1) { + printStatus("Found ${devices.length} devices with name or id matching '${deviceManager.specifiedDeviceId}':"); + Device.printDevices(devices); + return null; + } + return devices.first; } - List devices = await deviceManager.getAllConnectedDevices(); if (os.isMacOS) { // On Mac we look for the iOS Simulator. If available, we use that. Then diff --git a/packages/flutter_tools/lib/src/device.dart b/packages/flutter_tools/lib/src/device.dart index 0c8ce94fa8..e78b19c89c 100644 --- a/packages/flutter_tools/lib/src/device.dart +++ b/packages/flutter_tools/lib/src/device.dart @@ -35,13 +35,11 @@ class DeviceManager { bool get hasSpecifiedDeviceId => specifiedDeviceId != null; - /// Return the device with the matching ID; else, complete the Future with - /// `null`. - /// - /// This does a case insentitive compare with `deviceId`. - Future getDeviceById(String deviceId, [List devices]) async { + /// Return the devices with a name or id matching [deviceId]. + /// This does a case insentitive compare with [deviceId]. + Future> getDevicesById(String deviceId) async { deviceId = deviceId.toLowerCase(); - devices ??= await getAllConnectedDevices(); + List devices = await getAllConnectedDevices(); Device device = devices.firstWhere( (Device device) => device.id.toLowerCase() == deviceId || @@ -49,15 +47,13 @@ class DeviceManager { orElse: () => null); if (device != null) - return device; + return [device]; - // Match on a close id / name. - devices = devices.where((Device device) { + // Match on a id or name starting with [deviceId]. + return devices.where((Device device) { return (device.id.toLowerCase().startsWith(deviceId) || device.name.toLowerCase().startsWith(deviceId)); }).toList(); - - return devices.length == 1 ? devices.first : null; } /// Return the list of connected devices, filtered by any user-specified device id. @@ -65,8 +61,7 @@ class DeviceManager { if (specifiedDeviceId == null) { return getAllConnectedDevices(); } else { - Device device = await getDeviceById(specifiedDeviceId); - return device == null ? [] : [device]; + return getDevicesById(specifiedDeviceId); } } diff --git a/packages/flutter_tools/lib/src/runner/flutter_command.dart b/packages/flutter_tools/lib/src/runner/flutter_command.dart index 989f424df2..bb92b0d989 100644 --- a/packages/flutter_tools/lib/src/runner/flutter_command.dart +++ b/packages/flutter_tools/lib/src/runner/flutter_command.dart @@ -137,7 +137,8 @@ abstract class FlutterCommand extends Command { List devices = await deviceManager.getDevices(); if (devices.isEmpty && deviceManager.hasSpecifiedDeviceId) { - printError("No device found with id '${deviceManager.specifiedDeviceId}'."); + printStatus("No devices found with name or id " + "matching '${deviceManager.specifiedDeviceId}'"); return 1; } else if (devices.isEmpty) { printStatus('No connected devices.'); @@ -153,10 +154,15 @@ abstract class FlutterCommand extends Command { printStatus('No supported devices connected.'); return 1; } else if (devices.length > 1) { - printStatus("More than one device connected; please specify a device with " - "the '-d ' flag."); + if (deviceManager.hasSpecifiedDeviceId) { + printStatus("Found ${devices.length} devices with name or id matching " + "'${deviceManager.specifiedDeviceId}':"); + } else { + printStatus("More than one device connected; please specify a device with " + "the '-d ' flag."); + devices = await deviceManager.getAllConnectedDevices(); + } printStatus(''); - devices = await deviceManager.getAllConnectedDevices(); Device.printDevices(devices); return 1; } else { diff --git a/packages/flutter_tools/test/device_test.dart b/packages/flutter_tools/test/device_test.dart index 8c06692c49..f85b442a89 100644 --- a/packages/flutter_tools/test/device_test.dart +++ b/packages/flutter_tools/test/device_test.dart @@ -19,25 +19,36 @@ void main() { }); testUsingContext('getDeviceById', () async { - DeviceManager deviceManager = new DeviceManager(); _MockDevice device1 = new _MockDevice('Nexus 5', '0553790d0a4e726f'); _MockDevice device2 = new _MockDevice('Nexus 5X', '01abfc49119c410e'); _MockDevice device3 = new _MockDevice('iPod touch', '82564b38861a9a5'); List devices = [device1, device2, device3]; + DeviceManager deviceManager = new TestDeviceManager(devices); - Future expectDevice(String id, Device expected) async { - expect(await deviceManager.getDeviceById(id, devices), expected); + Future expectDevice(String id, List expected) async { + expect(await deviceManager.getDevicesById(id), expected); } - expectDevice('01abfc49119c410e', device2); - expectDevice('Nexus 5X', device2); - expectDevice('0553790d0a4e726f', device1); - expectDevice('Nexus 5', device1); - expectDevice('0553790', device1); - expectDevice('Nexus', null); + expectDevice('01abfc49119c410e', [device2]); + expectDevice('Nexus 5X', [device2]); + expectDevice('0553790d0a4e726f', [device1]); + expectDevice('Nexus 5', [device1]); + expectDevice('0553790', [device1]); + expectDevice('Nexus', [device1, device2]); }); }); } +class TestDeviceManager extends DeviceManager { + final List allDevices; + + TestDeviceManager(this.allDevices); + + @override + Future> getAllConnectedDevices() async { + return allDevices; + } +} + class _MockDevice extends Device { @override final String name; diff --git a/packages/flutter_tools/test/src/context.dart b/packages/flutter_tools/test/src/context.dart index 6f4932504d..5744731f4e 100644 --- a/packages/flutter_tools/test/src/context.dart +++ b/packages/flutter_tools/test/src/context.dart @@ -82,9 +82,8 @@ class MockDeviceManager implements DeviceManager { Future> getAllConnectedDevices() => new Future>.value(devices); @override - Future getDeviceById(String deviceId, [List _]) { - Device device = devices.firstWhere((Device device) => device.id == deviceId, orElse: () => null); - return new Future.value(device); + Future> getDevicesById(String deviceId) async { + return devices.where((Device device) => device.id == deviceId).toList(); } @override @@ -92,8 +91,7 @@ class MockDeviceManager implements DeviceManager { if (specifiedDeviceId == null) { return getAllConnectedDevices(); } else { - Device device = await getDeviceById(specifiedDeviceId); - return device == null ? [] : [device]; + return getDevicesById(specifiedDeviceId); } }