Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(test): Redact elapsed time in the minutes time frame #14233

Merged
merged 4 commits into from
Jul 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
208 changes: 120 additions & 88 deletions crates/cargo-test-support/src/compare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -127,41 +131,49 @@ 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]",
regex!(r"\[FINISHED\].*in (?<redacted>[0-9]+(\.[0-9]+))s"),
regex!(r"\[FINISHED\].*in (?<redacted>[0-9]+(\.[0-9]+)?(m [0-9]+)?)s"),
)
.unwrap();
// for UI tests
subs.insert(
"[ELAPSED]",
regex!(r"Finished.*in (?<redacted>[0-9]+(\.[0-9]+))s"),
regex!(r"Finished.*in (?<redacted>[0-9]+(\.[0-9]+)?(m [0-9]+)?)s"),
)
.unwrap();
// output from libtest
subs.insert(
"[ELAPSED]",
regex!(r"; finished in (?<redacted>[0-9]+(\.[0-9]+))s"),
regex!(r"; finished in (?<redacted>[0-9]+(\.[0-9]+)?(m [0-9]+)?)s"),
)
.unwrap();
subs.insert(
Expand All @@ -186,10 +198,6 @@ fn add_common_redactions(subs: &mut snapbox::Redactions) {
.unwrap();
subs.insert("[HASH]", regex!(r"/[a-z0-9\-_]+-(?<redacted>[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"(?<redacted>[0-9]+(\.[0-9]+)?) ns/iter"),
Expand Down Expand Up @@ -822,129 +830,153 @@ 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 snapbox::assert_data_eq;
use snapbox::prelude::*;
use snapbox::str;

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,
);
}

#[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 [ELAPSED]s"].raw()
);
}
}