Unverified Commit b1c62245 authored by Mikkel Nygaard Ravn's avatar Mikkel Nygaard Ravn Committed by GitHub

flutter attach should handle non-singular device gracefully (#19036)

parent 0329f0c1
...@@ -71,6 +71,8 @@ class AttachCommand extends FlutterCommand { ...@@ -71,6 +71,8 @@ class AttachCommand extends FlutterCommand {
await _validateArguments(); await _validateArguments();
final Device device = await findTargetDevice(); final Device device = await findTargetDevice();
if (device == null)
throwToolExit(null);
final int devicePort = observatoryPort; final int devicePort = observatoryPort;
Uri observatoryUri; Uri observatoryUri;
if (devicePort == null) { if (devicePort == null) {
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
import 'dart:async'; import 'dart:async';
import 'package:flutter_tools/src/base/common.dart';
import 'package:flutter_tools/src/cache.dart'; import 'package:flutter_tools/src/cache.dart';
import 'package:flutter_tools/src/commands/attach.dart'; import 'package:flutter_tools/src/commands/attach.dart';
import 'package:flutter_tools/src/device.dart'; import 'package:flutter_tools/src/device.dart';
...@@ -68,6 +69,37 @@ void main() { ...@@ -68,6 +69,37 @@ void main() {
verify(portForwarder.forward(devicePort)).called(1); verify(portForwarder.forward(devicePort)).called(1);
}); });
testUsingContext('exits when no device connected', () async {
final AttachCommand command = new AttachCommand();
await expectLater(
createTestCommandRunner(command).run(<String>['attach']),
throwsA(const isInstanceOf<ToolExit>()),
);
expect(testLogger.statusText, contains('No connected devices'));
});
testUsingContext('exits when multiple devices connected', () async {
Device aDeviceWithId(String id) {
final MockAndroidDevice device = new MockAndroidDevice();
when(device.name).thenReturn('d$id');
when(device.id).thenReturn(id);
when(device.isLocalEmulator).thenAnswer((_) async => false);
when(device.sdkNameAndVersion).thenAnswer((_) async => 'Android 46');
return device;
}
final AttachCommand command = new AttachCommand();
testDeviceManager.addDevice(aDeviceWithId('xx1'));
testDeviceManager.addDevice(aDeviceWithId('yy2'));
await expectLater(
createTestCommandRunner(command).run(<String>['attach']),
throwsA(const isInstanceOf<ToolExit>()),
);
expect(testLogger.statusText, contains('More than one device'));
expect(testLogger.statusText, contains('xx1'));
expect(testLogger.statusText, contains('yy2'));
});
}); });
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment