Unverified Commit 65549d77 authored by Jonah Williams's avatar Jonah Williams Committed by GitHub

[flutter_tools] reduce mocking in error handling io (#84443)

parent 80668d20
...@@ -39,23 +39,6 @@ final Platform macOSPlatform = FakePlatform( ...@@ -39,23 +39,6 @@ final Platform macOSPlatform = FakePlatform(
environment: <String, String>{} environment: <String, String>{}
); );
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({ void setupDirectoryMocks({
FileSystem mockFileSystem, FileSystem mockFileSystem,
ErrorHandlingFileSystem fs, ErrorHandlingFileSystem fs,
...@@ -367,19 +350,31 @@ void main() { ...@@ -367,19 +350,31 @@ void main() {
throwsToolExit(message: expectedMessage)); throwsToolExit(message: expectedMessage));
}); });
testWithoutContext('When reading from a file or directory without permission', () { testWithoutContext('When reading from a file without permission', () {
setupReadMocks( final ErrorHandlingFileSystem fileSystem = ErrorHandlingFileSystem(
mockFileSystem: mockFileSystem, delegate: MemoryFileSystem.test(opHandle: exceptionHandler.opHandle),
fs: fs, platform: windowsPlatform,
errorCode: kUserPermissionDenied,
); );
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'; const String expectedMessage = 'Flutter failed to read a file at';
expect(() => file.readAsStringSync(), expect(() => file.readAsStringSync(),
throwsToolExit(message: expectedMessage)); 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')); throwsToolExit(message: 'The flutter tool cannot access the file or directory'));
}); });
}); });
...@@ -563,16 +558,29 @@ void main() { ...@@ -563,16 +558,29 @@ void main() {
}); });
testWithoutContext('When the current working directory disappears', () async { testWithoutContext('When the current working directory disappears', () async {
setupReadMocks( final ErrorHandlingFileSystem fileSystem = ErrorHandlingFileSystem(
mockFileSystem: mockFileSystem, delegate: ThrowsOnCurrentDirectoryFileSystem()..errorCode = kSystemCannotFindFile,
fs: fs, platform: linuxPlatform,
errorCode: kSystemCannotFindFile,
); );
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. // 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() { ...@@ -753,19 +761,31 @@ void main() {
throwsToolExit(message: expectedMessage)); throwsToolExit(message: expectedMessage));
}); });
testWithoutContext('When reading from a file or directory without permission', () { testWithoutContext('When reading from a file without permission', () {
setupReadMocks( final ErrorHandlingFileSystem fileSystem = ErrorHandlingFileSystem(
mockFileSystem: mockFileSystem, delegate: MemoryFileSystem.test(opHandle: exceptionHandler.opHandle),
fs: fs, platform: linuxPlatform,
errorCode: eacces,
); );
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'; const String expectedMessage = 'Flutter failed to read a file at';
expect(() => file.readAsStringSync(), expect(() => file.readAsStringSync(),
throwsToolExit(message: expectedMessage)); 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')); throwsToolExit(message: 'The flutter tool cannot access the file or directory'));
}); });
}); });
...@@ -1250,3 +1270,10 @@ class ThrowingFakeProcessManager extends Fake implements ProcessManager { ...@@ -1250,3 +1270,10 @@ class ThrowingFakeProcessManager extends Fake implements ProcessManager {
throw _exception; throw _exception;
} }
} }
class ThrowsOnCurrentDirectoryFileSystem extends Fake implements FileSystem {
int errorCode;
@override
Directory get currentDirectory => throw FileSystemException('', '', OSError('', errorCode));
}
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