Skip to content

Commit

Permalink
test: migrate testsuite message_format to snapbox
Browse files Browse the repository at this point in the history
  • Loading branch information
henry40408 committed Jun 18, 2024
1 parent c7c28d9 commit ba19274
Showing 1 changed file with 54 additions and 26 deletions.
80 changes: 54 additions & 26 deletions tests/testsuite/message_format.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
//! Tests for --message-format flag.

#![allow(deprecated)]

use cargo_test_support::{basic_lib_manifest, basic_manifest, project};
use cargo_test_support::{basic_lib_manifest, basic_manifest, project, str};

#[cargo_test]
fn cannot_specify_two() {
Expand All @@ -13,12 +11,14 @@ fn cannot_specify_two() {

let formats = ["human", "json", "short"];

let two_kinds = "error: cannot specify two kinds of `message-format` arguments\n";
for a in formats.iter() {
for b in formats.iter() {
p.cargo(&format!("build --message-format {},{}", a, b))
.with_status(101)
.with_stderr(two_kinds)
.with_stderr_data(str![[r#"
[ERROR] cannot specify two kinds of `message-format` arguments
"#]])
.run();
}
}
Expand Down Expand Up @@ -66,17 +66,23 @@ fn cargo_renders() {

p.cargo("check --message-format json-render-diagnostics")
.with_status(101)
.with_stdout(
"{\"reason\":\"compiler-artifact\",[..]\n\
{\"reason\":\"build-finished\",\"success\":false}",
)
.with_stderr_contains(
"\
[CHECKING] bar [..]
[CHECKING] foo [..]
error[..]`main`[..]
",
)
.with_stdout_data(str![[r#"
{"reason":"compiler-artifact","package_id":"path+[ROOTURL]/foo/bar#0.1.0","manifest_path":"[..]","target":{"kind":["lib"],"crate_types":["lib"],"name":"bar","src_path":"[..]","edition":"2015","doc":true,"doctest":true,"test":true},"profile":{"opt_level":"0","debuginfo":2,"debug_assertions":true,"overflow_checks":true,"test":false},"features":[],"filenames":["[..]"],"executable":null,"fresh":false}
{"reason":"build-finished","success":false}
"#]])
.with_stderr_data(str![[r#"
[LOCKING] 2 packages to latest compatible versions
[CHECKING] bar v0.1.0 ([ROOT]/foo/bar)
[CHECKING] foo v0.1.0 ([ROOT]/foo)
error[E0601]: `main` function not found in crate `foo`
|
= [NOTE] consider adding a `main` function to `src/main.rs`
For more information about this error, try `rustc --explain E0601`.
[ERROR] could not compile `foo` (bin "foo") due to 1 previous error
"#]])
.run();
}

Expand All @@ -89,13 +95,12 @@ fn cargo_renders_short() {

p.cargo("check --message-format json-render-diagnostics,json-diagnostic-short")
.with_status(101)
.with_stderr_contains(
"\
[CHECKING] foo [..]
error[..]`main`[..]
",
)
.with_stderr_does_not_contain("note:")
.with_stderr_data(str![[r#"
[CHECKING] foo v0.1.0 ([ROOT]/foo)
error[E0601]: `main` function not found in crate `foo`
[ERROR] could not compile `foo` (bin "foo") due to 1 previous error
"#]])
.run();
}

Expand All @@ -108,7 +113,13 @@ fn cargo_renders_ansi() {

p.cargo("check --message-format json-diagnostic-rendered-ansi")
.with_status(101)
.with_stdout_contains("[..]\\u001b[38;5;9merror[..]")
.with_stdout_data(str![[r#"
{"reason":"compiler-message","package_id":"path+[ROOTURL]/foo#0.1.0","manifest_path":"[..]","target":{"kind":["bin"],"crate_types":["bin"],"name":"foo","src_path":"[..]","edition":"2015","doc":true,"doctest":false,"test":true},"message":{"$message_type":"diagnostic","message":"`main` function not found in crate `foo`","code":{"code":"E0601","explanation":"No `main` function was found in a binary crate./n/nTo fix this error, add a `main` function:/n/n```/nfn main() {/n // Your program will start here./n println!(/"Hello world!/");/n}/n```/n/nIf you don't know the basics of Rust, you can look at the/n[Rust Book][rust-book] to get started./n/n[rust-book]: https://doc.rust-lang.org/book//n"},"level":"error","spans":[{"file_name":"src/main.rs","byte_start":0,"byte_end":0,"line_start":0,"line_end":0,"column_start":1,"column_end":1,"is_primary":true,"text":[],"label":null,"suggested_replacement":null,"suggestion_applicability":null,"expansion":null}],"children":[{"message":"consider adding a `main` function to `src/main.rs`","code":null,"level":"note","spans":[],"children":[],"rendered":null}],"rendered":"[..]error[E0601][..]: `main` function not found in crate `foo`[..]|[..]= [..]: consider adding a `main` function to `src/main.rs`[..]/n/n"}}
{"reason":"compiler-message","package_id":"path+[ROOTURL]/foo#0.1.0","manifest_path":"[..]","target":{"kind":["bin"],"crate_types":["bin"],"name":"foo","src_path":"[..]","edition":"2015","doc":true,"doctest":false,"test":true},"message":{"$message_type":"diagnostic","message":"aborting due to 1 previous error","code":null,"level":"error","spans":[],"children":[],"rendered":"[..]error[..]: aborting due to 1 previous error[..]/n/n"}}
{"reason":"compiler-message","package_id":"path+[ROOTURL]/foo#0.1.0","manifest_path":"[..]","target":{"kind":["bin"],"crate_types":["bin"],"name":"foo","src_path":"[..]","edition":"2015","doc":true,"doctest":false,"test":true},"message":{"$message_type":"diagnostic","message":"For more information about this error, try `rustc --explain E0601`.","code":null,"level":"failure-note","spans":[],"children":[],"rendered":"[..]For more information about this error, try `rustc --explain E0601`.[..]/n"}}
{"reason":"build-finished","success":false}
"#]])
.run();
}

Expand All @@ -129,7 +140,24 @@ fn cargo_renders_doctests() {

p.cargo("test --doc --message-format short")
.with_status(101)
.with_stdout_contains("src/lib.rs:2:1: error[E0425]:[..]")
.with_stdout_contains("[..]src/lib.rs - bar (line 1)[..]")
.with_stdout_data(str![[r#"
running 1 test
test src/lib.rs - bar (line 1) ... FAILED
failures:
---- src/lib.rs - bar (line 1) stdout ----
src/lib.rs:2:1: error[E0425]: cannot find function `bar` in this scope
[ERROR] aborting due to 1 previous error
Couldn't compile the test.
failures:
src/lib.rs - bar (line 1)
test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in [ELAPSED]s
"#]])
.run();
}

0 comments on commit ba19274

Please sign in to comment.