Skip to content

Commit d7de937

Browse files
committed
test: Only compile regex replacements once
1 parent 6914c29 commit d7de937

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

crates/cargo-test-support/src/compare.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,15 @@ use std::path::Path;
4545
use std::str;
4646
use url::Url;
4747

48+
/// This makes it easier to write regex replacements that are guaranteed to only
49+
/// get compiled once
50+
macro_rules! regex {
51+
($re:literal $(,)?) => {{
52+
static RE: std::sync::OnceLock<regex::Regex> = std::sync::OnceLock::new();
53+
RE.get_or_init(|| regex::Regex::new($re).unwrap())
54+
}};
55+
}
56+
4857
/// Assertion policy for UI tests
4958
///
5059
/// This emphasizes showing as much content as possible at the cost of more brittleness
@@ -139,29 +148,29 @@ fn add_common_redactions(subs: &mut snapbox::Redactions) {
139148
// For e2e tests
140149
subs.insert(
141150
"[ELAPSED]",
142-
regex::Regex::new("[FINISHED].*in (?<redacted>[0-9]+(\\.[0-9]+))s").unwrap(),
151+
regex!("[FINISHED].*in (?<redacted>[0-9]+(\\.[0-9]+))s"),
143152
)
144153
.unwrap();
145154
// for UI tests
146155
subs.insert(
147156
"[ELAPSED]",
148-
regex::Regex::new("Finished.*in (?<redacted>[0-9]+(\\.[0-9]+))s").unwrap(),
157+
regex!("Finished.*in (?<redacted>[0-9]+(\\.[0-9]+))s"),
149158
)
150159
.unwrap();
151160
// output from libtest
152161
subs.insert(
153162
"[ELAPSED]",
154-
regex::Regex::new("; finished in (?<redacted>[0-9]+(\\.[0-9]+))s").unwrap(),
163+
regex!("; finished in (?<redacted>[0-9]+(\\.[0-9]+))s"),
155164
)
156165
.unwrap();
157166
subs.insert(
158167
"[FILE_SIZE]",
159-
regex::Regex::new("(?<redacted>[0-9]+(\\.[0-9]+)([a-zA-Z]i)?)B").unwrap(),
168+
regex!("(?<redacted>[0-9]+(\\.[0-9]+)([a-zA-Z]i)?)B"),
160169
)
161170
.unwrap();
162171
subs.insert(
163172
"[HASH]",
164-
regex::Regex::new("home/\\.cargo/registry/src/-(?<redacted>[a-z0-9]+)").unwrap(),
173+
regex!("home/\\.cargo/registry/src/-(?<redacted>[a-z0-9]+)"),
165174
)
166175
.unwrap();
167176
}

0 commit comments

Comments
 (0)