From c8113d3a1ce06867458a52e04bcd836988faf2ec Mon Sep 17 00:00:00 2001 From: Ed Page Date: Wed, 10 Jul 2024 16:33:14 -0500 Subject: [PATCH 1/4] refactor(test): Move compare tests into a mod --- crates/cargo-test-support/src/compare.rs | 157 ++++++++++++----------- 1 file changed, 81 insertions(+), 76 deletions(-) diff --git a/crates/cargo-test-support/src/compare.rs b/crates/cargo-test-support/src/compare.rs index 4a533bd31e4..c3ee1533e54 100644 --- a/crates/cargo-test-support/src/compare.rs +++ b/crates/cargo-test-support/src/compare.rs @@ -822,129 +822,134 @@ impl fmt::Debug for WildStr<'_> { } } -#[test] -fn wild_str_cmp() { - for (a, b) in &[ - ("a b", "a b"), - ("a[..]b", "a b"), - ("a[..]", "a b"), - ("[..]", "a b"), - ("[..]b", "a b"), - ] { - assert_eq!(WildStr::new(a), WildStr::new(b)); - } - for (a, b) in &[("[..]b", "c"), ("b", "c"), ("b", "cb")] { - assert_ne!(WildStr::new(a), WildStr::new(b)); +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn wild_str_cmp() { + for (a, b) in &[ + ("a b", "a b"), + ("a[..]b", "a b"), + ("a[..]", "a b"), + ("[..]", "a b"), + ("[..]b", "a b"), + ] { + assert_eq!(WildStr::new(a), WildStr::new(b)); + } + for (a, b) in &[("[..]b", "c"), ("b", "c"), ("b", "cb")] { + assert_ne!(WildStr::new(a), WildStr::new(b)); + } } -} -#[test] -fn dirty_msvc() { - let case = |expected: &str, wild: &str, msvc: bool| { - assert_eq!(expected, &replace_dirty_msvc_impl(wild, msvc)); - }; + #[test] + fn dirty_msvc() { + let case = |expected: &str, wild: &str, msvc: bool| { + assert_eq!(expected, &replace_dirty_msvc_impl(wild, msvc)); + }; - // no replacements - case("aa", "aa", false); - case("aa", "aa", true); + // no replacements + case("aa", "aa", false); + case("aa", "aa", true); - // with replacements - case( - "\ + // with replacements + case( + "\ [DIRTY] a", - "\ + "\ [DIRTY-MSVC] a", - true, - ); - case( - "", - "\ + true, + ); + case( + "", + "\ [DIRTY-MSVC] a", - false, - ); - case( - "\ + false, + ); + case( + "\ [DIRTY] a [COMPILING] a", - "\ + "\ [DIRTY-MSVC] a [COMPILING] a", - true, - ); - case( - "\ + true, + ); + case( + "\ [COMPILING] a", - "\ + "\ [DIRTY-MSVC] a [COMPILING] a", - false, - ); + false, + ); - // test trailing newline behavior - case( - "\ + // test trailing newline behavior + case( + "\ A B ", "\ A B ", true, - ); + ); - case( - "\ + case( + "\ A B ", "\ A B ", false, - ); + ); - case( - "\ + case( + "\ A B", "\ A B", true, - ); + ); - case( - "\ + case( + "\ A B", "\ A B", false, - ); + ); - case( - "\ + case( + "\ [DIRTY] a ", - "\ + "\ [DIRTY-MSVC] a ", - true, - ); - case( - "\n", - "\ + true, + ); + case( + "\n", + "\ [DIRTY-MSVC] a ", - false, - ); + false, + ); - case( - "\ + case( + "\ [DIRTY] a", - "\ + "\ [DIRTY-MSVC] a", - true, - ); - case( - "", - "\ + true, + ); + case( + "", + "\ [DIRTY-MSVC] a", - false, - ); + false, + ); + } } From a10b3ca8b28972bb5115fe0fae4c42758650771f Mon Sep 17 00:00:00 2001 From: Ed Page Date: Wed, 10 Jul 2024 16:37:12 -0500 Subject: [PATCH 2/4] refactor(test): Split out 'pure' redactions for testing --- crates/cargo-test-support/src/compare.rs | 26 ++++++++++++++++-------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/crates/cargo-test-support/src/compare.rs b/crates/cargo-test-support/src/compare.rs index c3ee1533e54..1a84df7c3b7 100644 --- a/crates/cargo-test-support/src/compare.rs +++ b/crates/cargo-test-support/src/compare.rs @@ -88,7 +88,11 @@ macro_rules! regex { /// - Carriage returns are removed, which can help when running on Windows. pub fn assert_ui() -> snapbox::Assert { let mut subs = snapbox::Redactions::new(); - add_common_redactions(&mut subs); + subs.extend(MIN_LITERAL_REDACTIONS.into_iter().cloned()) + .unwrap(); + add_test_support_redactions(&mut subs); + add_regex_redactions(&mut subs); + snapbox::Assert::new() .action_env(snapbox::assert::DEFAULT_ACTION_ENV) .redact_with(subs) @@ -127,25 +131,33 @@ pub fn assert_ui() -> snapbox::Assert { /// - 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(MIN_LITERAL_REDACTIONS.into_iter().cloned()) + .unwrap(); subs.extend(E2E_LITERAL_REDACTIONS.into_iter().cloned()) .unwrap(); + add_test_support_redactions(&mut subs); + add_regex_redactions(&mut subs); snapbox::Assert::new() .action_env(snapbox::assert::DEFAULT_ACTION_ENV) .redact_with(subs) } -fn add_common_redactions(subs: &mut snapbox::Redactions) { +fn add_test_support_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(); - subs.extend(MIN_LITERAL_REDACTIONS.into_iter().cloned()) - .unwrap(); subs.insert("[ROOT]", root).unwrap(); subs.insert("[ROOTURL]", root_url).unwrap(); + subs.insert("[HOST_TARGET]", rustc_host()).unwrap(); + if let Some(alt_target) = try_alternate() { + subs.insert("[ALT_TARGET]", alt_target).unwrap(); + } +} + +fn add_regex_redactions(subs: &mut snapbox::Redactions) { // For e2e tests subs.insert( "[ELAPSED]", @@ -186,10 +198,6 @@ fn add_common_redactions(subs: &mut snapbox::Redactions) { .unwrap(); subs.insert("[HASH]", regex!(r"/[a-z0-9\-_]+-(?[0-9a-f]{16})")) .unwrap(); - subs.insert("[HOST_TARGET]", rustc_host()).unwrap(); - if let Some(alt_target) = try_alternate() { - subs.insert("[ALT_TARGET]", alt_target).unwrap(); - } subs.insert( "[AVG_ELAPSED]", regex!(r"(?[0-9]+(\.[0-9]+)?) ns/iter"), From 5ed63926d36f6c0e5494ee989b62cf330b93ffba Mon Sep 17 00:00:00 2001 From: Ed Page Date: Wed, 10 Jul 2024 16:40:47 -0500 Subject: [PATCH 3/4] test(test): Verify elapsed redaction --- crates/cargo-test-support/src/compare.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/crates/cargo-test-support/src/compare.rs b/crates/cargo-test-support/src/compare.rs index 1a84df7c3b7..c6ed9765e72 100644 --- a/crates/cargo-test-support/src/compare.rs +++ b/crates/cargo-test-support/src/compare.rs @@ -832,6 +832,10 @@ impl fmt::Debug for WildStr<'_> { #[cfg(test)] mod test { + use snapbox::assert_data_eq; + use snapbox::prelude::*; + use snapbox::str; + use super::*; #[test] @@ -960,4 +964,19 @@ B", false, false, ); } + + #[test] + fn redact_elapsed_time() { + let mut subs = snapbox::Redactions::new(); + add_regex_redactions(&mut subs); + + assert_data_eq!( + subs.redact("[FINISHED] `release` profile [optimized] target(s) in 5.5s"), + str!["[FINISHED] `release` profile [optimized] target(s) in [ELAPSED]s"].raw() + ); + assert_data_eq!( + subs.redact("[FINISHED] `release` profile [optimized] target(s) in 1m 05s"), + str!["[FINISHED] `release` profile [optimized] target(s) in 1m 05s"].raw() + ); + } } From 7fcd580b3b4f125b8f6878ba4af90cd734cd0b95 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Wed, 10 Jul 2024 16:42:21 -0500 Subject: [PATCH 4/4] fix(test): Redact elapsed time in the minutes time frame This came up in #14231 ``` ---- expected: tests/testsuite/lto.rs:611:27 ++++ actual: stderr 1 1 | [FRESH] registry-shared v0.0.1 2 2 | [FRESH] registry v0.0.1 3 3 | [COMPILING] bar v0.0.0 ([ROOT]/foo/bar) 4 4 | [RUNNING] `rustc --crate-name bar [..]-C lto [..]--test [..]` 5 5 | [RUNNING] `rustc --crate-name b [..]-C lto [..]--test [..]` 6 - [FINISHED] `release` profile [optimized] target(s) in [ELAPSED]s 7 6 | [RUNNING] `[ROOT]/foo/target/release/deps/bar-[HASH][EXE]` 8 7 | [RUNNING] `[ROOT]/foo/target/release/deps/b-[HASH][EXE]` 9 8 | [DOCTEST] bar 10 9 | [RUNNING] `rustdoc --edition=2015 --crate-type cdylib --crate-type rlib --crate-name bar --test [..]-C lto [..] 10 + [FINISHED] `release` profile [optimized] target(s) in 1m 00s Update with SNAPSHOTS=overwrite ``` --- crates/cargo-test-support/src/compare.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/crates/cargo-test-support/src/compare.rs b/crates/cargo-test-support/src/compare.rs index c6ed9765e72..4a7c6a607e9 100644 --- a/crates/cargo-test-support/src/compare.rs +++ b/crates/cargo-test-support/src/compare.rs @@ -161,19 +161,19 @@ fn add_regex_redactions(subs: &mut snapbox::Redactions) { // For e2e tests subs.insert( "[ELAPSED]", - regex!(r"\[FINISHED\].*in (?[0-9]+(\.[0-9]+))s"), + regex!(r"\[FINISHED\].*in (?[0-9]+(\.[0-9]+)?(m [0-9]+)?)s"), ) .unwrap(); // for UI tests subs.insert( "[ELAPSED]", - regex!(r"Finished.*in (?[0-9]+(\.[0-9]+))s"), + regex!(r"Finished.*in (?[0-9]+(\.[0-9]+)?(m [0-9]+)?)s"), ) .unwrap(); // output from libtest subs.insert( "[ELAPSED]", - regex!(r"; finished in (?[0-9]+(\.[0-9]+))s"), + regex!(r"; finished in (?[0-9]+(\.[0-9]+)?(m [0-9]+)?)s"), ) .unwrap(); subs.insert( @@ -976,7 +976,7 @@ B", false, ); assert_data_eq!( subs.redact("[FINISHED] `release` profile [optimized] target(s) in 1m 05s"), - str!["[FINISHED] `release` profile [optimized] target(s) in 1m 05s"].raw() + str!["[FINISHED] `release` profile [optimized] target(s) in [ELAPSED]s"].raw() ); } }