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) {

View File

@@ -24,6 +24,16 @@ void main() {
fs = new MemoryFileSystem(style: style);
});
/// Converts posix-style paths to the style associated with [fs].
///
/// This allows us to deal in posix-style paths in the tests.
String fix(String path) {
if (path.startsWith('/')) {
path = '${fs.style.drive}$path';
}
return path.replaceAll('/', fs.path.separator);
}
void test(String description, FutureOr<void> body()) {
test_package.test(description, () {
return io.IOOverrides.runZoned(
@@ -59,18 +69,18 @@ void main() {
LocalFileComparator comparator;
setUp(() {
comparator = new LocalFileComparator(new Uri.file('/golden_test.dart'));
comparator = new LocalFileComparator(fs.file(fix('/golden_test.dart')).uri, pathStyle: fs.path.style);
});
test('calculates basedir correctly', () {
expect(comparator.basedir, new Uri.file('/'));
comparator = new LocalFileComparator(new Uri.file('/foo/bar/golden_test.dart'));
expect(comparator.basedir, new Uri.file('/foo/bar/'));
expect(comparator.basedir, fs.file(fix('/')).uri);
comparator = new LocalFileComparator(fs.file(fix('/foo/bar/golden_test.dart')).uri, pathStyle: fs.path.style);
expect(comparator.basedir, fs.directory(fix('/foo/bar/')).uri);
});
group('compare', () {
Future<bool> doComparison([String golden = 'golden.png']) {
final Uri uri = new Uri.file(golden);
final Uri uri = fs.file(fix(golden)).uri;
return comparator.compare(
new Uint8List.fromList(_kExpectedBytes),
uri,
@@ -79,13 +89,13 @@ void main() {
group('succeeds', () {
test('when golden file is in same folder as test', () async {
fs.file('/golden.png').writeAsBytesSync(_kExpectedBytes);
fs.file(fix('/golden.png')).writeAsBytesSync(_kExpectedBytes);
final bool success = await doComparison();
expect(success, isTrue);
});
test('when golden file is in subfolder of test', () async {
fs.file('/sub/foo.png')
fs.file(fix('/sub/foo.png'))
..createSync(recursive: true)
..writeAsBytesSync(_kExpectedBytes);
final bool success = await doComparison('sub/foo.png');
@@ -100,32 +110,32 @@ void main() {
});
test('when golden bytes are leading subset of image bytes', () async {
fs.file('/golden.png').writeAsBytesSync(<int>[1, 2]);
fs.file(fix('/golden.png')).writeAsBytesSync(<int>[1, 2]);
expect(await doComparison(), isFalse);
});
test('when golden bytes are leading superset of image bytes', () async {
fs.file('/golden.png').writeAsBytesSync(<int>[1, 2, 3, 4]);
fs.file(fix('/golden.png')).writeAsBytesSync(<int>[1, 2, 3, 4]);
expect(await doComparison(), isFalse);
});
test('when golden bytes are trailing subset of image bytes', () async {
fs.file('/golden.png').writeAsBytesSync(<int>[2, 3]);
fs.file(fix('/golden.png')).writeAsBytesSync(<int>[2, 3]);
expect(await doComparison(), isFalse);
});
test('when golden bytes are trailing superset of image bytes', () async {
fs.file('/golden.png').writeAsBytesSync(<int>[0, 1, 2, 3]);
fs.file(fix('/golden.png')).writeAsBytesSync(<int>[0, 1, 2, 3]);
expect(await doComparison(), isFalse);
});
test('when golden bytes are disjoint from image bytes', () async {
fs.file('/golden.png').writeAsBytesSync(<int>[4, 5, 6]);
fs.file(fix('/golden.png')).writeAsBytesSync(<int>[4, 5, 6]);
expect(await doComparison(), isFalse);
});
test('when golden bytes are empty', () async {
fs.file('/golden.png').writeAsBytesSync(<int>[]);
fs.file(fix('/golden.png')).writeAsBytesSync(<int>[]);
expect(await doComparison(), isFalse);
});
});
@@ -133,18 +143,18 @@ void main() {
group('update', () {
test('updates existing file', () async {
fs.file('/golden.png').writeAsBytesSync(_kExpectedBytes);
fs.file(fix('/golden.png')).writeAsBytesSync(_kExpectedBytes);
const List<int> newBytes = const <int>[11, 12, 13];
await comparator.update(new Uri.file('golden.png'), new Uint8List.fromList(newBytes));
expect(fs.file('/golden.png').readAsBytesSync(), newBytes);
await comparator.update(fs.file('golden.png').uri, new Uint8List.fromList(newBytes));
expect(fs.file(fix('/golden.png')).readAsBytesSync(), newBytes);
});
test('creates non-existent file', () async {
expect(fs.file('/foo.png').existsSync(), isFalse);
expect(fs.file(fix('/foo.png')).existsSync(), isFalse);
const List<int> newBytes = const <int>[11, 12, 13];
await comparator.update(new Uri.file('foo.png'), new Uint8List.fromList(newBytes));
expect(fs.file('/foo.png').existsSync(), isTrue);
expect(fs.file('/foo.png').readAsBytesSync(), newBytes);
await comparator.update(fs.file('foo.png').uri, new Uint8List.fromList(newBytes));
expect(fs.file(fix('/foo.png')).existsSync(), isTrue);
expect(fs.file(fix('/foo.png')).readAsBytesSync(), newBytes);
});
});
});