Add Flutter-repo-specific golden comparator (#17205)

In order to avoid checking binaries into flutter/flutter,
this comparator can be used to retrieve golden files from
a sibling flutter/goldens repository.

https://github.com/flutter/flutter/issues/16859
This commit is contained in:
Todd Volkert
2018-05-03 07:39:41 -07:00
committed by GitHub
parent 50afda01e1
commit 65079ad5f2
12 changed files with 482 additions and 35 deletions

View File

@@ -66,7 +66,11 @@ abstract class GoldenFileComparator {
///
/// * [flutter_test] for more information about how to configure tests at the
/// directory-level.
GoldenFileComparator goldenFileComparator = const _UninitializedComparator();
GoldenFileComparator _goldenFileComparator = const _UninitializedComparator();
GoldenFileComparator get goldenFileComparator => _goldenFileComparator;
set goldenFileComparator(GoldenFileComparator comparator) {
_goldenFileComparator = comparator ?? const _UninitializedComparator();
}
/// Whether golden files should be automatically updated during tests rather
/// than compared to the image bytes recorded by the tests.
@@ -120,15 +124,19 @@ class LocalFileComparator implements GoldenFileComparator {
/// directory in which [testFile] resides.
///
/// The [testFile] URI must represent a file.
LocalFileComparator(Uri testFile)
LocalFileComparator(Uri testFile, {path.Style pathStyle})
: assert(testFile.scheme == 'file'),
basedir = new Uri.directory(_path.dirname(_path.fromUri(testFile)));
basedir = _getBasedir(testFile, pathStyle),
_path = _getPath(pathStyle);
// Due to https://github.com/flutter/flutter/issues/17118, we need to
// explicitly set the path style.
static final path.Context _path = new path.Context(style: Platform.isWindows
? path.Style.windows
: path.Style.posix);
static path.Context _getPath(path.Style style) {
return new path.Context(style: style ?? path.Style.platform);
}
static Uri _getBasedir(Uri testFile, path.Style pathStyle) {
final path.Context context = _getPath(pathStyle);
return context.toUri(context.dirname(context.fromUri(testFile)) + context.separator);
}
/// The directory in which the test was loaded.
///
@@ -136,6 +144,11 @@ class LocalFileComparator implements GoldenFileComparator {
/// directory.
final Uri basedir;
/// Path context exists as an instance variable rather than just using the
/// system path context in order to support testing, where we can spoof the
/// platform to test behaviors with arbitrary path styles.
final path.Context _path;
@override
Future<bool> compare(Uint8List imageBytes, Uri golden) async {
final File goldenFile = _getFile(golden);
@@ -149,11 +162,12 @@ class LocalFileComparator implements GoldenFileComparator {
@override
Future<void> update(Uri golden, Uint8List imageBytes) async {
final File goldenFile = _getFile(golden);
await goldenFile.parent.create(recursive: true);
await goldenFile.writeAsBytes(imageBytes, flush: true);
}
File _getFile(Uri golden) {
return new File(_path.join(_path.fromUri(basedir), golden.path));
return new File(_path.join(_path.fromUri(basedir), _path.fromUri(golden.path)));
}
static bool _areListsEqual<T>(List<T> list1, List<T> list2) {