[flutter_tools] handle FileSystemException thrown when attempting to decode stamp file (#64559)

Fixes #64246

If decoding the string contents of a stamp file fails, treat it as if the stamp file is missing. This is always safe to do, since at worst it downloads more artifacts. The files could end up corrupted if they were edited by another program, for example
This commit is contained in:
Jonah Williams
2020-08-25 10:58:21 -07:00
committed by GitHub
parent bd7e2eeee9
commit f0a3cbace3
2 changed files with 58 additions and 1 deletions

View File

@@ -399,9 +399,19 @@ class Cache {
}
}
/// Read the stamp for [artifactName].
///
/// If the file is missing or cannot be parsed, returns `null`.
String getStampFor(String artifactName) {
final File stampFile = getStampFileFor(artifactName);
return stampFile.existsSync() ? stampFile.readAsStringSync().trim() : null;
if (!stampFile.existsSync()) {
return null;
}
try {
return stampFile.readAsStringSync().trim();
} on FileSystemException {
return null;
}
}
void setStampFor(String artifactName, String version) {

View File

@@ -634,6 +634,32 @@ void main() {
expect(logger.errorText, contains('Failed to delete some stamp files'));
});
testWithoutContext('Cache handles exception thrown if stamp file cannot be parsed', () {
final FileSystem fileSystem = MemoryFileSystem.test();
final Logger logger = BufferLogger.test();
final FakeCache cache = FakeCache(
fileSystem: fileSystem,
logger: logger,
platform: FakePlatform(),
osUtils: MockOperatingSystemUtils()
);
final MockFile file = MockFile();
cache.stampFile = file;
when(file.existsSync()).thenReturn(false);
expect(cache.getStampFor('foo'), null);
when(file.existsSync()).thenReturn(true);
when(file.readAsStringSync()).thenThrow(const FileSystemException());
expect(cache.getStampFor('foo'), null);
when(file.existsSync()).thenReturn(true);
when(file.readAsStringSync()).thenReturn('ABC ');
expect(cache.getStampFor('foo'), 'ABC');
});
}
class FakeCachedArtifact extends EngineCachedArtifact {
@@ -698,3 +724,24 @@ class MockInternetAddress extends Mock implements InternetAddress {}
class MockCache extends Mock implements Cache {}
class MockOperatingSystemUtils extends Mock implements OperatingSystemUtils {}
class MockVersionedPackageResolver extends Mock implements VersionedPackageResolver {}
class FakeCache extends Cache {
FakeCache({
@required Logger logger,
@required FileSystem fileSystem,
@required Platform platform,
@required OperatingSystemUtils osUtils,
}) : super(
logger: logger,
fileSystem: fileSystem,
platform: platform,
osUtils: osUtils,
artifacts: <ArtifactSet>[],
);
File stampFile;
@override
File getStampFileFor(String artifactName) {
return stampFile;
}
}