Skip to content

Commit

Permalink
test: migrate messages to snapbox
Browse files Browse the repository at this point in the history
  • Loading branch information
eth3lbert committed Jul 12, 2024
1 parent 0e2ef80 commit e358c30
Showing 1 changed file with 44 additions and 18 deletions.
62 changes: 44 additions & 18 deletions tests/testsuite/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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()
Expand All @@ -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();
}

Expand All @@ -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();
}

Expand All @@ -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
))
Expand Down

0 comments on commit e358c30

Please sign in to comment.