diff --git a/packages/flutter_goldens/lib/flutter_goldens.dart b/packages/flutter_goldens/lib/flutter_goldens.dart index fb8010d509..0a20010d77 100644 --- a/packages/flutter_goldens/lib/flutter_goldens.dart +++ b/packages/flutter_goldens/lib/flutter_goldens.dart @@ -9,6 +9,7 @@ import 'dart:typed_data' show Uint8List; import 'package:file/file.dart'; import 'package:file/local.dart'; +import 'package:flutter/foundation.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:meta/meta.dart'; import 'package:platform/platform.dart'; @@ -670,7 +671,7 @@ class FlutterLocalFileComparator extends FlutterGoldenFileComparator with LocalC if (result.passed) return true; - generateFailureOutput(result, golden, basedir); - return false; + final String error = await generateFailureOutput(result, golden, basedir); + throw FlutterError(error); } } diff --git a/packages/flutter_test/lib/src/_goldens_io.dart b/packages/flutter_test/lib/src/_goldens_io.dart index e95af97d44..dda3880f13 100644 --- a/packages/flutter_test/lib/src/_goldens_io.dart +++ b/packages/flutter_test/lib/src/_goldens_io.dart @@ -8,11 +8,13 @@ import 'dart:math' as math; import 'dart:typed_data'; import 'dart:ui'; +import 'package:flutter/foundation.dart'; import 'package:path/path.dart' as path; // ignore: deprecated_member_use import 'package:test_api/test_api.dart' as test_package show TestFailure; import 'goldens.dart'; +import 'test_async_utils.dart'; /// The default [GoldenFileComparator] implementation for `flutter test`. /// @@ -102,7 +104,8 @@ class LocalFileComparator extends GoldenFileComparator with LocalComparisonOutpu ); if (!result.passed) { - await generateFailureOutput(result, golden, basedir); + final String error = await generateFailureOutput(result, golden, basedir); + throw FlutterError(error); } return result.passed; } @@ -125,16 +128,15 @@ class LocalComparisonOutput { /// Writes out diffs from the [ComparisonResult] of a golden file test. /// /// Will throw an error if a null result is provided. - Future generateFailureOutput( + Future generateFailureOutput( ComparisonResult result, Uri golden, Uri basedir, { String key = '', - }) async { + }) async => TestAsyncUtils.guard(() async { String additionalFeedback = ''; if (result.diffs != null) { - additionalFeedback = '\nFailure feedback can be found at ' - '${path.join(basedir.path, 'failures')}'; + additionalFeedback = '\nFailure feedback can be found at ${path.join(basedir.path, 'failures')}'; final Map diffs = result.diffs!.cast(); for (final MapEntry entry in diffs.entries) { final File output = getFailureFile( @@ -143,15 +145,12 @@ class LocalComparisonOutput { basedir, ); output.parent.createSync(recursive: true); - final ByteData? pngBytes = - await entry.value.toByteData(format: ImageByteFormat.png); + final ByteData? pngBytes = await entry.value.toByteData(format: ImageByteFormat.png); output.writeAsBytesSync(pngBytes!.buffer.asUint8List()); } } - throw test_package.TestFailure( - 'Golden "$golden": ${result.error}$additionalFeedback' - ); - } + return 'Golden "$golden": ${result.error}$additionalFeedback'; + }); /// Returns the appropriate file for a given diff from a [ComparisonResult]. File getFailureFile(String failure, Uri golden, Uri basedir) { diff --git a/packages/flutter_test/test/goldens_test.dart b/packages/flutter_test/test/goldens_test.dart index fd04dc40ce..66193312b4 100644 --- a/packages/flutter_test/test/goldens_test.dart +++ b/packages/flutter_test/test/goldens_test.dart @@ -9,6 +9,7 @@ import 'dart:io' as io; import 'dart:typed_data'; import 'package:file/memory.dart'; +import 'package:flutter/foundation.dart' show DiagnosticLevel, DiagnosticsNode, DiagnosticPropertiesBuilder, FlutterError; import 'package:flutter_test/flutter_test.dart' hide test; import 'package:flutter_test/flutter_test.dart' as test_package; @@ -102,6 +103,36 @@ void main() { expect(comparator.basedir, Uri.parse('./')); }); + test('throws if local output is not awaited', () { + try { + comparator.generateFailureOutput( + ComparisonResult(passed: false), + Uri.parse('foo_test.dart'), + Uri.parse('/foo/bar/'), + ); + TestAsyncUtils.verifyAllScopesClosed(); + throw 'unexpectedly did not throw'; + } on FlutterError catch (e) { + final List lines = e.message.split('\n'); + expectSync(lines[0], 'Asynchronous call to guarded function leaked.'); + expectSync(lines[1], 'You must use "await" with all Future-returning test APIs.'); + expectSync( + lines[2], + matches(r'^The guarded method "generateFailureOutput" from class ' + r'LocalComparisonOutput was called from .*goldens_test.dart on line ' + r'[0-9]+, but never completed before its parent scope closed\.$'), + ); + expectSync(lines.length, 3); + final DiagnosticPropertiesBuilder propertiesBuilder = DiagnosticPropertiesBuilder(); + e.debugFillProperties(propertiesBuilder); + final List information = propertiesBuilder.properties; + expectSync(information.length, 3); + expectSync(information[0].level, DiagnosticLevel.summary); + expectSync(information[1].level, DiagnosticLevel.hint); + expectSync(information[2].level, DiagnosticLevel.info); + } + }); + group('compare', () { Future doComparison([ String golden = 'golden.png' ]) { final Uri uri = fs.file(fix(golden)).uri; @@ -157,7 +188,7 @@ void main() { try { await doComparison(); fail('TestFailure expected but not thrown.'); - } on TestFailure catch (error) { + } on FlutterError catch (error) { expect(error.message, contains('% diff detected')); final io.File master = fs.file( fix('/failures/golden_masterImage.png') @@ -186,7 +217,7 @@ void main() { try { await doComparison('subdir/golden.png'); fail('TestFailure expected but not thrown.'); - } on TestFailure catch (error) { + } on FlutterError catch (error) { expect(error.message, contains('% diff detected')); final io.File master = fs.file( fix('/failures/golden_masterImage.png') @@ -221,7 +252,7 @@ void main() { try { await doComparison(); fail('TestFailure expected but not thrown.'); - } on TestFailure catch (error) { + } on FlutterError catch (error) { expect(error.message, contains('image sizes do not match')); } }); @@ -231,7 +262,7 @@ void main() { try { await doComparison(); fail('TestFailure expected but not thrown.'); - } on TestFailure catch (error) { + } on FlutterError catch (error) { expect(error.message, contains('% diff detected')); } }); @@ -241,7 +272,7 @@ void main() { try { await doComparison(); fail('TestFailure expected but not thrown.'); - } on TestFailure catch (error) { + } on FlutterError catch (error) { expect(error.message, contains('null image provided')); } });