Skip to content

Commit 393819b

Browse files
committed
remove non-deterministic thread numbers from output
1 parent 6e84ace commit 393819b

1 file changed

Lines changed: 17 additions & 8 deletions

File tree

src/output.rs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
use std::sync::LazyLock;
2+
13
use crate::test_name_formatter::format_test_name;
4+
use regex::Regex;
25
use serde::{Deserialize, Serialize};
36

47
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
@@ -19,6 +22,9 @@ pub struct TestResult {
1922
pub output: Option<String>,
2023
}
2124

25+
static THREAD_PANICKED_REGEX: LazyLock<Regex> =
26+
LazyLock::new(|| Regex::new(r"thread '(?<testname>.*)' \(.*\) panicked at").unwrap());
27+
2228
impl TestResult {
2329
pub fn ok(name: String, test_code: String) -> TestResult {
2430
TestResult {
@@ -40,16 +46,19 @@ impl TestResult {
4046
_ => (None, message.map(|m| m.trim_start().to_owned())),
4147
};
4248

43-
// This note is attached to the error message of only one test case that fails,
44-
// but not always the same one. To avoid CI failing unnecessarily, this note
45-
// is stripped from all messages.
46-
// It's also not useful to students reading the output of the test runner,
47-
// as they can't set this environment variable in the test runner themselves.
4849
let message = message.map(|m| {
49-
m.trim_end_matches(
50+
// This note is attached to the error message of only one test case that fails,
51+
// but not always the same one. To avoid CI failing unnecessarily, this note
52+
// is stripped from all messages.
53+
// It's also not useful to students reading the output of the test runner,
54+
// as they can't set this environment variable in the test runner themselves.
55+
let m = m.trim_end_matches(
5056
"note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace\n",
51-
)
52-
.to_owned()
57+
);
58+
// If the test panics, the message contains a thread number that's
59+
// not deterministic. Strip it to improve test reliability.
60+
let m = THREAD_PANICKED_REGEX.replace(m, "thread '$testname' panicked at");
61+
m.to_string()
5362
});
5463

5564
TestResult {

0 commit comments

Comments
 (0)