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

This commit is contained in:
Mikkel Nygaard Ravn 2018-07-03 16:18:06 +02:00 committed by GitHub
parent 0329f0c179
commit b1c6224590
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 0 deletions

View file

@ -71,6 +71,8 @@ class AttachCommand extends FlutterCommand {
await _validateArguments();
final Device device = await findTargetDevice();
if (device == null)
throwToolExit(null);
final int devicePort = observatoryPort;
Uri observatoryUri;
if (devicePort == null) {

View file

@ -4,6 +4,7 @@
import 'dart:async';
import 'package:flutter_tools/src/base/common.dart';
import 'package:flutter_tools/src/cache.dart';
import 'package:flutter_tools/src/commands/attach.dart';
import 'package:flutter_tools/src/device.dart';
@ -68,6 +69,37 @@ void main() {
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'));
});
});
}