From 91437a0641eba1fd1bf9bb1159d3cdcab9188283 Mon Sep 17 00:00:00 2001 From: Jonah Williams Date: Wed, 27 Jan 2021 12:56:21 -0800 Subject: [PATCH] [flutter_tools] delete old directories when unzipping ontop of them (#74818) Fixes #74772 stale files from previous SDKs were getting left in the cache, confusing the analyzer. --- packages/flutter_tools/lib/src/cache.dart | 6 ++++ .../general.shard/artifact_updater_test.dart | 29 +++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/packages/flutter_tools/lib/src/cache.dart b/packages/flutter_tools/lib/src/cache.dart index 5d1484ca60..177ab4564c 100644 --- a/packages/flutter_tools/lib/src/cache.dart +++ b/packages/flutter_tools/lib/src/cache.dart @@ -1789,6 +1789,12 @@ class ArtifactUpdater { } finally { status.stop(); } + /// Unzipping multiple file into a directory will not remove old files + /// from previous versions that are not present in the new bundle. + final Directory destination = location.childDirectory( + tempFile.fileSystem.path.basenameWithoutExtension(tempFile.path) + ); + ErrorHandlingFileSystem.deleteIfExists(destination, recursive: true); _ensureExists(location); try { diff --git a/packages/flutter_tools/test/general.shard/artifact_updater_test.dart b/packages/flutter_tools/test/general.shard/artifact_updater_test.dart index 46acae6830..6ecb664841 100644 --- a/packages/flutter_tools/test/general.shard/artifact_updater_test.dart +++ b/packages/flutter_tools/test/general.shard/artifact_updater_test.dart @@ -44,6 +44,35 @@ void main() { expect(fileSystem.file('out/test'), exists); }); + testWithoutContext('ArtifactUpdater can download a zip archive and delete stale files', () async { + final MockOperatingSystemUtils operatingSystemUtils = MockOperatingSystemUtils(); + final MemoryFileSystem fileSystem = MemoryFileSystem.test(); + final BufferLogger logger = BufferLogger.test(); + final ArtifactUpdater artifactUpdater = ArtifactUpdater( + fileSystem: fileSystem, + logger: logger, + operatingSystemUtils: operatingSystemUtils, + platform: testPlatform, + httpClient: MockHttpClient(), + tempStorage: fileSystem.currentDirectory.childDirectory('temp') + ..createSync(), + ); + // Unrelated file from another cache. + fileSystem.file('out/bar').createSync(recursive: true); + // Stale file from current cache. + fileSystem.file('out/test/foo.txt').createSync(recursive: true); + + await artifactUpdater.downloadZipArchive( + 'test message', + Uri.parse('http:///test.zip'), + fileSystem.currentDirectory.childDirectory('out'), + ); + expect(logger.statusText, contains('test message')); + expect(fileSystem.file('out/test'), exists); + expect(fileSystem.file('out/bar'), exists); + expect(fileSystem.file('out/test/foo.txt'), isNot(exists)); + }); + testWithoutContext('ArtifactUpdater will not validate the md5 hash if the ' 'x-goog-hash header is present but missing an md5 entry', () async { final MockOperatingSystemUtils operatingSystemUtils = MockOperatingSystemUtils();