Skip to content

Commit

Permalink
Auto merge of #14577 - epage:tests-basic, r=weihanglo
Browse files Browse the repository at this point in the history
test: Migrate remaining with_stdout/with_stderr calls

### What does this PR try to resolve?

This is part of #14039 and is another step towards us not needing our own redaction logic.

Along the way, I switched us to using `expect` to make it easier to tell when `allow(deprecated)` should be removed.

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

### Additional information
  • Loading branch information
bors committed Sep 24, 2024
2 parents 5e44da4 + 7ab9320 commit 844457c
Show file tree
Hide file tree
Showing 46 changed files with 159 additions and 172 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ cargo-credential-macos-keychain = { version = "0.4.7", path = "credential/cargo-
cargo-credential-wincred = { version = "0.4.7", path = "credential/cargo-credential-wincred" }
cargo-platform = { path = "crates/cargo-platform", version = "0.1.5" }
cargo-test-macro = { version = "0.3.0", path = "crates/cargo-test-macro" }
cargo-test-support = { version = "0.4.0", path = "crates/cargo-test-support" }
cargo-test-support = { version = "0.5.0", path = "crates/cargo-test-support" }
cargo-util = { version = "0.2.14", path = "crates/cargo-util" }
cargo-util-schemas = { version = "0.6.0", path = "crates/cargo-util-schemas" }
cargo_metadata = "0.18.1"
Expand Down
2 changes: 1 addition & 1 deletion crates/cargo-test-support/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cargo-test-support"
version = "0.4.1"
version = "0.5.0"
edition.workspace = true
rust-version = "1.81" # MSRV:1
license.workspace = true
Expand Down
4 changes: 2 additions & 2 deletions crates/cargo-test-support/src/compare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//! # Deprecated comparisons
//!
//! Cargo's tests are in transition from internal-only pattern and normalization routines used in
//! asserts like [`crate::Execs::with_stdout`] to [`assert_e2e`] and [`assert_ui`].
//! asserts like [`crate::Execs::with_stdout_contains`] to [`assert_e2e`] and [`assert_ui`].
//!
//! ## Patterns
//!
Expand Down Expand Up @@ -208,7 +208,7 @@ fn add_regex_redactions(subs: &mut snapbox::Redactions) {
.unwrap();
subs.insert(
"[FILE_NUM]",
regex!(r"\[(REMOVED|SUMMARY)\] (?<redacted>[0-9]+) files"),
regex!(r"\[(REMOVED|SUMMARY)\] (?<redacted>[1-9][0-9]*) files"),
)
.unwrap();
subs.insert(
Expand Down
31 changes: 2 additions & 29 deletions crates/cargo-test-support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -472,9 +472,10 @@ impl Project {
/// # Example:
///
/// ```no_run
/// # use cargo_test_support::str;
/// # let p = cargo_test_support::project().build();
/// p.process(&p.bin("foo"))
/// .with_stdout("bar\n")
/// .with_stdout_data(str!["bar\n"])
/// .run();
/// ```
pub fn process<T: AsRef<OsStr>>(&self, program: T) -> Execs {
Expand Down Expand Up @@ -644,9 +645,7 @@ struct RawOutput {
pub struct Execs {
ran: bool,
process_builder: Option<ProcessBuilder>,
expect_stdout: Option<String>,
expect_stdin: Option<String>,
expect_stderr: Option<String>,
expect_exit_code: Option<i32>,
expect_stdout_data: Option<snapbox::Data>,
expect_stderr_data: Option<snapbox::Data>,
Expand All @@ -673,22 +672,6 @@ impl Execs {

/// # Configure assertions
impl Execs {
/// Verifies that stdout is equal to the given lines.
/// See [`compare`] for supported patterns.
#[deprecated(note = "replaced with `Execs::with_stdout_data(expected)`")]
pub fn with_stdout<S: ToString>(&mut self, expected: S) -> &mut Self {
self.expect_stdout = Some(expected.to_string());
self
}

/// Verifies that stderr is equal to the given lines.
/// See [`compare`] for supported patterns.
#[deprecated(note = "replaced with `Execs::with_stderr_data(expected)`")]
pub fn with_stderr<S: ToString>(&mut self, expected: S) -> &mut Self {
self.expect_stderr = Some(expected.to_string());
self
}

/// Verifies that stdout is equal to the given lines.
///
/// See [`compare::assert_e2e`] for assertion details.
Expand Down Expand Up @@ -1058,9 +1041,7 @@ impl Execs {
#[track_caller]
fn verify_checks_output(&self, stdout: &[u8], stderr: &[u8]) {
if self.expect_exit_code.unwrap_or(0) != 0
&& self.expect_stdout.is_none()
&& self.expect_stdin.is_none()
&& self.expect_stderr.is_none()
&& self.expect_stdout_data.is_none()
&& self.expect_stderr_data.is_none()
&& self.expect_stdout_contains.is_empty()
Expand Down Expand Up @@ -1154,12 +1135,6 @@ impl Execs {
),
}

if let Some(expect_stdout) = &self.expect_stdout {
compare::match_exact(expect_stdout, stdout, "stdout", stderr, cwd)?;
}
if let Some(expect_stderr) = &self.expect_stderr {
compare::match_exact(expect_stderr, stderr, "stderr", stdout, cwd)?;
}
if let Some(expect_stdout_data) = &self.expect_stdout_data {
if let Err(err) = self.assert.try_eq(
Some(&"stdout"),
Expand Down Expand Up @@ -1227,8 +1202,6 @@ pub fn execs() -> Execs {
Execs {
ran: false,
process_builder: None,
expect_stdout: None,
expect_stderr: None,
expect_stdin: None,
expect_exit_code: Some(0),
expect_stdout_data: None,
Expand Down
3 changes: 2 additions & 1 deletion crates/cargo-test-support/src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
//! ```no_run
//! use cargo_test_support::registry::Package;
//! use cargo_test_support::project;
//! use cargo_test_support::str;
//!
//! // Publish package "a" depending on "b".
//! Package::new("a", "1.0.0")
Expand Down Expand Up @@ -38,7 +39,7 @@
//! "#)
//! .build();
//!
//! p.cargo("run").with_stdout("24").run();
//! p.cargo("run").with_stdout_data(str!["24"]).run();
//! ```
use crate::git::repo;
Expand Down
10 changes: 5 additions & 5 deletions tests/testsuite/artifact_dep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,7 @@ fn build_script_with_bin_artifact_and_lib_false() {
)
.build();

#[allow(deprecated)]
#[expect(deprecated)]
p.cargo("build -Z bindeps")
.masquerade_as_nightly_cargo(&["bindeps"])
.with_status(101)
Expand Down Expand Up @@ -731,7 +731,7 @@ fn lib_with_bin_artifact_and_lib_false() {
)
.build();

#[allow(deprecated)]
#[expect(deprecated)]
p.cargo("build -Z bindeps")
.masquerade_as_nightly_cargo(&["bindeps"])
.with_status(101)
Expand Down Expand Up @@ -1117,7 +1117,7 @@ fn build_script_deps_adopt_specified_target_unconditionally() {
.file("bar/src/lib.rs", "pub fn doit() {}")
.build();

#[allow(deprecated)]
#[expect(deprecated)]
p.cargo("check -v -Z bindeps")
.masquerade_as_nightly_cargo(&["bindeps"])
.with_stderr_does_not_contain(format!(
Expand Down Expand Up @@ -1237,7 +1237,7 @@ fn non_build_script_deps_adopt_specified_target_unconditionally() {
.file("bar/src/lib.rs", "pub fn doit() {}")
.build();

#[allow(deprecated)]
#[expect(deprecated)]
p.cargo("check -v -Z bindeps")
.masquerade_as_nightly_cargo(&["bindeps"])
.with_stderr_contains(format!(
Expand Down Expand Up @@ -1385,7 +1385,7 @@ fn build_script_deps_adopts_target_platform_if_target_equals_target() {
.build();

let alternate_target = cross_compile::alternate();
#[allow(deprecated)]
#[expect(deprecated)]
p.cargo("check -v -Z bindeps --target")
.arg(alternate_target)
.masquerade_as_nightly_cargo(&["bindeps"])
Expand Down
17 changes: 8 additions & 9 deletions tests/testsuite/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ fn cargo_compile_incremental() {
.run();
}

#[allow(deprecated)]
#[expect(deprecated)]
#[cargo_test]
fn incremental_profile() {
let p = project()
Expand Down Expand Up @@ -176,7 +176,7 @@ fn incremental_profile() {
.run();
}

#[allow(deprecated)]
#[expect(deprecated)]
#[cargo_test]
fn incremental_config() {
let p = project()
Expand Down Expand Up @@ -1524,7 +1524,6 @@ fn ignores_carriage_return_in_lockfile() {
p.cargo("build").run();
}

#[allow(deprecated)]
#[cargo_test]
fn cargo_default_env_metadata_env_var() {
// Ensure that path dep + dylib + env_var get metadata
Expand Down Expand Up @@ -4124,7 +4123,7 @@ fn panic_abort_compiles_with_panic_abort() {
.run();
}

#[allow(deprecated)]
#[expect(deprecated)]
#[cargo_test]
fn compiler_json_error_format() {
let p = project()
Expand Down Expand Up @@ -4314,7 +4313,7 @@ fn wrong_message_format_option() {
.run();
}

#[allow(deprecated)]
#[expect(deprecated)]
#[cargo_test]
fn message_format_json_forward_stderr() {
let p = project()
Expand Down Expand Up @@ -5177,7 +5176,7 @@ WRAPPER CALLED: rustc --crate-name foo [..]
.run();
}

#[allow(deprecated)]
#[expect(deprecated)]
#[cargo_test]
fn rustc_wrapper_queries() {
// Check that the invocations querying rustc for information are done with the wrapper.
Expand Down Expand Up @@ -5917,7 +5916,7 @@ fn build_filter_infer_profile() {
.run();
}

#[allow(deprecated)]
#[expect(deprecated)]
#[cargo_test]
fn targets_selected_default() {
let p = project().file("src/main.rs", "fn main() {}").build();
Expand Down Expand Up @@ -6871,7 +6870,7 @@ Caused by:
.run();
}

#[allow(deprecated)]
#[expect(deprecated)]
#[cargo_test]
fn build_script_o0_default() {
let p = project()
Expand All @@ -6884,7 +6883,7 @@ fn build_script_o0_default() {
.run();
}

#[allow(deprecated)]
#[expect(deprecated)]
#[cargo_test]
fn build_script_o0_default_even_with_release() {
let p = project()
Expand Down
2 changes: 1 addition & 1 deletion tests/testsuite/build_script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3779,7 +3779,7 @@ fn custom_target_dir() {
p.cargo("build -v").run();
}

#[allow(deprecated)]
#[expect(deprecated)]
#[cargo_test]
fn panic_abort_with_build_scripts() {
let p = project()
Expand Down
2 changes: 1 addition & 1 deletion tests/testsuite/build_script_extra_link_arg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ the future. For more information, see <https://github.com/rust-lang/cargo/issues
.run();
}

#[allow(deprecated)]
#[expect(deprecated)]
#[cargo_test]
fn link_arg_transitive_not_allowed() {
// Verify that transitive dependencies don't pass link args.
Expand Down
2 changes: 1 addition & 1 deletion tests/testsuite/cache_messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ WRAPPER CALLED: rustc [..]
.run();
}

#[allow(deprecated)]
#[expect(deprecated)]
#[cargo_test]
fn wacky_hashless_fingerprint() {
// On Windows, executables don't have hashes. This checks for a bad
Expand Down
7 changes: 3 additions & 4 deletions tests/testsuite/cargo_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ fn list_command_looks_at_path_case_mismatch() {
);
}

#[allow(deprecated)]
#[expect(deprecated)]
#[cargo_test]
fn list_command_handles_known_external_commands() {
let p = project()
Expand Down Expand Up @@ -358,7 +358,6 @@ fn override_cargo_home() {
assert!(paths::root().join("foo2/.git").is_dir());
}

#[allow(deprecated)]
#[cargo_test]
fn cargo_subcommand_env() {
let src = format!(
Expand Down Expand Up @@ -390,7 +389,7 @@ fn cargo_subcommand_env() {

cargo_process("envtest")
.env("PATH", &path)
.with_stdout(cargo.to_str().unwrap())
.with_stdout_data(format!("{}\n", cargo.to_str().unwrap()).raw())
.run();

// Check that subcommands inherit an overridden $CARGO
Expand All @@ -403,7 +402,7 @@ fn cargo_subcommand_env() {
cargo_process("envtest")
.env("PATH", &path)
.env(cargo::CARGO_ENV, &envtest_bin)
.with_stdout(envtest_bin)
.with_stdout_data(format!("{}\n", envtest_bin).raw().raw())
.run();
}

Expand Down
16 changes: 8 additions & 8 deletions tests/testsuite/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ fn check_all() {
.run();
}

#[allow(deprecated)]
#[expect(deprecated)]
#[cargo_test]
fn check_all_exclude() {
let p = project()
Expand All @@ -433,7 +433,7 @@ fn check_all_exclude() {
.run();
}

#[allow(deprecated)]
#[expect(deprecated)]
#[cargo_test]
fn check_all_exclude_glob() {
let p = project()
Expand Down Expand Up @@ -491,7 +491,7 @@ fn check_virtual_all_implied() {
.run();
}

#[allow(deprecated)]
#[expect(deprecated)]
#[cargo_test]
fn check_virtual_manifest_one_project() {
let p = project()
Expand All @@ -518,7 +518,7 @@ fn check_virtual_manifest_one_project() {
.run();
}

#[allow(deprecated)]
#[expect(deprecated)]
#[cargo_test]
fn check_virtual_manifest_glob() {
let p = project()
Expand Down Expand Up @@ -559,7 +559,7 @@ fn exclude_warns_on_non_existing_package() {
.run();
}

#[allow(deprecated)]
#[expect(deprecated)]
#[cargo_test]
fn targets_selected_default() {
let foo = project()
Expand Down Expand Up @@ -633,7 +633,7 @@ error[E0425]: cannot find value `badtext` in this scope
.run();
}

#[allow(deprecated)]
#[expect(deprecated)]
// Verify what is checked with various command-line filters.
#[cargo_test]
fn check_filters() {
Expand Down Expand Up @@ -1000,7 +1000,7 @@ WRAPPER CALLED: rustc --crate-name foo [..]
.run();
}

#[allow(deprecated)]
#[expect(deprecated)]
#[cargo_test]
fn rustc_workspace_wrapper_respects_primary_units() {
let p = project()
Expand All @@ -1024,7 +1024,7 @@ fn rustc_workspace_wrapper_respects_primary_units() {
.run();
}

#[allow(deprecated)]
#[expect(deprecated)]
#[cargo_test]
fn rustc_workspace_wrapper_excludes_published_deps() {
let p = project()
Expand Down
Loading

0 comments on commit 844457c

Please sign in to comment.