Revert "Revert "Fix remaining holes in stack trace demangling (#60478)" (#60916)" (#60996)

This reverts commit 2c7e5dd935.
This commit is contained in:
Dan Field
2020-07-07 15:30:32 -07:00
committed by GitHub
parent 8ed8b06ac5
commit fb57edcc80
7 changed files with 176 additions and 24 deletions

View File

@@ -516,6 +516,7 @@ abstract class TestWidgetsFlutterBinding extends BindingBase
return result;
}
FlutterExceptionHandler _oldExceptionHandler;
StackTraceDemangler _oldStackTraceDemangler;
FlutterErrorDetails _pendingExceptionDetails;
static const TextStyle _messageStyle = TextStyle(
@@ -579,10 +580,6 @@ 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;
@@ -613,9 +610,9 @@ abstract class TestWidgetsFlutterBinding extends BindingBase
assert(description != null);
assert(inTest);
_oldExceptionHandler = FlutterError.onError;
_oldStackTraceDemangler = FlutterError.demangleStackTrace;
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) {
@@ -634,6 +631,17 @@ abstract class TestWidgetsFlutterBinding extends BindingBase
_pendingExceptionDetails = details;
}
};
FlutterError.demangleStackTrace = (StackTrace stack) {
// package:stack_trace uses ZoneSpecification.errorCallback to add useful
// information to stack traces, in this case the Trace and Chain classes
// can be present. Because these StackTrace implementations do not follow
// the format the framework expects, we covert them to a vm trace here.
if (stack is stack_trace.Trace)
return stack.vmTrace;
if (stack is stack_trace.Chain)
return stack.toTrace().vmTrace;
return stack;
};
final Completer<void> testCompleter = Completer<void>();
final VoidCallback testCompletionHandler = _createTestCompletionHandler(description, testCompleter);
void handleUncaughtError(dynamic exception, StackTrace stack) {
@@ -647,7 +655,7 @@ abstract class TestWidgetsFlutterBinding extends BindingBase
debugPrint = debugPrintOverride; // just in case the test overrides it -- otherwise we won't see the error!
FlutterError.dumpErrorToConsole(FlutterErrorDetails(
exception: exception,
stack: _unmangle(stack),
stack: stack,
context: ErrorDescription('running a test (but after the test had completed)'),
library: 'Flutter test framework',
), forceReport: true);
@@ -694,7 +702,7 @@ abstract class TestWidgetsFlutterBinding extends BindingBase
final int stackLinesToOmit = reportExpectCall(stack, omittedFrames);
FlutterError.reportError(FlutterErrorDetails(
exception: exception,
stack: _unmangle(stack),
stack: stack,
context: ErrorDescription('running a test'),
library: 'Flutter test framework',
stackFilter: (Iterable<String> frames) {
@@ -842,6 +850,7 @@ abstract class TestWidgetsFlutterBinding extends BindingBase
void postTest() {
assert(inTest);
FlutterError.onError = _oldExceptionHandler;
FlutterError.demangleStackTrace = _oldStackTraceDemangler;
_pendingExceptionDetails = null;
_parentZone = null;
buildOwner.focusManager = FocusManager();
@@ -1713,11 +1722,3 @@ class _LiveTestRenderView extends RenderView {
_label?.paint(context.canvas, offset - const Offset(0.0, 10.0));
}
}
StackTrace _unmangle(StackTrace stack) {
if (stack is stack_trace.Trace)
return stack.vmTrace;
if (stack is stack_trace.Chain)
return stack.toTrace().vmTrace;
return stack;
}

View File

@@ -49,4 +49,4 @@ Future<void> main() async {
class CustomException implements Exception {
const CustomException();
}
}

View File

@@ -0,0 +1,73 @@
// 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<void> main() async {
// We use AutomatedTestWidgetsFlutterBinding to allow the test binding to set
// FlutterError.demangleStackTrace and FlutterError.onError without testWidgets.
final AutomatedTestWidgetsFlutterBinding binding = AutomatedTestWidgetsFlutterBinding();
test('FlutterErrorDetails demangles', () async {
await binding.runTest(() async {
// When we call toString on a FlutterErrorDetails, it attempts to parse and
// filter the stack trace, which fails if demangleStackTrace returns a
// mangled stack trace.
FlutterErrorDetails(
exception: const CustomException(),
stack: await getMangledStack(),
).toString();
// Additional logic is used to parse assertion stack traces.
FlutterErrorDetails(
exception: AssertionError('Some assertion'),
stack: await getMangledStack(),
).toString();
}, () {});
binding.postTest();
});
test('debugPrintStack demangles', () async {
await binding.runTest(() async {
final DebugPrintCallback oldDebugPrint = debugPrint;
try {
debugPrint = (String message, {int wrapWidth}) {};
debugPrintStack(
stackTrace: await getMangledStack(),
);
} finally {
debugPrint = oldDebugPrint;
}
}, () {});
binding.postTest();
});
}
Future<StackTrace> getMangledStack() {
// package:test uses package:stack_trace to wrap tests in a Zone that overrides
// errorCallback, the error callback transforms any StackTrace propagated
// to futures into a Chain, which has a format different from the vm.
final Completer<StackTrace> stackCompleter = Completer<StackTrace>();
final Completer<void> completer = Completer<void>();
completer.future.then(
(void value) {
assert(false);
},
onError: (Object error, StackTrace stack) {
expect(error, isA<CustomException>());
expect(stack, isA<stack_trace.Chain>());
stackCompleter.complete(stack);
},
);
completer.completeError(const CustomException());
return stackCompleter.future;
}
class CustomException implements Exception {
const CustomException();
}