|
4 | 4 | r"""
|
5 | 5 | For documentation and usage instructions, please see
|
6 | 6 | https://rustc-dev-guide.rust-lang.org/rustdoc-internals/rustdoc-test-suite.html
|
7 |
| -
|
8 |
| -htmldocck.py is a custom checker script for Rustdoc HTML outputs. |
9 |
| -
|
10 |
| -# How and why? |
11 |
| -
|
12 |
| -The principle is simple: This script receives a path to generated HTML |
13 |
| -documentation and a "template" script, which has a series of check |
14 |
| -commands like `@has` or `@matches`. Each command is used to check if |
15 |
| -some pattern is present or not present in the particular file or in |
16 |
| -a particular node of the HTML tree. In many cases, the template script |
17 |
| -happens to be the source code given to rustdoc. |
18 |
| -
|
19 |
| -While it indeed is possible to test in smaller portions, it has been |
20 |
| -hard to construct tests in this fashion and major rendering errors were |
21 |
| -discovered much later. This script is designed to make black-box and |
22 |
| -regression testing of Rustdoc easy. This does not preclude the needs for |
23 |
| -unit testing, but can be used to complement related tests by quickly |
24 |
| -showing the expected renderings. |
25 |
| -
|
26 |
| -In order to avoid one-off dependencies for this task, this script uses |
27 |
| -a reasonably working HTML parser and the existing XPath implementation |
28 |
| -from Python's standard library. Hopefully, we won't render |
29 |
| -non-well-formed HTML. |
30 |
| -
|
31 |
| -# Commands |
32 |
| -
|
33 |
| -Commands start with an `@` followed by a command name (letters and |
34 |
| -hyphens), and zero or more arguments separated by one or more whitespace |
35 |
| -characters and optionally delimited with single or double quotes. The `@` |
36 |
| -mark cannot be preceded by a non-whitespace character. Other lines |
37 |
| -(including every text up to the first `@`) are ignored, but it is |
38 |
| -recommended to avoid the use of `@` in the template file. |
39 |
| -
|
40 |
| -There are a number of supported commands: |
41 |
| -
|
42 |
| -* `@has PATH` checks for the existence of the given file. |
43 |
| -
|
44 |
| - `PATH` is relative to the output directory. It can be given as `-` |
45 |
| - which repeats the most recently used `PATH`. |
46 |
| -
|
47 |
| -* `@hasraw PATH PATTERN` and `@matchesraw PATH PATTERN` checks |
48 |
| - for the occurrence of the given pattern `PATTERN` in the specified file. |
49 |
| - Only one occurrence of the pattern is enough. |
50 |
| -
|
51 |
| - For `@hasraw`, `PATTERN` is a whitespace-normalized (every consecutive |
52 |
| - whitespace being replaced by one single space character) string. |
53 |
| - The entire file is also whitespace-normalized including newlines. |
54 |
| -
|
55 |
| - For `@matchesraw`, `PATTERN` is a Python-supported regular expression. |
56 |
| - The file remains intact but the regexp is matched without the `MULTILINE` |
57 |
| - and `IGNORECASE` options. You can still use a prefix `(?m)` or `(?i)` |
58 |
| - to override them, and `\A` and `\Z` for definitely matching |
59 |
| - the beginning and end of the file. |
60 |
| -
|
61 |
| - (The same distinction goes to other variants of these commands.) |
62 |
| -
|
63 |
| -* `@has PATH XPATH PATTERN` and `@matches PATH XPATH PATTERN` checks for |
64 |
| - the presence of the given XPath `XPATH` in the specified HTML file, |
65 |
| - and also the occurrence of the given pattern `PATTERN` in the matching |
66 |
| - node or attribute. Only one occurrence of the pattern in the match |
67 |
| - is enough. |
68 |
| -
|
69 |
| - `PATH` should be a valid and well-formed HTML file. It does *not* |
70 |
| - accept arbitrary HTML5; it should have matching open and close tags |
71 |
| - and correct entity references at least. |
72 |
| -
|
73 |
| - `XPATH` is an XPath expression to match. The XPath is fairly limited: |
74 |
| - `tag`, `*`, `.`, `//`, `..`, `[@attr]`, `[@attr='value']`, `[tag]`, |
75 |
| - `[POS]` (element located in given `POS`), `[last()-POS]`, `text()` |
76 |
| - and `@attr` (both as the last segment) are supported. Some examples: |
77 |
| -
|
78 |
| - - `//pre` or `.//pre` matches any element with a name `pre`. |
79 |
| - - `//a[@href]` matches any element with an `href` attribute. |
80 |
| - - `//*[@class="impl"]//code` matches any element with a name `code`, |
81 |
| - which is an ancestor of some element which `class` attr is `impl`. |
82 |
| - - `//h1[@class="fqn"]/span[1]/a[last()]/@class` matches a value of |
83 |
| - `class` attribute in the last `a` element (can be followed by more |
84 |
| - elements that are not `a`) inside the first `span` in the `h1` with |
85 |
| - a class of `fqn`. Note that there cannot be any additional elements |
86 |
| - between them due to the use of `/` instead of `//`. |
87 |
| -
|
88 |
| - Do not try to use non-absolute paths, it won't work due to the flawed |
89 |
| - ElementTree implementation. The script rejects them. |
90 |
| -
|
91 |
| - For the text matches (i.e. paths not ending with `@attr`), any |
92 |
| - subelements are flattened into one string; this is handy for ignoring |
93 |
| - highlights for example. If you want to simply check for the presence of |
94 |
| - a given node or attribute, use an empty string (`""`) as a `PATTERN`. |
95 |
| -
|
96 |
| -* `@count PATH XPATH COUNT` checks for the occurrence of the given XPath |
97 |
| - in the specified file. The number of occurrences must match the given |
98 |
| - count. |
99 |
| -
|
100 |
| -* `@count PATH XPATH TEXT COUNT` checks for the occurrence of the given XPath |
101 |
| - with the given text in the specified file. The number of occurrences must |
102 |
| - match the given count. |
103 |
| -
|
104 |
| -* `@snapshot NAME PATH XPATH` creates a snapshot test named NAME. |
105 |
| - A snapshot test captures a subtree of the DOM, at the location |
106 |
| - determined by the XPath, and compares it to a pre-recorded value |
107 |
| - in a file. The file's name is the test's name with the `.rs` extension |
108 |
| - replaced with `.NAME.html`, where NAME is the snapshot's name. |
109 |
| -
|
110 |
| - htmldocck supports the `--bless` option to accept the current subtree |
111 |
| - as expected, saving it to the file determined by the snapshot's name. |
112 |
| - compiletest's `--bless` flag is forwarded to htmldocck. |
113 |
| -
|
114 |
| -* `@has-dir PATH` checks for the existence of the given directory. |
115 |
| -
|
116 |
| -* `@files FOLDER_PATH [ENTRIES]`, checks that `FOLDER_PATH` contains exactly |
117 |
| - `[ENTRIES]`. |
118 |
| -
|
119 |
| -All conditions can be negated with `!`. `@!has foo/type.NoSuch.html` |
120 |
| -checks if the given file does not exist, for example. |
121 |
| -
|
122 | 7 | """
|
123 | 8 |
|
124 | 9 | from __future__ import absolute_import, print_function, unicode_literals
|
|
0 commit comments