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(
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({
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));
}
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