Skip to content

Commit c7123c9

Browse files
authored
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.
1 parent b24b6b1 commit c7123c9

File tree

5 files changed

+96
-35
lines changed

5 files changed

+96
-35
lines changed
Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1 @@
1-
[0-9]+:[0-9]+ [+]0: Local passes non-existent baseline for new test, null expectation *
2-
*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.
3-
*Validate image output found at flutter/test/library/
4-
[0-9]+:[0-9]+ [+]1: Local passes non-existent baseline for new test, empty expectation *
5-
*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.
6-
*Validate image output found at flutter/test/library/
71
[0-9]+:[0-9]+ [+]2: All tests passed! *

dev/automated_tests/flutter_test/flutter_gold_test.dart

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const List<int> _kFailPngBytes = <int>[
2121
];
2222

2323
void main() {
24+
final List<String> log = <String>[];
2425
final MemoryFileSystem fs = MemoryFileSystem();
2526
final Directory basedir = fs.directory('flutter/test/library/')
2627
..createSync(recursive: true);
@@ -34,26 +35,39 @@ void main() {
3435
environment: <String, String>{'FLUTTER_ROOT': '/flutter'},
3536
operatingSystem: 'macos'
3637
),
38+
log: log.add,
3739
);
3840

3941
test('Local passes non-existent baseline for new test, null expectation', () async {
42+
log.clear();
4043
expect(
4144
await comparator.compare(
4245
Uint8List.fromList(_kFailPngBytes),
4346
Uri.parse('flutter.new_golden_test.1.png'),
4447
),
4548
isTrue,
4649
);
50+
const String expectation =
51+
'No expectations provided by Skia Gold for test: library.flutter.new_golden_test.1.png. '
52+
'This may be a new test. If this is an unexpected result, check https://flutter-gold.skia.org.\n'
53+
'Validate image output found at flutter/test/library/';
54+
expect(log, const <String>[expectation]);
4755
});
4856

4957
test('Local passes non-existent baseline for new test, empty expectation', () async {
58+
log.clear();
5059
expect(
5160
await comparator.compare(
5261
Uint8List.fromList(_kFailPngBytes),
5362
Uri.parse('flutter.new_golden_test.2.png'),
5463
),
5564
isTrue,
5665
);
66+
const String expectation =
67+
'No expectations provided by Skia Gold for test: library.flutter.new_golden_test.2.png. '
68+
'This may be a new test. If this is an unexpected result, check https://flutter-gold.skia.org.\n'
69+
'Validate image output found at flutter/test/library/';
70+
expect(log, const <String>[expectation]);
5771
});
5872
}
5973

packages/flutter_goldens/lib/flutter_goldens.dart

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,17 @@ bool _isMainBranch(String? branch) {
3939
Future<void> testExecutable(FutureOr<void> Function() testMain, {String? namePrefix}) async {
4040
const Platform platform = LocalPlatform();
4141
if (FlutterPostSubmitFileComparator.isForEnvironment(platform)) {
42-
goldenFileComparator = await FlutterPostSubmitFileComparator.fromDefaultComparator(platform, namePrefix: namePrefix);
42+
goldenFileComparator = await FlutterPostSubmitFileComparator.fromDefaultComparator(platform, namePrefix: namePrefix, log: print);
4343
} else if (FlutterPreSubmitFileComparator.isForEnvironment(platform)) {
44-
goldenFileComparator = await FlutterPreSubmitFileComparator.fromDefaultComparator(platform, namePrefix: namePrefix);
44+
goldenFileComparator = await FlutterPreSubmitFileComparator.fromDefaultComparator(platform, namePrefix: namePrefix, log: print);
4545
} else if (FlutterSkippingFileComparator.isForEnvironment(platform)) {
4646
goldenFileComparator = FlutterSkippingFileComparator.fromDefaultComparator(
4747
'Golden file testing is not executed on Cirrus, or LUCI environments outside of flutter/flutter.',
48-
namePrefix: namePrefix
48+
namePrefix: namePrefix,
49+
log: print
4950
);
5051
} else {
51-
goldenFileComparator = await FlutterLocalFileComparator.fromDefaultComparator(platform);
52+
goldenFileComparator = await FlutterLocalFileComparator.fromDefaultComparator(platform, log: print);
5253
}
5354
await testMain();
5455
}
@@ -103,6 +104,7 @@ abstract class FlutterGoldenFileComparator extends GoldenFileComparator {
103104
this.fs = const LocalFileSystem(),
104105
this.platform = const LocalPlatform(),
105106
this.namePrefix,
107+
required this.log,
106108
});
107109

