Skip to content

Commit

Permalink
Enable doctest-in-workspace by default
Browse files Browse the repository at this point in the history
This stabilizes and enables the `-Z doctest-in-workspace` flag by default.

Also adds another testcase to make sure that the `include!()` and `file!()` macros interact well together.
  • Loading branch information
Swatinem committed Jun 2, 2023
1 parent 1548f3b commit 55d30bb
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 14 deletions.
6 changes: 4 additions & 2 deletions src/cargo/core/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -710,7 +710,6 @@ unstable_cli_options!(
config_include: bool = ("Enable the `include` key in config files"),
credential_process: bool = ("Add a config setting to fetch registry authentication tokens by calling an external process"),
direct_minimal_versions: bool = ("Resolve minimal dependency versions instead of maximum (direct dependencies only)"),
doctest_in_workspace: bool = ("Compile doctests with paths relative to the workspace root"),
doctest_xcompile: bool = ("Compile and run doctests for non-host target using runner config"),
dual_proc_macros: bool = ("Build proc-macros for both the host and the target"),
features: Option<Vec<String>> = (HIDDEN),
Expand Down Expand Up @@ -779,6 +778,9 @@ const STABILIZED_NAMED_PROFILES: &str = "The named-profiles feature is now alway
See https://doc.rust-lang.org/nightly/cargo/reference/profiles.html#custom-profiles \
for more information";

const STABILIZED_DOCTEST_IN_WORKSPACE: &str =
"The doctest-in-workspace feature is now always enabled.";

const STABILIZED_FUTURE_INCOMPAT_REPORT: &str =
"The future-incompat-report feature is now always enabled.";

Expand Down Expand Up @@ -1034,7 +1036,7 @@ impl CliUnstable {
}
"build-std-features" => self.build_std_features = Some(parse_features(v)),
"doctest-xcompile" => self.doctest_xcompile = parse_empty(k, v)?,
"doctest-in-workspace" => self.doctest_in_workspace = parse_empty(k, v)?,
"doctest-in-workspace" => stabilized_warn(k, "1.72", STABILIZED_DOCTEST_IN_WORKSPACE),
"panic-abort-tests" => self.panic_abort_tests = parse_empty(k, v)?,
"jobserver-per-rustc" => self.jobserver_per_rustc = parse_empty(k, v)?,
"gitoxide" => {
Expand Down
11 changes: 3 additions & 8 deletions src/cargo/ops/cargo_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,6 @@ fn run_doc_tests(
let config = ws.config();
let mut errors = Vec::new();
let doctest_xcompile = config.cli_unstable().doctest_xcompile;
let doctest_in_workspace = config.cli_unstable().doctest_in_workspace;

for doctest_info in &compilation.to_doc_test {
let Doctest {
Expand Down Expand Up @@ -215,13 +214,9 @@ fn run_doc_tests(
p.arg("--crate-name").arg(&unit.target.crate_name());
p.arg("--test");

if doctest_in_workspace {
add_path_args(ws, unit, &mut p);
p.arg("--test-run-directory")
.arg(unit.pkg.root().to_path_buf());
} else {
p.arg(unit.target.src_path().path().unwrap());
}
add_path_args(ws, unit, &mut p);
p.arg("--test-run-directory")
.arg(unit.pkg.root().to_path_buf());

if let CompileKind::Target(target) = unit.kind {
// use `rustc_target()` to properly handle JSON target paths
Expand Down
96 changes: 92 additions & 4 deletions tests/testsuite/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2041,7 +2041,7 @@ fn crate_versions_flag_is_overridden() {
asserts(output_documentation());
}

#[cargo_test(nightly, reason = "-Zdoctest-in-workspace is unstable")]
#[cargo_test]
fn doc_test_in_workspace() {
let p = project()
.file(
Expand Down Expand Up @@ -2087,16 +2087,14 @@ fn doc_test_in_workspace() {
",
)
.build();
p.cargo("test -Zdoctest-in-workspace --doc -vv")
.masquerade_as_nightly_cargo(&["doctest-in-workspace"])
p.cargo("test --doc -vv")
.with_stderr_contains("[DOCTEST] crate-a")
.with_stdout_contains(
"
running 1 test
test crate-a/src/lib.rs - (line 1) ... ok
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out[..]
",
)
.with_stderr_contains("[DOCTEST] crate-b")
Expand All @@ -2106,7 +2104,97 @@ running 1 test
test crate-b/src/lib.rs - (line 1) ... ok
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out[..]
",
)
.run();
}

/// This is a test for <https://github.com/rust-lang/rust/issues/46372>.
/// The `file!()` macro inside of an `include!()` should output
/// workspace-relative paths, just like it does in other cases.
#[cargo_test]
fn doc_test_include_file() {
let p = project()
.file(
"Cargo.toml",
r#"
[workspace]
members = [
"child",
]
[package]
name = "root"
version = "0.1.0"
"#,
)
.file(
"src/lib.rs",
r#"
/// ```
/// assert_eq!("src/lib.rs", file!().replace("\\", "/"))
/// ```
pub mod included {
include!(concat!("../", file!(), ".included.rs"));
}
"#,
)
.file(
"src/lib.rs.included.rs",
r#"
/// ```
/// assert_eq!(1, 1)
/// ```
pub fn foo() {}
"#,
)
.file(
"child/Cargo.toml",
r#"
[package]
name = "child"
version = "0.1.0"
"#,
)
.file(
"child/src/lib.rs",
r#"
/// ```
/// assert_eq!("child/src/lib.rs", file!().replace("\\", "/"))
/// ```
pub mod included {
include!(concat!("../../", file!(), ".included.rs"));
}
"#,
)
.file(
"child/src/lib.rs.included.rs",
r#"
/// ```
/// assert_eq!(1, 1)
/// ```
pub fn foo() {}
"#,
)
.build();
p.cargo("test --workspace --doc -vv")
.with_stderr_contains("[DOCTEST] root")
.with_stdout_contains(
"
running 2 tests
test src/../src/lib.rs.included.rs - included::foo (line 2) ... ok
test src/lib.rs - included (line 2) ... ok
test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out[..]
",
)
.with_stderr_contains("[DOCTEST] child")
.with_stdout_contains(
"
running 2 tests
test child/src/../../child/src/lib.rs.included.rs - included::foo (line 2) ... ok
test child/src/lib.rs - included (line 2) ... ok
test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out[..]
",
)
.run();
Expand Down

0 comments on commit 55d30bb

Please sign in to comment.