Skip to content

Commit 504d6de

Browse files
Rollup merge of #81264 - Swatinem:doctest-run-directory, r=jyn514
Add unstable option to control doctest run directory This option will allow splitting the compile-time from the run-time directory of doctest invocations and is one step to solve rust-lang/cargo#8993 (comment) r? `@jyn514`
2 parents 22dc82f + 9b1d27d commit 504d6de

File tree

6 files changed

+51
-0
lines changed

6 files changed

+51
-0
lines changed

src/librustdoc/config.rs

+5
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ crate struct Options {
103103
crate should_test: bool,
104104
/// List of arguments to pass to the test harness, if running tests.
105105
crate test_args: Vec<String>,
106+
/// The working directory in which to run tests.
107+
crate test_run_directory: Option<PathBuf>,
106108
/// Optional path to persist the doctest executables to, defaults to a
107109
/// temporary directory if not set.
108110
crate persist_doctests: Option<PathBuf>,
@@ -175,6 +177,7 @@ impl fmt::Debug for Options {
175177
.field("lint_cap", &self.lint_cap)
176178
.field("should_test", &self.should_test)
177179
.field("test_args", &self.test_args)
180+
.field("test_run_directory", &self.test_run_directory)
178181
.field("persist_doctests", &self.persist_doctests)
179182
.field("default_passes", &self.default_passes)
180183
.field("manual_passes", &self.manual_passes)
@@ -572,6 +575,7 @@ impl Options {
572575
let enable_index_page = matches.opt_present("enable-index-page") || index_page.is_some();
573576
let static_root_path = matches.opt_str("static-root-path");
574577
let generate_search_filter = !matches.opt_present("disable-per-crate-search");
578+
let test_run_directory = matches.opt_str("test-run-directory").map(PathBuf::from);
575579
let persist_doctests = matches.opt_str("persist-doctests").map(PathBuf::from);
576580
let test_builder = matches.opt_str("test-builder").map(PathBuf::from);
577581
let codegen_options_strs = matches.opt_strs("C");
@@ -613,6 +617,7 @@ impl Options {
613617
display_warnings,
614618
show_coverage,
615619
crate_version,
620+
test_run_directory,
616621
persist_doctests,
617622
runtool,
618623
runtool_args,

src/librustdoc/doctest.rs

+3
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,9 @@ fn run_test(
365365
} else {
366366
cmd = Command::new(output_file);
367367
}
368+
if let Some(run_directory) = options.test_run_directory {
369+
cmd.current_dir(run_directory);
370+
}
368371

369372
match cmd.output() {
370373
Err(e) => return Err(TestFailure::ExecutionError(e)),

src/librustdoc/lib.rs

+8
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,14 @@ fn opts() -> Vec<RustcOptGroup> {
168168
stable("test-args", |o| {
169169
o.optmulti("", "test-args", "arguments to pass to the test runner", "ARGS")
170170
}),
171+
unstable("test-run-directory", |o| {
172+
o.optopt(
173+
"",
174+
"test-run-directory",
175+
"The working directory in which to run tests",
176+
"PATH",
177+
)
178+
}),
171179
stable("target", |o| o.optopt("", "target", "target triple to document", "TRIPLE")),
172180
stable("markdown-css", |o| {
173181
o.optmulti(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
running 1 test
3+
test $DIR/run-directory.rs - foo (line 10) ... ok
4+
5+
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in $TIME
6+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
running 1 test
3+
test $DIR/run-directory.rs - foo (line 19) ... ok
4+
5+
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in $TIME
6+

src/test/rustdoc-ui/run-directory.rs

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// this test asserts that the cwd of doctest invocations is set correctly.
2+
3+
// revisions: correct incorrect
4+
// check-pass
5+
// [correct]compile-flags:--test --test-run-directory={{src-base}}
6+
// [incorrect]compile-flags:--test --test-run-directory={{src-base}}/coverage
7+
// normalize-stdout-test: "src/test/rustdoc-ui" -> "$$DIR"
8+
// normalize-stdout-test "finished in \d+\.\d+s" -> "finished in $$TIME"
9+
10+
/// ```
11+
/// assert_eq!(
12+
/// std::fs::read_to_string("run-directory.rs").unwrap(),
13+
/// include_str!("run-directory.rs"),
14+
/// );
15+
/// ```
16+
#[cfg(correct)]
17+
pub fn foo() {}
18+
19+
/// ```
20+
/// assert!(std::fs::read_to_string("run-directory.rs").is_err());
21+
/// ```
22+
#[cfg(incorrect)]
23+
pub fn foo() {}

0 commit comments

Comments
 (0)