From 53e0c50a7c407fc264de92a36f5c57a494f2596b Mon Sep 17 00:00:00 2001 From: Jonah Williams Date: Thu, 3 Jun 2021 08:24:04 -0700 Subject: [PATCH] [flutter_tools] use ProcessManager.canRun instead of checking for ArgumentErrors (#83845) --- packages/flutter_tools/lib/src/base/os.dart | 21 ++++----- .../test/general.shard/base/os_test.dart | 43 +++---------------- 2 files changed, 16 insertions(+), 48 deletions(-) diff --git a/packages/flutter_tools/lib/src/base/os.dart b/packages/flutter_tools/lib/src/base/os.dart index f4faf8b627..3fb130ef81 100644 --- a/packages/flutter_tools/lib/src/base/os.dart +++ b/packages/flutter_tools/lib/src/base/os.dart @@ -222,13 +222,7 @@ class _PosixUtils extends OperatingSystemUtils { // unzip -o -q zipfile -d dest @override void unzip(File file, Directory targetDirectory) { - try { - _processUtils.runSync( - ['unzip', '-o', '-q', file.path, '-d', targetDirectory.path], - throwOnError: true, - verboseExceptions: true, - ); - } on ArgumentError { + if (!_processManager.canRun('unzip')) { // unzip is not available. this error message is modeled after the download // error in bin/internal/update_dart_sdk.sh String message = 'Please install unzip.'; @@ -241,6 +235,11 @@ class _PosixUtils extends OperatingSystemUtils { 'Missing "unzip" tool. Unable to extract ${file.path}.\n$message' ); } + _processUtils.runSync( + ['unzip', '-o', '-q', file.path, '-d', targetDirectory.path], + throwOnError: true, + verboseExceptions: true, + ); } // tar -xzf tarball -C dest @@ -454,11 +453,7 @@ class _WindowsUtils extends OperatingSystemUtils { @override List _which(String execName, { bool all = false }) { - // `where` always returns all matches, not just the first one. - ProcessResult result; - try { - result = _processManager.runSync(['where', execName]); - } on ArgumentError { + if (!_processManager.canRun('where')) { // `where` could be missing if system32 is not on the PATH. throwToolExit( 'Cannot find the executable for `where`. This can happen if the System32 ' @@ -467,6 +462,8 @@ class _WindowsUtils extends OperatingSystemUtils { 'the terminal and/or IDE.' ); } + // `where` always returns all matches, not just the first one. + final ProcessResult result = _processManager.runSync(['where', execName]); if (result.exitCode != 0) { return const []; } diff --git a/packages/flutter_tools/test/general.shard/base/os_test.dart b/packages/flutter_tools/test/general.shard/base/os_test.dart index 2a9f27ad05..a7817bd891 100644 --- a/packages/flutter_tools/test/general.shard/base/os_test.dart +++ b/packages/flutter_tools/test/general.shard/base/os_test.dart @@ -81,16 +81,8 @@ void main() { }); group('which on Windows', () { - testWithoutContext('throws tool exit if where throws an argument error', () async { - fakeProcessManager.addCommand( - FakeCommand( - command: const [ - 'where', - kExecutable, - ], - exception: ArgumentError('Cannot find executable for where'), - ), - ); + testWithoutContext('throws tool exit if where.exe cannot be run', () async { + fakeProcessManager.excludedExecutables.add('where'); final OperatingSystemUtils utils = OperatingSystemUtils( fileSystem: MemoryFileSystem.test(), @@ -544,17 +536,10 @@ void main() { ); }); - group('display an install message when unzip throws an ArgumentError', () { + group('display an install message when unzip cannot be run', () { testWithoutContext('Linux', () { final FileSystem fileSystem = MemoryFileSystem.test(); - fakeProcessManager.addCommand( - FakeCommand( - command: [ - 'unzip', '-o', '-q', 'foo.zip', '-d', fileSystem.currentDirectory.path, - ], - exception: ArgumentError(), - ), - ); + fakeProcessManager.excludedExecutables.add('unzip'); final OperatingSystemUtils linuxOsUtils = OperatingSystemUtils( fileSystem: fileSystem, @@ -573,14 +558,7 @@ void main() { testWithoutContext('macOS', () { final FileSystem fileSystem = MemoryFileSystem.test(); - fakeProcessManager.addCommand( - FakeCommand( - command: [ - 'unzip', '-o', '-q', 'foo.zip', '-d', fileSystem.currentDirectory.path, - ], - exception: ArgumentError(), - ), - ); + fakeProcessManager.excludedExecutables.add('unzip'); final OperatingSystemUtils macOSUtils = OperatingSystemUtils( fileSystem: fileSystem, @@ -590,7 +568,7 @@ void main() { ); expect( - () => macOSUtils.unzip(fileSystem.file('foo.zip'), fileSystem.currentDirectory), + () => macOSUtils.unzip(fileSystem.file('foo.zip'), fileSystem.currentDirectory), throwsToolExit (message: 'Missing "unzip" tool. Unable to extract foo.zip.\n' 'Consider running "brew install unzip".'), @@ -599,14 +577,7 @@ void main() { testWithoutContext('unknown OS', () { final FileSystem fileSystem = MemoryFileSystem.test(); - fakeProcessManager.addCommand( - FakeCommand( - command: [ - 'unzip', '-o', '-q', 'foo.zip', '-d', fileSystem.currentDirectory.path, - ], - exception: ArgumentError(), - ), - ); + fakeProcessManager.excludedExecutables.add('unzip'); final OperatingSystemUtils unknownOsUtils = OperatingSystemUtils( fileSystem: fileSystem,