From 5e6146679449afb44810bdd7120bee0725405d75 Mon Sep 17 00:00:00 2001 From: Jenn Magder Date: Wed, 17 Feb 2021 23:41:04 -0800 Subject: [PATCH] Adopt FileExceptionHandler in a few tests (#76279) --- .../commands.shard/hermetic/clean_test.dart | 18 ++++++++--- .../test/general.shard/cache_test.dart | 31 ++++++++++++++----- .../test/general.shard/web/chrome_test.dart | 1 - 3 files changed, 36 insertions(+), 14 deletions(-) diff --git a/packages/flutter_tools/test/commands.shard/hermetic/clean_test.dart b/packages/flutter_tools/test/commands.shard/hermetic/clean_test.dart index fbdbffeb15..6bb4bcb1b7 100644 --- a/packages/flutter_tools/test/commands.shard/hermetic/clean_test.dart +++ b/packages/flutter_tools/test/commands.shard/hermetic/clean_test.dart @@ -116,24 +116,32 @@ void main() { group('Windows', () { FakePlatform windowsPlatform; + MemoryFileSystem fileSystem; + FileExceptionHandler exceptionHandler; setUp(() { windowsPlatform = FakePlatform(operatingSystem: 'windows'); + exceptionHandler = FileExceptionHandler(); + fileSystem = MemoryFileSystem.test(opHandle: exceptionHandler.opHandle); }); testUsingContext('$CleanCommand prints a helpful error message on Windows', () async { when(mockXcodeProjectInterpreter.isInstalled).thenReturn(false); - final MockFile mockFile = MockFile(); - when(mockFile.existsSync()).thenReturn(true); + final File file = fileSystem.file('file')..createSync(); + exceptionHandler.addError( + file, + FileSystemOp.delete, + const FileSystemException('Deletion failed'), + ); - when(mockFile.deleteSync(recursive: true)).thenThrow(const FileSystemException('Deletion failed')); final CleanCommand command = CleanCommand(); - command.deleteFile(mockFile); + command.deleteFile(file); expect(testLogger.errorText, contains('A program may still be using a file')); - verify(mockFile.deleteSync(recursive: true)).called(1); }, overrides: { Platform: () => windowsPlatform, Xcode: () => xcode, + FileSystem: () => fileSystem, + ProcessManager: () => FakeProcessManager.any(), }); testUsingContext('$CleanCommand handles missing permissions;', () async { diff --git a/packages/flutter_tools/test/general.shard/cache_test.dart b/packages/flutter_tools/test/general.shard/cache_test.dart index 429194b056..e3e094edf9 100644 --- a/packages/flutter_tools/test/general.shard/cache_test.dart +++ b/packages/flutter_tools/test/general.shard/cache_test.dart @@ -665,7 +665,8 @@ void main() { }); testWithoutContext('Cache handles exception thrown if stamp file cannot be parsed', () { - final FileSystem fileSystem = MemoryFileSystem.test(); + final FileExceptionHandler exceptionHandler = FileExceptionHandler(); + final FileSystem fileSystem = MemoryFileSystem.test(opHandle: exceptionHandler.opHandle); final Logger logger = BufferLogger.test(); final FakeCache cache = FakeCache( fileSystem: fileSystem, @@ -673,19 +674,33 @@ void main() { platform: FakePlatform(), osUtils: MockOperatingSystemUtils() ); - final MockFile file = MockFile(); + final File file = fileSystem.file('stamp'); cache.stampFile = file; - when(file.existsSync()).thenReturn(false); expect(cache.getStampFor('foo'), null); - when(file.existsSync()).thenReturn(true); - when(file.readAsStringSync()).thenThrow(const FileSystemException()); + file.createSync(); + exceptionHandler.addError( + file, + FileSystemOp.read, + const FileSystemException(), + ); expect(cache.getStampFor('foo'), null); + }); - when(file.existsSync()).thenReturn(true); - when(file.readAsStringSync()).thenReturn('ABC '); + testWithoutContext('Cache parses stamp file', () { + final FileSystem fileSystem = MemoryFileSystem.test(); + final Logger logger = BufferLogger.test(); + final FakeCache cache = FakeCache( + fileSystem: fileSystem, + logger: logger, + platform: FakePlatform(), + osUtils: MockOperatingSystemUtils() + ); + + final File file = fileSystem.file('stamp')..writeAsStringSync('ABC '); + cache.stampFile = file; expect(cache.getStampFor('foo'), 'ABC'); }); @@ -860,7 +875,6 @@ class FakeDownloadedArtifact extends CachedArtifact { } class MockArtifactUpdater extends Mock implements ArtifactUpdater {} -class MockFile extends Mock implements File {} class MockCachedArtifact extends Mock implements CachedArtifact {} class MockIosUsbArtifacts extends Mock implements IosUsbArtifacts {} class MockInternetAddress extends Mock implements InternetAddress {} @@ -868,6 +882,7 @@ class MockCache extends Mock implements Cache {} class MockOperatingSystemUtils extends Mock implements OperatingSystemUtils {} class MockVersionedPackageResolver extends Mock implements VersionedPackageResolver {} class MockPub extends Mock implements Pub {} + class FakeCache extends Cache { FakeCache({ @required Logger logger, diff --git a/packages/flutter_tools/test/general.shard/web/chrome_test.dart b/packages/flutter_tools/test/general.shard/web/chrome_test.dart index 94c7f38407..8571b9374a 100644 --- a/packages/flutter_tools/test/general.shard/web/chrome_test.dart +++ b/packages/flutter_tools/test/general.shard/web/chrome_test.dart @@ -356,7 +356,6 @@ void main() { }); } -class MockFileSystemUtils extends Mock implements FileSystemUtils {} class MockOperatingSystemUtils extends Mock implements OperatingSystemUtils {} Future _testLaunchChrome(String userDataDir, FakeProcessManager processManager, ChromiumLauncher chromeLauncher) {