Unverified Commit c7123c96 authored by Ian Hickson's avatar Ian Hickson Committed by GitHub

Factor out use of "print" in flutter_goldens (#144846)

This is part 5 of a broken down version of the #140101 refactor.

This PR removes direct use of `print` and replaces it with a callback.
parent b24b6b1a
[0-9]+:[0-9]+ [+]0: Local passes non-existent baseline for new test, null expectation *
*No expectations provided by Skia Gold for test: library.flutter.new_golden_test.1.png. This may be a new test. If this is an unexpected result, check https://flutter-gold.skia.org.
*Validate image output found at flutter/test/library/
[0-9]+:[0-9]+ [+]1: Local passes non-existent baseline for new test, empty expectation *
*No expectations provided by Skia Gold for test: library.flutter.new_golden_test.2.png. This may be a new test. If this is an unexpected result, check https://flutter-gold.skia.org.
*Validate image output found at flutter/test/library/
[0-9]+:[0-9]+ [+]2: All tests passed! * [0-9]+:[0-9]+ [+]2: All tests passed! *
...@@ -21,6 +21,7 @@ const List<int> _kFailPngBytes = <int>[ ...@@ -21,6 +21,7 @@ const List<int> _kFailPngBytes = <int>[
]; ];
void main() { void main() {
final List<String> log = <String>[];
final MemoryFileSystem fs = MemoryFileSystem(); final MemoryFileSystem fs = MemoryFileSystem();
final Directory basedir = fs.directory('flutter/test/library/') final Directory basedir = fs.directory('flutter/test/library/')
..createSync(recursive: true); ..createSync(recursive: true);
...@@ -34,9 +35,11 @@ void main() { ...@@ -34,9 +35,11 @@ void main() {
environment: <String, String>{'FLUTTER_ROOT': '/flutter'}, environment: <String, String>{'FLUTTER_ROOT': '/flutter'},
operatingSystem: 'macos' operatingSystem: 'macos'
), ),
log: log.add,
); );
test('Local passes non-existent baseline for new test, null expectation', () async { test('Local passes non-existent baseline for new test, null expectation', () async {
log.clear();
expect( expect(
await comparator.compare( await comparator.compare(
Uint8List.fromList(_kFailPngBytes), Uint8List.fromList(_kFailPngBytes),
...@@ -44,9 +47,15 @@ void main() { ...@@ -44,9 +47,15 @@ void main() {
), ),
isTrue, isTrue,
); );
const String expectation =
'No expectations provided by Skia Gold for test: library.flutter.new_golden_test.1.png. '
'This may be a new test. If this is an unexpected result, check https://flutter-gold.skia.org.\n'
'Validate image output found at flutter/test/library/';
expect(log, const <String>[expectation]);
}); });
test('Local passes non-existent baseline for new test, empty expectation', () async { test('Local passes non-existent baseline for new test, empty expectation', () async {
log.clear();
expect( expect(
await comparator.compare( await comparator.compare(
Uint8List.fromList(_kFailPngBytes), Uint8List.fromList(_kFailPngBytes),
...@@ -54,6 +63,11 @@ void main() { ...@@ -54,6 +63,11 @@ void main() {
), ),
isTrue, isTrue,
); );
const String expectation =
'No expectations provided by Skia Gold for test: library.flutter.new_golden_test.2.png. '
'This may be a new test. If this is an unexpected result, check https://flutter-gold.skia.org.\n'
'Validate image output found at flutter/test/library/';
expect(log, const <String>[expectation]);
}); });
} }
......
...@@ -39,16 +39,17 @@ bool _isMainBranch(String? branch) { ...@@ -39,16 +39,17 @@ bool _isMainBranch(String? branch) {
Future<void> testExecutable(FutureOr<void> Function() testMain, {String? namePrefix}) async { Future<void> testExecutable(FutureOr<void> Function() testMain, {String? namePrefix}) async {
const Platform platform = LocalPlatform(); const Platform platform = LocalPlatform();
if (FlutterPostSubmitFileComparator.isForEnvironment(platform)) { if (FlutterPostSubmitFileComparator.isForEnvironment(platform)) {
goldenFileComparator = await FlutterPostSubmitFileComparator.fromDefaultComparator(platform, namePrefix: namePrefix); goldenFileComparator = await FlutterPostSubmitFileComparator.fromDefaultComparator(platform, namePrefix: namePrefix, log: print);
} else if (FlutterPreSubmitFileComparator.isForEnvironment(platform)) { } else if (FlutterPreSubmitFileComparator.isForEnvironment(platform)) {
goldenFileComparator = await FlutterPreSubmitFileComparator.fromDefaultComparator(platform, namePrefix: namePrefix); goldenFileComparator = await FlutterPreSubmitFileComparator.fromDefaultComparator(platform, namePrefix: namePrefix, log: print);
} else if (FlutterSkippingFileComparator.isForEnvironment(platform)) { } else if (FlutterSkippingFileComparator.isForEnvironment(platform)) {
goldenFileComparator = FlutterSkippingFileComparator.fromDefaultComparator( goldenFileComparator = FlutterSkippingFileComparator.fromDefaultComparator(
'Golden file testing is not executed on Cirrus, or LUCI environments outside of flutter/flutter.', 'Golden file testing is not executed on Cirrus, or LUCI environments outside of flutter/flutter.',
namePrefix: namePrefix namePrefix: namePrefix,
log: print
); );
} else { } else {
goldenFileComparator = await FlutterLocalFileComparator.fromDefaultComparator(platform); goldenFileComparator = await FlutterLocalFileComparator.fromDefaultComparator(platform, log: print);
} }
await testMain(); await testMain();
} }
...@@ -103,6 +104,7 @@ abstract class FlutterGoldenFileComparator extends GoldenFileComparator { ...@@ -103,6 +104,7 @@ abstract class FlutterGoldenFileComparator extends GoldenFileComparator {
this.fs = const LocalFileSystem(), this.fs = const LocalFileSystem(),
this.platform = const LocalPlatform(), this.platform = const LocalPlatform(),
this.namePrefix, this.namePrefix,
required this.log,
}); });
/// The directory to which golden file URIs will be resolved in [compare] and /// The directory to which golden file URIs will be resolved in [compare] and
...@@ -124,6 +126,9 @@ abstract class FlutterGoldenFileComparator extends GoldenFileComparator { ...@@ -124,6 +126,9 @@ abstract class FlutterGoldenFileComparator extends GoldenFileComparator {
/// The prefix that is added to all golden names. /// The prefix that is added to all golden names.
final String? namePrefix; final String? namePrefix;
/// The logging function to use when reporting messages to the console.
final LogCallback log;
@override @override
Future<void> update(Uri golden, Uint8List imageBytes) async { Future<void> update(Uri golden, Uint8List imageBytes) async {
final File goldenFile = getGoldenFile(golden); final File goldenFile = getGoldenFile(golden);
...@@ -225,6 +230,7 @@ class FlutterPostSubmitFileComparator extends FlutterGoldenFileComparator { ...@@ -225,6 +230,7 @@ class FlutterPostSubmitFileComparator extends FlutterGoldenFileComparator {
super.fs, super.fs,
super.platform, super.platform,
super.namePrefix, super.namePrefix,
required super.log,
}); });
/// Creates a new [FlutterPostSubmitFileComparator] that mirrors the relative /// Creates a new [FlutterPostSubmitFileComparator] that mirrors the relative
...@@ -237,6 +243,7 @@ class FlutterPostSubmitFileComparator extends FlutterGoldenFileComparator { ...@@ -237,6 +243,7 @@ class FlutterPostSubmitFileComparator extends FlutterGoldenFileComparator {
SkiaGoldClient? goldens, SkiaGoldClient? goldens,
LocalFileComparator? defaultComparator, LocalFileComparator? defaultComparator,
String? namePrefix, String? namePrefix,
required LogCallback log,
}) async { }) async {
defaultComparator ??= goldenFileComparator as LocalFileComparator; defaultComparator ??= goldenFileComparator as LocalFileComparator;
...@@ -247,9 +254,9 @@ class FlutterPostSubmitFileComparator extends FlutterGoldenFileComparator { ...@@ -247,9 +254,9 @@ class FlutterPostSubmitFileComparator extends FlutterGoldenFileComparator {
); );
baseDirectory.createSync(recursive: true); baseDirectory.createSync(recursive: true);
goldens ??= SkiaGoldClient(baseDirectory); goldens ??= SkiaGoldClient(baseDirectory, log: log);
await goldens.auth(); await goldens.auth();
return FlutterPostSubmitFileComparator(baseDirectory.uri, goldens, namePrefix: namePrefix); return FlutterPostSubmitFileComparator(baseDirectory.uri, goldens, namePrefix: namePrefix, log: log);
} }
@override @override
...@@ -302,6 +309,7 @@ class FlutterPreSubmitFileComparator extends FlutterGoldenFileComparator { ...@@ -302,6 +309,7 @@ class FlutterPreSubmitFileComparator extends FlutterGoldenFileComparator {
super.fs, super.fs,
super.platform, super.platform,
super.namePrefix, super.namePrefix,
required super.log,
}); });
/// Creates a new [FlutterPreSubmitFileComparator] that mirrors the /// Creates a new [FlutterPreSubmitFileComparator] that mirrors the
...@@ -315,6 +323,7 @@ class FlutterPreSubmitFileComparator extends FlutterGoldenFileComparator { ...@@ -315,6 +323,7 @@ class FlutterPreSubmitFileComparator extends FlutterGoldenFileComparator {
LocalFileComparator? defaultComparator, LocalFileComparator? defaultComparator,
Directory? testBasedir, Directory? testBasedir,
String? namePrefix, String? namePrefix,
required LogCallback log,
}) async { }) async {
defaultComparator ??= goldenFileComparator as LocalFileComparator; defaultComparator ??= goldenFileComparator as LocalFileComparator;
...@@ -328,13 +337,14 @@ class FlutterPreSubmitFileComparator extends FlutterGoldenFileComparator { ...@@ -328,13 +337,14 @@ class FlutterPreSubmitFileComparator extends FlutterGoldenFileComparator {
baseDirectory.createSync(recursive: true); baseDirectory.createSync(recursive: true);
} }
goldens ??= SkiaGoldClient(baseDirectory); goldens ??= SkiaGoldClient(baseDirectory, log: log);
await goldens.auth(); await goldens.auth();
return FlutterPreSubmitFileComparator( return FlutterPreSubmitFileComparator(
baseDirectory.uri, baseDirectory.uri,
goldens, platform: platform, goldens, platform: platform,
namePrefix: namePrefix, namePrefix: namePrefix,
log: log,
); );
} }
...@@ -388,6 +398,7 @@ class FlutterSkippingFileComparator extends FlutterGoldenFileComparator { ...@@ -388,6 +398,7 @@ class FlutterSkippingFileComparator extends FlutterGoldenFileComparator {
super.skiaClient, super.skiaClient,
this.reason, { this.reason, {
super.namePrefix, super.namePrefix,
required super.log,
}); });
/// Describes the reason for using the [FlutterSkippingFileComparator]. /// Describes the reason for using the [FlutterSkippingFileComparator].
...@@ -399,21 +410,18 @@ class FlutterSkippingFileComparator extends FlutterGoldenFileComparator { ...@@ -399,21 +410,18 @@ class FlutterSkippingFileComparator extends FlutterGoldenFileComparator {
String reason, { String reason, {
LocalFileComparator? defaultComparator, LocalFileComparator? defaultComparator,
String? namePrefix, String? namePrefix,
required LogCallback log,
}) { }) {
defaultComparator ??= goldenFileComparator as LocalFileComparator; defaultComparator ??= goldenFileComparator as LocalFileComparator;
const FileSystem fs = LocalFileSystem(); const FileSystem fs = LocalFileSystem();
final Uri basedir = defaultComparator.basedir; final Uri basedir = defaultComparator.basedir;
final SkiaGoldClient skiaClient = SkiaGoldClient(fs.directory(basedir)); final SkiaGoldClient skiaClient = SkiaGoldClient(fs.directory(basedir), log: log);
return FlutterSkippingFileComparator(basedir, skiaClient, reason, namePrefix: namePrefix); return FlutterSkippingFileComparator(basedir, skiaClient, reason, namePrefix: namePrefix, log: log);
} }
@override @override
Future<bool> compare(Uint8List imageBytes, Uri golden) async { Future<bool> compare(Uint8List imageBytes, Uri golden) async {
// Ideally we would use markTestSkipped here but in some situations, log('Skipping "$golden" test: $reason');
// comparators are called outside of tests.
// See also: https://github.com/flutter/flutter/issues/91285
// ignore: avoid_print
print('Skipping "$golden" test: $reason');
return true; return true;
} }
...@@ -471,6 +479,7 @@ class FlutterLocalFileComparator extends FlutterGoldenFileComparator with LocalC ...@@ -471,6 +479,7 @@ class FlutterLocalFileComparator extends FlutterGoldenFileComparator with LocalC
super.skiaClient, { super.skiaClient, {
super.fs, super.fs,
super.platform, super.platform,
required super.log,
}); });
/// Creates a new [FlutterLocalFileComparator] that mirrors the /// Creates a new [FlutterLocalFileComparator] that mirrors the
...@@ -483,6 +492,7 @@ class FlutterLocalFileComparator extends FlutterGoldenFileComparator with LocalC ...@@ -483,6 +492,7 @@ class FlutterLocalFileComparator extends FlutterGoldenFileComparator with LocalC
SkiaGoldClient? goldens, SkiaGoldClient? goldens,
LocalFileComparator? defaultComparator, LocalFileComparator? defaultComparator,
Directory? baseDirectory, Directory? baseDirectory,
required LogCallback log,
}) async { }) async {
defaultComparator ??= goldenFileComparator as LocalFileComparator; defaultComparator ??= goldenFileComparator as LocalFileComparator;
baseDirectory ??= FlutterGoldenFileComparator.getBaseDirectory( baseDirectory ??= FlutterGoldenFileComparator.getBaseDirectory(
...@@ -494,7 +504,7 @@ class FlutterLocalFileComparator extends FlutterGoldenFileComparator with LocalC ...@@ -494,7 +504,7 @@ class FlutterLocalFileComparator extends FlutterGoldenFileComparator with LocalC
baseDirectory.createSync(recursive: true); baseDirectory.createSync(recursive: true);
} }
goldens ??= SkiaGoldClient(baseDirectory); goldens ??= SkiaGoldClient(baseDirectory, log: log);
try { try {
// Check if we can reach Gold. // Check if we can reach Gold.
await goldens.getExpectationForTest(''); await goldens.getExpectationForTest('');
...@@ -504,6 +514,7 @@ class FlutterLocalFileComparator extends FlutterGoldenFileComparator with LocalC ...@@ -504,6 +514,7 @@ class FlutterLocalFileComparator extends FlutterGoldenFileComparator with LocalC
goldens, goldens,
'OSError occurred, could not reach Gold. ' 'OSError occurred, could not reach Gold. '
'Switching to FlutterSkippingGoldenFileComparator.', 'Switching to FlutterSkippingGoldenFileComparator.',
log: log,
); );
} on io.SocketException catch (_) { } on io.SocketException catch (_) {
return FlutterSkippingFileComparator( return FlutterSkippingFileComparator(
...@@ -511,6 +522,7 @@ class FlutterLocalFileComparator extends FlutterGoldenFileComparator with LocalC ...@@ -511,6 +522,7 @@ class FlutterLocalFileComparator extends FlutterGoldenFileComparator with LocalC
goldens, goldens,
'SocketException occurred, could not reach Gold. ' 'SocketException occurred, could not reach Gold. '
'Switching to FlutterSkippingGoldenFileComparator.', 'Switching to FlutterSkippingGoldenFileComparator.',
log: log,
); );
} on FormatException catch (_) { } on FormatException catch (_) {
return FlutterSkippingFileComparator( return FlutterSkippingFileComparator(
...@@ -518,10 +530,11 @@ class FlutterLocalFileComparator extends FlutterGoldenFileComparator with LocalC ...@@ -518,10 +530,11 @@ class FlutterLocalFileComparator extends FlutterGoldenFileComparator with LocalC
goldens, goldens,
'FormatException occurred, could not reach Gold. ' 'FormatException occurred, could not reach Gold. '
'Switching to FlutterSkippingGoldenFileComparator.', 'Switching to FlutterSkippingGoldenFileComparator.',
log: log,
); );
} }
return FlutterLocalFileComparator(baseDirectory.uri, goldens); return FlutterLocalFileComparator(baseDirectory.uri, goldens, log: log);
} }
@override @override
...@@ -532,12 +545,7 @@ class FlutterLocalFileComparator extends FlutterGoldenFileComparator with LocalC ...@@ -532,12 +545,7 @@ class FlutterLocalFileComparator extends FlutterGoldenFileComparator with LocalC
testExpectation = await skiaClient.getExpectationForTest(testName); testExpectation = await skiaClient.getExpectationForTest(testName);
if (testExpectation == null || testExpectation.isEmpty) { if (testExpectation == null || testExpectation.isEmpty) {
// There is no baseline for this test. log(
// Ideally we would use markTestSkipped here but in some situations,
// comparators are called outside of tests.
// See also: https://github.com/flutter/flutter/issues/91285
// ignore: avoid_print
print(
'No expectations provided by Skia Gold for test: $golden. ' 'No expectations provided by Skia Gold for test: $golden. '
'This may be a new test. If this is an unexpected result, check ' 'This may be a new test. If this is an unexpected result, check '
'https://flutter-gold.skia.org.\n' 'https://flutter-gold.skia.org.\n'
......
...@@ -23,6 +23,9 @@ const String _kTestBrowserKey = 'FLUTTER_TEST_BROWSER'; ...@@ -23,6 +23,9 @@ const String _kTestBrowserKey = 'FLUTTER_TEST_BROWSER';
const String _kWebRendererKey = 'FLUTTER_WEB_RENDERER'; const String _kWebRendererKey = 'FLUTTER_WEB_RENDERER';
const String _kImpellerKey = 'FLUTTER_TEST_IMPELLER'; const String _kImpellerKey = 'FLUTTER_TEST_IMPELLER';
/// Signature of callbacks used to inject [print] replacements.
typedef LogCallback = void Function(String);
/// Exception thrown when an error is returned from the [SkiaClient]. /// Exception thrown when an error is returned from the [SkiaClient].
class SkiaException implements Exception { class SkiaException implements Exception {
/// Creates a new `SkiaException` with a required error [message]. /// Creates a new `SkiaException` with a required error [message].
...@@ -52,6 +55,7 @@ class SkiaGoldClient { ...@@ -52,6 +55,7 @@ class SkiaGoldClient {
this.platform = const LocalPlatform(), this.platform = const LocalPlatform(),
Abi? abi, Abi? abi,
io.HttpClient? httpClient, io.HttpClient? httpClient,
required this.log,
}) : httpClient = httpClient ?? io.HttpClient(), }) : httpClient = httpClient ?? io.HttpClient(),
abi = abi ?? Abi.current(); abi = abi ?? Abi.current();
...@@ -90,6 +94,9 @@ class SkiaGoldClient { ...@@ -90,6 +94,9 @@ class SkiaGoldClient {
/// be null. /// be null.
final Directory workDirectory; final Directory workDirectory;
/// The logging function to use when reporting messages to the console.
final LogCallback log;
/// The local [Directory] where the Flutter repository is hosted. /// The local [Directory] where the Flutter repository is hosted.
/// ///
/// Uses the [fs] file system. /// Uses the [fs] file system.
...@@ -436,10 +443,7 @@ class SkiaGoldClient { ...@@ -436,10 +443,7 @@ class SkiaGoldClient {
} }
expectation = jsonResponse['digest'] as String?; expectation = jsonResponse['digest'] as String?;
} on FormatException catch (error) { } on FormatException catch (error) {
// Ideally we'd use something like package:test's printOnError, but best reliability log(
// in getting logs on CI for now we're just using print.
// See also: https://github.com/flutter/flutter/issues/91285
print( // ignore: avoid_print
'Formatting error detected requesting expectations from Flutter Gold.\n' 'Formatting error detected requesting expectations from Flutter Gold.\n'
'error: $error\n' 'error: $error\n'
'url: $requestForExpectations\n' 'url: $requestForExpectations\n'
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment