Skip to content

Commit 7350a7f

Browse files
authored
Rollup merge of #124561 - GuillaumeGomez:run-make-normalize, r=jieyouxu
Add `normalize()` in run-make `Diff` type I need it to do the same as: ``` //@ normalize-stdout-test "finished in \d+\.\d+s" -> "finished in $$TIME" ``` in doctests. I need it in particular for the #123974 PR (which contains this commit until this PR current PR is merged). cc `@Urgau` r? `@jieyouxu`
2 parents 9ef81e0 + e0ec71f commit 7350a7f

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

src/tools/run-make-support/src/diff/mod.rs

+25-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use regex::Regex;
12
use similar::TextDiff;
23
use std::path::Path;
34

@@ -14,12 +15,19 @@ pub struct Diff {
1415
expected_name: Option<String>,
1516
actual: Option<String>,
1617
actual_name: Option<String>,
18+
normalizers: Vec<(String, String)>,
1719
}
1820

1921
impl Diff {
2022
/// Construct a bare `diff` invocation.
2123
pub fn new() -> Self {
22-
Self { expected: None, expected_name: None, actual: None, actual_name: None }
24+
Self {
25+
expected: None,
26+
expected_name: None,
27+
actual: None,
28+
actual_name: None,
29+
normalizers: Vec::new(),
30+
}
2331
}
2432

2533
/// Specify the expected output for the diff from a file.
@@ -58,15 +66,29 @@ impl Diff {
5866
self
5967
}
6068

69+
/// Specify a regex that should replace text in the "actual" text that will be compared.
70+
pub fn normalize<R: Into<String>, I: Into<String>>(
71+
&mut self,
72+
regex: R,
73+
replacement: I,
74+
) -> &mut Self {
75+
self.normalizers.push((regex.into(), replacement.into()));
76+
self
77+
}
78+
6179
/// Executes the diff process, prints any differences to the standard error.
6280
#[track_caller]
6381
pub fn run(&self) {
6482
let expected = self.expected.as_ref().expect("expected text not set");
65-
let actual = self.actual.as_ref().expect("actual text not set");
83+
let mut actual = self.actual.as_ref().expect("actual text not set").to_string();
6684
let expected_name = self.expected_name.as_ref().unwrap();
6785
let actual_name = self.actual_name.as_ref().unwrap();
86+
for (regex, replacement) in &self.normalizers {
87+
let re = Regex::new(regex).expect("bad regex in custom normalization rule");
88+
actual = re.replace_all(&actual, replacement).into_owned();
89+
}
6890

69-
let output = TextDiff::from_lines(expected, actual)
91+
let output = TextDiff::from_lines(expected, &actual)
7092
.unified_diff()
7193
.header(expected_name, actual_name)
7294
.to_string();

src/tools/run-make-support/src/diff/tests.rs

+22
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,26 @@ test failed: `EXPECTED_TEXT` is different from `ACTUAL_TEXT`
3636

3737
assert_eq!(output.downcast_ref::<String>().unwrap(), expected_output);
3838
}
39+
40+
#[test]
41+
fn test_normalize() {
42+
let expected = "
43+
running 2 tests
44+
..
45+
46+
test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in $TIME
47+
";
48+
let actual = "
49+
running 2 tests
50+
..
51+
52+
test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.02s
53+
";
54+
55+
diff()
56+
.expected_text("EXPECTED_TEXT", expected)
57+
.actual_text("ACTUAL_TEXT", actual)
58+
.normalize(r#"finished in \d+\.\d+s"#, "finished in $$TIME")
59+
.run();
60+
}
3961
}

0 commit comments

Comments
 (0)