From e358c3035cabbe761f7edef7b7aebfdeb14297a8 Mon Sep 17 00:00:00 2001 From: eth3lbert Date: Fri, 12 Jul 2024 13:06:56 +0800 Subject: [PATCH] test: migrate messages to snapbox --- tests/testsuite/messages.rs | 62 ++++++++++++++++++++++++++----------- 1 file changed, 44 insertions(+), 18 deletions(-) diff --git a/tests/testsuite/messages.rs b/tests/testsuite/messages.rs index 0b083b5be9e1..db80529f39b1 100644 --- a/tests/testsuite/messages.rs +++ b/tests/testsuite/messages.rs @@ -2,8 +2,6 @@ //! //! Tests for message caching can be found in `cache_messages`. -#![allow(deprecated)] - use cargo_test_support::{process, project, Project}; use cargo_util::ProcessError; @@ -47,6 +45,20 @@ pub fn raw_rustc_output(project: &Project, path: &str, extra: &[&str]) -> String result } +fn redact_rustc_message(msg: &str) -> String { + let mut msg = msg + .replace("help:", "[HELP]") + .replace("note:", "[NOTE]") + .replace("warning:", "[WARNING]") + .replace("error:", "[ERROR]"); + + if cfg!(windows) { + msg = msg.replace("/", "\\"); + } + + msg +} + #[cargo_test] fn deduplicate_messages_basic() { let p = project() @@ -60,21 +72,27 @@ fn deduplicate_messages_basic() { ) .build(); let rustc_message = raw_rustc_output(&p, "src/lib.rs", &[]); + let rustc_message = redact_rustc_message(&rustc_message); let expected_output = format!( "{}\ -warning: `foo` (lib) generated 1 warning[..] -warning: `foo` (lib test) generated 1 warning (1 duplicate) -[FINISHED] [..] -[EXECUTABLE] unittests src/lib.rs (target/debug/deps/foo-[..][EXE]) +[WARNING] `foo` (lib) generated 1 warning[..] +[WARNING] `foo` (lib test) generated 1 warning (1 duplicate) +[FINISHED] `test` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s +[EXECUTABLE] unittests src/lib.rs (target/debug/deps/foo-[HASH][EXE]) ", rustc_message ); p.cargo("test --no-run -j1") - .with_stderr(&format!("[COMPILING] foo [..]\n{}", expected_output)) + .with_stderr_data(&format!( + "\ +[COMPILING] foo v0.0.1 ([ROOT]/foo) +{}", + expected_output + )) .run(); // Run again, to check for caching behavior. p.cargo("test --no-run -j1") - .with_stderr(expected_output) + .with_stderr_data(expected_output) .run(); } @@ -98,27 +116,34 @@ fn deduplicate_messages_mismatched_warnings() { ) .build(); let lib_output = raw_rustc_output(&p, "src/lib.rs", &[]); - let mut lib_test_output = raw_rustc_output(&p, "src/lib.rs", &["--test"]); + let lib_output = redact_rustc_message(&lib_output); + let lib_test_output = raw_rustc_output(&p, "src/lib.rs", &["--test"]); + let mut lib_test_output = redact_rustc_message(&lib_test_output); // Remove the duplicate warning. let start = lib_test_output.find(&lib_output).expect("same warning"); lib_test_output.replace_range(start..start + lib_output.len(), ""); let expected_output = format!( "\ {}\ -warning: `foo` (lib) generated 1 warning[..] +[WARNING] `foo` (lib) generated 1 warning[..] {}\ -warning: `foo` (lib test) generated 2 warnings (1 duplicate) -[FINISHED] [..] -[EXECUTABLE] unittests src/lib.rs (target/debug/deps/foo-[..][EXE]) +[WARNING] `foo` (lib test) generated 2 warnings (1 duplicate) +[FINISHED] `test` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s +[EXECUTABLE] unittests src/lib.rs (target/debug/deps/foo-[HASH][EXE]) ", lib_output, lib_test_output ); p.cargo("test --no-run -j1") - .with_stderr(&format!("[COMPILING] foo v0.0.1 [..]\n{}", expected_output)) + .with_stderr_data(&format!( + "\ +[COMPILING] foo v0.0.1 ([ROOT]/foo) +{}", + expected_output + )) .run(); // Run again, to check for caching behavior. p.cargo("test --no-run -j1") - .with_stderr(expected_output) + .with_stderr_data(expected_output) .run(); } @@ -133,12 +158,13 @@ fn deduplicate_errors() { ) .build(); let rustc_message = raw_rustc_output(&p, "src/lib.rs", &[]); + let rustc_message = redact_rustc_message(&rustc_message); p.cargo("test -j1") .with_status(101) - .with_stderr(&format!( + .with_stderr_data(&format!( "\ -[COMPILING] foo v0.0.1 [..] -{}error: could not compile `foo` (lib) due to 1 previous error +[COMPILING] foo v0.0.1 ([ROOT]/foo) +{}[ERROR] could not compile `foo` (lib) due to 1 previous error ", rustc_message ))