From d527dd959eba19635e0eef12b03937578899dfcb Mon Sep 17 00:00:00 2001 From: gaaclarke <30870216+gaaclarke@users.noreply.github.com> Date: Wed, 20 Mar 2024 15:49:16 -0700 Subject: [PATCH] [golden_test_harvester]: Put back sending the dimensions to the SkiaGoldClient (flutter/engine#51536) fixes https://github.com/flutter/flutter/issues/145413 ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide] and the [C++, Objective-C, Java style guides]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I added new tests to check the change I am making or feature I am adding, or the PR is [test-exempt]. See [testing the engine] for instructions on writing and running engine tests. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I signed the [CLA]. - [x] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. [Contributor Guide]: https://github.com/flutter/flutter/wiki/Tree-hygiene#overview [Tree Hygiene]: https://github.com/flutter/flutter/wiki/Tree-hygiene [test-exempt]: https://github.com/flutter/flutter/wiki/Tree-hygiene#tests [Flutter Style Guide]: https://github.com/flutter/flutter/wiki/Style-guide-for-Flutter-repo [C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style [testing the engine]: https://github.com/flutter/flutter/wiki/Testing-the-engine [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/wiki/Tree-hygiene#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/wiki/Chat --- .../bin/golden_tests_harvester.dart | 23 +- .../lib/golden_tests_harvester.dart | 219 +++++++++++++----- .../test/golden_tests_harvester_test.dart | 136 +++++++---- 3 files changed, 261 insertions(+), 117 deletions(-) diff --git a/engine/src/flutter/tools/golden_tests_harvester/bin/golden_tests_harvester.dart b/engine/src/flutter/tools/golden_tests_harvester/bin/golden_tests_harvester.dart index a4d318e046..aa61833658 100644 --- a/engine/src/flutter/tools/golden_tests_harvester/bin/golden_tests_harvester.dart +++ b/engine/src/flutter/tools/golden_tests_harvester/bin/golden_tests_harvester.dart @@ -41,23 +41,18 @@ Future main(List args) async { } final io.Directory workDirectory = io.Directory(rest.single); - final AddImageToSkiaGold addImg; - final bool dryRun = results['dry-run'] as bool; - if (dryRun) { + final bool isDryRun = results['dry-run'] as bool; + final Harvester harvester; + if (isDryRun) { io.stderr.writeln('=== DRY RUN. Results not submitted to Skia Gold. ==='); - addImg = _dryRunAddImg; + harvester = + await Harvester.create(workDirectory, io.stderr, + addImageToSkiaGold: _dryRunAddImg); } else { - // If GOLDCTL is not configured (i.e. on CI), this will throw. - final SkiaGoldClient client = SkiaGoldClient(workDirectory); - await client.auth(); - addImg = client.addImg; + harvester = + await Harvester.create(workDirectory, io.stderr); } - - await harvest( - workDirectory: workDirectory, - addImg: addImg, - stderr: io.stderr, - ); + await harvest(harvester); } Future _dryRunAddImg( diff --git a/engine/src/flutter/tools/golden_tests_harvester/lib/golden_tests_harvester.dart b/engine/src/flutter/tools/golden_tests_harvester/lib/golden_tests_harvester.dart index 1568cf2b3d..7eeff6044d 100644 --- a/engine/src/flutter/tools/golden_tests_harvester/lib/golden_tests_harvester.dart +++ b/engine/src/flutter/tools/golden_tests_harvester/lib/golden_tests_harvester.dart @@ -5,77 +5,176 @@ import 'dart:io' as io; import 'package:path/path.dart' as p; +import 'package:skia_gold_client/skia_gold_client.dart'; import 'src/digests_json_format.dart'; -/// Uploads the images of digests in [workDirectory] to Skia Gold. -/// -/// The directory is expected to match the following structure: -/// ```txt -/// workDirectory/ -/// - digest.json -/// - test_name_1.png -/// - test_name_2.png -/// - ... -/// ``` -/// -/// The format of `digest.json` is expected to match the following: -/// ```jsonc -/// { -/// "dimensions": { -/// // Key-value pairs of dimensions to provide to Skia Gold. -/// // For example: -/// "platform": "linux", -/// }, -/// "entries": [ -/// // Each entry is a test-run with the following format: -/// { -/// // Path must be a direct sibling of digest.json. -/// "filename": "test_name_1.png", -/// -/// // Called `screenshotSize` in Skia Gold (width * height). -/// "width": 100, -/// "height": 100, -/// -/// // Called `differentPixelsRate` in Skia Gold. -/// "maxDiffPixelsPercent": 0.01, -/// -/// // Called `pixelColorDelta` in Skia Gold. -/// "maxColorDelta": 0 -/// } -/// ] -/// } -/// ``` -Future harvest({ - required io.Directory workDirectory, - required AddImageToSkiaGold addImg, - required StringSink stderr, -}) async { - final io.File file = io.File(p.join(workDirectory.path, 'digest.json')); - if (!file.existsSync()) { - // Check if the directory exists or if the file is just missing. - if (!workDirectory.existsSync()) { - throw ArgumentError('Directory not found: ${workDirectory.path}.'); +/// Used by [harvest] to process a directory for Skia Gold upload. +abstract class Harvester { + /// Creates a new [Harvester] from the directory at [workDirectory]. + /// + /// The directory is expected to match the following structure: + /// ```txt + /// workDirectory/ + /// - digest.json + /// - test_name_1.png + /// - test_name_2.png + /// - ... + /// ``` + /// + /// The format of `digest.json` is expected to match the following: + /// ```jsonc + /// { + /// "dimensions": { + /// // Key-value pairs of dimensions to provide to Skia Gold. + /// // For example: + /// "platform": "linux", + /// }, + /// "entries": [ + /// // Each entry is a test-run with the following format: + /// { + /// // Path must be a direct sibling of digest.json. + /// "filename": "test_name_1.png", + /// + /// // Called `screenshotSize` in Skia Gold (width * height). + /// "width": 100, + /// "height": 100, + /// + /// // Called `differentPixelsRate` in Skia Gold. + /// "maxDiffPixelsPercent": 0.01, + /// + /// // Called `pixelColorDelta` in Skia Gold. + /// "maxColorDelta": 0 + /// } + /// ] + /// } + /// ``` + static Future create( + io.Directory workDirectory, StringSink stderr, + {AddImageToSkiaGold? addImageToSkiaGold}) async { + final io.File file = io.File(p.join(workDirectory.path, 'digest.json')); + if (!file.existsSync()) { + // Check if the directory exists or if the file is just missing. + if (!workDirectory.existsSync()) { + throw ArgumentError('Directory not found: ${workDirectory.path}.'); + } + // Lookup sibling files to help the user understand what's missing. + final List files = workDirectory.listSync(); + throw StateError( + 'File "digest.json" not found in ${workDirectory.path}.\n\n' + 'Found files: ${files.map((io.FileSystemEntity e) => p.basename(e.path)).join(', ')}', + ); + } + final Digests digests = Digests.parse(file.readAsStringSync()); + + if (addImageToSkiaGold != null) { + return _DryRunHarvester(digests, stderr, workDirectory, addImageToSkiaGold); + } else { + return SkiaGoldHarvester._create(digests, stderr, workDirectory); } - // Lookup sibling files to help the user understand what's missing. - final List files = workDirectory.listSync(); - throw StateError( - 'File "digest.json" not found in ${workDirectory.path}.\n\n' - 'Found files: ${files.map((io.FileSystemEntity e) => p.basename(e.path)).join(', ')}', - ); } - final Digests digests = Digests.parse(file.readAsStringSync()); + + Future _addImg( + String testName, + io.File goldenFile, { + double differentPixelsRate, + int pixelColorDelta, + required int screenshotSize, + }); + + Future _auth(); + + Digests get _digests; + StringSink get _stderr; + io.Directory get _workDirectory; +} + +/// A [Harvester] that communicates with a real [SkiaGoldClient]. +class SkiaGoldHarvester implements Harvester { + SkiaGoldHarvester._init( + this._digests, this._stderr, this._workDirectory, this.client); + + @override + final Digests _digests; + @override + final StringSink _stderr; + @override + final io.Directory _workDirectory; + /// The [SkiaGoldClient] that will be used for harvesting. + final SkiaGoldClient client; + + static Future _create( + Digests digests, StringSink stderr, io.Directory workDirectory) async { + final SkiaGoldClient client = + SkiaGoldClient(workDirectory, dimensions: digests.dimensions); + return SkiaGoldHarvester._init(digests, stderr, workDirectory, client); + } + + @override + Future _addImg(String testName, io.File goldenFile, + {double differentPixelsRate = 0.01, + int pixelColorDelta = 0, + required int screenshotSize}) async { + return client.addImg(testName, goldenFile, + differentPixelsRate: differentPixelsRate, + pixelColorDelta: pixelColorDelta, + screenshotSize: screenshotSize); + } + + @override + Future _auth() { + return client.auth(); + } +} + +/// A [Harvester] that doesn't harvest, just calls a callback. +class _DryRunHarvester implements Harvester { + _DryRunHarvester( + this._digests, this._stderr, this._workDirectory, + this._addImageToSkiaGold); + + @override + final Digests _digests; + @override + final StringSink _stderr; + @override + final io.Directory _workDirectory; + final AddImageToSkiaGold _addImageToSkiaGold; + + @override + Future _addImg(String testName, io.File goldenFile, + {double differentPixelsRate = 0.01, + int pixelColorDelta = 0, + required int screenshotSize}) async { + return _addImageToSkiaGold(testName, goldenFile, + differentPixelsRate: differentPixelsRate, + pixelColorDelta: pixelColorDelta, + screenshotSize: screenshotSize); + } + + @override + Future _auth() async { + _stderr.writeln('using dimensions: ${_digests.dimensions}'); + } +} + +/// Uploads the images of digests in [harvester] to Skia Gold. +Future harvest(Harvester harvester) async { + await harvester._auth(); final List> pendingComparisons = >[]; - for (final DigestEntry entry in digests.entries) { - final io.File goldenFile = io.File(p.join(workDirectory.path, entry.filename)); - final Future future = addImg( + for (final DigestEntry entry in harvester._digests.entries) { + final io.File goldenFile = + io.File(p.join(harvester._workDirectory.path, entry.filename)); + final Future future = harvester + ._addImg( entry.filename, goldenFile, screenshotSize: entry.width * entry.height, differentPixelsRate: entry.maxDiffPixelsPercent, pixelColorDelta: entry.maxColorDelta, - ).catchError((Object e) { - stderr.writeln('Failed to add image to Skia Gold: $e'); + ) + .catchError((Object e) { + harvester._stderr.writeln('Failed to add image to Skia Gold: $e'); throw FailedComparisonException(entry.filename); }); pendingComparisons.add(future); diff --git a/engine/src/flutter/tools/golden_tests_harvester/test/golden_tests_harvester_test.dart b/engine/src/flutter/tools/golden_tests_harvester/test/golden_tests_harvester_test.dart index 9eec6ccfca..bdbbabe7fb 100644 --- a/engine/src/flutter/tools/golden_tests_harvester/test/golden_tests_harvester_test.dart +++ b/engine/src/flutter/tools/golden_tests_harvester/test/golden_tests_harvester_test.dart @@ -10,8 +10,10 @@ import 'package:litetest/litetest.dart'; import 'package:path/path.dart' as p; void main() async { - Future withTempDirectory(FutureOr Function(io.Directory) callback) async { - final io.Directory tempDirectory = await io.Directory.systemTemp.createTemp('golden_tests_harvester_test.'); + Future withTempDirectory( + FutureOr Function(io.Directory) callback) async { + final io.Directory tempDirectory = await io.Directory.systemTemp + .createTemp('golden_tests_harvester_test.'); try { await callback(tempDirectory); } finally { @@ -23,27 +25,26 @@ void main() async { await withTempDirectory((io.Directory tempDirectory) async { final StringSink stderr = StringBuffer(); final ArgumentError error = await _expectThrow(() async { - await harvest( - workDirectory: io.Directory(p.join(tempDirectory.path, 'non_existent')), - addImg: _alwaysThrowsAddImg, - stderr: stderr, - ); + await Harvester.create( + io.Directory(p.join(tempDirectory.path, 'non_existent')), + stderr, + addImageToSkiaGold: _alwaysThrowsAddImg); }); expect(error.message, contains('non_existent')); expect(stderr.toString(), isEmpty); }); }); - test('should require a file named "digest.json" in the working directory', () async { + test('should require a file named "digest.json" in the working directory', + () async { await withTempDirectory((io.Directory tempDirectory) async { final StringSink stderr = StringBuffer(); final StateError error = await _expectThrow(() async { - await harvest( - workDirectory: tempDirectory, - addImg: _alwaysThrowsAddImg, - stderr: stderr, - ); + await Harvester.create( + tempDirectory, + stderr, + addImageToSkiaGold: _alwaysThrowsAddImg); }); expect(error.toString(), contains('digest.json')); expect(stderr.toString(), isEmpty); @@ -53,15 +54,17 @@ void main() async { test('should throw if "digest.json" is in an unexpected format', () async { await withTempDirectory((io.Directory tempDirectory) async { final StringSink stderr = StringBuffer(); - final io.File digestsFile = io.File(p.join(tempDirectory.path, 'digest.json')); - await digestsFile.writeAsString('{"dimensions": "not a map", "entries": []}'); + final io.File digestsFile = + io.File(p.join(tempDirectory.path, 'digest.json')); + await digestsFile + .writeAsString('{"dimensions": "not a map", "entries": []}'); - final FormatException error = await _expectThrow(() async { - await harvest( - workDirectory: tempDirectory, - addImg: _alwaysThrowsAddImg, - stderr: stderr, - ); + final FormatException error = + await _expectThrow(() async { + await Harvester.create( + tempDirectory, + stderr, + addImageToSkiaGold: _alwaysThrowsAddImg); }); expect(error.message, contains('dimensions')); expect(stderr.toString(), isEmpty); @@ -70,7 +73,8 @@ void main() async { test('should fail eagerly if addImg fails', () async { await withTempDirectory((io.Directory tempDirectory) async { - final io.File digestsFile = io.File(p.join(tempDirectory.path, 'digest.json')); + final io.File digestsFile = + io.File(p.join(tempDirectory.path, 'digest.json')); final StringSink stderr = StringBuffer(); await digestsFile.writeAsString(''' { @@ -87,12 +91,13 @@ void main() async { } '''); - final FailedComparisonException error = await _expectThrow(() async { - await harvest( - workDirectory: tempDirectory, - addImg: _alwaysThrowsAddImg, - stderr: stderr, - ); + final FailedComparisonException error = + await _expectThrow(() async { + final Harvester harvester = await Harvester.create( + tempDirectory, + stderr, + addImageToSkiaGold: _alwaysThrowsAddImg); + await harvest(harvester); }); expect(error.testName, 'test_name_1.png'); expect(stderr.toString(), contains('IntentionalError')); @@ -101,7 +106,8 @@ void main() async { test('should invoke addImg per test', () async { await withTempDirectory((io.Directory tempDirectory) async { - final io.File digestsFile = io.File(p.join(tempDirectory.path, 'digest.json')); + final io.File digestsFile = + io.File(p.join(tempDirectory.path, 'digest.json')); await digestsFile.writeAsString(''' { "dimensions": {}, @@ -125,29 +131,73 @@ void main() async { '''); final List addImgCalls = []; final StringSink stderr = StringBuffer(); - await harvest( - workDirectory: tempDirectory, - addImg: ( - String testName, - io.File goldenFile, { - required int screenshotSize, - double differentPixelsRate = 0.01, - int pixelColorDelta = 0, - }) async { - addImgCalls.add('$testName $screenshotSize $differentPixelsRate $pixelColorDelta'); - }, - stderr: stderr, - ); + + final Harvester harvester = + await Harvester.create(tempDirectory, stderr, addImageToSkiaGold: ( + String testName, + io.File goldenFile, { + required int screenshotSize, + double differentPixelsRate = 0.01, + int pixelColorDelta = 0, + }) async { + addImgCalls.add( + '$testName $screenshotSize $differentPixelsRate $pixelColorDelta'); + }); + await harvest(harvester); expect(addImgCalls, [ 'test_name_1.png 10000 0.01 0', 'test_name_2.png 40000 0.02 1', ]); }); + }); + test('client has dimensions', () async { + await withTempDirectory((io.Directory tempDirectory) async { + final StringSink stderr = StringBuffer(); + final io.File digestsFile = + io.File(p.join(tempDirectory.path, 'digest.json')); + await digestsFile + .writeAsString('{"dimensions": {"key":"value"}, "entries": []}'); + final Harvester harvester = await Harvester.create(tempDirectory, stderr); + expect(harvester is SkiaGoldHarvester, true); + final SkiaGoldHarvester skiaGoldHarvester = + harvester as SkiaGoldHarvester; + expect(skiaGoldHarvester.client.dimensions, + {'key': 'value'}); + }); + }); + + test('throws without GOLDCTL', () async { + await withTempDirectory((io.Directory tempDirectory) async { + final StringSink stderr = StringBuffer(); + final io.File digestsFile = + io.File(p.join(tempDirectory.path, 'digest.json')); + await digestsFile.writeAsString(''' +{ + "dimensions": {"key":"value"}, + "entries": [ + { + "filename": "foo.png", + "width": 100, + "height": 100, + "maxDiffPixelsPercent": 0.01, + "maxColorDelta": 0 + } + ]} +'''); + final Harvester harvester = await Harvester.create(tempDirectory, stderr); + final StateError error = await _expectThrow(() async { + await harvest(harvester); + }); + expect(error.message, contains('GOLDCTL')); + expect(stderr.toString(), isEmpty); + }); }); } -FutureOr _expectThrow(FutureOr Function() callback) async { + +FutureOr _expectThrow( + FutureOr Function() callback) async { try { await callback(); fail('Expected an exception of type $T');