From 3a46ef16f7e0bef967fdf388d3b42823cc391e59 Mon Sep 17 00:00:00 2001 From: eth3lbert Date: Tue, 18 Jun 2024 18:54:17 +0800 Subject: [PATCH 1/2] test: migrate rustdoc to snapbox --- tests/testsuite/rustdoc.rs | 208 ++++++++++++++++--------------------- 1 file changed, 90 insertions(+), 118 deletions(-) diff --git a/tests/testsuite/rustdoc.rs b/tests/testsuite/rustdoc.rs index 647abce59de..34a1f045129 100644 --- a/tests/testsuite/rustdoc.rs +++ b/tests/testsuite/rustdoc.rs @@ -1,7 +1,6 @@ //! Tests for the `cargo rustdoc` command. -#![allow(deprecated)] - +use cargo_test_support::str; use cargo_test_support::{basic_manifest, cross_compile, project}; #[cargo_test] @@ -9,17 +8,13 @@ fn rustdoc_simple() { let p = project().file("src/lib.rs", "").build(); p.cargo("rustdoc -v") - .with_stderr( - "\ -[DOCUMENTING] foo v0.0.1 ([CWD]) -[RUNNING] `rustdoc [..]--crate-name foo src/lib.rs [..]\ - -o [CWD]/target/doc \ - [..] \ - -L dependency=[CWD]/target/debug/deps [..]` -[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [..] -[GENERATED] [CWD]/target/doc/foo/index.html -", - ) + .with_stderr_data(str![[r#" +[DOCUMENTING] foo v0.0.1 ([ROOT]/foo) +[RUNNING] `rustdoc [..] --crate-name foo src/lib.rs -o [ROOT]/foo/target/doc [..] -L dependency=[ROOT]/foo/target/debug/deps [..]` +[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s +[GENERATED] [ROOT]/foo/target/doc/foo/index.html + +"#]]) .run(); } @@ -29,13 +24,12 @@ fn rustdoc_simple_html() { p.cargo("rustdoc --output-format html --open -v") .with_status(101) - .with_stderr( - "\ -error: the `--output-format` flag is unstable, and only available on the nightly channel of Cargo, but this is the `stable` channel -[..] + .with_stderr_data(str![[r#" +[ERROR] the `--output-format` flag is unstable, and only available on the nightly channel of Cargo, but this is the `stable` channel +See https://doc.rust-lang.org/book/[..].html for more information about Rust release channels. See https://github.com/rust-lang/cargo/issues/12103 for more information about the `--output-format` flag. -", - ) + +"#]]) .run(); } @@ -45,14 +39,13 @@ fn rustdoc_simple_json() { p.cargo("rustdoc -Z unstable-options --output-format json -v") .masquerade_as_nightly_cargo(&["rustdoc-output-format"]) - .with_stderr( - "\ -[DOCUMENTING] foo v0.0.1 ([CWD]) -[RUNNING] `rustdoc [..]--crate-name foo [..]-o [CWD]/target/doc [..]--output-format=json[..] -[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [..] -[GENERATED] [CWD]/target/doc/foo.json -", - ) + .with_stderr_data(str![[r#" +[DOCUMENTING] foo v0.0.1 ([ROOT]/foo) +[RUNNING] `rustdoc [..] --crate-name foo [..]-o [ROOT]/foo/target/doc [..] --output-format=json[..] +[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s +[GENERATED] [ROOT]/foo/target/doc/foo.json + +"#]]) .run(); assert!(p.root().join("target/doc/foo.json").is_file()); } @@ -64,14 +57,13 @@ fn rustdoc_invalid_output_format() { p.cargo("rustdoc -Z unstable-options --output-format pdf -v") .masquerade_as_nightly_cargo(&["rustdoc-output-format"]) .with_status(1) - .with_stderr( - "\ -error: invalid value 'pdf' for '--output-format ' + .with_stderr_data(str![[r#" +[ERROR] invalid value 'pdf' for '--output-format ' [possible values: html, json] For more information, try '--help'. -", - ) + +"#]]) .run(); } @@ -81,11 +73,12 @@ fn rustdoc_json_stable() { p.cargo("rustdoc -Z unstable-options --output-format json -v") .with_status(101) - .with_stderr( - "\ -error: the `-Z` flag is only accepted on the nightly channel of Cargo, but this is the `stable` channel -[..] -", + .with_stderr_data( + str![[r#" +[ERROR] the `-Z` flag is only accepted on the nightly channel of Cargo, but this is the `stable` channel +See https://doc.rust-lang.org/book/[..].html for more information about Rust release channels. + +"#]] ) .run(); } @@ -97,12 +90,11 @@ fn rustdoc_json_without_unstable_options() { p.cargo("rustdoc --output-format json -v") .masquerade_as_nightly_cargo(&["rustdoc-output-format"]) .with_status(101) - .with_stderr( - "\ -error: the `--output-format` flag is unstable, pass `-Z unstable-options` to enable it -[..] -", - ) + .with_stderr_data(str![[r#" +[ERROR] the `--output-format` flag is unstable, pass `-Z unstable-options` to enable it +See https://github.com/rust-lang/cargo/issues/12103 for more information about the `--output-format` flag. + +"#]]) .run(); } @@ -111,19 +103,13 @@ fn rustdoc_args() { let p = project().file("src/lib.rs", "").build(); p.cargo("rustdoc -v -- --cfg=foo") - .with_stderr( - "\ -[DOCUMENTING] foo v0.0.1 ([CWD]) -[RUNNING] `rustdoc [..]--crate-name foo src/lib.rs [..]\ - -o [CWD]/target/doc \ - [..] \ - --cfg=foo \ - -C metadata=[..] \ - -L dependency=[CWD]/target/debug/deps [..]` -[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [..] -[GENERATED] [CWD]/target/doc/foo/index.html -", - ) + .with_stderr_data(str![[r#" +[DOCUMENTING] foo v0.0.1 ([ROOT]/foo) +[RUNNING] `rustdoc [..] --crate-name foo src/lib.rs -o [ROOT]/foo/target/doc [..] --cfg=foo -C metadata=[..] -L dependency=[ROOT]/foo/target/debug/deps [..]` +[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s +[GENERATED] [ROOT]/foo/target/doc/foo/index.html + +"#]]) .run(); } @@ -134,7 +120,11 @@ fn rustdoc_binary_args_passed() { p.cargo("rustdoc -v") .arg("--") .arg("--markdown-no-toc") - .with_stderr_contains("[RUNNING] `rustdoc [..] --markdown-no-toc[..]`") + .with_stderr_data(str![[r#" +... +[RUNNING] `rustdoc [..] --markdown-no-toc[..]` +... +"#]]) .run(); } @@ -163,23 +153,16 @@ fn rustdoc_foo_with_bar_dependency() { .build(); foo.cargo("rustdoc -v -- --cfg=foo") - .with_stderr( - "\ + .with_stderr_data(str![[r#" [LOCKING] 2 packages to latest compatible versions -[CHECKING] bar v0.0.1 ([..]) -[RUNNING] `rustc [..]bar/src/lib.rs [..]` -[DOCUMENTING] foo v0.0.1 ([CWD]) -[RUNNING] `rustdoc [..]--crate-name foo src/lib.rs [..]\ - -o [CWD]/target/doc \ - [..] \ - --cfg=foo \ - -C metadata=[..] \ - -L dependency=[CWD]/target/debug/deps \ - --extern [..]` -[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [..] -[GENERATED] [CWD]/target/doc/foo/index.html -", - ) +[CHECKING] bar v0.0.1 ([ROOT]/bar) +[RUNNING] `rustc [..] [ROOT]/bar/src/lib.rs [..]` +[DOCUMENTING] foo v0.0.1 ([ROOT]/foo) +[RUNNING] `rustdoc [..] --crate-name foo src/lib.rs -o [ROOT]/foo/target/doc [..] --cfg=foo -C metadata=[..] -L dependency=[ROOT]/foo/target/debug/deps --extern [..]` +[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s +[GENERATED] [ROOT]/foo/target/doc/foo/index.html + +"#]]) .run(); } @@ -208,20 +191,14 @@ fn rustdoc_only_bar_dependency() { .build(); foo.cargo("rustdoc -v -p bar -- --cfg=foo") - .with_stderr( - "\ + .with_stderr_data(str![[r#" [LOCKING] 2 packages to latest compatible versions -[DOCUMENTING] bar v0.0.1 ([..]) -[RUNNING] `rustdoc [..]--crate-name bar [..]bar/src/lib.rs [..]\ - -o [CWD]/target/doc \ - [..] \ - --cfg=foo \ - -C metadata=[..] \ - -L dependency=[CWD]/target/debug/deps [..]` -[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [..] -[GENERATED] [CWD]/target/doc/bar/index.html -", - ) +[DOCUMENTING] bar v0.0.1 ([ROOT]/bar) +[RUNNING] `rustdoc [..] --crate-name bar [ROOT]/bar/src/lib.rs -o [ROOT]/foo/target/doc [..] --cfg=foo -C metadata=[..] -L dependency=[ROOT]/foo/target/debug/deps [..]` +[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s +[GENERATED] [ROOT]/foo/target/doc/bar/index.html + +"#]]) .run(); } @@ -233,19 +210,13 @@ fn rustdoc_same_name_documents_lib() { .build(); p.cargo("rustdoc -v -- --cfg=foo") - .with_stderr( - "\ -[DOCUMENTING] foo v0.0.1 ([..]) -[RUNNING] `rustdoc [..]--crate-name foo src/lib.rs [..]\ - -o [CWD]/target/doc \ - [..] \ - --cfg=foo \ - -C metadata=[..] \ - -L dependency=[CWD]/target/debug/deps [..]` -[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [..] -[GENERATED] [CWD]/target/doc/foo/index.html -", - ) + .with_stderr_data(str![[r#" +[DOCUMENTING] foo v0.0.1 ([ROOT]/foo) +[RUNNING] `rustdoc [..] --crate-name foo src/lib.rs -o [ROOT]/foo/target/doc [..] --cfg=foo -C metadata=[..] -L dependency=[ROOT]/foo/target/debug/deps [..]` +[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s +[GENERATED] [ROOT]/foo/target/doc/foo/index.html + +"#]]) .run(); } @@ -269,7 +240,11 @@ fn features() { .build(); p.cargo("rustdoc --verbose --features quux") - .with_stderr_contains("[..]feature=[..]quux[..]") + .with_stderr_data(str![[r#" +... +[RUNNING] `rustdoc [..]feature=[..]quux[..]` +... +"#]]) .run(); } @@ -294,11 +269,11 @@ fn proc_macro_crate_type() { .build(); p.cargo("rustdoc --verbose") - .with_stderr_contains( - "\ + .with_stderr_data(str![[r#" +... [RUNNING] `rustdoc --edition=2015 --crate-type proc-macro [..]` -", - ) +... +"#]]) .run(); } @@ -312,19 +287,13 @@ fn rustdoc_target() { p.cargo("rustdoc --verbose --target") .arg(cross_compile::alternate()) - .with_stderr(format!( - "\ -[DOCUMENTING] foo v0.0.1 ([..]) -[RUNNING] `rustdoc [..]--crate-name foo src/lib.rs [..]\ - --target {target} \ - -o [CWD]/target/{target}/doc \ - [..] \ - -L dependency=[CWD]/target/{target}/debug/deps \ - -L dependency=[CWD]/target/debug/deps[..]` -[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [..] -[GENERATED] [CWD]/target/[..]/doc/foo/index.html", - target = cross_compile::alternate() - )) + .with_stderr_data(str![[r#" +[DOCUMENTING] foo v0.0.1 ([ROOT]/foo) +[RUNNING] `rustdoc [..]--crate-name foo src/lib.rs [..]--target [ALT_TARGET] -o [ROOT]/foo/target/[ALT_TARGET]/doc [..] -L dependency=[ROOT]/foo/target/[ALT_TARGET]/debug/deps -L dependency=[ROOT]/foo/target/debug/deps[..]` +[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s +[GENERATED] [ROOT]/foo/target/[..]/doc/foo/index.html + +"#]]) .run(); } @@ -344,6 +313,9 @@ fn fail_with_glob() { p.cargo("rustdoc -p '*z'") .with_status(101) - .with_stderr("[ERROR] Glob patterns on package selection are not supported.") + .with_stderr_data(str![[r#" +[ERROR] Glob patterns on package selection are not supported. + +"#]]) .run(); } From fa4f3908690bd937ac70f478b76d7546a2f352b7 Mon Sep 17 00:00:00 2001 From: eth3lbert Date: Tue, 18 Jun 2024 23:59:37 +0800 Subject: [PATCH 2/2] test: migrate rustdocflags to snapbox --- tests/testsuite/rustdocflags.rs | 110 ++++++++++++++++++++++---------- 1 file changed, 78 insertions(+), 32 deletions(-) diff --git a/tests/testsuite/rustdocflags.rs b/tests/testsuite/rustdocflags.rs index 7afc8e78bb2..1ab834567eb 100644 --- a/tests/testsuite/rustdocflags.rs +++ b/tests/testsuite/rustdocflags.rs @@ -1,10 +1,9 @@ //! Tests for setting custom rustdoc flags. -#![allow(deprecated)] - use cargo_test_support::project; use cargo_test_support::rustc_host; use cargo_test_support::rustc_host_env; +use cargo_test_support::str; #[cargo_test] fn parses_env() { @@ -12,7 +11,11 @@ fn parses_env() { p.cargo("doc -v") .env("RUSTDOCFLAGS", "--cfg=foo") - .with_stderr_contains("[RUNNING] `rustdoc [..] --cfg=foo[..]`") + .with_stderr_data(str![[r#" +... +[RUNNING] `rustdoc [..] --cfg=foo[..]` +... +"#]]) .run(); } @@ -30,7 +33,11 @@ fn parses_config() { .build(); p.cargo("doc -v") - .with_stderr_contains("[RUNNING] `rustdoc [..] --cfg foo[..]`") + .with_stderr_data(str![[r#" +... +[RUNNING] `rustdoc [..] --cfg foo [..]` +... +"#]]) .run(); } @@ -41,7 +48,11 @@ fn bad_flags() { p.cargo("doc") .env("RUSTDOCFLAGS", "--bogus") .with_status(101) - .with_stderr_contains("[..]bogus[..]") + .with_stderr_data(str![[r#" +... +[ERROR] Unrecognized option: 'bogus' +... +"#]]) .run(); } @@ -52,20 +63,20 @@ fn rerun() { p.cargo("doc").env("RUSTDOCFLAGS", "--cfg=foo").run(); p.cargo("doc") .env("RUSTDOCFLAGS", "--cfg=foo") - .with_stderr( - "[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [..] -[GENERATED] [CWD]/target/doc/foo/index.html", - ) + .with_stderr_data(str![[r#" +[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s +[GENERATED] [ROOT]/foo/target/doc/foo/index.html + +"#]]) .run(); p.cargo("doc") .env("RUSTDOCFLAGS", "--cfg=bar") - .with_stderr( - "\ -[DOCUMENTING] foo v0.0.1 ([..]) -[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [..] -[GENERATED] [CWD]/target/doc/foo/index.html -", - ) + .with_stderr_data(str![[r#" +[DOCUMENTING] foo v0.0.1 ([ROOT]/foo) +[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s +[GENERATED] [ROOT]/foo/target/doc/foo/index.html + +"#]]) .run(); } @@ -102,7 +113,10 @@ fn rustdocflags_misspelled() { p.cargo("doc") .env("RUSTDOC_FLAGS", "foo") - .with_stderr_contains("[WARNING] Cargo does not read `RUSTDOC_FLAGS` environment variable. Did you mean `RUSTDOCFLAGS`?") + .with_stderr_data(str![[r#" +[WARNING] Cargo does not read `RUSTDOC_FLAGS` environment variable. Did you mean `RUSTDOCFLAGS`? +... +"#]]) .run(); } @@ -114,7 +128,11 @@ fn whitespace() { // "too many operands" p.cargo("doc") .env("RUSTDOCFLAGS", "--crate-version this has spaces") - .with_stderr_contains("[ERROR] could not document `foo`") + .with_stderr_data(str![[r#" +... +[ERROR] could not document `foo` +... +"#]]) .with_status(101) .run(); @@ -155,12 +173,20 @@ fn not_affected_by_target_rustflags() { // `cargo build` should fail due to missing docs. p.cargo("build -v") .with_status(101) - .with_stderr_contains("[RUNNING] `rustc [..] -D missing-docs[..]`") + .with_stderr_data(str![[r#" +... +[RUNNING] `rustc [..] -D missing-docs` +... +"#]]) .run(); // `cargo doc` shouldn't fail. p.cargo("doc -v") - .with_stderr_contains("[RUNNING] `rustdoc [..] --cfg foo[..]`") + .with_stderr_data(str![[r#" +... +[RUNNING] `rustdoc [..] --cfg foo[..]` +... +"#]]) .run(); } @@ -176,14 +202,22 @@ fn target_triple_rustdocflags_works() { &format!("CARGO_TARGET_{host_env}_RUSTDOCFLAGS"), "--cfg=foo", ) - .with_stderr_contains("[RUNNING] `rustdoc[..]--cfg[..]foo[..]`") + .with_stderr_data(str![[r#" +... +[RUNNING] `rustdoc[..]--cfg[..]foo[..]` +... +"#]]) .run(); // target.triple.rustdocflags in config works p.cargo("doc -v") .arg("--config") .arg(format!("target.{host}.rustdocflags=['--cfg', 'foo']")) - .with_stderr_contains("[RUNNING] `rustdoc[..]--cfg[..]foo[..]`") + .with_stderr_data(str![[r#" +... +[RUNNING] `rustdoc [..] --cfg foo [..]` +... +"#]]) .run(); } @@ -208,27 +242,39 @@ fn target_triple_rustdocflags_works_through_cargo_test() { &format!("CARGO_TARGET_{host_env}_RUSTDOCFLAGS"), "--cfg=foo", ) - .with_stderr_contains("[RUNNING] `rustdoc[..]--test[..]--cfg[..]foo[..]`") - .with_stdout_contains( - "\ + .with_stderr_data(str![[r#" +... +[RUNNING] `rustdoc[..]--test[..]--cfg[..]foo[..]` + +"#]]) + .with_stdout_data(str![[r#" + running 1 test test src/lib.rs - (line 2) ... ok -test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out[..]", - ) +test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in [ELAPSED]s + + +"#]]) .run(); // target.triple.rustdocflags in config works p.cargo("test --doc -v") .arg("--config") .arg(format!("target.{host}.rustdocflags=['--cfg', 'foo']")) - .with_stderr_contains("[RUNNING] `rustdoc[..]--test[..]--cfg[..]foo[..]`") - .with_stdout_contains( - "\ + .with_stderr_data(str![[r#" +... +[RUNNING] `rustdoc[..]--test[..]--cfg[..]foo[..]` + +"#]]) + .with_stdout_data(str![[r#" + running 1 test test src/lib.rs - (line 2) ... ok -test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out[..]", - ) +test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in [ELAPSED]s + + +"#]]) .run(); }