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

test(gc): Update remaining unordered tests to snapbox #14785

Merged
merged 2 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
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
52 changes: 1 addition & 51 deletions crates/cargo-test-support/src/compare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@

use crate::cross_compile::try_alternate;
use crate::paths;
use crate::{diff, rustc_host};
use crate::rustc_host;
use anyhow::{bail, Result};
use snapbox::Data;
use snapbox::IntoData;
Expand Down Expand Up @@ -431,56 +431,6 @@ fn substitute_macros(input: &str) -> String {
result
}

/// Checks that the given string contains the given lines, ignoring the order
/// of the lines.
///
/// See [Patterns](index.html#patterns) for more information on pattern matching.
pub(crate) fn match_unordered(expected: &str, actual: &str, cwd: Option<&Path>) -> Result<()> {
let expected = normalize_expected(expected, cwd);
let actual = normalize_actual(actual, cwd);
let e: Vec<_> = expected.lines().map(|line| WildStr::new(line)).collect();
let mut a: Vec<_> = actual.lines().map(|line| WildStr::new(line)).collect();
// match more-constrained lines first, although in theory we'll
// need some sort of recursive match here. This handles the case
// that you expect "a\n[..]b" and two lines are printed out,
// "ab\n"a", where technically we do match unordered but a naive
// search fails to find this. This simple sort at least gets the
// test suite to pass for now, but we may need to get more fancy
// if tests start failing again.
a.sort_by_key(|s| s.line.len());
let mut changes = Vec::new();
let mut a_index = 0;
let mut failure = false;

use crate::diff::Change;
for (e_i, e_line) in e.into_iter().enumerate() {
match a.iter().position(|a_line| e_line == *a_line) {
Some(index) => {
let a_line = a.remove(index);
changes.push(Change::Keep(e_i, index, a_line));
a_index += 1;
}
None => {
failure = true;
changes.push(Change::Remove(e_i, e_line));
}
}
}
for unmatched in a {
failure = true;
changes.push(Change::Add(a_index, unmatched));
a_index += 1;
}
if failure {
bail!(
"Expected lines did not match (ignoring order):\n{}\n",
diff::render_colored_changes(&changes)
);
} else {
Ok(())
}
}

/// Checks that the given string contains the given contiguous lines
/// somewhere.
///
Expand Down
49 changes: 0 additions & 49 deletions crates/cargo-test-support/src/diff.rs

This file was deleted.

50 changes: 0 additions & 50 deletions crates/cargo-test-support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ pub use cargo_test_macro::cargo_test;
pub mod compare;
pub mod containers;
pub mod cross_compile;
mod diff;
pub mod git;
pub mod install;
pub mod paths;
Expand Down Expand Up @@ -651,8 +650,6 @@ pub struct Execs {
expect_stderr_contains: Vec<String>,
expect_stdout_not_contains: Vec<String>,
expect_stderr_not_contains: Vec<String>,
expect_stdout_unordered: Vec<String>,
expect_stderr_unordered: Vec<String>,
expect_stderr_with_without: Vec<(Vec<String>, Vec<String>)>,
stream_output: bool,
assert: snapbox::Assert,
Expand Down Expand Up @@ -751,43 +748,6 @@ impl Execs {
self
}

/// Verifies that all of the stdout output is equal to the given lines,
/// ignoring the order of the lines.
///
/// See [`Execs::with_stderr_unordered`] for more details.
#[deprecated(note = "replaced with `Execs::with_stdout_data(expected.unordered())`")]
pub fn with_stdout_unordered<S: ToString>(&mut self, expected: S) -> &mut Self {
self.expect_stdout_unordered.push(expected.to_string());
self
}

/// Verifies that all of the stderr output is equal to the given lines,
/// ignoring the order of the lines.
///
/// See [`compare`] for supported patterns.
///
/// This is useful when checking the output of `cargo build -v` since
/// the order of the output is not always deterministic.
/// Recommend use `with_stderr_contains` instead unless you really want to
/// check *every* line of output.
///
/// Be careful when using patterns such as `[..]`, because you may end up
/// with multiple lines that might match, and this is not smart enough to
/// do anything like longest-match. For example, avoid something like:
///
/// ```text
/// [RUNNING] `rustc [..]
/// [RUNNING] `rustc --crate-name foo [..]
/// ```
///
/// This will randomly fail if the other crate name is `bar`, and the
/// order changes.
#[deprecated(note = "replaced with `Execs::with_stderr_data(expected.unordered())`")]
pub fn with_stderr_unordered<S: ToString>(&mut self, expected: S) -> &mut Self {
self.expect_stderr_unordered.push(expected.to_string());
self
}

/// Verify that a particular line appears in stderr with and without the
/// given substrings. Exactly one line must match.
///
Expand Down Expand Up @@ -993,8 +953,6 @@ impl Execs {
&& self.expect_stderr_contains.is_empty()
&& self.expect_stdout_not_contains.is_empty()
&& self.expect_stderr_not_contains.is_empty()
&& self.expect_stdout_unordered.is_empty()
&& self.expect_stderr_unordered.is_empty()
&& self.expect_stderr_with_without.is_empty()
{
panic!(
Expand Down Expand Up @@ -1107,12 +1065,6 @@ impl Execs {
for expect in self.expect_stderr_not_contains.iter() {
compare::match_does_not_contain(expect, stderr, cwd)?;
}
for expect in self.expect_stdout_unordered.iter() {
compare::match_unordered(expect, stdout, cwd)?;
}
for expect in self.expect_stderr_unordered.iter() {
compare::match_unordered(expect, stderr, cwd)?;
}
for (with, without) in self.expect_stderr_with_without.iter() {
compare::match_with_without(stderr, with, without, cwd)?;
}
Expand Down Expand Up @@ -1141,8 +1093,6 @@ pub fn execs() -> Execs {
expect_stderr_contains: Vec::new(),
expect_stdout_not_contains: Vec::new(),
expect_stderr_not_contains: Vec::new(),
expect_stdout_unordered: Vec::new(),
expect_stderr_unordered: Vec::new(),
expect_stderr_with_without: Vec::new(),
stream_output: false,
assert: compare::assert_e2e(),
Expand Down
Loading