update Android device detection to handle adb error messages (#6521)

This commit is contained in:
Dan Rubel 2016-10-27 12:06:47 +01:00 committed by GitHub
parent ca90656fba
commit 7acc996547
2 changed files with 25 additions and 4 deletions

View file

@ -629,18 +629,24 @@ final RegExp _kDeviceRegex = new RegExp(r'^(\S+)\s+(\S+)(.*)');
/// [mockAdbOutput] is public for testing. /// [mockAdbOutput] is public for testing.
List<AndroidDevice> getAdbDevices({ String mockAdbOutput }) { List<AndroidDevice> getAdbDevices({ String mockAdbOutput }) {
List<AndroidDevice> devices = <AndroidDevice>[]; List<AndroidDevice> devices = <AndroidDevice>[];
List<String> output; String text;
if (mockAdbOutput == null) { if (mockAdbOutput == null) {
String adbPath = getAdbPath(androidSdk); String adbPath = getAdbPath(androidSdk);
if (adbPath == null) if (adbPath == null)
return <AndroidDevice>[]; return <AndroidDevice>[];
output = runSync(<String>[adbPath, 'devices', '-l']).trim().split('\n'); text = runSync(<String>[adbPath, 'devices', '-l']);
} else { } else {
output = mockAdbOutput.trim().split('\n'); text = mockAdbOutput;
} }
for (String line in output) { // Check for error messages from adb
if (!text.contains('List of devices')) {
printError(text);
return <AndroidDevice>[];
}
for (String line in text.trim().split('\n')) {
// Skip lines like: * daemon started successfully * // Skip lines like: * daemon started successfully *
if (line.startsWith('* daemon ')) if (line.startsWith('* daemon '))
continue; continue;

View file

@ -3,6 +3,7 @@
// found in the LICENSE file. // found in the LICENSE file.
import 'package:flutter_tools/src/android/android_device.dart'; import 'package:flutter_tools/src/android/android_device.dart';
import 'package:flutter_tools/src/base/logger.dart';
import 'package:test/test.dart'; import 'package:test/test.dart';
import 'src/context.dart'; import 'src/context.dart';
@ -41,11 +42,25 @@ emulator-5612 host features:shell_2
testUsingContext('android n', () { testUsingContext('android n', () {
List<AndroidDevice> devices = getAdbDevices(mockAdbOutput: ''' List<AndroidDevice> devices = getAdbDevices(mockAdbOutput: '''
List of devices attached
ZX1G22JJWR device usb:3-3 product:shamu model:Nexus_6 device:shamu features:cmd,shell_v2 ZX1G22JJWR device usb:3-3 product:shamu model:Nexus_6 device:shamu features:cmd,shell_v2
'''); ''');
expect(devices, hasLength(1)); expect(devices, hasLength(1));
expect(devices.first.name, 'Nexus 6'); expect(devices.first.name, 'Nexus 6');
}); });
BufferLogger logger = new BufferLogger();
testUsingContext('adb error message', () {
List<AndroidDevice> devices = getAdbDevices(mockAdbOutput: '''
It appears you do not have 'Android SDK Platform-tools' installed.
Use the 'android' tool to install them:
android update sdk --no-ui --filter 'platform-tools'
''');
expect(devices, hasLength(0));
expect(logger.errorText, contains('you do not have'));
}, overrides: <Type, dynamic>{
Logger : logger,
});
}); });
group('parseAdbDeviceProperties', () { group('parseAdbDeviceProperties', () {