diff --git a/packages/flutter_tools/lib/src/commands/devices.dart b/packages/flutter_tools/lib/src/commands/devices.dart index 312e82ac11..dafe88406d 100644 --- a/packages/flutter_tools/lib/src/commands/devices.dart +++ b/packages/flutter_tools/lib/src/commands/devices.dart @@ -60,34 +60,39 @@ class DevicesCommand extends FlutterCommand { if (boolArg('machine')) { await printDevicesAsJson(devices); - } else if (devices.isEmpty) { - final StringBuffer status = StringBuffer('No devices detected.'); - status.writeln(); - status.writeln(); - status.writeln('Run "flutter emulators" to list and start any available device emulators.'); - status.writeln(); - status.write('If you expected your device to be detected, please run "flutter doctor" to diagnose potential issues. '); - if (timeout == null) { - status.write('You may also try increasing the time to wait for connected devices with the --timeout flag. '); - } - status.write('Visit https://flutter.dev/setup/ for troubleshooting tips.'); - - globals.printStatus(status.toString()); - final List diagnostics = await deviceManager.getDeviceDiagnostics(); - if (diagnostics.isNotEmpty) { - globals.printStatus(''); - for (final String diagnostic in diagnostics) { - globals.printStatus('• $diagnostic', hangingIndent: 2); - } - } } else { - globals.printStatus('${devices.length} connected ${pluralize('device', devices.length)}:\n'); - await Device.printDevices(devices); - } + if (devices.isEmpty) { + final StringBuffer status = StringBuffer('No devices detected.'); + status.writeln(); + status.writeln(); + status.writeln('Run "flutter emulators" to list and start any available device emulators.'); + status.writeln(); + status.write('If you expected your device to be detected, please run "flutter doctor" to diagnose potential issues. '); + if (timeout == null) { + status.write('You may also try increasing the time to wait for connected devices with the --timeout flag. '); + } + status.write('Visit https://flutter.dev/setup/ for troubleshooting tips.'); + globals.printStatus(status.toString()); + } else { + globals.printStatus('${devices.length} connected ${pluralize('device', devices.length)}:\n'); + await Device.printDevices(devices); + } + await _printDiagnostics(); + } return FlutterCommandResult.success(); } + Future _printDiagnostics() async { + final List diagnostics = await deviceManager.getDeviceDiagnostics(); + if (diagnostics.isNotEmpty) { + globals.printStatus(''); + for (final String diagnostic in diagnostics) { + globals.printStatus('• $diagnostic', hangingIndent: 2); + } + } + } + Future printDevicesAsJson(List devices) async { globals.printStatus( const JsonEncoder.withIndent(' ').convert( diff --git a/packages/flutter_tools/test/commands.shard/hermetic/devices_test.dart b/packages/flutter_tools/test/commands.shard/hermetic/devices_test.dart index 32cafe11f8..b2d1404c49 100644 --- a/packages/flutter_tools/test/commands.shard/hermetic/devices_test.dart +++ b/packages/flutter_tools/test/commands.shard/hermetic/devices_test.dart @@ -84,6 +84,25 @@ void main() { DeviceManager: () => _FakeDeviceManager(), ProcessManager: () => MockProcessManager(), }); + + testUsingContext('available devices and diagnostics', () async { + final DevicesCommand command = DevicesCommand(); + await createTestCommandRunner(command).run(['devices']); + expect( + testLogger.statusText, + ''' +2 connected devices: + +ephemeral • ephemeral • android-arm • Test SDK (1.2.3) (emulator) +webby • webby • web-javascript • Web SDK (1.2.4) (emulator) + +• Cannot connect to device ABC +''' + ); + }, overrides: { + DeviceManager: () => _FakeDeviceManager(), + ProcessManager: () => MockProcessManager(), + }); }); } @@ -126,4 +145,8 @@ class _FakeDeviceManager extends DeviceManager { Future> refreshAllConnectedDevices({Duration timeout}) => getAllConnectedDevices(); + @override + Future> getDeviceDiagnostics() => Future>.value( + ['Cannot connect to device ABC'] + ); }