Skip to content

Commit 55d30bb

Browse files
committed
Enable doctest-in-workspace by default
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.
1 parent 1548f3b commit 55d30bb

File tree

3 files changed

+99
-14
lines changed

3 files changed

+99
-14
lines changed

src/cargo/core/features.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -710,7 +710,6 @@ unstable_cli_options!(
710710
config_include: bool = ("Enable the `include` key in config files"),
711711
credential_process: bool = ("Add a config setting to fetch registry authentication tokens by calling an external process"),
712712
direct_minimal_versions: bool = ("Resolve minimal dependency versions instead of maximum (direct dependencies only)"),
713-
doctest_in_workspace: bool = ("Compile doctests with paths relative to the workspace root"),
714713
doctest_xcompile: bool = ("Compile and run doctests for non-host target using runner config"),
715714
dual_proc_macros: bool = ("Build proc-macros for both the host and the target"),
716715
features: Option<Vec<String>> = (HIDDEN),
@@ -779,6 +778,9 @@ const STABILIZED_NAMED_PROFILES: &str = "The named-profiles feature is now alway
779778
See https://doc.rust-lang.org/nightly/cargo/reference/profiles.html#custom-profiles \
780779
for more information";
781780

781+
const STABILIZED_DOCTEST_IN_WORKSPACE: &str =
782+
"The doctest-in-workspace feature is now always enabled.";
783+
782784
const STABILIZED_FUTURE_INCOMPAT_REPORT: &str =
783785
"The future-incompat-report feature is now always enabled.";
784786

@@ -1034,7 +1036,7 @@ impl CliUnstable {
10341036
}
10351037
"build-std-features" => self.build_std_features = Some(parse_features(v)),
10361038
"doctest-xcompile" => self.doctest_xcompile = parse_empty(k, v)?,
1037-
"doctest-in-workspace" => self.doctest_in_workspace = parse_empty(k, v)?,
1039+
"doctest-in-workspace" => stabilized_warn(k, "1.72", STABILIZED_DOCTEST_IN_WORKSPACE),
10381040
"panic-abort-tests" => self.panic_abort_tests = parse_empty(k, v)?,
10391041
"jobserver-per-rustc" => self.jobserver_per_rustc = parse_empty(k, v)?,
10401042
"gitoxide" => {

src/cargo/ops/cargo_test.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,6 @@ fn run_doc_tests(
172172
let config = ws.config();
173173
let mut errors = Vec::new();
174174
let doctest_xcompile = config.cli_unstable().doctest_xcompile;
175-
let doctest_in_workspace = config.cli_unstable().doctest_in_workspace;
176175

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

218-
if doctest_in_workspace {
219-
add_path_args(ws, unit, &mut p);
220-
p.arg("--test-run-directory")
221-
.arg(unit.pkg.root().to_path_buf());
222-
} else {
223-
p.arg(unit.target.src_path().path().unwrap());
224-
}
217+
add_path_args(ws, unit, &mut p);
218+
p.arg("--test-run-directory")
219+
.arg(unit.pkg.root().to_path_buf());
225220

226221
if let CompileKind::Target(target) = unit.kind {
227222
// use `rustc_target()` to properly handle JSON target paths

tests/testsuite/doc.rs

Lines changed: 92 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2041,7 +2041,7 @@ fn crate_versions_flag_is_overridden() {
20412041
asserts(output_documentation());
20422042
}
20432043

2044-
#[cargo_test(nightly, reason = "-Zdoctest-in-workspace is unstable")]
2044+
#[cargo_test]
20452045
fn doc_test_in_workspace() {
20462046
let p = project()
20472047
.file(
@@ -2087,16 +2087,14 @@ fn doc_test_in_workspace() {
20872087
",
20882088
)
20892089
.build();
2090-
p.cargo("test -Zdoctest-in-workspace --doc -vv")
2091-
.masquerade_as_nightly_cargo(&["doctest-in-workspace"])
2090+
p.cargo("test --doc -vv")
20922091
.with_stderr_contains("[DOCTEST] crate-a")
20932092
.with_stdout_contains(
20942093
"
20952094
running 1 test
20962095
test crate-a/src/lib.rs - (line 1) ... ok
20972096
20982097
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out[..]
2099-
21002098
",
21012099
)
21022100
.with_stderr_contains("[DOCTEST] crate-b")
@@ -2106,7 +2104,97 @@ running 1 test
21062104
test crate-b/src/lib.rs - (line 1) ... ok
21072105
21082106
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out[..]
2107+
",
2108+
)
2109+
.run();
2110+
}
2111+
2112+
/// This is a test for <https://github.com/rust-lang/rust/issues/46372>.
2113+
/// The `file!()` macro inside of an `include!()` should output
2114+
/// workspace-relative paths, just like it does in other cases.
2115+
#[cargo_test]
2116+
fn doc_test_include_file() {
2117+
let p = project()
2118+
.file(
2119+
"Cargo.toml",
2120+
r#"
2121+
[workspace]
2122+
members = [
2123+
"child",
2124+
]
2125+
[package]
2126+
name = "root"
2127+
version = "0.1.0"
2128+
"#,
2129+
)
2130+
.file(
2131+
"src/lib.rs",
2132+
r#"
2133+
/// ```
2134+
/// assert_eq!("src/lib.rs", file!().replace("\\", "/"))
2135+
/// ```
2136+
pub mod included {
2137+
include!(concat!("../", file!(), ".included.rs"));
2138+
}
2139+
"#,
2140+
)
2141+
.file(
2142+
"src/lib.rs.included.rs",
2143+
r#"
2144+
/// ```
2145+
/// assert_eq!(1, 1)
2146+
/// ```
2147+
pub fn foo() {}
2148+
"#,
2149+
)
2150+
.file(
2151+
"child/Cargo.toml",
2152+
r#"
2153+
[package]
2154+
name = "child"
2155+
version = "0.1.0"
2156+
"#,
2157+
)
2158+
.file(
2159+
"child/src/lib.rs",
2160+
r#"
2161+
/// ```
2162+
/// assert_eq!("child/src/lib.rs", file!().replace("\\", "/"))
2163+
/// ```
2164+
pub mod included {
2165+
include!(concat!("../../", file!(), ".included.rs"));
2166+
}
2167+
"#,
2168+
)
2169+
.file(
2170+
"child/src/lib.rs.included.rs",
2171+
r#"
2172+
/// ```
2173+
/// assert_eq!(1, 1)
2174+
/// ```
2175+
pub fn foo() {}
2176+
"#,
2177+
)
2178+
.build();
2179+
p.cargo("test --workspace --doc -vv")
2180+
.with_stderr_contains("[DOCTEST] root")
2181+
.with_stdout_contains(
2182+
"
2183+
running 2 tests
2184+
test src/../src/lib.rs.included.rs - included::foo (line 2) ... ok
2185+
test src/lib.rs - included (line 2) ... ok
2186+
2187+
test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out[..]
2188+
",
2189+
)
2190+
.with_stderr_contains("[DOCTEST] child")
2191+
.with_stdout_contains(
2192+
"
2193+
running 2 tests
2194+
test child/src/../../child/src/lib.rs.included.rs - included::foo (line 2) ... ok
2195+
test child/src/lib.rs - included (line 2) ... ok
21092196
2197+
test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out[..]
21102198
",
21112199
)
21122200
.run();

0 commit comments

Comments
 (0)