diff --git a/packages/flutter_goldens/lib/flutter_goldens.dart b/packages/flutter_goldens/lib/flutter_goldens.dart index 831ef65dac..e212ad6cbd 100644 --- a/packages/flutter_goldens/lib/flutter_goldens.dart +++ b/packages/flutter_goldens/lib/flutter_goldens.dart @@ -320,7 +320,17 @@ class FlutterPostSubmitFileComparator extends FlutterGoldenFileComparator { golden = _addPrefix(golden); await update(golden, imageBytes); final File goldenFile = getGoldenFile(golden); - return skiaClient.imgtestAdd(golden.path, goldenFile); + try { + return await skiaClient.imgtestAdd(golden.path, goldenFile); + } on SkiaException catch (e) { + // Convert SkiaException -> TestFailure so that this class implements the + // contract of GoldenFileComparator, and matchesGoldenFile() converts the + // TestFailure into a standard reported test error (with a better stack + // trace, for example). + // + // https://github.com/flutter/flutter/issues/162621 + throw TestFailure('$e'); + } } /// Decides based on the current environment if goldens tests should be diff --git a/packages/flutter_goldens/test/flutter_goldens_test.dart b/packages/flutter_goldens/test/flutter_goldens_test.dart index 72c5ee675e..ae0117e4f4 100644 --- a/packages/flutter_goldens/test/flutter_goldens_test.dart +++ b/packages/flutter_goldens/test/flutter_goldens_test.dart @@ -853,6 +853,43 @@ void main() { ); expect(fakeSkiaClient.initCalls, 0); }); + + test('reports a failure as a TestFailure', () async { + final List log = []; + final MemoryFileSystem fs = MemoryFileSystem(); + final FakePlatform platform = FakePlatform( + environment: {'FLUTTER_ROOT': _kFlutterRoot}, + operatingSystem: 'macos', + ); + fs.directory(_kFlutterRoot).createSync(recursive: true); + final Directory basedir = fs.directory('flutter/test/library/') + ..createSync(recursive: true); + final FlutterGoldenFileComparator comparator = FlutterPostSubmitFileComparator( + basedir.uri, + ThrowsOnImgTestAddSkiaClient( + message: 'Skia Gold received an unapproved image in post-submit', + ), + fs: fs, + platform: platform, + log: log.add, + ); + await expectLater( + () async { + return comparator.compare( + Uint8List.fromList(_kTestPngBytes), + Uri.parse('flutter.golden_test.1.png'), + ); + }, + throwsA( + isA().having( + (TestFailure error) => error.toString(), + 'description', + contains('Skia Gold received an unapproved image in post-submit'), + ), + ), + ); + expect(log, isEmpty); + }); }); group('Pre-Submit', () { @@ -1210,6 +1247,21 @@ class FakeSkiaGoldClient extends Fake implements SkiaGoldClient { String cleanTestName(String fileName) => cleanTestNameValues[fileName] ?? ''; } +class ThrowsOnImgTestAddSkiaClient extends Fake implements SkiaGoldClient { + ThrowsOnImgTestAddSkiaClient({required this.message}); + final String message; + + @override + Future imgtestInit() async { + // Assume this function works. + } + + @override + Future imgtestAdd(String testName, File goldenFile) { + throw SkiaException(message); + } +} + class FakeLocalFileComparator extends Fake implements LocalFileComparator { @override late Uri basedir;