From b1c62245905e1e62cca98a91d0796ebbcb9ae817 Mon Sep 17 00:00:00 2001 From: Mikkel Nygaard Ravn Date: Tue, 3 Jul 2018 16:18:06 +0200 Subject: [PATCH] flutter attach should handle non-singular device gracefully (#19036) --- .../lib/src/commands/attach.dart | 2 ++ .../test/commands/attach_test.dart | 32 +++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/packages/flutter_tools/lib/src/commands/attach.dart b/packages/flutter_tools/lib/src/commands/attach.dart index d4004f1e48..2331688a4c 100644 --- a/packages/flutter_tools/lib/src/commands/attach.dart +++ b/packages/flutter_tools/lib/src/commands/attach.dart @@ -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) { diff --git a/packages/flutter_tools/test/commands/attach_test.dart b/packages/flutter_tools/test/commands/attach_test.dart index e550b93952..4ac267341a 100644 --- a/packages/flutter_tools/test/commands/attach_test.dart +++ b/packages/flutter_tools/test/commands/attach_test.dart @@ -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(['attach']), + throwsA(const isInstanceOf()), + ); + 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(['attach']), + throwsA(const isInstanceOf()), + ); + expect(testLogger.statusText, contains('More than one device')); + expect(testLogger.statusText, contains('xx1')); + expect(testLogger.statusText, contains('yy2')); + }); }); }