Skip to content

Commit

Permalink
fix: handle flutter errors
Browse files Browse the repository at this point in the history
  • Loading branch information
rIIh committed Dec 21, 2024
1 parent 775316d commit 2b1dc75
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 6 deletions.
4 changes: 4 additions & 0 deletions packages/allure_report/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.2.4

- **fix:** handle flutter errors

## 1.2.3

- **chore:** tighten test_reporter dependency version
Expand Down
7 changes: 6 additions & 1 deletion packages/allure_report/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,9 @@ testFile.copySync(test);
print('event:attachment:$base');
print('event:attachment:$test');
```
```

## TODO

- [x] Handle Flutter errors
- [ ] Add steps support
63 changes: 59 additions & 4 deletions packages/allure_report/lib/src/allure_reporter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import 'dart:convert';

import 'package:allure_report/src/allure.dart';
import 'package:allure_report/src/events/diff_event.dart';
import 'package:allure_report/src/models/allure_link.dart';
import 'package:allure_report/src/models/severity.dart';
import 'package:allure_report/src/test_report.dart';
import 'package:mime/mime.dart';
import 'package:test_reporter/test_reporter.dart';
Expand Down Expand Up @@ -120,13 +118,32 @@ class AllureReporter implements TestReporter {
):
break;

case TestMessageEvent event:
handleFlutterError(event);

case TestErrorEvent event:
tests.putIfAbsent(event.testID, () => TestReport());
tests[event.testID] = tests[event.testID]!.copyWith(error: event);
if (tests[event.testID] case TestReport report) {
// attach error if no error was attached before.
if (report.error == null) {
tests[event.testID] = report.copyWith(error: event);
}
} else {
tests[event.testID] = tests[event.testID]!.copyWith(error: event);
}

case TestDoneEvent event:
tests.putIfAbsent(event.testID, () => TestReport());
tests[event.testID] = tests[event.testID]!.copyWith(end: event);

final error = tests[event.testID]!.error;
final status = switch (error) {
TestErrorEvent error when error.isFailure => TestResult.failure,
_ => event.result,
};

tests[event.testID] = tests[event.testID]!.copyWith(
end: event.copyWith(result: status),
);

onReportCreated(tests[event.testID]!);

Expand Down Expand Up @@ -285,6 +302,44 @@ class AllureReporter implements TestReporter {
File('allure-results/${json['id']}-result.json')
.writeAsString(JsonEncoder.withIndent(' ').convert(json));
}

/// Flutter errors are pushed to console and require additional processing.
void handleFlutterError(TestMessageEvent event) {
if (event.message.contains('EXCEPTION CAUGHT')) {
final parts = event.message
.split('When the exception was thrown, this was the stack:');

final isTestFailure = event.message.contains('following TestFailure was');
final String details;
final String stackTrace;

if (parts.length > 1) {
RegExp(r'^═+╡.+╞═+$');
details = parts[0] //
.replaceAll(RegExp(r'^═+╡.+╞═+\n'), '')
.replaceAll(
'The following TestFailure was thrown running a test:\n',
'',
);

stackTrace = parts[1].replaceAll(RegExp(r'\n═+$'), '');
} else {
details = event.message;
stackTrace = '';
}

tests.putIfAbsent(event.testID, () => TestReport());
tests[event.testID] = tests[event.testID]!.copyWith(
error: TestErrorEvent(
testID: event.testID,
error: details,
stackTrace: stackTrace,
isFailure: isTestFailure,
time: event.time,
),
);
}
}
}

/// Special event to attach files to test cases
Expand Down
2 changes: 1 addition & 1 deletion packages/allure_report/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: allure_report
description: Allure Report Adapter. Use it with [test_reporter](https://pub.dev/packages/test_reporter) package
version: 1.2.3
version: 1.2.4
repository: https://github.com/rIIh/dart_test_reporter
issue_tracker: https://github.com/rIIh/dart_test_reporter/issues?q=is%3Aissue+is%3Aopen+sort%3Aupdated-desc+%5Ballure_report%5D

Expand Down

0 comments on commit 2b1dc75

Please sign in to comment.