108110
/// The directory to which golden file URIs will be resolved in [compare] and
@@ -124,6 +126,9 @@ abstract class FlutterGoldenFileComparator extends GoldenFileComparator {
124126
/// The prefix that is added to all golden names.
125127
final String? namePrefix;
126128

129+
/// The logging function to use when reporting messages to the console.
130+
final LogCallback log;
131+
127132
@override
128133
Future<void> update(Uri golden, Uint8List imageBytes) async {
129134
final File goldenFile = getGoldenFile(golden);
@@ -225,6 +230,7 @@ class FlutterPostSubmitFileComparator extends FlutterGoldenFileComparator {
225230
super.fs,
226231
super.platform,
227232
super.namePrefix,
233+
required super.log,
228234
});
229235

230236
/// Creates a new [FlutterPostSubmitFileComparator] that mirrors the relative
@@ -237,6 +243,7 @@ class FlutterPostSubmitFileComparator extends FlutterGoldenFileComparator {
237243
SkiaGoldClient? goldens,
238244
LocalFileComparator? defaultComparator,
239245
String? namePrefix,
246+
required LogCallback log,
240247
}) async {
241248

242249
defaultComparator ??= goldenFileComparator as LocalFileComparator;
@@ -247,9 +254,9 @@ class FlutterPostSubmitFileComparator extends FlutterGoldenFileComparator {
247254
);
248255
baseDirectory.createSync(recursive: true);
249256

250-
goldens ??= SkiaGoldClient(baseDirectory);
257+
goldens ??= SkiaGoldClient(baseDirectory, log: log);
251258
await goldens.auth();
252-
return FlutterPostSubmitFileComparator(baseDirectory.uri, goldens, namePrefix: namePrefix);
259+
return FlutterPostSubmitFileComparator(baseDirectory.uri, goldens, namePrefix: namePrefix, log: log);
253260
}
254261

255262
@override
@@ -302,6 +309,7 @@ class FlutterPreSubmitFileComparator extends FlutterGoldenFileComparator {
302309
super.fs,
303310
super.platform,
304311
super.namePrefix,
312+
required super.log,
305313
});
306314

307315
/// Creates a new [FlutterPreSubmitFileComparator] that mirrors the
@@ -315,6 +323,7 @@ class FlutterPreSubmitFileComparator extends FlutterGoldenFileComparator {
315323
LocalFileComparator? defaultComparator,
316324
Directory? testBasedir,
317325
String? namePrefix,
326+
required LogCallback log,
318327
}) async {
319328

320329
defaultComparator ??= goldenFileComparator as LocalFileComparator;
@@ -328,13 +337,14 @@ class FlutterPreSubmitFileComparator extends FlutterGoldenFileComparator {
328337
baseDirectory.createSync(recursive: true);
329338
}
330339

331-
goldens ??= SkiaGoldClient(baseDirectory);
340+
goldens ??= SkiaGoldClient(baseDirectory, log: log);
332341

333342
await goldens.auth();
334343
return FlutterPreSubmitFileComparator(
335344
baseDirectory.uri,
336345
goldens, platform: platform,
337346
namePrefix: namePrefix,
347+
log: log,
338348
);
339349
}
340350

@@ -388,6 +398,7 @@ class FlutterSkippingFileComparator extends FlutterGoldenFileComparator {
388398
super.skiaClient,
389399
this.reason, {
390400
super.namePrefix,
401+
required super.log,
391402
});
392403

393404
/// Describes the reason for using the [FlutterSkippingFileComparator].
@@ -399,21 +410,18 @@ class FlutterSkippingFileComparator extends FlutterGoldenFileComparator {
399410
String reason, {
400411
LocalFileComparator? defaultComparator,
401412
String? namePrefix,
413+
required LogCallback log,
402414
}) {
403415
defaultComparator ??= goldenFileComparator as LocalFileComparator;
404416
const FileSystem fs = LocalFileSystem();
405417
final Uri basedir = defaultComparator.basedir;
406-
final SkiaGoldClient skiaClient = SkiaGoldClient(fs.directory(basedir));
407-
return FlutterSkippingFileComparator(basedir, skiaClient, reason, namePrefix: namePrefix);
418+
final SkiaGoldClient skiaClient = SkiaGoldClient(fs.directory(basedir), log: log);
419+
return FlutterSkippingFileComparator(basedir, skiaClient, reason, namePrefix: namePrefix, log: log);
408420
}
409421

410422
@override
411423
Future<bool> compare(Uint8List imageBytes, Uri golden) async {
412-
// Ideally we would use markTestSkipped here but in some situations,
413-
// comparators are called outside of tests.
414-
// See also: https://github.com/flutter/flutter/issues/91285
415-
// ignore: avoid_print
416-
print('Skipping "$golden" test: $reason');
424+
log('Skipping "$golden" test: $reason');
417425
return true;
418426
}
419427

@@ -471,6 +479,7 @@ class FlutterLocalFileComparator extends FlutterGoldenFileComparator with LocalC
471479
super.skiaClient, {
472480
super.fs,
473481
super.platform,
482+
required super.log,
474483
});
475484

