Unverified Commit 53e0c50a authored by Jonah Williams's avatar Jonah Williams Committed by GitHub

[flutter_tools] use ProcessManager.canRun instead of checking for ArgumentErrors (#83845)

parent 320f2771
...@@ -222,13 +222,7 @@ class _PosixUtils extends OperatingSystemUtils { ...@@ -222,13 +222,7 @@ class _PosixUtils extends OperatingSystemUtils {
// unzip -o -q zipfile -d dest // unzip -o -q zipfile -d dest
@override @override
void unzip(File file, Directory targetDirectory) { void unzip(File file, Directory targetDirectory) {
try { if (!_processManager.canRun('unzip')) {
_processUtils.runSync(
<String>['unzip', '-o', '-q', file.path, '-d', targetDirectory.path],
throwOnError: true,
verboseExceptions: true,
);
} on ArgumentError {
// unzip is not available. this error message is modeled after the download // unzip is not available. this error message is modeled after the download
// error in bin/internal/update_dart_sdk.sh // error in bin/internal/update_dart_sdk.sh
String message = 'Please install unzip.'; String message = 'Please install unzip.';
...@@ -241,6 +235,11 @@ class _PosixUtils extends OperatingSystemUtils { ...@@ -241,6 +235,11 @@ class _PosixUtils extends OperatingSystemUtils {
'Missing "unzip" tool. Unable to extract ${file.path}.\n$message' 'Missing "unzip" tool. Unable to extract ${file.path}.\n$message'
); );
} }
_processUtils.runSync(
<String>['unzip', '-o', '-q', file.path, '-d', targetDirectory.path],
throwOnError: true,
verboseExceptions: true,
);
} }
// tar -xzf tarball -C dest // tar -xzf tarball -C dest
...@@ -454,11 +453,7 @@ class _WindowsUtils extends OperatingSystemUtils { ...@@ -454,11 +453,7 @@ class _WindowsUtils extends OperatingSystemUtils {
@override @override
List<File> _which(String execName, { bool all = false }) { List<File> _which(String execName, { bool all = false }) {
// `where` always returns all matches, not just the first one. if (!_processManager.canRun('where')) {
ProcessResult result;
try {
result = _processManager.runSync(<String>['where', execName]);
} on ArgumentError {
// `where` could be missing if system32 is not on the PATH. // `where` could be missing if system32 is not on the PATH.
throwToolExit( throwToolExit(
'Cannot find the executable for `where`. This can happen if the System32 ' 'Cannot find the executable for `where`. This can happen if the System32 '
...@@ -467,6 +462,8 @@ class _WindowsUtils extends OperatingSystemUtils { ...@@ -467,6 +462,8 @@ class _WindowsUtils extends OperatingSystemUtils {
'the terminal and/or IDE.' 'the terminal and/or IDE.'
); );
} }
// `where` always returns all matches, not just the first one.
final ProcessResult result = _processManager.runSync(<String>['where', execName]);
if (result.exitCode != 0) { if (result.exitCode != 0) {
return const <File>[]; return const <File>[];
} }
......
...@@ -81,16 +81,8 @@ void main() { ...@@ -81,16 +81,8 @@ void main() {
}); });
group('which on Windows', () { group('which on Windows', () {
testWithoutContext('throws tool exit if where throws an argument error', () async { testWithoutContext('throws tool exit if where.exe cannot be run', () async {
fakeProcessManager.addCommand( fakeProcessManager.excludedExecutables.add('where');
FakeCommand(
command: const <String>[
'where',
kExecutable,
],
exception: ArgumentError('Cannot find executable for where'),
),
);
final OperatingSystemUtils utils = OperatingSystemUtils( final OperatingSystemUtils utils = OperatingSystemUtils(
fileSystem: MemoryFileSystem.test(), fileSystem: MemoryFileSystem.test(),
...@@ -544,17 +536,10 @@ void main() { ...@@ -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', () { testWithoutContext('Linux', () {
final FileSystem fileSystem = MemoryFileSystem.test(); final FileSystem fileSystem = MemoryFileSystem.test();
fakeProcessManager.addCommand( fakeProcessManager.excludedExecutables.add('unzip');
FakeCommand(
command: <String>[
'unzip', '-o', '-q', 'foo.zip', '-d', fileSystem.currentDirectory.path,
],
exception: ArgumentError(),
),
);
final OperatingSystemUtils linuxOsUtils = OperatingSystemUtils( final OperatingSystemUtils linuxOsUtils = OperatingSystemUtils(
fileSystem: fileSystem, fileSystem: fileSystem,
...@@ -573,14 +558,7 @@ void main() { ...@@ -573,14 +558,7 @@ void main() {
testWithoutContext('macOS', () { testWithoutContext('macOS', () {
final FileSystem fileSystem = MemoryFileSystem.test(); final FileSystem fileSystem = MemoryFileSystem.test();
fakeProcessManager.addCommand( fakeProcessManager.excludedExecutables.add('unzip');
FakeCommand(
command: <String>[
'unzip', '-o', '-q', 'foo.zip', '-d', fileSystem.currentDirectory.path,
],
exception: ArgumentError(),
),
);
final OperatingSystemUtils macOSUtils = OperatingSystemUtils( final OperatingSystemUtils macOSUtils = OperatingSystemUtils(
fileSystem: fileSystem, fileSystem: fileSystem,
...@@ -590,7 +568,7 @@ void main() { ...@@ -590,7 +568,7 @@ void main() {
); );
expect( expect(
() => macOSUtils.unzip(fileSystem.file('foo.zip'), fileSystem.currentDirectory), () => macOSUtils.unzip(fileSystem.file('foo.zip'), fileSystem.currentDirectory),
throwsToolExit throwsToolExit
(message: 'Missing "unzip" tool. Unable to extract foo.zip.\n' (message: 'Missing "unzip" tool. Unable to extract foo.zip.\n'
'Consider running "brew install unzip".'), 'Consider running "brew install unzip".'),
...@@ -599,14 +577,7 @@ void main() { ...@@ -599,14 +577,7 @@ void main() {
testWithoutContext('unknown OS', () { testWithoutContext('unknown OS', () {
final FileSystem fileSystem = MemoryFileSystem.test(); final FileSystem fileSystem = MemoryFileSystem.test();
fakeProcessManager.addCommand( fakeProcessManager.excludedExecutables.add('unzip');
FakeCommand(
command: <String>[
'unzip', '-o', '-q', 'foo.zip', '-d', fileSystem.currentDirectory.path,
],
exception: ArgumentError(),
),
);
final OperatingSystemUtils unknownOsUtils = OperatingSystemUtils( final OperatingSystemUtils unknownOsUtils = OperatingSystemUtils(
fileSystem: fileSystem, fileSystem: fileSystem,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment