Skip to content

Commit

Permalink
Auto merge of #14233 - epage:elapsed, r=weihanglo
Browse files Browse the repository at this point in the history
fix(test): Redact elapsed time in the minutes time frame

### What does this PR try to resolve?

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
```

### How should we test and review this PR?

### Additional information
  • Loading branch information
bors committed Jul 10, 2024
2 parents 17b52b0 + 7fcd580 commit 0d67af0
Showing 1 changed file with 120 additions and 88 deletions.
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()
);
}
}

0 comments on commit 0d67af0

Please sign in to comment.