From 2b1dc75a9f51a15394705ea072322fe6c61653a3 Mon Sep 17 00:00:00 2001 From: melvspace Date: Sat, 21 Dec 2024 14:07:03 +0300 Subject: [PATCH] fix: handle flutter errors --- packages/allure_report/CHANGELOG.md | 4 ++ packages/allure_report/README.md | 7 ++- .../lib/src/allure_reporter.dart | 63 +++++++++++++++++-- packages/allure_report/pubspec.yaml | 2 +- 4 files changed, 70 insertions(+), 6 deletions(-) diff --git a/packages/allure_report/CHANGELOG.md b/packages/allure_report/CHANGELOG.md index 016b609..6eafb3d 100644 --- a/packages/allure_report/CHANGELOG.md +++ b/packages/allure_report/CHANGELOG.md @@ -1,3 +1,7 @@ +## 1.2.4 + +- **fix:** handle flutter errors + ## 1.2.3 - **chore:** tighten test_reporter dependency version diff --git a/packages/allure_report/README.md b/packages/allure_report/README.md index 31bd5b9..23d5e6b 100644 --- a/packages/allure_report/README.md +++ b/packages/allure_report/README.md @@ -64,4 +64,9 @@ testFile.copySync(test); print('event:attachment:$base'); print('event:attachment:$test'); -``` \ No newline at end of file +``` + +## TODO + +- [x] Handle Flutter errors +- [ ] Add steps support \ No newline at end of file diff --git a/packages/allure_report/lib/src/allure_reporter.dart b/packages/allure_report/lib/src/allure_reporter.dart index 4428a27..02d78c9 100644 --- a/packages/allure_report/lib/src/allure_reporter.dart +++ b/packages/allure_report/lib/src/allure_reporter.dart @@ -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'; @@ -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]!); @@ -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 diff --git a/packages/allure_report/pubspec.yaml b/packages/allure_report/pubspec.yaml index b02120d..4644cc4 100644 --- a/packages/allure_report/pubspec.yaml +++ b/packages/allure_report/pubspec.yaml @@ -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