Skip to content

Commit 9aa24fd

Browse files
authored
Rollup merge of #103682 - Swatinem:stable-run-directory, r=GuillaumeGomez
Stabilize rustdoc `--test-run-directory` This should resolve #84674
2 parents 3e565f1 + 2e924bb commit 9aa24fd

File tree

4 files changed

+29
-4
lines changed

4 files changed

+29
-4
lines changed

src/doc/rustdoc/src/command-line-arguments.md

+14-1
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ $ rustdoc src/lib.rs --test
179179
This flag will run your code examples as tests. For more, see [the chapter
180180
on documentation tests](write-documentation/documentation-tests.md).
181181

182-
See also `--test-args`.
182+
See also `--test-args` and `--test-run-directory`.
183183

184184
## `--test-args`: pass options to test runner
185185

@@ -194,6 +194,19 @@ For more, see [the chapter on documentation tests](write-documentation/documenta
194194

195195
See also `--test`.
196196

197+
## `--test-run-directory`: run code examples in a specific directory
198+
199+
Using this flag looks like this:
200+
201+
```bash
202+
$ rustdoc src/lib.rs --test --test-run-directory=/path/to/working/directory
203+
```
204+
205+
This flag will run your code examples in the specified working directory.
206+
For more, see [the chapter on documentation tests](write-documentation/documentation-tests.md).
207+
208+
See also `--test`.
209+
197210
## `--target`: generate documentation for the specified target triple
198211

199212
Using this flag looks like this:

src/doc/rustdoc/src/write-documentation/documentation-tests.md

+12
Original file line numberDiff line numberDiff line change
@@ -443,3 +443,15 @@ pub struct ReadmeDoctests;
443443

444444
This will include your README as documentation on the hidden struct `ReadmeDoctests`, which will
445445
then be tested alongside the rest of your doctests.
446+
447+
## Controlling the compilation and run directories
448+
449+
By default, `rustdoc --test` will compile and run documentation test examples
450+
from the same working directory.
451+
The compilation directory is being used for compiler diagnostics, the `file!()` macro and
452+
the output of `rustdoc` test runner itself, whereas the run directory has an influence on file-system
453+
operations within documentation test examples, such as `std::fs::read_to_string`.
454+
455+
The `--test-run-directory` flag allows controlling the run directory separately from the compilation directory.
456+
This is particularly useful in workspaces, where compiler invocations and thus diagnostics should be
457+
relative to the workspace directory, but documentation test examples should run relative to the crate directory.

src/librustdoc/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ fn opts() -> Vec<RustcOptGroup> {
284284
stable("test-args", |o| {
285285
o.optmulti("", "test-args", "arguments to pass to the test runner", "ARGS")
286286
}),
287-
unstable("test-run-directory", |o| {
287+
stable("test-run-directory", |o| {
288288
o.optopt(
289289
"",
290290
"test-run-directory",

tests/rustdoc-ui/run-directory.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
// revisions: correct incorrect
44
// check-pass
5-
// [correct]compile-flags:--test --test-run-directory={{src-base}} -Zunstable-options
6-
// [incorrect]compile-flags:--test --test-run-directory={{src-base}}/coverage -Zunstable-options
5+
// [correct]compile-flags:--test --test-run-directory={{src-base}}
6+
// [incorrect]compile-flags:--test --test-run-directory={{src-base}}/coverage
77
// normalize-stdout-test: "tests/rustdoc-ui" -> "$$DIR"
88
// normalize-stdout-test "finished in \d+\.\d+s" -> "finished in $$TIME"
99

0 commit comments

Comments
 (0)