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 bef33d7
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 36 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
52 changes: 16 additions & 36 deletions lib/src/cli/flutter_cli.dart
Original file line number Diff line number Diff line change
Expand Up @@ -332,10 +332,13 @@ Future<int> _flutterTest({
stderr('$clearLine${event.stackTrace}');
}

final pathFromStackTrace = getPathFromStackTrace(event.stackTrace);
final traceLocation = _getTraceLocation(
cwd: cwd,
stackTrace: event.stackTrace,
);

final testErrorDescription =
pathFromStackTrace ?? event.error.replaceAll('\n', ' ');
traceLocation ?? event.error.replaceAll('\n', ' ');

failedTestErrorMessages[event.testID] = testErrorDescription;
}
Expand Down Expand Up @@ -388,7 +391,6 @@ Future<int> _flutterTest({
'$clearLine - $errorMessage',
);
}

stderr(lines.toString());
}
}
Expand Down Expand Up @@ -462,39 +464,17 @@ 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 cwd,
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
6 changes: 6 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,9 @@ void main() {
verify(
() => logger.write(any(that: contains('Some tests failed.'))),
).called(1);
verify(
() => logger.err(any(that: contains('- test/example_test.dart 4:5'))),
).called(1);
});

test('completes when there is a test directory (skipping)', () async {
Expand Down Expand Up @@ -534,6 +537,9 @@ void main() {
verify(
() => logger.write(any(that: contains('-1: Some tests failed.'))),
).called(1);
verify(
() => logger.err(any(that: contains('- test/example_test.dart 5:5'))),
).called(1);
});

test('completes and truncates really long test name', () async {
Expand Down

0 comments on commit bef33d7

Please sign in to comment.