From 8b038ebdf0ac4a9497b8a1677b86e62e17759391 Mon Sep 17 00:00:00 2001 From: l00556901 Date: Sun, 30 Jun 2024 22:43:23 +0800 Subject: [PATCH] test: migrate docscrape to snapbox --- tests/testsuite/docscrape.rs | 215 ++++++++++++++++++----------------- 1 file changed, 113 insertions(+), 102 deletions(-) diff --git a/tests/testsuite/docscrape.rs b/tests/testsuite/docscrape.rs index 641ba6db655..51361b1515f 100644 --- a/tests/testsuite/docscrape.rs +++ b/tests/testsuite/docscrape.rs @@ -1,8 +1,8 @@ //! Tests for the `cargo doc` command with `-Zrustdoc-scrape-examples`. -#![allow(deprecated)] - +use cargo_test_support::prelude::*; use cargo_test_support::project; +use cargo_test_support::str; #[cargo_test(nightly, reason = "rustdoc scrape examples flags are unstable")] fn basic() { @@ -23,24 +23,23 @@ fn basic() { p.cargo("doc -Zunstable-options -Zrustdoc-scrape-examples") .masquerade_as_nightly_cargo(&["rustdoc-scrape-examples"]) - .with_stderr( - "\ -[CHECKING] foo v0.0.1 ([CWD]) -[SCRAPING] foo v0.0.1 ([CWD]) -[DOCUMENTING] foo v0.0.1 ([CWD]) -[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [..] -[GENERATED] [CWD]/target/doc/foo/index.html -", - ) + .with_stderr_data(str![[r#" +[CHECKING] foo v0.0.1 ([ROOT]/foo) +[SCRAPING] foo v0.0.1 ([ROOT]/foo) +[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(); p.cargo("doc -Zunstable-options -Z rustdoc-scrape-examples") .masquerade_as_nightly_cargo(&["rustdoc-scrape-examples"]) - .with_stderr( - "[FINISHED] [..] -[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(); let doc_html = p.read_file("target/doc/foo/fn.foo.html"); @@ -115,15 +114,18 @@ impl Foo { p.cargo("doc -Zunstable-options -Zrustdoc-scrape-examples --no-deps") .masquerade_as_nightly_cargo(&["rustdoc-scrape-examples"]) - .with_stderr_unordered( - "\ + .with_stderr_data( + str![[r#" [LOCKING] 2 packages to latest compatible versions -[CHECKING] a v0.0.1 ([CWD]/crates/a) -[CHECKING] foo v0.0.1 ([CWD]) -[SCRAPING] foo v0.0.1 ([CWD]) -[DOCUMENTING] foo v0.0.1 ([CWD]) -[FINISHED] [..] -[GENERATED] [CWD]/target/doc/foo/index.html", +[CHECKING] a v0.0.1 ([ROOT]/foo/crates/a) +[CHECKING] foo v0.0.1 ([ROOT]/foo) +[SCRAPING] foo v0.0.1 ([ROOT]/foo) +[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 + +"#]] + .unordered(), ) .run(); @@ -402,25 +404,23 @@ fn cache() { p.cargo("doc -Zunstable-options -Zrustdoc-scrape-examples") .masquerade_as_nightly_cargo(&["rustdoc-scrape-examples"]) - .with_stderr( - "\ -[CHECKING] foo v0.0.1 ([CWD]) -[SCRAPING] foo v0.0.1 ([CWD]) -[DOCUMENTING] foo v0.0.1 ([CWD]) -[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [..] -[GENERATED] [CWD]/target/doc/foo/index.html -", - ) + .with_stderr_data(str![[r#" +[CHECKING] foo v0.0.1 ([ROOT]/foo) +[SCRAPING] foo v0.0.1 ([ROOT]/foo) +[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(); p.cargo("doc -Zunstable-options -Zrustdoc-scrape-examples") .masquerade_as_nightly_cargo(&["rustdoc-scrape-examples"]) - .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(); } @@ -444,27 +444,26 @@ fn no_fail_bad_lib() { p.cargo("doc -Zunstable-options -Z rustdoc-scrape-examples") .masquerade_as_nightly_cargo(&["rustdoc-scrape-examples"]) - .with_stderr_unordered( - "\ -[CHECKING] foo v0.0.1 ([CWD]) -[SCRAPING] foo v0.0.1 ([CWD]) -warning: failed to check lib in package `foo` as a prerequisite for scraping examples from: example \"ex\", example \"ex2\" + .with_stderr_data(str![[r#" +[CHECKING] foo v0.0.1 ([ROOT]/foo) +[SCRAPING] foo v0.0.1 ([ROOT]/foo) +[WARNING] failed to check lib in package `foo` as a prerequisite for scraping examples from: example "ex", example "ex2" Try running with `--verbose` to see the error message. If an example should not be scanned, then consider adding `doc-scrape-examples = false` to its `[[example]]` definition in Cargo.toml -warning: `foo` (lib) generated 1 warning -warning: failed to scan example \"ex\" in package `foo` for example code usage +[WARNING] `foo` (lib) generated 1 warning +[WARNING] failed to scan example "ex" in package `foo` for example code usage Try running with `--verbose` to see the error message. If an example should not be scanned, then consider adding `doc-scrape-examples = false` to its `[[example]]` definition in Cargo.toml -warning: `foo` (example \"ex\") generated 1 warning -warning: failed to scan example \"ex2\" in package `foo` for example code usage +[WARNING] `foo` (example "ex") generated 1 warning +[WARNING] failed to scan example "ex2" in package `foo` for example code usage Try running with `--verbose` to see the error message. If an example should not be scanned, then consider adding `doc-scrape-examples = false` to its `[[example]]` definition in Cargo.toml -warning: `foo` (example \"ex2\") generated 1 warning -[DOCUMENTING] foo v0.0.1 ([CWD]) -[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [..] -[GENERATED] [CWD]/target/doc/foo/index.html -", - ) +[WARNING] `foo` (example "ex2") generated 1 warning +[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 + +"#]].unordered()) .run(); } @@ -489,14 +488,22 @@ fn fail_bad_build_script() { // `cargo doc` fails p.cargo("doc") .with_status(101) - .with_stderr_contains("[..]You shall not pass[..]") + .with_stderr_data(str![[r#" +... +[..]You shall not pass[..] +... +"#]]) .run(); // scrape examples should fail whenever `cargo doc` fails. p.cargo("doc -Zunstable-options -Z rustdoc-scrape-examples") .masquerade_as_nightly_cargo(&["rustdoc-scrape-examples"]) .with_status(101) - .with_stderr_contains("[..]You shall not pass[..]") + .with_stderr_data(str![[r#" +... +[..]You shall not pass[..] +... +"#]]) .run(); } @@ -520,43 +527,44 @@ fn no_fail_bad_example() { p.cargo("doc -Zunstable-options -Z rustdoc-scrape-examples") .masquerade_as_nightly_cargo(&["rustdoc-scrape-examples"]) - .with_stderr( - "\ -[CHECKING] foo v0.0.1 ([CWD]) -[SCRAPING] foo v0.0.1 ([CWD]) -warning: failed to scan example \"ex1\" in package `foo` for example code usage + .with_stderr_data(str![[r#" +[CHECKING] foo v0.0.1 ([ROOT]/foo) +[SCRAPING] foo v0.0.1 ([ROOT]/foo) +[WARNING] failed to scan example "ex1" in package `foo` for example code usage Try running with `--verbose` to see the error message. If an example should not be scanned, then consider adding `doc-scrape-examples = false` to its `[[example]]` definition in Cargo.toml -warning: `foo` (example \"ex1\") generated 1 warning -[DOCUMENTING] foo v0.0.1 ([CWD]) -[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [..] -[GENERATED] [CWD]/target/doc/foo/index.html -", - ) +[WARNING] `foo` (example "ex1") generated 1 warning +[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(); p.cargo("clean").run(); p.cargo("doc -v -Zunstable-options -Z rustdoc-scrape-examples") .masquerade_as_nightly_cargo(&["rustdoc-scrape-examples"]) - .with_stderr_unordered( - "\ -[CHECKING] foo v0.0.1 ([CWD]) + .with_stderr_data( + str![[r#" +[CHECKING] foo v0.0.1 ([ROOT]/foo) [RUNNING] `rustc --crate-name foo[..] -[SCRAPING] foo v0.0.1 ([CWD]) +[SCRAPING] foo v0.0.1 ([ROOT]/foo) [RUNNING] `rustdoc[..] --crate-name ex1[..] [RUNNING] `rustdoc[..] --crate-name ex2[..] [RUNNING] `rustdoc[..] --crate-name foo[..] -error: expected one of `!` or `::`, found `NOT` +[ERROR] expected one of `!` or `::`, found `NOT` --> examples/ex1.rs:1:6 | 1 | DOES NOT COMPILE | ^^^ expected one of `!` or `::` -[DOCUMENTING] foo v0.0.1 ([CWD]) -[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [..] -[GENERATED] [CWD]/target/doc/foo/index.html -", +[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 + +"#]] + .unordered(), ) .run(); @@ -603,32 +611,33 @@ fn no_scrape_with_dev_deps() { // should be raised. p.cargo("doc -Zunstable-options -Z rustdoc-scrape-examples") .masquerade_as_nightly_cargo(&["rustdoc-scrape-examples"]) - .with_stderr( - "\ + .with_stderr_data(str![[r#" [LOCKING] 2 packages to latest compatible versions -warning: Rustdoc did not scrape the following examples because they require dev-dependencies: ex +[WARNING] Rustdoc did not scrape the following examples because they require dev-dependencies: ex If you want Rustdoc to scrape these examples, then add `doc-scrape-examples = true` to the [[example]] target configuration of at least one example. -[DOCUMENTING] foo v0.0.1 ([CWD]) -[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [..] -[GENERATED] [CWD]/target/doc/foo/index.html -", - ) +[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(); // If --examples is provided, then the example is scanned. p.cargo("doc --examples -Zunstable-options -Z rustdoc-scrape-examples") .masquerade_as_nightly_cargo(&["rustdoc-scrape-examples"]) - .with_stderr_unordered( - "\ -[CHECKING] a v0.0.1 ([CWD]/a) -[CHECKING] foo v0.0.1 ([CWD]) -[DOCUMENTING] a v0.0.1 ([CWD]/a) -[SCRAPING] foo v0.0.1 ([CWD]) -[DOCUMENTING] foo v0.0.1 ([CWD]) -[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [..] -[GENERATED] [CWD]/target/doc/ex/index.html -", + .with_stderr_data( + str![[r#" +[CHECKING] a v0.0.1 ([ROOT]/foo/a) +[CHECKING] foo v0.0.1 ([ROOT]/foo) +[DOCUMENTING] a v0.0.1 ([ROOT]/foo/a) +[SCRAPING] foo v0.0.1 ([ROOT]/foo) +[DOCUMENTING] foo v0.0.1 ([ROOT]/foo) +[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s +[GENERATED] [ROOT]/foo/target/doc/ex/index.html + +"#]] + .unordered(), ) .run(); } @@ -671,16 +680,18 @@ fn use_dev_deps_if_explicitly_enabled() { // If --examples is not provided, then the example is never scanned. p.cargo("doc -Zunstable-options -Z rustdoc-scrape-examples") .masquerade_as_nightly_cargo(&["rustdoc-scrape-examples"]) - .with_stderr_unordered( - "\ + .with_stderr_data( + str![[r#" [LOCKING] 2 packages to latest compatible versions -[CHECKING] foo v0.0.1 ([CWD]) -[CHECKING] a v0.0.1 ([CWD]/a) -[SCRAPING] foo v0.0.1 ([CWD]) -[DOCUMENTING] foo v0.0.1 ([CWD]) +[CHECKING] a v0.0.1 ([ROOT]/foo/a) +[CHECKING] foo v0.0.1 ([ROOT]/foo) +[SCRAPING] foo v0.0.1 ([ROOT]/foo) +[DOCUMENTING] foo v0.0.1 ([ROOT]/foo) [FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [..] -[GENERATED] [CWD]/target/doc/foo/index.html -", +[GENERATED] [ROOT]/foo/target/doc/foo/index.html + +"#]] + .unordered(), ) .run(); }