From e41c718229426577929078d343e8f7c1026b2bc7 Mon Sep 17 00:00:00 2001 From: heisen-li Date: Mon, 17 Jun 2024 19:31:44 +0800 Subject: [PATCH 1/3] test: migrate build_script_extra_link_arg to snapbox --- .../testsuite/build_script_extra_link_arg.rs | 123 +++++++++++------- 1 file changed, 73 insertions(+), 50 deletions(-) diff --git a/tests/testsuite/build_script_extra_link_arg.rs b/tests/testsuite/build_script_extra_link_arg.rs index 25132ef4db8..ca7b4863cd3 100644 --- a/tests/testsuite/build_script_extra_link_arg.rs +++ b/tests/testsuite/build_script_extra_link_arg.rs @@ -4,9 +4,8 @@ // because MSVC link.exe just gives a warning on unknown flags (how helpful!), // and other linkers will return an error. -#![allow(deprecated)] - use cargo_test_support::registry::Package; +use cargo_test_support::str; use cargo_test_support::{basic_bin_manifest, basic_lib_manifest, basic_manifest, project}; #[cargo_test] @@ -26,8 +25,11 @@ fn build_script_extra_link_arg_bin() { p.cargo("build -v") .without_status() - .with_stderr_contains( - "[RUNNING] `rustc --crate-name foo [..]-C link-arg=--this-is-a-bogus-flag[..]", + .with_stderr_data( + "\ +... +[RUNNING] `rustc --crate-name foo [..]-C link-arg=--this-is-a-bogus-flag[..] +...", ) .run(); } @@ -66,11 +68,17 @@ fn build_script_extra_link_arg_bin_single() { p.cargo("build -v") .without_status() - .with_stderr_contains( - "[RUNNING] `rustc --crate-name foo [..]-C link-arg=--bogus-flag-all -C link-arg=--bogus-flag-foo[..]", + .with_stderr_data( + "\ +... +[RUNNING] `rustc --crate-name foo [..]-C link-arg=--bogus-flag-all -C link-arg=--bogus-flag-foo[..] +...", ) - .with_stderr_contains( - "[RUNNING] `rustc --crate-name bar [..]-C link-arg=--bogus-flag-all -C link-arg=--bogus-flag-bar[..]", + .with_stderr_data( + "\ +... +[RUNNING] `rustc --crate-name bar [..]-C link-arg=--bogus-flag-all -C link-arg=--bogus-flag-bar[..] +...", ) .run(); } @@ -92,8 +100,11 @@ fn build_script_extra_link_arg() { p.cargo("build -v") .without_status() - .with_stderr_contains( - "[RUNNING] `rustc --crate-name foo [..]-C link-arg=--this-is-a-bogus-flag[..]", + .with_stderr_data( + "\ +... +[RUNNING] `rustc --crate-name foo [..]-C link-arg=--this-is-a-bogus-flag[..] +...", ) .run(); } @@ -127,11 +138,12 @@ fn link_arg_missing_target() { p.cargo("check") .with_status(101) - .with_stderr("\ -[COMPILING] foo [..] -error: invalid instruction `cargo::rustc-link-arg-bins` from build script of `foo v0.0.1 ([ROOT]/foo)` + .with_stderr_data(str![[r#" +[COMPILING] foo v0.0.1 ([ROOT]/foo) +[ERROR] invalid instruction `cargo::rustc-link-arg-bins` from build script of `foo v0.0.1 ([ROOT]/foo)` The package foo v0.0.1 ([ROOT]/foo) does not have a bin target. -") + +"#]]) .run(); p.change_file( @@ -141,13 +153,12 @@ The package foo v0.0.1 ([ROOT]/foo) does not have a bin target. p.cargo("check") .with_status(101) - .with_stderr( - "\ -[COMPILING] foo [..] -error: invalid instruction `cargo::rustc-link-arg-bin` from build script of `foo v0.0.1 ([ROOT]/foo)` + .with_stderr_data(str![[r#" +[COMPILING] foo v0.0.1 ([ROOT]/foo) +[ERROR] invalid instruction `cargo::rustc-link-arg-bin` from build script of `foo v0.0.1 ([ROOT]/foo)` The package foo v0.0.1 ([ROOT]/foo) does not have a bin target with the name `abc`. -", - ) + +"#]]) .run(); p.change_file( @@ -157,13 +168,12 @@ The package foo v0.0.1 ([ROOT]/foo) does not have a bin target with the name `ab p.cargo("check") .with_status(101) - .with_stderr( - "\ -[COMPILING] foo [..] -error: invalid instruction `cargo::rustc-link-arg-bin=abc` from build script of `foo v0.0.1 ([ROOT]/foo)` + .with_stderr_data(str![[r#" +[COMPILING] foo v0.0.1 ([ROOT]/foo) +[ERROR] invalid instruction `cargo::rustc-link-arg-bin=abc` from build script of `foo v0.0.1 ([ROOT]/foo)` The instruction should have the form cargo::rustc-link-arg-bin=BIN=ARG -", - ) + +"#]]) .run(); } @@ -203,24 +213,26 @@ fn cdylib_link_arg_transitive() { .build(); p.cargo("build -v") .without_status() - .with_stderr_contains( + .with_stderr_data( "\ -[COMPILING] bar v1.0.0 [..] +... +[COMPILING] bar v1.0.0 ([ROOT]/foo/bar) [RUNNING] `rustc --crate-name build_script_build --edition=2015 bar/build.rs [..] -[RUNNING] `[..]build-script-build[..] -warning: bar@1.0.0: cargo::rustc-link-arg-cdylib was specified in the build script of bar v1.0.0 \ +[RUNNING] `[ROOT]/foo/target/debug/build/bar-[HASH]/build-script-build` +[WARNING] bar@1.0.0: cargo::rustc-link-arg-cdylib was specified in the build script of bar v1.0.0 \ ([ROOT]/foo/bar), but that package does not contain a cdylib target Allowing this was an unintended change in the 1.50 release, and may become an error in \ the future. For more information, see . [RUNNING] `rustc --crate-name bar --edition=2015 bar/src/lib.rs [..] -[COMPILING] foo v0.1.0 [..] +[COMPILING] foo v0.1.0 ([ROOT]/foo) [RUNNING] `rustc --crate-name foo --edition=2015 src/lib.rs [..]-C link-arg=--bogus[..]` -", +...", ) .run(); } +#[allow(deprecated)] #[cargo_test] fn link_arg_transitive_not_allowed() { // Verify that transitive dependencies don't pass link args. @@ -260,21 +272,20 @@ fn link_arg_transitive_not_allowed() { .build(); p.cargo("build -v") - .with_stderr( - "\ -[UPDATING] [..] + .with_stderr_data(str![[r#" +[UPDATING] `dummy-registry` index [LOCKING] 2 packages to latest compatible versions -[DOWNLOADING] [..] -[DOWNLOADED] [..] +[DOWNLOADING] crates ... +[DOWNLOADED] bar v1.0.0 (registry `dummy-registry`) [COMPILING] bar v1.0.0 [RUNNING] `rustc --crate-name build_script_build [..] -[RUNNING] `[..]/build-script-build[..] +[RUNNING] `[ROOT]/foo/target/debug/build/bar-[HASH]/build-script-build` [RUNNING] `rustc --crate-name bar [..] -[COMPILING] foo v0.1.0 [..] +[COMPILING] foo v0.1.0 ([ROOT]/foo) [RUNNING] `rustc --crate-name foo --edition=2015 src/lib.rs [..] -[FINISHED] `dev` profile [..] -", - ) +[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s + +"#]]) .with_stderr_does_not_contain("--bogus") .run(); } @@ -303,8 +314,11 @@ fn link_arg_with_doctest() { p.cargo("test --doc -v") .without_status() - .with_stderr_contains( - "[RUNNING] `rustdoc [..]--crate-name foo [..]-C link-arg=--this-is-a-bogus-flag[..]", + .with_stderr_data( + "\ +... +[RUNNING] `rustdoc [..]--crate-name foo [..]-C link-arg=--this-is-a-bogus-flag[..] +...", ) .run(); } @@ -327,8 +341,11 @@ fn build_script_extra_link_arg_tests() { p.cargo("test -v") .without_status() - .with_stderr_contains( - "[RUNNING] `rustc --crate-name test_foo [..]-C link-arg=--this-is-a-bogus-flag[..]", + .with_stderr_data( + "\ +... +[RUNNING] `rustc --crate-name test_foo [..]-C link-arg=--this-is-a-bogus-flag[..] +...", ) .run(); } @@ -351,8 +368,11 @@ fn build_script_extra_link_arg_benches() { p.cargo("bench -v") .without_status() - .with_stderr_contains( - "[RUNNING] `rustc --crate-name bench_foo [..]-C link-arg=--this-is-a-bogus-flag[..]", + .with_stderr_data( + "\ +... +[RUNNING] `rustc --crate-name bench_foo [..]-C link-arg=--this-is-a-bogus-flag[..] +...", ) .run(); } @@ -375,8 +395,11 @@ fn build_script_extra_link_arg_examples() { p.cargo("build -v --examples") .without_status() - .with_stderr_contains( - "[RUNNING] `rustc --crate-name example_foo [..]-C link-arg=--this-is-a-bogus-flag[..]", + .with_stderr_data( + "\ +... +[RUNNING] `rustc --crate-name example_foo [..]-C link-arg=--this-is-a-bogus-flag[..] +...", ) .run(); } From 413844bc131db0d953ebc90499ad1be3373f8668 Mon Sep 17 00:00:00 2001 From: heisen-li Date: Tue, 18 Jun 2024 18:59:06 +0800 Subject: [PATCH 2/3] test: migrate cache_message to snapbox --- tests/testsuite/cache_messages.rs | 174 +++++++++++++++++++----------- 1 file changed, 114 insertions(+), 60 deletions(-) diff --git a/tests/testsuite/cache_messages.rs b/tests/testsuite/cache_messages.rs index 51ca2a30def..6a14f9c74f6 100644 --- a/tests/testsuite/cache_messages.rs +++ b/tests/testsuite/cache_messages.rs @@ -1,8 +1,7 @@ //! Tests for caching compiler diagnostics. -#![allow(deprecated)] - use super::messages::raw_rustc_output; +use cargo_test_support::str; use cargo_test_support::tools; use cargo_test_support::{basic_manifest, is_coarse_mtime, project, registry::Package, sleep_ms}; @@ -160,7 +159,16 @@ fn clears_cache_after_fix() { // Make sure the cache is invalidated when there is no output. let p = project().file("src/lib.rs", "fn asdf() {}").build(); // Fill the cache. - p.cargo("check").with_stderr_contains("[..]asdf[..]").run(); + p.cargo("check") + .with_stderr_data(str![[r#" +[CHECKING] foo v0.0.1 ([ROOT]/foo) +[WARNING] function `asdf` is never used +... +[WARNING] `foo` (lib) generated 1 warning +[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s + +"#]]) + .run(); let cpath = p .glob("target/debug/.fingerprint/foo-*/output-*") .next() @@ -175,13 +183,12 @@ fn clears_cache_after_fix() { p.change_file("src/lib.rs", ""); p.cargo("check") - .with_stdout("") - .with_stderr( - "\ -[CHECKING] foo [..] -[FINISHED] [..] -", - ) + .with_stdout_data("") + .with_stderr_data(str![[r#" +[CHECKING] foo v0.0.1 ([ROOT]/foo) +[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s + +"#]]) .run(); assert_eq!( p.glob("target/debug/.fingerprint/foo-*/output-*").count(), @@ -190,12 +197,11 @@ fn clears_cache_after_fix() { // And again, check the cache is correct. p.cargo("check") - .with_stdout("") - .with_stderr( - "\ -[FINISHED] [..] -", - ) + .with_stdout_data("") + .with_stderr_data(str![[r#" +[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s + +"#]]) .run(); } @@ -267,13 +273,39 @@ fn very_verbose() { .build(); p.cargo("check -vv") - .with_stderr_contains("[..]not_used[..]") + .with_stderr_data(str![[r#" +[UPDATING] `dummy-registry` index +[LOCKING] 2 packages to latest compatible versions +[DOWNLOADING] crates ... +[DOWNLOADED] bar v1.0.0 (registry `dummy-registry`) +[CHECKING] bar v1.0.0 +[RUNNING] [..] +[WARNING] function `not_used` is never used +... +[CHECKING] foo v0.1.0 ([ROOT]/foo) +[RUNNING] [..] +[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s + +"#]]) .run(); - p.cargo("check").with_stderr("[FINISHED] [..]").run(); + p.cargo("check") + .with_stderr_data(str![[r#" +[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s + +"#]]) + .run(); p.cargo("check -vv") - .with_stderr_contains("[..]not_used[..]") + .with_stderr_data(str![[r#" +[FRESH] bar v1.0.0 +[WARNING] function `not_used` is never used +... +[WARNING] `bar` (lib) generated 1 warning +[FRESH] foo v0.1.0 ([ROOT]/foo) +[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s + +"#]]) .run(); } @@ -346,25 +378,23 @@ fn replay_non_json() { let p = project().file("src/lib.rs", "").build(); p.cargo("check") .env("RUSTC", rustc.bin("rustc_alt")) - .with_stderr( - "\ -[CHECKING] foo [..] + .with_stderr_data(str![[r#" +[CHECKING] foo v0.0.1 ([ROOT]/foo) line 1 line 2 -[FINISHED] `dev` profile [..] -", - ) +[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s + +"#]]) .run(); p.cargo("check") .env("RUSTC", rustc.bin("rustc_alt")) - .with_stderr( - "\ + .with_stderr_data(str![[r#" line 1 line 2 -[FINISHED] `dev` profile [..] -", - ) +[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s + +"#]]) .run(); } @@ -407,11 +437,11 @@ fn caching_large_output() { let p = project().file("src/lib.rs", "").build(); p.cargo("check") .env("RUSTC", rustc.bin("rustc_alt")) - .with_stderr(&format!( + .with_stderr_data(&format!( "\ -[CHECKING] foo [..] -{}warning: `foo` (lib) generated 250 warnings -[FINISHED] `dev` profile [..] +[CHECKING] foo v0.0.1 ([ROOT]/foo) +{}[WARNING] `foo` (lib) generated 250 warnings +[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s ", expected )) @@ -419,10 +449,10 @@ fn caching_large_output() { p.cargo("check") .env("RUSTC", rustc.bin("rustc_alt")) - .with_stderr(&format!( + .with_stderr_data(&format!( "\ -{}warning: `foo` (lib) generated 250 warnings -[FINISHED] `dev` profile [..] +{}[WARNING] `foo` (lib) generated 250 warnings +[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s ", expected )) @@ -441,44 +471,61 @@ fn rustc_workspace_wrapper() { p.cargo("check -v") .env("RUSTC_WORKSPACE_WRAPPER", tools::echo_wrapper()) - .with_stderr_contains( - "WRAPPER CALLED: rustc --crate-name foo --edition=2015 src/lib.rs [..]", - ) + .with_stderr_data(str![[r#" +[CHECKING] foo v0.0.1 ([ROOT]/foo) +[RUNNING] [..]/rustc-echo-wrapper[EXE] rustc --crate-name foo [..] +WRAPPER CALLED: rustc --crate-name foo --edition=2015 src/lib.rs [..] +[WARNING] function `unused_func` is never used +... +[WARNING] `foo` (lib) generated 1 warning +[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s + +"#]]) .run(); // Check without a wrapper should rebuild p.cargo("check -v") - .with_stderr_contains( - "\ -[CHECKING] foo [..] -[RUNNING] `rustc[..] -[WARNING] [..]unused_func[..] -", - ) - .with_stdout_does_not_contain( - "WRAPPER CALLED: rustc --crate-name foo --edition=2015 src/lib.rs [..]", - ) + .with_stderr_data(str![[r#" +[CHECKING] foo v0.0.1 ([ROOT]/foo) +[RUNNING] `rustc[..]` +[WARNING] function `unused_func` is never used +... +[WARNING] `foo` (lib) generated 1 warning +[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s + +"#]]) + .with_stdout_data("") .run(); // Again, reading from the cache. p.cargo("check -v") .env("RUSTC_WORKSPACE_WRAPPER", tools::echo_wrapper()) - .with_stderr_contains("[FRESH] foo [..]") - .with_stdout_does_not_contain( - "WRAPPER CALLED: rustc --crate-name foo --edition=2015 src/lib.rs [..]", - ) + .with_stderr_data(str![[r#" +[FRESH] foo v0.0.1 ([ROOT]/foo) +WRAPPER CALLED: rustc [..] +... +[WARNING] `foo` (lib) generated 1 warning +[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s + +"#]]) + .with_stdout_data("") .run(); // And `check` should also be fresh, reading from cache. p.cargo("check -v") - .with_stderr_contains("[FRESH] foo [..]") - .with_stderr_contains("[WARNING] [..]unused_func[..]") - .with_stdout_does_not_contain( - "WRAPPER CALLED: rustc --crate-name foo --edition=2015 src/lib.rs [..]", - ) + .with_stderr_data(str![[r#" +[FRESH] foo v0.0.1 ([ROOT]/foo) +[WARNING] function `unused_func` is never used +... +[WARNING] `foo` (lib) generated 1 warning +[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s + +"#]]) + .with_stdout_data("") .run(); } +#[allow(deprecated)] #[cargo_test] fn wacky_hashless_fingerprint() { // On Windows, executables don't have hashes. This checks for a bad @@ -491,7 +538,14 @@ fn wacky_hashless_fingerprint() { .with_stderr_does_not_contain("[..]unused[..]") .run(); p.cargo("check --bin a") - .with_stderr_contains("[..]unused[..]") + .with_stderr_data(str![[r#" +[CHECKING] foo v0.0.1 ([ROOT]/foo) +[WARNING] unused variable: `unused` +... +[WARNING] `foo` (bin "a") generated 1 warning +[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s + +"#]]) .run(); // This should not pick up the cache from `a`. p.cargo("check --bin b") From 2e1f7b47c707f117aefb08f32ace5b8d1e4f52f6 Mon Sep 17 00:00:00 2001 From: heisen-li Date: Tue, 18 Jun 2024 18:59:47 +0800 Subject: [PATCH 3/3] test: migrate cache_lock to snapbox --- tests/testsuite/cache_lock.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/testsuite/cache_lock.rs b/tests/testsuite/cache_lock.rs index c6ed0874cb5..8304eaebe72 100644 --- a/tests/testsuite/cache_lock.rs +++ b/tests/testsuite/cache_lock.rs @@ -1,7 +1,5 @@ //! Tests for `CacheLock`. -#![allow(deprecated)] - use crate::config::GlobalContextBuilder; use cargo::util::cache_lock::{CacheLockMode, CacheLocker}; use cargo_test_support::paths::{self, CargoPathExt};