fix(test): strip ansi colors in junit output

This commit is contained in:
Marvin Hagemeister 2024-04-15 12:42:43 +02:00 committed by Luca Casonato
parent 585ba28d47
commit 69bc3f14ed
No known key found for this signature in database
GPG Key ID: 01A83EB62563811F
4 changed files with 71 additions and 9 deletions

View File

@ -3,9 +3,14 @@
use std::collections::VecDeque;
use std::path::PathBuf;
use lazy_regex::Lazy;
use super::fmt::to_relative_path_or_remote_url;
use super::*;
static ANSI_ESCAPE_REMOVE: Lazy<Regex> =
lazy_regex::lazy_regex!(r#"\x1b\[([\x30-\x3f]*[\x20-\x2f]*[\x40-\x7e])"#);
pub struct JunitTestReporter {
cwd: Url,
output_path: String,
@ -31,13 +36,21 @@ impl JunitTestReporter {
match status {
TestResult::Ok => quick_junit::TestCaseStatus::success(),
TestResult::Ignored => quick_junit::TestCaseStatus::skipped(),
TestResult::Failed(failure) => quick_junit::TestCaseStatus::NonSuccess {
kind: quick_junit::NonSuccessKind::Failure,
message: Some(failure.overview()),
ty: None,
description: Some(failure.detail()),
reruns: vec![],
},
TestResult::Failed(failure) => {
let message = failure.overview();
let detail = failure.detail();
let message_stripped =
ANSI_ESCAPE_REMOVE.replace_all(message.as_str(), "");
let detail_stripped =
ANSI_ESCAPE_REMOVE.replace_all(detail.as_str(), "");
quick_junit::TestCaseStatus::NonSuccess {
kind: quick_junit::NonSuccessKind::Failure,
message: Some(message_stripped.into()),
ty: None,
description: Some(detail_stripped.into()),
reruns: vec![],
}
}
TestResult::Cancelled => quick_junit::TestCaseStatus::NonSuccess {
kind: quick_junit::NonSuccessKind::Error,
message: Some("Cancelled".to_string()),
@ -55,11 +68,17 @@ impl JunitTestReporter {
TestStepResult::Ok => quick_junit::TestCaseStatus::success(),
TestStepResult::Ignored => quick_junit::TestCaseStatus::skipped(),
TestStepResult::Failed(failure) => {
let message = failure.overview();
let detail = failure.detail();
let message_stripped =
ANSI_ESCAPE_REMOVE.replace_all(message.as_str(), "");
let detail_stripped =
ANSI_ESCAPE_REMOVE.replace_all(detail.as_str(), "");
quick_junit::TestCaseStatus::NonSuccess {
kind: quick_junit::NonSuccessKind::Failure,
message: Some(failure.overview()),
message: Some(message_stripped.into()),
ty: None,
description: Some(failure.detail()),
description: Some(detail_stripped.into()),
reruns: vec![],
}
}

View File

@ -338,6 +338,12 @@ itest!(junit_multiple_test_files {
exit_code: 1,
});
itest!(junit_strip_ansi {
args: "test --reporter junit test/fail_color.ts",
output: "test/junit_strip_ansi.junit.out",
exit_code: 1,
});
#[test]
fn junit_path() {
let context = TestContextBuilder::new().use_temp_cwd().build();

10
tests/testdata/test/fail_color.ts vendored Normal file
View File

@ -0,0 +1,10 @@
Deno.test("fail color", () => {
throw new Error(`RedMessage: \x1b[31mThis should be red text\x1b[39m`);
});
// deno-lint-ignore no-explicit-any
Deno.test("step fail color", async (t: any) => {
await t.step("step", () => {
throw new Error(`RedMessage: \x1b[31mThis should be red text\x1b[39m`);
});
});

View File

@ -0,0 +1,27 @@
Check file:///[WILDCARD]/test/fail_color.ts
<?xml version="1.0" encoding="UTF-8"?>
<testsuites name="deno test" tests="3" failures="3" errors="0" time="[WILDCARD]">
<testsuite name="./test/fail_color.ts" tests="3" disabled="0" errors="0" failures="3">
<testcase name="fail color" classname="./test/fail_color.ts" time="[WILDCARD]" line="1" col="6">
<failure message="Uncaught Error: RedMessage: This should be red text">Error: RedMessage: This should be red text
throw new Error(`RedMessage: \x1b[31mThis should be red text\x1b[39m`);
^
at file:///[WILDCARD]/test/fail_color.ts:2:9</failure>
</testcase>
<testcase name="step fail color" classname="./test/fail_color.ts" time="[WILDCARD]" line="6" col="6">
<failure message="1 test step failed">1 test step failed.</failure>
</testcase>
<testcase name="step fail color &gt; step" classname="./test/fail_color.ts" time="[WILDCARD]" line="7" col="11">
<failure message="Uncaught Error: RedMessage: This should be red text">Error: RedMessage: This should be red text
throw new Error(`RedMessage: \x1b[31mThis should be red text\x1b[39m`);
^
at file:///[WILDCARD]/test/fail_color.ts:8:11
at innerWrapped (ext:cli/40_test.js:174:11)
at exitSanitizer (ext:cli/40_test.js:103:33)
at Object.outerWrapped [as fn] (ext:cli/40_test.js:117:20)
at TestContext.step (ext:cli/40_test.js:475:37)
at file:///[WILDCARD]/test/fail_color.ts:7:11</failure>
</testcase>
</testsuite>
</testsuites>
error: Test failed