From fa8bf67cb64c3cbbdd4c2179c71b3388a784b3e5 Mon Sep 17 00:00:00 2001 From: Jonah Williams Date: Tue, 26 Jan 2021 12:49:03 -0800 Subject: [PATCH] [flutter_tools] catch errors when getting cwd (#74744) --- .../flutter_tools/lib/src/base/error_handling_io.dart | 6 ++++-- .../test/general.shard/base/error_handling_io_test.dart | 9 +++++++-- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/packages/flutter_tools/lib/src/base/error_handling_io.dart b/packages/flutter_tools/lib/src/base/error_handling_io.dart index f350511e1d0..337e652696a 100644 --- a/packages/flutter_tools/lib/src/base/error_handling_io.dart +++ b/packages/flutter_tools/lib/src/base/error_handling_io.dart @@ -102,7 +102,9 @@ class ErrorHandlingFileSystem extends ForwardingFileSystem { static bool _noExitOnFailure = false; @override - Directory get currentDirectory => directory(delegate.currentDirectory); + Directory get currentDirectory { + return _runSync(() => directory(delegate.currentDirectory), platform: _platform); + } @override File file(dynamic path) => ErrorHandlingFile( @@ -710,7 +712,7 @@ void _handleWindowsException(Exception e, String message, int errorCode) { switch (errorCode) { case kAccessDenied: errorMessage = - '$message. The flutter tool cannot access the file.\n' + '$message. The flutter tool cannot access the file or directory.\n' 'Please ensure that the SDK and/or project is installed in a location ' 'that has read/write permissions for the current user.'; break; diff --git a/packages/flutter_tools/test/general.shard/base/error_handling_io_test.dart b/packages/flutter_tools/test/general.shard/base/error_handling_io_test.dart index f68eadb7aa4..cf14d98a787 100644 --- a/packages/flutter_tools/test/general.shard/base/error_handling_io_test.dart +++ b/packages/flutter_tools/test/general.shard/base/error_handling_io_test.dart @@ -87,6 +87,7 @@ void setupReadMocks({ }) { final MockFile mockFile = MockFile(); when(mockFileSystem.file(any)).thenReturn(mockFile); + when(mockFileSystem.currentDirectory).thenThrow(FileSystemException('', '', OSError('', errorCode))); when(mockFile.readAsStringSync( encoding: anyNamed('encoding'), )).thenThrow(FileSystemException('', '', OSError('', errorCode))); @@ -345,7 +346,7 @@ void main() { throwsToolExit(message: expectedMessage)); }); - testWithoutContext('When reading from a file without permission', () { + testWithoutContext('When reading from a file or directory without permission', () { setupReadMocks( mockFileSystem: mockFileSystem, fs: fs, @@ -357,6 +358,8 @@ void main() { const String expectedMessage = 'Flutter failed to read a file at'; expect(() => file.readAsStringSync(), throwsToolExit(message: expectedMessage)); + expect(() => fs.currentDirectory, + throwsToolExit(message: 'The flutter tool cannot access the file or directory')); }); }); @@ -579,7 +582,7 @@ void main() { throwsToolExit(message: expectedMessage)); }); - testWithoutContext('When reading from a file without permission', () { + testWithoutContext('When reading from a file or directory without permission', () { setupReadMocks( mockFileSystem: mockFileSystem, fs: fs, @@ -591,6 +594,8 @@ void main() { const String expectedMessage = 'Flutter failed to read a file at'; expect(() => file.readAsStringSync(), throwsToolExit(message: expectedMessage)); + expect(() => fs.currentDirectory, + throwsToolExit(message: 'The flutter tool cannot access the file or directory')); }); });