Skip to content

Commit 30c298d

Browse files
authored
Clean up compiletest chapter (#935)
1 parent 53a7fd3 commit 30c298d

File tree

2 files changed

+52
-54
lines changed

2 files changed

+52
-54
lines changed

src/SUMMARY.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
- [The compiler testing framework](./tests/intro.md)
2121
- [Running tests](./tests/running.md)
2222
- [Adding new tests](./tests/adding.md)
23-
- [Using `compiletest` + commands to control test execution](./compiletest.md)
23+
- [Using `compiletest` commands to control test execution](./compiletest.md)
2424
- [Debugging the Compiler](./compiler-debugging.md)
2525
- [Profiling the compiler](./profiling.md)
2626
- [with the linux perf tool](./profiling/with_perf.md)

src/compiletest.md

Lines changed: 51 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,51 @@
1-
# `compiletest`
1+
# Using `compiletest` commands to control test execution
22

33
## Introduction
44

5-
`compiletest` is the main test harness of the Rust test suite. It allows
5+
`compiletest` is the main test harness of the Rust test suite. It allows
66
test authors to organize large numbers of tests (the Rust compiler has many
77
thousands), efficient test execution (parallel execution is supported), and
88
allows the test author to configure behavior and expected results of both
99
individual and groups of tests.
1010

11-
`compiletest` tests may check test code for success, for failure or in some
12-
cases, even failure to compile. Tests are typically organized as a Rust source
13-
file with annotations in comments before and/or within the test code, which
14-
serve to direct `compiletest` on if or how to run the test, what behavior to
15-
expect, and more. If you are unfamiliar with the compiler testing framework,
16-
see [this chapter](./tests/intro.html) for additional background.
11+
`compiletest` tests may check test code for success, for runtime failure, or for
12+
compile-time failure. Tests are typically organized as a Rust source file with
13+
annotations in comments before and/or within the test code, which serve to
14+
direct `compiletest` on if or how to run the test, what behavior to expect,
15+
and more. If you are unfamiliar with the compiler testing framework,
16+
see [this chapter](./tests/intro.md) for additional background.
1717

1818
The tests themselves are typically (but not always) organized into
19-
"suites" – for example, `run-fail`,
20-
a folder holding tests that should compile successfully,
21-
but return a failure (non-zero status), `compile-fail`, a folder holding tests
22-
that should fail to compile, and many more. The various suites are defined in
23-
[src/tools/compiletest/src/common.rs][common] in the `pub enum Mode`
24-
declaration. And a very good introduction to the different suites of compiler
25-
tests along with details about them can be found in [Adding new
26-
tests](./tests/adding.html).
19+
"suites" – for example, `run-fail`, a folder holding tests that should compile
20+
successfully, but return a failure (non-zero status) at runtime, `compile-fail`,
21+
a folder holding tests that should fail to compile, and many more. The various
22+
suites are defined in [`src/tools/compiletest/src/common.rs`] in the
23+
`pub enum Mode` declaration. And a good introduction to the different
24+
suites of compiler tests along with details about them can be found in
25+
[Adding new tests](./tests/adding.md).
2726

2827
## Adding a new test file
2928

3029
Briefly, simply create your new test in the appropriate location under
31-
[src/test][test]. No registration of test files is necessary as `compiletest`
32-
will scan the [src/test][test] subfolder recursively, and will execute any Rust
33-
source files it finds as tests. See [`Adding new tests`](./tests/adding.html)
34-
for a complete guide on how to adding new tests.
30+
[`src/test`]. No registration of test files is necessary as `compiletest`
31+
will scan the [`src/test`] subfolder recursively, and will execute any
32+
Rust source files it finds as tests.
33+
See [Adding new tests](./tests/adding.md) for a complete guide on how to add
34+
new tests.
3535

3636
## Header Commands
3737

3838
Source file annotations which appear in comments near the top of the source
39-
file *before* any test code are known as header commands. These commands can
39+
file *before* any test code are known as header commands. These commands can
4040
instruct `compiletest` to ignore this test, set expectations on whether it is
4141
expected to succeed at compiling, or what the test's return code is expected to
42-
be. Header commands (and their inline counterparts, Error Info commands) are
43-
described more fully
44-
[here](./tests/adding.html#header-commands-configuring-rustc).
42+
be. Header commands and inline `//~ ERROR` commands are described more fully
43+
[here](./tests/adding.md#header-commands-configuring-rustc).
4544

4645
### Adding a new header command
4746

4847
Header commands are defined in the `TestProps` struct in
49-
[src/tools/compiletest/src/header.rs][header]. At a high level, there are
48+
[`src/tools/compiletest/src/header.rs`]. At a high level, there are
5049
dozens of test properties defined here, all set to default values in the
5150
`TestProp` struct's `impl` block. Any test can override this default value by
5251
specifying the property in question as header command as a comment (`//`) in
@@ -77,48 +76,47 @@ fn main() -> Result<(), Box<Error>> {
7776
#### Adding a new header command property
7877

7978
One would add a new header command if there is a need to define some test
80-
property or behavior on an individual, test-by-test basis. A header command
79+
property or behavior on an individual, test-by-test basis. A header command
8180
property serves as the header command's backing store (holds the command's
8281
current value) at runtime.
8382

8483
To add a new header command property:
85-
1. Look for the `pub struct TestProps` declaration in
86-
[src/tools/compiletest/src/header.rs][header] and add the new public
87-
property to the end of the declaration.
88-
2. Look for the `impl TestProps` implementation block immediately following
89-
the struct declaration and initialize the new property to its default
90-
value.
84+
85+
1. Look for the `pub struct TestProps` declaration in
86+
[`src/tools/compiletest/src/header.rs`] and add the new public property to
87+
the end of the declaration.
88+
2. Look for the `impl TestProps` implementation block immediately following
89+
the struct declaration and initialize the new property to its default
90+
value.
9191

9292
#### Adding a new header command parser
9393

9494
When `compiletest` encounters a test file, it parses the file a line at a time
9595
by calling every parser defined in the `Config` struct's implementation block,
96-
also in [src/tools/compiletest/src/header.rs][header] (note the `Config`
97-
struct's declaration block is found in
98-
[src/tools/compiletest/src/common.rs][common]. `TestProps`'s `load_from()`
99-
method will try passing the current line of text to each parser, which, in turn
100-
typically checks to see if the line begins with a particular commented (`//`)
101-
header command such as `// must-compile-successfully` or `// failure-status`.
102-
Whitespace after the comment marker is optional.
96+
also in [`src/tools/compiletest/src/header.rs`][] (note that the `Config`
97+
struct's declaration block is found in [`src/tools/compiletest/src/common.rs`]).
98+
`TestProps`'s `load_from()` method will try passing the current line of text to
99+
each parser, which, in turn typically checks to see if the line begins with a
100+
particular commented (`//`) header command such as `// must-compile-successfully`
101+
or `// failure-status`. Whitespace after the comment marker is optional.
103102

104103
Parsers will override a given header command property's default value merely by
105104
being specified in the test file as a header command or by having a parameter
106105
value specified in the test file, depending on the header command.
107106

108107
Parsers defined in `impl Config` are typically named `parse_<header_command>`
109108
(note kebab-case `<header-command>` transformed to snake-case
110-
`<header_command>`). `impl Config` also defines several 'low-level' parsers
109+
`<header_command>`). `impl Config` also defines several 'low-level' parsers
111110
which make it simple to parse common patterns like simple presence or not
112111
(`parse_name_directive()`), header-command:parameter(s)
113112
(`parse_name_value_directive()`), optional parsing only if a particular `cfg`
114-
attribute is defined (`has_cfg_prefix()`) and many more. The low-level parsers
113+
attribute is defined (`has_cfg_prefix()`) and many more. The low-level parsers
115114
are found near the end of the `impl Config` block; be sure to look through them
116115
and their associated parsers immediately above to see how they are used to
117116
avoid writing additional parsing code unnecessarily.
118117

119118
As a concrete example, here is the implementation for the
120-
`parse_failure_status()` parser, in
121-
[src/tools/compiletest/src/header.rs][header]:
119+
`parse_failure_status()` parser, in [`src/tools/compiletest/src/header.rs`]:
122120

123121
```diff
124122
@@ -232,6 +232,7 @@ pub struct TestProps {
@@ -163,18 +161,17 @@ As a concrete example, here is the implementation for the
163161
## Implementing the behavior change
164162

165163
When a test invokes a particular header command, it is expected that some
166-
behavior will change as a result. What behavior, obviously, will depend on the
167-
purpose of the header command. In the case of `failure-status`, the behavior
164+
behavior will change as a result. What behavior, obviously, will depend on the
165+
purpose of the header command. In the case of `failure-status`, the behavior
168166
that changes is that `compiletest` expects the failure code defined by the
169167
header command invoked in the test, rather than the default value.
170168

171169
Although specific to `failure-status` (as every header command will have a
172170
different implementation in order to invoke behavior change) perhaps it is
173171
helpful to see the behavior change implementation of one case, simply as an
174-
example. To implement `failure-status`, the `check_correct_failure_status()`
172+
example. To implement `failure-status`, the `check_correct_failure_status()`
175173
function found in the `TestCx` implementation block, located in
176-
[src/tools/compiletest/src/runtest.rs](https://github.com/rust-lang/rust/tree/master/src/tools/compiletest/src/runtest.rs),
177-
was modified as per below:
174+
[`src/tools/compiletest/src/runtest.rs`], was modified as per below:
178175

179176
```diff
180177
@@ -295,11 +295,14 @@ impl<'test> TestCx<'test> {
@@ -213,13 +210,14 @@ was modified as per below:
213210
}
214211
```
215212
Note the use of `self.props.failure_status` to access the header command
216-
property. In tests which do not specify the failure status header command,
213+
property. In tests which do not specify the failure status header command,
217214
`self.props.failure_status` will evaluate to the default value of 101 at the
218-
time of this writing. But for a test which specifies a header command of, for
215+
time of this writing. But for a test which specifies a header command of, for
219216
example, `// failure-status: 1`, `self.props.failure_status` will evaluate to
220217
1, as `parse_failure_status()` will have overridden the `TestProps` default
221218
value, for that test specifically.
222219

223-
[test]: https://github.com/rust-lang/rust/tree/master/src/test
224-
[header]: https://github.com/rust-lang/rust/tree/master/src/tools/compiletest/src/header.rs
225-
[common]: https://github.com/rust-lang/rust/tree/master/src/tools/compiletest/src/common.rs
220+
[`src/test`]: https://github.com/rust-lang/rust/tree/master/src/test
221+
[`src/tools/compiletest/src/header.rs`]: https://github.com/rust-lang/rust/tree/master/src/tools/compiletest/src/header.rs
222+
[`src/tools/compiletest/src/common.rs`]: https://github.com/rust-lang/rust/tree/master/src/tools/compiletest/src/common.rs
223+
[`src/tools/compiletest/src/runtest.rs`]: https://github.com/rust-lang/rust/tree/master/src/tools/compiletest/src/runtest.rs

0 commit comments

Comments
 (0)