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 ff98d6d3f1..2d8958b0f1 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 @@ -39,23 +39,6 @@ final Platform macOSPlatform = FakePlatform( environment: {} ); -void setupReadMocks({ - FileSystem mockFileSystem, - ErrorHandlingFileSystem fs, - int errorCode, -}) { - final MockFile mockFile = MockFile(); - final MockDirectory mockParentDirectory = MockDirectory(); - when(mockFileSystem.file(any)).thenReturn(mockFile); - when(mockFile.path).thenReturn('parent/file'); - when(mockFile.parent).thenReturn(mockParentDirectory); - when(mockParentDirectory.path).thenReturn('parent'); - when(mockFileSystem.currentDirectory).thenThrow(FileSystemException('', '', OSError('', errorCode))); - when(mockFile.readAsStringSync( - encoding: anyNamed('encoding'), - )).thenThrow(FileSystemException('', '', OSError('', errorCode))); -} - void setupDirectoryMocks({ FileSystem mockFileSystem, ErrorHandlingFileSystem fs, @@ -367,19 +350,31 @@ void main() { throwsToolExit(message: expectedMessage)); }); - testWithoutContext('When reading from a file or directory without permission', () { - setupReadMocks( - mockFileSystem: mockFileSystem, - fs: fs, - errorCode: kUserPermissionDenied, + testWithoutContext('When reading from a file without permission', () { + final ErrorHandlingFileSystem fileSystem = ErrorHandlingFileSystem( + delegate: MemoryFileSystem.test(opHandle: exceptionHandler.opHandle), + platform: windowsPlatform, ); + final File file = fileSystem.file('file'); - final File file = fs.file('file'); + exceptionHandler.addError( + file, + FileSystemOp.read, + FileSystemException('', file.path, const OSError('', kUserPermissionDenied)), + ); const String expectedMessage = 'Flutter failed to read a file at'; expect(() => file.readAsStringSync(), throwsToolExit(message: expectedMessage)); - expect(() => fs.currentDirectory, + }); + + testWithoutContext('When reading from a file or directory without permission', () { + final ErrorHandlingFileSystem fileSystem = ErrorHandlingFileSystem( + delegate: ThrowsOnCurrentDirectoryFileSystem()..errorCode = kUserPermissionDenied, + platform: windowsPlatform, + ); + + expect(() => fileSystem.currentDirectory, throwsToolExit(message: 'The flutter tool cannot access the file or directory')); }); }); @@ -563,16 +558,29 @@ void main() { }); testWithoutContext('When the current working directory disappears', () async { - setupReadMocks( - mockFileSystem: mockFileSystem, - fs: fs, - errorCode: kSystemCannotFindFile, + final ErrorHandlingFileSystem fileSystem = ErrorHandlingFileSystem( + delegate: ThrowsOnCurrentDirectoryFileSystem()..errorCode = kSystemCannotFindFile, + platform: linuxPlatform, ); - expect(() => fs.currentDirectory, throwsToolExit(message: 'Unable to read current working directory')); + expect(() => fileSystem.currentDirectory, throwsToolExit(message: 'Unable to read current working directory')); + }); + + testWithoutContext('Rethrows os error $kSystemCannotFindFile', () { + final ErrorHandlingFileSystem fileSystem = ErrorHandlingFileSystem( + delegate: MemoryFileSystem.test(opHandle: exceptionHandler.opHandle), + platform: linuxPlatform, + ); + final File file = fileSystem.file('file'); + + exceptionHandler.addError( + file, + FileSystemOp.read, + FileSystemException('', file.path, const OSError('', kSystemCannotFindFile)), + ); // Error is not caught by other operations. - expect(() => fs.file('foo').readAsStringSync(), throwsFileSystemException(kSystemCannotFindFile)); + expect(() => fileSystem.file('foo').readAsStringSync(), throwsFileSystemException(kSystemCannotFindFile)); }); }); @@ -753,19 +761,31 @@ void main() { throwsToolExit(message: expectedMessage)); }); - testWithoutContext('When reading from a file or directory without permission', () { - setupReadMocks( - mockFileSystem: mockFileSystem, - fs: fs, - errorCode: eacces, + testWithoutContext('When reading from a file without permission', () { + final ErrorHandlingFileSystem fileSystem = ErrorHandlingFileSystem( + delegate: MemoryFileSystem.test(opHandle: exceptionHandler.opHandle), + platform: linuxPlatform, ); + final File file = fileSystem.file('file'); - final File file = fs.file('file'); + exceptionHandler.addError( + file, + FileSystemOp.read, + FileSystemException('', file.path, const OSError('', eacces)), + ); const String expectedMessage = 'Flutter failed to read a file at'; expect(() => file.readAsStringSync(), throwsToolExit(message: expectedMessage)); - expect(() => fs.currentDirectory, + }); + + testWithoutContext('When reading from current directory without permission', () { + final ErrorHandlingFileSystem fileSystem = ErrorHandlingFileSystem( + delegate: ThrowsOnCurrentDirectoryFileSystem()..errorCode = eacces, + platform: linuxPlatform, + ); + + expect(() => fileSystem.currentDirectory, throwsToolExit(message: 'The flutter tool cannot access the file or directory')); }); }); @@ -1250,3 +1270,10 @@ class ThrowingFakeProcessManager extends Fake implements ProcessManager { throw _exception; } } + +class ThrowsOnCurrentDirectoryFileSystem extends Fake implements FileSystem { + int errorCode; + + @override + Directory get currentDirectory => throw FileSystemException('', '', OSError('', errorCode)); +}