From f442f006ee24a194592e374295715aa038f96fab Mon Sep 17 00:00:00 2001 From: Renan Araujo Date: Thu, 19 May 2022 11:05:10 +0100 Subject: [PATCH] use stack_trace --- lib/src/cli/cli.dart | 1 + lib/src/cli/flutter_cli.dart | 59 +++++++++--------------------- pubspec.yaml | 1 + test/src/cli/flutter_cli_test.dart | 8 ++++ 4 files changed, 28 insertions(+), 41 deletions(-) diff --git a/lib/src/cli/cli.dart b/lib/src/cli/cli.dart index 5e2f1a1db..b78640950 100644 --- a/lib/src/cli/cli.dart +++ b/lib/src/cli/cli.dart @@ -5,6 +5,7 @@ import 'package:lcov_parser/lcov_parser.dart'; import 'package:mason/mason.dart'; import 'package:path/path.dart' as p; import 'package:pubspec_parse/pubspec_parse.dart'; +import 'package:stack_trace/stack_trace.dart'; import 'package:universal_io/io.dart'; import 'package:very_good_cli/src/commands/test/templates/test_runner_bundle.dart'; import 'package:very_good_test_runner/very_good_test_runner.dart'; diff --git a/lib/src/cli/flutter_cli.dart b/lib/src/cli/flutter_cli.dart index b3b639a39..a71de7ef7 100644 --- a/lib/src/cli/flutter_cli.dart +++ b/lib/src/cli/flutter_cli.dart @@ -332,12 +332,15 @@ Future _flutterTest({ stderr('$clearLine${event.stackTrace}'); } - final pathFromStackTrace = getPathFromStackTrace(event.stackTrace); + final traceLocation = _getTraceLocation(stackTrace: event.stackTrace); - final testErrorDescription = - pathFromStackTrace ?? event.error.replaceAll('\n', ' '); + // When failing to recover the location from the stack trace, + // save a short description of the error + final testErrorDescription = traceLocation ?? + event.error.replaceAll('\n', ' ').truncated(_lineLength); - failedTestErrorMessages[event.testID] = testErrorDescription; + final prefix = event.isFailure ? '[FAILED]' : '[ERROR]'; + failedTestErrorMessages[event.testID] = '$prefix $testErrorDescription'; } if (event is TestDoneEvent) { @@ -384,11 +387,8 @@ Future _flutterTest({ final lines = StringBuffer('$clearLine$title\n'); for (final errorMessage in failedTestErrorMessages.values) { - lines.writeln( - '$clearLine - $errorMessage', - ); + lines.writeln('$clearLine - $errorMessage'); } - stderr(lines.toString()); } } @@ -462,39 +462,16 @@ extension on String { } } -String? getPathFromStackTrace(String stackTrace) { - final trimmedStackTrace = stackTrace.trim(); - - if (trimmedStackTrace.isEmpty) { - return null; - } - - final splittedStackTrace = - trimmedStackTrace.split('\n').where((element) => element.isNotEmpty); - - if (splittedStackTrace.isEmpty) { - return null; - } - - final lastLine = splittedStackTrace.last.trim(); - - if (lastLine.isEmpty) { - return null; - } - - final lastLineIterator = lastLine.split(' ').iterator; - - if (!lastLineIterator.moveNext()) { - return null; - } - - final path = p.normalize(lastLineIterator.current); - if (!File(path).existsSync()) { +String? _getTraceLocation({ + required String stackTrace, +}) { + try { + final trace = Trace.parse(stackTrace); + if (trace.frames.isEmpty) { + return null; + } + return trace.frames.last.location; + } on FormatException { return null; } - if (!lastLineIterator.moveNext()) { - return path; - } - - return '$path:${lastLineIterator.current}'; } diff --git a/pubspec.yaml b/pubspec.yaml index da095185c..46c8fde43 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -16,6 +16,7 @@ dependencies: path: ^1.8.0 pub_updater: ^0.2.1 pubspec_parse: ^1.2.0 + stack_trace: 1.10.0 universal_io: ^2.0.4 usage: ^4.0.2 very_good_analysis: ^2.4.0 diff --git a/test/src/cli/flutter_cli_test.dart b/test/src/cli/flutter_cli_test.dart index e2aa6256c..fffeecf67 100644 --- a/test/src/cli/flutter_cli_test.dart +++ b/test/src/cli/flutter_cli_test.dart @@ -381,6 +381,10 @@ void main() { verify( () => logger.write(any(that: contains('Some tests failed.'))), ).called(1); + verify( + () => logger.err( + any(that: contains('- [FAILED] test/example_test.dart 4:5'))), + ).called(1); }); test('completes when there is a test directory (skipping)', () async { @@ -534,6 +538,10 @@ void main() { verify( () => logger.write(any(that: contains('-1: Some tests failed.'))), ).called(1); + verify( + () => logger + .err(any(that: contains('- [ERROR] test/example_test.dart 5:5'))), + ).called(1); }); test('completes and truncates really long test name', () async {