Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stabilize rustdoc --test-run-directory #103682

Merged
merged 1 commit into from
Apr 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/doc/rustdoc/src/command-line-arguments.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ $ rustdoc src/lib.rs --test
This flag will run your code examples as tests. For more, see [the chapter
on documentation tests](write-documentation/documentation-tests.md).

See also `--test-args`.
See also `--test-args` and `--test-run-directory`.

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

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

See also `--test`.

## `--test-run-directory`: run code examples in a specific directory

Using this flag looks like this:

```bash
$ rustdoc src/lib.rs --test --test-run-directory=/path/to/working/directory
```

This flag will run your code examples in the specified working directory.
For more, see [the chapter on documentation tests](write-documentation/documentation-tests.md).

See also `--test`.

## `--target`: generate documentation for the specified target triple

Using this flag looks like this:
Expand Down
12 changes: 12 additions & 0 deletions src/doc/rustdoc/src/write-documentation/documentation-tests.md
Original file line number Diff line number Diff line change
Expand Up @@ -443,3 +443,15 @@ pub struct ReadmeDoctests;

This will include your README as documentation on the hidden struct `ReadmeDoctests`, which will
then be tested alongside the rest of your doctests.

## Controlling the compilation and run directories

By default, `rustdoc --test` will compile and run documentation test examples
from the same working directory.
The compilation directory is being used for compiler diagnostics, the `file!()` macro and
the output of `rustdoc` test runner itself, whereas the run directory has an influence on file-system
operations within documentation test examples, such as `std::fs::read_to_string`.

The `--test-run-directory` flag allows controlling the run directory separately from the compilation directory.
This is particularly useful in workspaces, where compiler invocations and thus diagnostics should be
relative to the workspace directory, but documentation test examples should run relative to the crate directory.
2 changes: 1 addition & 1 deletion src/librustdoc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ fn opts() -> Vec<RustcOptGroup> {
stable("test-args", |o| {
o.optmulti("", "test-args", "arguments to pass to the test runner", "ARGS")
}),
unstable("test-run-directory", |o| {
stable("test-run-directory", |o| {
o.optopt(
"",
"test-run-directory",
Expand Down
4 changes: 2 additions & 2 deletions tests/rustdoc-ui/run-directory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

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

Expand Down