Skip to content

Commit bef33d7

Browse files
author
Renan Araujo
committed
use stack_trace
1 parent b3aa866 commit bef33d7

File tree

4 files changed

+24
-36
lines changed

4 files changed

+24
-36
lines changed

lib/src/cli/cli.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import 'package:lcov_parser/lcov_parser.dart';
55
import 'package:mason/mason.dart';
66
import 'package:path/path.dart' as p;
77
import 'package:pubspec_parse/pubspec_parse.dart';
8+
import 'package:stack_trace/stack_trace.dart';
89
import 'package:universal_io/io.dart';
910
import 'package:very_good_cli/src/commands/test/templates/test_runner_bundle.dart';
1011
import 'package:very_good_test_runner/very_good_test_runner.dart';

lib/src/cli/flutter_cli.dart

Lines changed: 16 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -332,10 +332,13 @@ Future<int> _flutterTest({
332332
stderr('$clearLine${event.stackTrace}');
333333
}
334334

335-
final pathFromStackTrace = getPathFromStackTrace(event.stackTrace);
335+
final traceLocation = _getTraceLocation(
336+
cwd: cwd,
337+
stackTrace: event.stackTrace,
338+
);
336339

337340
final testErrorDescription =
338-
pathFromStackTrace ?? event.error.replaceAll('\n', ' ');
341+
traceLocation ?? event.error.replaceAll('\n', ' ');
339342

340343
failedTestErrorMessages[event.testID] = testErrorDescription;
341344
}
@@ -388,7 +391,6 @@ Future<int> _flutterTest({
388391
'$clearLine - $errorMessage',
389392
);
390393
}
391-
392394
stderr(lines.toString());
393395
}
394396
}
@@ -462,39 +464,17 @@ extension on String {
462464
}
463465
}
464466

465-
String? getPathFromStackTrace(String stackTrace) {
466-
final trimmedStackTrace = stackTrace.trim();
467-
468-
if (trimmedStackTrace.isEmpty) {
469-
return null;
470-
}
471-
472-
final splittedStackTrace =
473-
trimmedStackTrace.split('\n').where((element) => element.isNotEmpty);
474-
475-
if (splittedStackTrace.isEmpty) {
476-
return null;
477-
}
478-
479-
final lastLine = splittedStackTrace.last.trim();
480-
481-
if (lastLine.isEmpty) {
482-
return null;
483-
}
484-
485-
final lastLineIterator = lastLine.split(' ').iterator;
486-
487-
if (!lastLineIterator.moveNext()) {
488-
return null;
489-
}
490-
491-
final path = p.normalize(lastLineIterator.current);
492-
if (!File(path).existsSync()) {
467+
String? _getTraceLocation({
468+
required String cwd,
469+
required String stackTrace,
470+
}) {
471+
try {
472+
final trace = Trace.parse(stackTrace);
473+
if (trace.frames.isEmpty) {
474+
return null;
475+
}
476+
return trace.frames.last.location;
477+
} on FormatException {
493478
return null;
494479
}
495-
if (!lastLineIterator.moveNext()) {
496-
return path;
497-
}
498-
499-
return '$path:${lastLineIterator.current}';
500480
}

pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ dependencies:
1616
path: ^1.8.0
1717
pub_updater: ^0.2.1
1818
pubspec_parse: ^1.2.0
19+
stack_trace: 1.10.0
1920
universal_io: ^2.0.4
2021
usage: ^4.0.2
2122
very_good_analysis: ^2.4.0

test/src/cli/flutter_cli_test.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,9 @@ void main() {
381381
verify(
382382
() => logger.write(any(that: contains('Some tests failed.'))),
383383
).called(1);
384+
verify(
385+
() => logger.err(any(that: contains('- test/example_test.dart 4:5'))),
386+
).called(1);
384387
});
385388

386389
test('completes when there is a test directory (skipping)', () async {
@@ -534,6 +537,9 @@ void main() {
534537
verify(
535538
() => logger.write(any(that: contains('-1: Some tests failed.'))),
536539
).called(1);
540+
verify(
541+
() => logger.err(any(that: contains('- test/example_test.dart 5:5'))),
542+
).called(1);
537543
});
538544

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

0 commit comments

Comments
 (0)