From ed85dd49c7910f7c870856d9ac7f1184d0790ba8 Mon Sep 17 00:00:00 2001 From: Jonah Williams Date: Thu, 13 May 2021 21:44:02 -0700 Subject: [PATCH] [flutter_tools] use try to delete in web cache (#82478) --- .../flutter_tools/lib/src/flutter_cache.dart | 4 +--- .../test/general.shard/cache_test.dart | 23 +++++++++++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/packages/flutter_tools/lib/src/flutter_cache.dart b/packages/flutter_tools/lib/src/flutter_cache.dart index a9feb1b971..73dd9d30d5 100644 --- a/packages/flutter_tools/lib/src/flutter_cache.dart +++ b/packages/flutter_tools/lib/src/flutter_cache.dart @@ -183,9 +183,7 @@ class FlutterWebSdk extends CachedArtifact { platformName += 'windows-x64'; } final Uri url = Uri.parse('${cache.storageBaseUrl}/flutter_infra_release/flutter/$version/$platformName.zip'); - if (location.existsSync()) { - location.deleteSync(recursive: true); - } + ErrorHandlingFileSystem.deleteIfExists(location, recursive: true); await artifactUpdater.downloadZipArchive('Downloading Web SDK...', url, location); // This is a temporary work-around for not being able to safely download into a shared directory. final FileSystem fileSystem = location.fileSystem; diff --git a/packages/flutter_tools/test/general.shard/cache_test.dart b/packages/flutter_tools/test/general.shard/cache_test.dart index 67b8d03d5e..79274ad366 100644 --- a/packages/flutter_tools/test/general.shard/cache_test.dart +++ b/packages/flutter_tools/test/general.shard/cache_test.dart @@ -745,6 +745,29 @@ void main() { expect(webStuff.childFile('bar'), isNot(exists)); }); + testWithoutContext('FlutterWebSdk uses tryToDelete to handle directory edge cases', () async { + final FileExceptionHandler handler = FileExceptionHandler(); + final MemoryFileSystem fileSystem = MemoryFileSystem.test(opHandle: handler.opHandle); + final Directory webStuff = fileSystem.directory('web-stuff'); + final MockCache cache = MockCache(); + final MockArtifactUpdater artifactUpdater = MockArtifactUpdater(); + final FlutterWebSdk webSdk = FlutterWebSdk(cache, platform: FakePlatform(operatingSystem: 'linux')); + + when(cache.getWebSdkDirectory()).thenReturn(webStuff); + when(artifactUpdater.downloadZipArchive('Downloading Web SDK...', any, any)) + .thenAnswer((Invocation invocation) async { + final Directory location = invocation.positionalArguments[2] as Directory; + location.createSync(recursive: true); + location.childFile('foo').createSync(); + }); + webStuff.childFile('bar').createSync(recursive: true); + handler.addError(webStuff, FileSystemOp.delete, const FileSystemException('', '', OSError('', 2))); + + await expectLater(() => webSdk.updateInner(artifactUpdater, fileSystem, MockOperatingSystemUtils()), throwsToolExit( + message: RegExp('The Flutter tool tried to delete the file or directory web-stuff but was unable to'), + )); + }); + testWithoutContext('Cache handles exception thrown if stamp file cannot be parsed', () { final FileExceptionHandler exceptionHandler = FileExceptionHandler(); final FileSystem fileSystem = MemoryFileSystem.test(opHandle: exceptionHandler.opHandle);