Skip to content

Commit 4f053a9

Browse files
committed
add new section on the rustdoc test suite
1 parent a9f46f1 commit 4f053a9

File tree

3 files changed

+109
-8
lines changed

3 files changed

+109
-8
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# `rustdoc` tests
2+
3+
This page is specifically about the `rustdoc` test suite, for other test suites used for testing rustdoc, see [Rustdoc § Tests](../rustdoc.md#tests).
4+
5+
`htmldocck.py` is a custom checker script that uses [XPath] to verify the HTML output of rustdoc.
6+
7+
[XPath]: https://en.wikipedia.org/wiki/XPath
8+
9+
## Directives
10+
Directives to htmldocck are similar to those given to `compiletest` in that they take the form of `//@` comments.
11+
12+
All `PATH`s in directives are relative to the the rustdoc output directory (`build/TARGET/test/rustdoc/TESTNAME`),
13+
so it is conventional to use a `#![crate_name = "foo"]` attribute to avoid writing paths.
14+
To avoid repetion, `-` can be used in any `PATH` argument to re-use the previous `PATH` argument.
15+
16+
All arguments take the form of quoted strings,
17+
with the exception of `COUNT` and the special `-` form of `PATH`.
18+
19+
Directives are assertions that place constraints on the generated HTML.
20+
21+
All directives (except `files`) can be negated by putting a `!` in front of their name.
22+
23+
Similar to shell commands,
24+
directives can extend across multiple lines if their last char is `\`.
25+
In this case, the start of the next line should be `//`, with no `@`.
26+
27+
For example, `//@ !has 'foo/struct.Bar.html'` checks that crate `foo` does not have a page for a struct named `Bar` in the crate root.
28+
29+
### `has`
30+
31+
Usage 1: `//@ has PATH`
32+
Usage 2: `//@ has PATH XPATH PATTERN`
33+
34+
In the first form, `has` checks that a given file exists.
35+
36+
In the second form, `has` is an alias for `matches`,
37+
except `PATTERN` is a whitespace-normalized[^1] string instead of a regex.
38+
39+
### `matches`
40+
41+
Usage: `//@ matches PATH XPATH PATTERN`
42+
43+
Checks that the text of each element selected by `XPATH` in `PATH` matches the python-flavored regex `PATTERN`.
44+
45+
### `matchesraw`
46+
47+
Usage: `//@ matchesraw PATH PATTERN`
48+
49+
Checks that the contents of the file `PATH` matches the regex `PATTERN`.
50+
51+
### `hasraw`
52+
53+
Usage: `//@ hasraw PATH PATTERN`
54+
55+
Same as `matchesraw`, except `PATTERN` is a whitespace-normalized[^1] string instead of a regex.
56+
57+
### `count`
58+
59+
Usage: `//@ count PATH XPATH COUNT`
60+
61+
Checks that there are exactly `COUNT` matches for `XPATH` within the file `PATH`.
62+
63+
### `snapshot`
64+
65+
Usage: `//@ snapshot NAME PATH XPATH`
66+
67+
Creates a snapshot test named NAME.
68+
A snapshot test captures a subtree of the DOM, at the location
69+
determined by the XPath, and compares it to a pre-recorded value
70+
in a file. The file's name is the test's name with the `.rs` extension
71+
replaced with `.NAME.html`, where NAME is the snapshot's name.
72+
73+
htmldocck supports the `--bless` option to accept the current subtree
74+
as expected, saving it to the file determined by the snapshot's name.
75+
compiletest's `--bless` flag is forwarded to htmldocck.
76+
77+
### `has-dir`
78+
79+
Usage: `//@ has-dir PATH`
80+
81+
Checks for the existance of directory `PATH`.
82+
83+
### `files`
84+
85+
Usage: `//@ files PATH ENTRIES`
86+
87+
Checks that the directory `PATH` contains exactly `ENTRIES`.
88+
`ENTRIES` is a python list of strings inside a quoted string,
89+
as if it were to be parsed by `eval`.
90+
(note that the list is actually parsed by `shlex.split`,
91+
so it cannot contain arbitrary python expressions).
92+
93+
Example: `//@ files "foo/bar" '["index.html", "sidebar-items.js"]'`
94+
95+
[^1]: Whitespace normalization means that all spans of consecutive whitespace are replaced with a single space. The files themselves are also whitespace-normalized.
96+
97+
## Limitations
98+
All `XPATH` arguments must start with `//` due to a flaw in the implemention.
99+
100+
Only well-formed HTML can be parsed (hopefully rustdoc doesn't output mismatched tags).
101+

src/doc/rustc-dev-guide/src/rustdoc.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,27 +77,27 @@ does is call the `main()` that's in this crate's `lib.rs`, though.)
7777
`doctest.rs`.
7878
* The Markdown renderer is loaded up in `html/markdown.rs`, including functions
7979
for extracting doctests from a given block of Markdown.
80-
* The tests on the structure of rustdoc HTML output are located in `tests/rustdoc`, where
81-
they're handled by the test runner of bootstrap and the supplementary script
82-
`src/etc/htmldocck.py`.
8380
* Frontend CSS and JavaScript are stored in `html/static/`.
8481

8582
## Tests
8683

87-
* All paths in this section are relative to `tests` in the rust-lang/rust repository.
88-
* Tests on search engine and index are located in `rustdoc-js` and `rustdoc-js-std`.
84+
* Tests on search engine and index are located in `tests/rustdoc-js` and `tests/rustdoc-js-std`.
8985
The format is specified
9086
[in the search guide](rustdoc-internals/search.md#testing-the-search-engine).
9187
* Tests on the "UI" of rustdoc (the terminal output it produces when run) are in
92-
`rustdoc-ui`
88+
`tests/rustdoc-ui`
9389
* Tests on the "GUI" of rustdoc (the HTML, JS, and CSS as rendered in a browser)
94-
are in `rustdoc-gui`. These use a [NodeJS tool called
90+
are in `tests/rustdoc-gui`. These use a [NodeJS tool called
9591
browser-UI-test](https://github.com/GuillaumeGomez/browser-UI-test/) that uses
9692
puppeteer to run tests in a headless browser and check rendering and
9793
interactivity.
9894
* Additionally, JavaScript type annotations are written using [TypeScript-flavored JSDoc]
9995
comments and an external d.ts file. The code itself is plain, valid JavaScript; we only
10096
use tsc as a linter.
97+
* The tests on the structure of rustdoc HTML output are located in `tests/rustdoc`,
98+
where they're handled by the test runner of bootstrap and
99+
the supplementary script `src/etc/htmldocck.py`.
100+
[These tests have several extra directives available to them](./rustdoc-internals/htmldocck.md).
101101

102102
[TypeScript-flavored JSDoc]: https://www.typescriptlang.org/docs/handbook/jsdoc-supported-types.html
103103

src/doc/rustc-dev-guide/src/tests/compiletest.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ The following test suites are available, with links for more information:
7878

7979
### Rustdoc test suites
8080

81-
See [Rustdoc tests](../rustdoc.md#tests) for more details.
81+
See [Rustdoc § Tests](../rustdoc.md#tests) for more details.
8282

8383
| Test suite | Purpose |
8484
|------------------|--------------------------------------------------------------------------|

0 commit comments

Comments
 (0)