Unverified Commit f0a3cbac authored by Jonah Williams's avatar Jonah Williams Committed by GitHub

[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
parent bd7e2eee
......@@ -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) {
......
......@@ -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;
}
}
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