476485
/// Creates a new [FlutterLocalFileComparator] that mirrors the
@@ -483,6 +492,7 @@ class FlutterLocalFileComparator extends FlutterGoldenFileComparator with LocalC
483492
SkiaGoldClient? goldens,
484493
LocalFileComparator? defaultComparator,
485494
Directory? baseDirectory,
495+
required LogCallback log,
486496
}) async {
487497
defaultComparator ??= goldenFileComparator as LocalFileComparator;
488498
baseDirectory ??= FlutterGoldenFileComparator.getBaseDirectory(
@@ -494,7 +504,7 @@ class FlutterLocalFileComparator extends FlutterGoldenFileComparator with LocalC
494504
baseDirectory.createSync(recursive: true);
495505
}
496506

497-
goldens ??= SkiaGoldClient(baseDirectory);
507+
goldens ??= SkiaGoldClient(baseDirectory, log: log);
498508
try {
499509
// Check if we can reach Gold.
500510
await goldens.getExpectationForTest('');
@@ -503,25 +513,28 @@ class FlutterLocalFileComparator extends FlutterGoldenFileComparator with LocalC
503513
baseDirectory.uri,
504514
goldens,
505515
'OSError occurred, could not reach Gold. '
506-
'Switching to FlutterSkippingGoldenFileComparator.',
516+
'Switching to FlutterSkippingGoldenFileComparator.',
517+
log: log,
507518
);
508519
} on io.SocketException catch (_) {
509520
return FlutterSkippingFileComparator(
510521
baseDirectory.uri,
511522
goldens,
512523
'SocketException occurred, could not reach Gold. '
513-
'Switching to FlutterSkippingGoldenFileComparator.',
524+
'Switching to FlutterSkippingGoldenFileComparator.',
525+
log: log,
514526
);
515527
} on FormatException catch (_) {
516528
return FlutterSkippingFileComparator(
517529
baseDirectory.uri,
518530
goldens,
519531
'FormatException occurred, could not reach Gold. '
520-
'Switching to FlutterSkippingGoldenFileComparator.',
532+
'Switching to FlutterSkippingGoldenFileComparator.',
533+
log: log,
521534
);
522535
}
523536

524-
return FlutterLocalFileComparator(baseDirectory.uri, goldens);
537+
return FlutterLocalFileComparator(baseDirectory.uri, goldens, log: log);
525538
}
526539

527540
@override
@@ -532,12 +545,7 @@ class FlutterLocalFileComparator extends FlutterGoldenFileComparator with LocalC
532545
testExpectation = await skiaClient.getExpectationForTest(testName);
533546

534547
if (testExpectation == null || testExpectation.isEmpty) {
535-
// There is no baseline for this test.
536-
// Ideally we would use markTestSkipped here but in some situations,
537-
// comparators are called outside of tests.
538-
// See also: https://github.com/flutter/flutter/issues/91285
539-
// ignore: avoid_print
540-
print(
548+
log(
541549
'No expectations provided by Skia Gold for test: $golden. '
542550
'This may be a new test. If this is an unexpected result, check '
543551
'https://flutter-gold.skia.org.\n'

packages/flutter_goldens/lib/skia_client.dart

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ const String _kTestBrowserKey = 'FLUTTER_TEST_BROWSER';
2323
const String _kWebRendererKey = 'FLUTTER_WEB_RENDERER';
2424
const String _kImpellerKey = 'FLUTTER_TEST_IMPELLER';
2525

26+
/// Signature of callbacks used to inject [print] replacements.
27+
typedef LogCallback = void Function(String);
28+
2629
/// Exception thrown when an error is returned from the [SkiaClient].
2730
class SkiaException implements Exception {
2831
/// Creates a new `SkiaException` with a required error [message].
@@ -52,6 +55,7 @@ class SkiaGoldClient {
5255
this.platform = const LocalPlatform(),
5356
Abi? abi,
5457
io.HttpClient? httpClient,
58+
required this.log,
5559
}) : httpClient = httpClient ?? io.HttpClient(),
5660
abi = abi ?? Abi.current();
5761

@@ -90,6 +94,9 @@ class SkiaGoldClient {
9094
/// be null.
9195
final Directory workDirectory;
9296

97+
/// The logging function to use when reporting messages to the console.
98+
final LogCallback log;
99+
93100
/// The local [Directory] where the Flutter repository is hosted.
94101
///
95102
/// Uses the [fs] file system.
@@ -436,10 +443,7 @@ class SkiaGoldClient {
436443
}
437444
expectation = jsonResponse['digest'] as String?;
438445
} on FormatException catch (error) {
439-
// Ideally we'd use something like package:test's printOnError, but best reliability
440-
// in getting logs on CI for now we're just using print.
441-
// See also: https://github.com/flutter/flutter/issues/91285
442-
print( // ignore: avoid_print
446+
log(
443447
'Formatting error detected requesting expectations from Flutter Gold.\n'
444448
'error: $error\n'
445449
'url: $requestForExpectations\n'

0 commit comments

Comments
 (0)