Skip to content

Commit 6914c29

Browse files
committed
test: Merge common redactions
1 parent 4dcbca1 commit 6914c29

File tree

1 file changed

+19
-36
lines changed

1 file changed

+19
-36
lines changed

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

Lines changed: 19 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -77,37 +77,8 @@ use url::Url;
7777
/// a problem.
7878
/// - Carriage returns are removed, which can help when running on Windows.
7979
pub fn assert_ui() -> snapbox::Assert {
80-
let root = paths::root();
81-
// Use `from_file_path` instead of `from_dir_path` so the trailing slash is
82-
// put in the users output, rather than hidden in the variable
83-
let root_url = url::Url::from_file_path(&root).unwrap().to_string();
84-
8580
let mut subs = snapbox::Redactions::new();
86-
subs.extend(MIN_LITERAL_REDACTIONS.into_iter().cloned())
87-
.unwrap();
88-
subs.insert("[ROOT]", root).unwrap();
89-
subs.insert("[ROOTURL]", root_url).unwrap();
90-
subs.insert(
91-
"[ELAPSED]",
92-
regex::Regex::new("Finished.*in (?<redacted>[0-9]+(\\.[0-9]+))s").unwrap(),
93-
)
94-
.unwrap();
95-
// output from libtest
96-
subs.insert(
97-
"[ELAPSED]",
98-
regex::Regex::new("; finished in (?<redacted>[0-9]+(\\.[0-9]+))s").unwrap(),
99-
)
100-
.unwrap();
101-
subs.insert(
102-
"[FILE_SIZE]",
103-
regex::Regex::new("(?<redacted>[0-9]+(\\.[0-9]+)([a-zA-Z]i)?)B").unwrap(),
104-
)
105-
.unwrap();
106-
subs.insert(
107-
"[HASH]",
108-
regex::Regex::new("home/\\.cargo/registry/src/-(?<redacted>[a-z0-9]+)").unwrap(),
109-
)
110-
.unwrap();
81+
add_common_redactions(&mut subs);
11182
snapbox::Assert::new()
11283
.action_env(snapbox::assert::DEFAULT_ACTION_ENV)
11384
.redact_with(subs)
@@ -145,23 +116,38 @@ pub fn assert_ui() -> snapbox::Assert {
145116
/// a problem.
146117
/// - Carriage returns are removed, which can help when running on Windows.
147118
pub fn assert_e2e() -> snapbox::Assert {
119+
let mut subs = snapbox::Redactions::new();
120+
add_common_redactions(&mut subs);
121+
subs.extend(E2E_LITERAL_REDACTIONS.into_iter().cloned())
122+
.unwrap();
123+
124+
snapbox::Assert::new()
125+
.action_env(snapbox::assert::DEFAULT_ACTION_ENV)
126+
.redact_with(subs)
127+
}
128+
129+
fn add_common_redactions(subs: &mut snapbox::Redactions) {
148130
let root = paths::root();
149131
// Use `from_file_path` instead of `from_dir_path` so the trailing slash is
150132
// put in the users output, rather than hidden in the variable
151133
let root_url = url::Url::from_file_path(&root).unwrap().to_string();
152134

153-
let mut subs = snapbox::Redactions::new();
154135
subs.extend(MIN_LITERAL_REDACTIONS.into_iter().cloned())
155136
.unwrap();
156-
subs.extend(E2E_LITERAL_REDACTIONS.into_iter().cloned())
157-
.unwrap();
158137
subs.insert("[ROOT]", root).unwrap();
159138
subs.insert("[ROOTURL]", root_url).unwrap();
139+
// For e2e tests
160140
subs.insert(
161141
"[ELAPSED]",
162142
regex::Regex::new("[FINISHED].*in (?<redacted>[0-9]+(\\.[0-9]+))s").unwrap(),
163143
)
164144
.unwrap();
145+
// for UI tests
146+
subs.insert(
147+
"[ELAPSED]",
148+
regex::Regex::new("Finished.*in (?<redacted>[0-9]+(\\.[0-9]+))s").unwrap(),
149+
)
150+
.unwrap();
165151
// output from libtest
166152
subs.insert(
167153
"[ELAPSED]",
@@ -178,9 +164,6 @@ pub fn assert_e2e() -> snapbox::Assert {
178164
regex::Regex::new("home/\\.cargo/registry/src/-(?<redacted>[a-z0-9]+)").unwrap(),
179165
)
180166
.unwrap();
181-
snapbox::Assert::new()
182-
.action_env(snapbox::assert::DEFAULT_ACTION_ENV)
183-
.redact_with(subs)
184167
}
185168

186169
static MIN_LITERAL_REDACTIONS: &[(&str, &str)] = &[

0 commit comments

Comments
 (0)