Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions tools/compare_goldens/README.md
Original file line number Diff line number Diff line change
@@ -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
```
10 changes: 10 additions & 0 deletions tools/compare_goldens/bin/compare_goldens.dart
Original file line number Diff line number Diff line change
@@ -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);
}
85 changes: 85 additions & 0 deletions tools/compare_goldens/lib/compare_goldens.dart
Original file line number Diff line number Diff line change
@@ -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;
}
3 changes: 3 additions & 0 deletions tools/compare_goldens/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name: compare_goldens
environment:
sdk: '>=3.2.0-0 <4.0.0'
5 changes: 5 additions & 0 deletions tools/compare_goldens/test/compare_goldens_test.dart
Original file line number Diff line number Diff line change
@@ -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() {}
1 change: 1 addition & 0 deletions tools/pub_get_offline.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
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'),
Expand Down