From 868c4d8ce0e890a3dff49cc306ee4ee8ebe8b088 Mon Sep 17 00:00:00 2001 From: Dan Field Date: Tue, 23 Jun 2020 15:29:57 -0700 Subject: [PATCH] Fix issue with stack traces getting mangled (#59900) --- .../lib/src/foundation/assertions.dart | 22 ++++++++ packages/flutter_test/lib/src/binding.dart | 5 ++ .../test/bindings_async_gap_test.dart | 52 +++++++++++++++++++ 3 files changed, 79 insertions(+) create mode 100644 packages/flutter_test/test/bindings_async_gap_test.dart diff --git a/packages/flutter/lib/src/foundation/assertions.dart b/packages/flutter/lib/src/foundation/assertions.dart index ea3db5970d..393a69b05b 100644 --- a/packages/flutter/lib/src/foundation/assertions.dart +++ b/packages/flutter/lib/src/foundation/assertions.dart @@ -389,6 +389,28 @@ class FlutterErrorDetails with Diagnosticable { this.silent = false, }); + /// Creates a copy of the error details but with the given fields replaced + /// with new values. + FlutterErrorDetails copyWith({ + DiagnosticsNode context, + dynamic exception, + InformationCollector informationCollector, + String library, + bool silent, + StackTrace stack, + IterableFilter stackFilter, + }) { + return FlutterErrorDetails( + context: context ?? this.context, + exception: exception ?? this.exception, + informationCollector: informationCollector ?? this.informationCollector, + library: library ?? this.library, + silent: silent ?? this.silent, + stack: stack ?? this.stack, + stackFilter: stackFilter ?? this.stackFilter, + ); + } + /// Transformers to transform [DiagnosticsNode] in [DiagnosticPropertiesBuilder] /// into a more descriptive form. /// diff --git a/packages/flutter_test/lib/src/binding.dart b/packages/flutter_test/lib/src/binding.dart index 6e90dc1c43..57e43d2ed3 100644 --- a/packages/flutter_test/lib/src/binding.dart +++ b/packages/flutter_test/lib/src/binding.dart @@ -579,6 +579,10 @@ abstract class TestWidgetsFlutterBinding extends BindingBase // our main future completing. assert(Zone.current == _parentZone); if (_pendingExceptionDetails != null) { + assert( + _unmangle(_pendingExceptionDetails.stack) == _pendingExceptionDetails.stack, + 'The test binding presented an unmangled stack trace to the framework.', + ); debugPrint = debugPrintOverride; // just in case the test overrides it -- otherwise we won't see the error! reportTestException(_pendingExceptionDetails, testDescription); _pendingExceptionDetails = null; @@ -611,6 +615,7 @@ abstract class TestWidgetsFlutterBinding extends BindingBase _oldExceptionHandler = FlutterError.onError; int _exceptionCount = 0; // number of un-taken exceptions FlutterError.onError = (FlutterErrorDetails details) { + details = details.copyWith(stack: _unmangle(details.stack)); if (_pendingExceptionDetails != null) { debugPrint = debugPrintOverride; // just in case the test overrides it -- otherwise we won't see the errors! if (_exceptionCount == 0) { diff --git a/packages/flutter_test/test/bindings_async_gap_test.dart b/packages/flutter_test/test/bindings_async_gap_test.dart new file mode 100644 index 0000000000..27b7cb1c59 --- /dev/null +++ b/packages/flutter_test/test/bindings_async_gap_test.dart @@ -0,0 +1,52 @@ +// Copyright 2014 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +import 'dart:async'; + +import 'package:flutter/foundation.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:stack_trace/stack_trace.dart' as stack_trace; + +Future main() async { + test('demangles stacks', () async { + // Test that the tester bindings unmangle stacks that come in as + // package:stack_trace types. + // Uses runTest directly so that the test does not get hung up waiting for + // the error reporter to be reset to the original one. + + final Completer errorCompleter = Completer(); + final TestExceptionReporter oldReporter = reportTestException; + reportTestException = (FlutterErrorDetails details, String testDescription) { + errorCompleter.complete(details); + reportTestException = oldReporter; + }; + + final AutomatedTestWidgetsFlutterBinding binding = AutomatedTestWidgetsFlutterBinding(); + await binding.runTest(() async { + final Completer completer = Completer(); + + completer.future.then( + (String value) {}, + onError: (dynamic error, StackTrace stack) { + assert(stack is stack_trace.Chain); + FlutterError.reportError(FlutterErrorDetails( + exception: error, + stack: stack, + )); + } + ); + + completer.completeError(const CustomException()); + }, null); + + final FlutterErrorDetails details = await errorCompleter.future; + expect(details, isNotNull); + expect(details.exception, isA()); + reportTestException = oldReporter; + }); +} + +class CustomException implements Exception { + const CustomException(); +}