From ba1927404b71d09d26c4393ca7f13660cbc3d80a Mon Sep 17 00:00:00 2001 From: Heng-Yi Wu <2316687+henry40408@users.noreply.github.com> Date: Tue, 18 Jun 2024 06:27:00 +0800 Subject: [PATCH] test: migrate testsuite message_format to snapbox --- tests/testsuite/message_format.rs | 80 +++++++++++++++++++++---------- 1 file changed, 54 insertions(+), 26 deletions(-) diff --git a/tests/testsuite/message_format.rs b/tests/testsuite/message_format.rs index 3a1e60d21fe4..ffbc59e673d1 100644 --- a/tests/testsuite/message_format.rs +++ b/tests/testsuite/message_format.rs @@ -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() { @@ -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(); } } @@ -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(); } @@ -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(); } @@ -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(); } @@ -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(); }