Skip to content

Commit 3929007

Browse files
Add normalize() in run-make Diff type
1 parent a743116 commit 3929007

File tree

1 file changed

+20
-3
lines changed
  • src/tools/run-make-support/src/diff

1 file changed

+20
-3
lines changed

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

Lines changed: 20 additions & 3 deletions
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,24 @@ impl Diff {
5866
self
5967
}
6068

69+
pub fn normalize(&mut self, regex: &str, replacement: &str) -> &mut Self {
70+
self.normalizers.push((regex.to_owned(), replacement.to_owned()));
71+
self
72+
}
73+
6174
/// Executes the diff process, prints any differences to the standard error.
6275
#[track_caller]
6376
pub fn run(&self) {
6477
let expected = self.expected.as_ref().expect("expected text not set");
65-
let actual = self.actual.as_ref().expect("actual text not set");
78+
let mut actual = self.actual.as_ref().expect("actual text not set").to_string();
6679
let expected_name = self.expected_name.as_ref().unwrap();
6780
let actual_name = self.actual_name.as_ref().unwrap();
81+
for (regex, replacement) in &self.normalizers {
82+
let re = Regex::new(regex).expect("bad regex in custom normalization rule");
83+
actual = re.replace_all(&actual, replacement).into_owned();
84+
}
6885

69-
let output = TextDiff::from_lines(expected, actual)
86+
let output = TextDiff::from_lines(expected, &actual)
7087
.unified_diff()
7188
.header(expected_name, actual_name)
7289
.to_string();

0 commit comments

Comments
 (0)