From 8df5e9f738da87efcbe3c23309af01139be46067 Mon Sep 17 00:00:00 2001 From: Adam Barth Date: Mon, 12 Oct 2015 00:49:39 -0700 Subject: [PATCH] Polish up some error handling This patch improves the error handling for several arbitrary issues I encountered while experimenting with the tool this evening. --- .../flutter_tools/lib/src/commands/install.dart | 4 ++-- .../flutter_tools/lib/src/commands/listen.dart | 11 ++++++++++- packages/flutter_tools/lib/src/device.dart | 17 +++++++++++++---- packages/flutter_tools/test/install_test.dart | 6 ++++++ packages/flutter_tools/test/start_test.dart | 6 ++++++ 5 files changed, 37 insertions(+), 7 deletions(-) diff --git a/packages/flutter_tools/lib/src/commands/install.dart b/packages/flutter_tools/lib/src/commands/install.dart index 9dc7daaf22..a8cb776ac3 100644 --- a/packages/flutter_tools/lib/src/commands/install.dart +++ b/packages/flutter_tools/lib/src/commands/install.dart @@ -10,7 +10,7 @@ import 'flutter_command.dart'; class InstallCommand extends FlutterCommand { final String name = 'install'; - final String description = 'Install your Flutter app on attached devices.'; + final String description = 'Install Flutter apps on attached devices.'; InstallCommand() { argParser.addFlag('boot', @@ -31,7 +31,7 @@ class InstallCommand extends FlutterCommand { for (Device device in devices.all) { ApplicationPackage package = applicationPackages.getPackageForPlatform(device.platform); - if (package == null || !device.isConnected()) + if (package == null || !device.isConnected() || device.isAppInstalled(package)) continue; if (device.installApp(package)) installedSomewhere = true; diff --git a/packages/flutter_tools/lib/src/commands/listen.dart b/packages/flutter_tools/lib/src/commands/listen.dart index 9e294c5961..4b9e59ea2e 100644 --- a/packages/flutter_tools/lib/src/commands/listen.dart +++ b/packages/flutter_tools/lib/src/commands/listen.dart @@ -22,7 +22,16 @@ class ListenCommand extends FlutterCommand { /// Only run once. Used for testing. bool singleRun; - ListenCommand({ this.singleRun: false }); + ListenCommand({ this.singleRun: false }) { + argParser.addFlag('checked', + negatable: true, + defaultsTo: true, + help: 'Toggle Dart\'s checked mode.'); + argParser.addOption('target', + defaultsTo: '.', + abbr: 't', + help: 'Target app path or filename to start.'); + } @override Future run() async { diff --git a/packages/flutter_tools/lib/src/device.dart b/packages/flutter_tools/lib/src/device.dart index 41878583ed..ea874b6d3e 100644 --- a/packages/flutter_tools/lib/src/device.dart +++ b/packages/flutter_tools/lib/src/device.dart @@ -142,10 +142,15 @@ class IOSDevice extends Device { static Iterable _getAttachedDeviceIDs([IOSDevice mockIOS]) { String listerPath = (mockIOS != null) ? mockIOS.listerPath : _checkForCommand('idevice_id'); - return runSync([listerPath, '-l']) - .trim() - .split('\n') - .where((String s) => s != null && s.length > 0); + String output; + try { + output = runSync([listerPath, '-l']); + } catch (e) { + return []; + } + return output.trim() + .split('\n') + .where((String s) => s != null && s.length > 0); } static String _getDeviceName(String deviceID, [IOSDevice mockIOS]) { @@ -805,6 +810,10 @@ class AndroidDevice extends Device { // Kill the server if (Platform.isMacOS) { String pid = runSync(['lsof', '-i', ':$_serverPort', '-t']); + if (pid.isEmpty) { + _logging.fine('No process to kill for port $_serverPort'); + return true; + } // Killing a pid with a shell command from within dart is hard, // so use a library command, but it's still nice to give the // equivalent command when doing verbose logging. diff --git a/packages/flutter_tools/test/install_test.dart b/packages/flutter_tools/test/install_test.dart index f20d640648..d10c652746 100644 --- a/packages/flutter_tools/test/install_test.dart +++ b/packages/flutter_tools/test/install_test.dart @@ -19,12 +19,15 @@ defineTests() { MockDeviceStore mockDevices = command.devices; when(mockDevices.android.isConnected()).thenReturn(true); + when(mockDevices.android.isAppInstalled(any)).thenReturn(false); when(mockDevices.android.installApp(any)).thenReturn(true); when(mockDevices.iOS.isConnected()).thenReturn(false); + when(mockDevices.iOS.isAppInstalled(any)).thenReturn(false); when(mockDevices.iOS.installApp(any)).thenReturn(false); when(mockDevices.iOSSimulator.isConnected()).thenReturn(false); + when(mockDevices.iOSSimulator.isAppInstalled(any)).thenReturn(false); when(mockDevices.iOSSimulator.installApp(any)).thenReturn(false); @@ -39,12 +42,15 @@ defineTests() { MockDeviceStore mockDevices = command.devices; when(mockDevices.android.isConnected()).thenReturn(false); + when(mockDevices.android.isAppInstalled(any)).thenReturn(false); when(mockDevices.android.installApp(any)).thenReturn(false); when(mockDevices.iOS.isConnected()).thenReturn(true); + when(mockDevices.iOS.isAppInstalled(any)).thenReturn(false); when(mockDevices.iOS.installApp(any)).thenReturn(true); when(mockDevices.iOSSimulator.isConnected()).thenReturn(false); + when(mockDevices.iOSSimulator.isAppInstalled(any)).thenReturn(false); when(mockDevices.iOSSimulator.installApp(any)).thenReturn(false); CommandRunner runner = new CommandRunner('test_flutter', '') diff --git a/packages/flutter_tools/test/start_test.dart b/packages/flutter_tools/test/start_test.dart index 8b2a679481..f9719c5e82 100644 --- a/packages/flutter_tools/test/start_test.dart +++ b/packages/flutter_tools/test/start_test.dart @@ -19,16 +19,19 @@ defineTests() { MockDeviceStore mockDevices = command.devices; when(mockDevices.android.isConnected()).thenReturn(true); + when(mockDevices.android.isAppInstalled(any)).thenReturn(false); when(mockDevices.android.installApp(any)).thenReturn(true); when(mockDevices.android.startServer(any, any, any, any)).thenReturn(true); when(mockDevices.android.stopApp(any)).thenReturn(true); when(mockDevices.iOS.isConnected()).thenReturn(false); + when(mockDevices.iOS.isAppInstalled(any)).thenReturn(false); when(mockDevices.iOS.installApp(any)).thenReturn(false); when(mockDevices.iOS.startApp(any)).thenReturn(false); when(mockDevices.iOS.stopApp(any)).thenReturn(false); when(mockDevices.iOSSimulator.isConnected()).thenReturn(false); + when(mockDevices.iOSSimulator.isAppInstalled(any)).thenReturn(false); when(mockDevices.iOSSimulator.installApp(any)).thenReturn(false); when(mockDevices.iOSSimulator.startApp(any)).thenReturn(false); when(mockDevices.iOSSimulator.stopApp(any)).thenReturn(false); @@ -44,16 +47,19 @@ defineTests() { MockDeviceStore mockDevices = command.devices; when(mockDevices.android.isConnected()).thenReturn(false); + when(mockDevices.android.isAppInstalled(any)).thenReturn(false); when(mockDevices.android.installApp(any)).thenReturn(false); when(mockDevices.android.startServer(any, any, any, any)).thenReturn(false); when(mockDevices.android.stopApp(any)).thenReturn(false); when(mockDevices.iOS.isConnected()).thenReturn(true); + when(mockDevices.iOS.isAppInstalled(any)).thenReturn(false); when(mockDevices.iOS.installApp(any)).thenReturn(true); when(mockDevices.iOS.startApp(any)).thenReturn(true); when(mockDevices.iOS.stopApp(any)).thenReturn(false); when(mockDevices.iOSSimulator.isConnected()).thenReturn(false); + when(mockDevices.iOSSimulator.isAppInstalled(any)).thenReturn(false); when(mockDevices.iOSSimulator.installApp(any)).thenReturn(false); when(mockDevices.iOSSimulator.startApp(any)).thenReturn(false); when(mockDevices.iOSSimulator.stopApp(any)).thenReturn(false);