Make LocalFileComparator.compare easier to override (#77825)

This commit is contained in:
Kate Lovett 2021-03-10 14:20:04 -06:00 committed by GitHub
parent 0cc087c2ae
commit 9840d65943
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -91,16 +91,9 @@ class LocalFileComparator extends GoldenFileComparator with LocalComparisonOutpu
@override
Future<bool> compare(Uint8List imageBytes, Uri golden) async {
final File goldenFile = _getGoldenFile(golden);
if (!goldenFile.existsSync()) {
throw test_package.TestFailure(
'Could not be compared against non-existent file: "$golden"'
);
}
final List<int> goldenBytes = await goldenFile.readAsBytes();
final ComparisonResult result = await GoldenFileComparator.compareLists(
imageBytes,
goldenBytes,
await getGoldenBytes(golden),
);
if (!result.passed) {
@ -117,9 +110,22 @@ class LocalFileComparator extends GoldenFileComparator with LocalComparisonOutpu
await goldenFile.writeAsBytes(imageBytes, flush: true);
}
File _getGoldenFile(Uri golden) {
return File(_path.join(_path.fromUri(basedir), _path.fromUri(golden.path)));
/// Returns the bytes of the given [golden] file.
///
/// If the file cannot be found, an error will be thrown.
@protected
Future<List<int>> getGoldenBytes(Uri golden) async {
final File goldenFile = _getGoldenFile(golden);
if (!goldenFile.existsSync()) {
throw test_package.TestFailure(
'Could not be compared against non-existent file: "$golden"'
);
}
final List<int> goldenBytes = await goldenFile.readAsBytes();
return goldenBytes;
}
File _getGoldenFile(Uri golden) => File(_path.join(_path.fromUri(basedir), _path.fromUri(golden.path)));
}
/// A class for use in golden file comparators that run locally and provide