Added tool to easily check golden diffs locally. (flutter/engine#50654)

This addresses the problem where people feel like they have to upload a PR to get diffs from Skia Gold.  This should work for most workflows.

[C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style
This commit is contained in:
gaaclarke
2024-02-15 09:50:06 -08:00
committed by GitHub
parent 7acb0fb5ff
commit fcbd40e8e4
6 changed files with 141 additions and 0 deletions

View File

@@ -0,0 +1,37 @@
# Compare Goldens
This is a script that will let you check golden image diffs locally.
The directories are scanned for png files that match in name, then the diff
is written to `diff_<name of file>` in the CWD. This allows you to get
results quicker than having to upload to skia gold. By default it uses fuzzy
RMSE to compare.
## Usage
```sh
dart run compare_goldens <dir path> <dir path>
```
Here's the steps for using this with something like impeller golden tests:
1) Checkout a base revision
2) Build impeller_golden_tests
3) Execute `impeller_golden_tests --working_dir=\<path a\>
4) Checkout test revision
5) Build impeller_golden_tests
6) Execute `impeller_golden_tests --working_dir=\<path b\>
7) Execute `compare_goldens \<path a\> \<path b\>
## Requirements
- ImageMagick is installed on $PATH
## Testing
To run the tests:
```sh
dart pub get
find . -name "*_test.dart" | xargs -n 1 dart --enable-asserts
```

View File

@@ -0,0 +1,10 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:io' show exitCode;
import 'package:compare_goldens/compare_goldens.dart' as compare_goldens;
void main(List<String> args) {
exitCode = compare_goldens.run(args);
}

View File

@@ -0,0 +1,85 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:io';
bool _hasCommandOnPath(String name) {
final ProcessResult result = Process.runSync('which', <String>[name]);
return result.exitCode == 0;
}
List<String> _findPairs(Set<String> as, Set<String> bs) {
final List<String> result = <String>[];
for (final String a in as) {
if (bs.contains(a)) {
result.add(a);
} else {
print('Mix match file $a.');
}
}
for (final String b in bs) {
if (!as.contains(b)) {
print('Mix match file $b.');
}
}
return result;
}
String _basename(String path) {
return path.split(Platform.pathSeparator).last;
}
Set<String> _grabPngFilenames(Directory dir) {
return dir.listSync()
.map((FileSystemEntity e) => _basename(e.path))
.where((String e) => e.endsWith('.png'))
.toSet();
}
/// The main entry point to the tool, execute it like `main`. Returns the
/// `exitCode`.
int run(List<String> args) {
int returnCode = 0;
if (!_hasCommandOnPath('compare')) {
throw Exception(r'Could not find `compare` from ImageMagick on $PATH.');
}
if (args.length != 2) {
throw Exception('Usage: compare_goldens.dart <dir path> <dir path>');
}
final Directory dirA = Directory(args[0]);
if (!dirA.existsSync()) {
throw Exception('Unable to find $dirA');
}
final Directory dirB = Directory(args[1]);
if (!dirB.existsSync()) {
throw Exception('Unable to find $dirB');
}
final Set<String> filesA = _grabPngFilenames(dirA);
final Set<String> filesB = _grabPngFilenames(dirB);
final List<String> pairs = _findPairs(filesA, filesB);
if (filesA.length != pairs.length || filesB.length != pairs.length) {
returnCode = 1;
}
int count = 0;
for (final String name in pairs) {
count += 1;
final String pathA = <String>[dirA.path, name].join(Platform.pathSeparator);
final String pathB = <String>[dirB.path, name].join(Platform.pathSeparator);
final String output = 'diff_$name';
print('compare ($count / ${pairs.length}) $name');
final ProcessResult result = Process.runSync('compare',
<String>['-metric', 'RMSE', '-fuzz', '5%', pathA, pathB, output]);
if (result.exitCode != 0) {
print('DIFF FOUND: saved to $output');
returnCode = 1;
} else {
File(output).deleteSync();
}
}
return returnCode;
}

View File

@@ -0,0 +1,3 @@
name: compare_goldens
environment:
sdk: '>=3.2.0-0 <4.0.0'

View File

@@ -0,0 +1,5 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
void main() {}

View File

@@ -34,6 +34,7 @@ ALL_PACKAGES = [
os.path.join(ENGINE_DIR, 'tools', 'api_check'),
os.path.join(ENGINE_DIR, 'tools', 'build_bucket_golden_scraper'),
os.path.join(ENGINE_DIR, 'tools', 'clang_tidy'),
os.path.join(ENGINE_DIR, 'tools', 'compare_goldens'),
os.path.join(ENGINE_DIR, 'tools', 'const_finder'),
os.path.join(ENGINE_DIR, 'tools', 'engine_tool'),
os.path.join(ENGINE_DIR, 'tools', 'gen_web_locale_keymap'),