From 6914c2958f12ed553cfcf4d0c11ff8027d15005a Mon Sep 17 00:00:00 2001 From: Scott Schafer Date: Wed, 12 Jun 2024 09:28:24 -0600 Subject: [PATCH] test: Merge common redactions --- crates/cargo-test-support/src/compare.rs | 55 ++++++++---------------- 1 file changed, 19 insertions(+), 36 deletions(-) diff --git a/crates/cargo-test-support/src/compare.rs b/crates/cargo-test-support/src/compare.rs index 8f7a510eb..c877c2378 100644 --- a/crates/cargo-test-support/src/compare.rs +++ b/crates/cargo-test-support/src/compare.rs @@ -77,37 +77,8 @@ use url::Url; /// a problem. /// - Carriage returns are removed, which can help when running on Windows. pub fn assert_ui() -> snapbox::Assert { - let root = paths::root(); - // Use `from_file_path` instead of `from_dir_path` so the trailing slash is - // put in the users output, rather than hidden in the variable - let root_url = url::Url::from_file_path(&root).unwrap().to_string(); - let mut subs = snapbox::Redactions::new(); - subs.extend(MIN_LITERAL_REDACTIONS.into_iter().cloned()) - .unwrap(); - subs.insert("[ROOT]", root).unwrap(); - subs.insert("[ROOTURL]", root_url).unwrap(); - subs.insert( - "[ELAPSED]", - regex::Regex::new("Finished.*in (?[0-9]+(\\.[0-9]+))s").unwrap(), - ) - .unwrap(); - // output from libtest - subs.insert( - "[ELAPSED]", - regex::Regex::new("; finished in (?[0-9]+(\\.[0-9]+))s").unwrap(), - ) - .unwrap(); - subs.insert( - "[FILE_SIZE]", - regex::Regex::new("(?[0-9]+(\\.[0-9]+)([a-zA-Z]i)?)B").unwrap(), - ) - .unwrap(); - subs.insert( - "[HASH]", - regex::Regex::new("home/\\.cargo/registry/src/-(?[a-z0-9]+)").unwrap(), - ) - .unwrap(); + add_common_redactions(&mut subs); snapbox::Assert::new() .action_env(snapbox::assert::DEFAULT_ACTION_ENV) .redact_with(subs) @@ -145,23 +116,38 @@ pub fn assert_ui() -> snapbox::Assert { /// a problem. /// - Carriage returns are removed, which can help when running on Windows. pub fn assert_e2e() -> snapbox::Assert { + let mut subs = snapbox::Redactions::new(); + add_common_redactions(&mut subs); + subs.extend(E2E_LITERAL_REDACTIONS.into_iter().cloned()) + .unwrap(); + + snapbox::Assert::new() + .action_env(snapbox::assert::DEFAULT_ACTION_ENV) + .redact_with(subs) +} + +fn add_common_redactions(subs: &mut snapbox::Redactions) { let root = paths::root(); // Use `from_file_path` instead of `from_dir_path` so the trailing slash is // put in the users output, rather than hidden in the variable let root_url = url::Url::from_file_path(&root).unwrap().to_string(); - let mut subs = snapbox::Redactions::new(); subs.extend(MIN_LITERAL_REDACTIONS.into_iter().cloned()) .unwrap(); - subs.extend(E2E_LITERAL_REDACTIONS.into_iter().cloned()) - .unwrap(); subs.insert("[ROOT]", root).unwrap(); subs.insert("[ROOTURL]", root_url).unwrap(); + // For e2e tests subs.insert( "[ELAPSED]", regex::Regex::new("[FINISHED].*in (?[0-9]+(\\.[0-9]+))s").unwrap(), ) .unwrap(); + // for UI tests + subs.insert( + "[ELAPSED]", + regex::Regex::new("Finished.*in (?[0-9]+(\\.[0-9]+))s").unwrap(), + ) + .unwrap(); // output from libtest subs.insert( "[ELAPSED]", @@ -178,9 +164,6 @@ pub fn assert_e2e() -> snapbox::Assert { regex::Regex::new("home/\\.cargo/registry/src/-(?[a-z0-9]+)").unwrap(), ) .unwrap(); - snapbox::Assert::new() - .action_env(snapbox::assert::DEFAULT_ACTION_ENV) - .redact_with(subs) } static MIN_LITERAL_REDACTIONS: &[(&str, &str)] = &[