Skip to content

Commit 07c8789

Browse files
Add documentation for --output-format=doctest
1 parent 5e9e27e commit 07c8789

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

src/doc/rustdoc/src/unstable-features.md

+64
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,8 @@ use `-o -`.
524524

525525
## `-w`/`--output-format`: output format
526526

527+
### json
528+
527529
`--output-format json` emits documentation in the experimental
528530
[JSON format](https://doc.rust-lang.org/nightly/nightly-rustc/rustdoc_json_types/). `--output-format html` has no effect,
529531
and is also accepted on stable toolchains.
@@ -542,6 +544,68 @@ It can also be used with `--show-coverage`. Take a look at its
542544
[documentation](#--show-coverage-calculate-the-percentage-of-items-with-documentation) for more
543545
information.
544546

547+
### doctest
548+
549+
`--output-format doctest` emits JSON on stdout which gives you information about doctests in the
550+
provided crate.
551+
552+
Tracking issue: [#134529](https://github.com/rust-lang/rust/issues/134529)
553+
554+
You can use this option like this:
555+
556+
```bash
557+
rustdoc -Zunstable-options --output-format=doctest src/lib.rs
558+
```
559+
560+
For this rust code:
561+
562+
```rust
563+
/// ```
564+
/// let x = 12;
565+
/// ```
566+
pub trait Trait {}
567+
```
568+
569+
The generated output (formatted) will look like this:
570+
571+
```json
572+
{
573+
"format_version": 1,
574+
"doctests": [
575+
{
576+
"file": "foo.rs",
577+
"line": 1,
578+
"doctest_attributes": {
579+
"original": "",
580+
"should_panic": false,
581+
"no_run": false,
582+
"ignore": "None",
583+
"rust": true,
584+
"test_harness": false,
585+
"compile_fail": false,
586+
"standalone_crate": false,
587+
"error_codes": [],
588+
"edition": null,
589+
"added_css_classes": [],
590+
"unknown": []
591+
},
592+
"original_code": "let x = 12;",
593+
"doctest_code": "#![allow(unused)]\nfn main() {\nlet x = 12;\n}",
594+
"name": "foo.rs - Trait (line 1)"
595+
}
596+
]
597+
}
598+
```
599+
600+
* `format_version` gives you the current version of the generated JSON. If we change the output in any way, the number will increase.
601+
* `doctests` contains the list of doctests present in the crate.
602+
* `file` is the file path where the doctest is located.
603+
* `line` is the line where the doctest starts (so where the \`\`\` is located in the current code).
604+
* `doctest_attributes` contains computed information about the attributes used on the doctests. For more information about doctest attributes, take a look [here](write-documentation/documentation-tests.html#attributes).
605+
* `original_code` is the code as written in the source code before rustdoc modifies it.
606+
* `doctest_code` is the code modified by rustdoc that will be run. If there is a fatal syntax error, this field will not be present.
607+
* `name` is the name generated by rustdoc which represents this doctest.
608+
545609
## `--enable-per-target-ignores`: allow `ignore-foo` style filters for doctests
546610

547611
* Tracking issue: [#64245](https://github.com/rust-lang/rust/issues/64245)

0 commit comments

Comments
 (0)