Skip to content

Commit

Permalink
test: migrate docscrape to snapbox
Browse files Browse the repository at this point in the history
  • Loading branch information
heisen-li committed Jun 30, 2024
1 parent 8a997bf commit 8b038eb
Showing 1 changed file with 113 additions and 102 deletions.
215 changes: 113 additions & 102 deletions tests/testsuite/docscrape.rs
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -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");
Expand Down Expand Up @@ -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();

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

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

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

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

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

0 comments on commit 8b038eb

Please sign in to comment.