Skip to content

Commit

Permalink
use stack_trace
Browse files Browse the repository at this point in the history
  • Loading branch information
Renan Araujo committed May 19, 2022
1 parent b3aa866 commit f442f00
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 41 deletions.
1 change: 1 addition & 0 deletions lib/src/cli/cli.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
59 changes: 18 additions & 41 deletions lib/src/cli/flutter_cli.dart
Original file line number Diff line number Diff line change
Expand Up @@ -332,12 +332,15 @@ Future<int> _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) {
Expand Down Expand Up @@ -384,11 +387,8 @@ Future<int> _flutterTest({

final lines = StringBuffer('$clearLine$title\n');
for (final errorMessage in failedTestErrorMessages.values) {
lines.writeln(
'$clearLine - $errorMessage',
);
lines.writeln('$clearLine - $errorMessage');
}

stderr(lines.toString());
}
}
Expand Down Expand Up @@ -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}';
}
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions test/src/cli/flutter_cli_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit f442f00

Please sign in to comment.