test: migrate messages to snapbox

This commit is contained in:
eth3lbert 2024-07-12 13:06:56 +08:00
parent 9a0c8b3ad1
commit f9026f1884
No known key found for this signature in database

View File

@ -2,8 +2,7 @@
//!
//! Tests for message caching can be found in `cache_messages`.
#![allow(deprecated)]
use cargo_test_support::compare::assert_e2e;
use cargo_test_support::prelude::*;
use cargo_test_support::{process, project, Project};
use cargo_util::ProcessError;
@ -48,6 +47,14 @@ pub fn raw_rustc_output(project: &Project, path: &str, extra: &[&str]) -> String
result
}
fn redact_rustc_message(msg: &str) -> impl IntoData {
use snapbox::filter::{Filter, FilterPaths};
let assert = assert_e2e();
let redactions = assert.redactions();
let msg = redactions.redact(msg);
FilterPaths.filter(msg.into())
}
#[cargo_test]
fn deduplicate_messages_basic() {
let p = project()
@ -63,19 +70,24 @@ fn deduplicate_messages_basic() {
let rustc_message = raw_rustc_output(&p, "src/lib.rs", &[]);
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(redact_rustc_message(&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(redact_rustc_message(&expected_output))
.run();
}
@ -106,20 +118,25 @@ fn deduplicate_messages_mismatched_warnings() {
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(redact_rustc_message(&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(redact_rustc_message(&expected_output))
.run();
}
@ -136,12 +153,12 @@ fn deduplicate_errors() {
let rustc_message = raw_rustc_output(&p, "src/lib.rs", &[]);
p.cargo("test -j1")
.with_status(101)
.with_stderr(&format!(
.with_stderr_data(redact_rustc_message(&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
))
)))
.run();
}