Skip to content

Commit 00ed1ea

Browse files
committed
give some advice about which test suite to use
1 parent b195442 commit 00ed1ea

File tree

2 files changed

+59
-27
lines changed

2 files changed

+59
-27
lines changed

src/tests/adding.md

Lines changed: 51 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# Adding new tests
22

33
**In general, we expect every PR that fixes a bug in rustc to come
4-
accompanied with a regression test of some kind.** This test should
5-
fail in master but pass after the PR. These tests are really useful
6-
for preventing us from repeating the mistakes of the past.
4+
accompanied by a regression test of some kind.** This test should fail
5+
in master but pass after the PR. These tests are really useful for
6+
preventing us from repeating the mistakes of the past.
77

88
To add a new test, the first thing you generally do is to create a
99
file, typically a Rust source file. Test files have a particular
@@ -20,18 +20,36 @@ structure:
2020

2121
Depending on the test suite, there may be some other details to be aware of:
2222
- For [the `ui` test suite](#ui), you need to generate reference output files.
23-
23+
24+
## What kind of test should I add?
25+
26+
It can be difficult to know what kind of test to use. Here are some
27+
rough heuristics:
28+
29+
- Some tests have specialized needs:
30+
- need to run gdb or lldb? use the `debuginfo` test suite
31+
- need to inspect LLVM IR or MIR IR? use the `codegen` or `mir-opt` test suites
32+
- need to run rustdoc? Prefer a `rustdoc` test
33+
- need to inspect the resulting binary in some way? Then use `run-make`
34+
- For most other things, [a `ui` (or `ui-fulldeps`) test](#ui) is to be preferred:
35+
- `ui` tests subsume both run-pass, compile-fail, and parse-fail tests
36+
- in the case of warnings or errors, `ui` tests capture the full output,
37+
which makes it easier to review but also helps prevent "hidden" regressions
38+
in the output
39+
2440
## Naming your test
2541

2642
We have not traditionally had a lot of structure in the names of
2743
tests. Moreover, for a long time, the rustc test runner did not
2844
support subdirectories (it now does), so test suites like
29-
`src/test/run-pass` have a huge mess of files in them. This is not
45+
[`src/test/run-pass`] have a huge mess of files in them. This is not
3046
considered an ideal setup.
3147

48+
[`src/test/run-pass`]: https://github.com/rust-lang/rust/tree/master/src/test/run-pass/
49+
3250
For regression tests -- basically, some random snippet of code that
3351
came in from the internet -- we often just name the test after the
34-
issue. For example, `src/test/run-pass/issue-1234.rs`. If possible,
52+
issue. For example, `src/test/run-pass/issue-12345.rs`. If possible,
3553
though, it is better if you can put the test into a directory that
3654
helps identify what piece of code is being tested here (e.g.,
3755
`borrowck/issue-12345.rs` is much better), or perhaps give it a more
@@ -204,21 +222,31 @@ customized to a revision are error patterns and compiler flags.
204222

205223
The UI tests are intended to capture the compiler's complete output,
206224
so that we can test all aspects of the presentation. They work by
207-
compiling a file (e.g., `ui/hello_world/main.rs`), capturing the output,
208-
and then applying some normalization (see below). This normalized
209-
result is then compared against reference files named
210-
`ui/hello_world/main.stderr` and `ui/hello_world/main.stdout`. If either of
211-
those files doesn't exist, the output must be empty. If the test run
212-
fails, we will print out the current output, but it is also saved in
225+
compiling a file (e.g., [`ui/hello_world/main.rs`][hw-main]),
226+
capturing the output, and then applying some normalization (see
227+
below). This normalized result is then compared against reference
228+
files named `ui/hello_world/main.stderr` and
229+
`ui/hello_world/main.stdout`. If either of those files doesn't exist,
230+
the output must be empty (that is actually the case for
231+
[this particular test][hw]). If the test run fails, we will print out
232+
the current output, but it is also saved in
213233
`build/<target-triple>/test/ui/hello_world/main.stdout` (this path is
214-
printed as part of the test failure message), so you can run `diff` and
215-
so forth.
234+
printed as part of the test failure message), so you can run `diff`
235+
and so forth.
216236

217-
Normally, the test-runner checks that UI tests fail compilation. If you want
218-
to do a UI test for code that *compiles* (e.g. to test warnings, or if you
219-
have a collection of tests, only some of which error out), you can use the
220-
`// must-compile-successfully` header command to have the test runner instead
221-
check that the test compiles successfully.
237+
[hw-main]: https://github.com/rust-lang/rust/blob/master/src/test/ui/hello_world/main.rs
238+
[hw]: https://github.com/rust-lang/rust/blob/master/src/test/ui/hello_world/
239+
240+
### Tests that do not result in compile errors
241+
242+
By default, a UI test is expected **not to compile** (in which case,
243+
it should contain at least one `//~ ERROR` annotation). However, you
244+
can also make UI tests where compilation is expected to succeed, and
245+
you can even run the resulting program. Just add one of the following
246+
[header commands](#header_commands):
247+
248+
- `// must-compile-successfully` -- compilation should succeed but do not run the resulting binary
249+
- `// run-pass` -- compilation should succeed and we should run the resulting binary
222250

223251
### Editing and updating the reference files
224252

@@ -265,7 +293,10 @@ The corresponding reference file will use the normalized output to test both
265293
...
266294
```
267295

268-
Please see `ui/transmute/main.rs` and `.stderr` for a concrete usage example.
296+
Please see [`ui/transmute/main.rs`][mrs] and [`main.stderr`][] for a concrete usage example.
297+
298+
[mrs]: https://github.com/rust-lang/rust/blob/master/src/test/ui/transmute/main.rs
299+
[`main.stderr`]: https://github.com/rust-lang/rust/blob/master/src/test/ui/transmute/main.stderr
269300

270301
Besides `normalize-stderr-32bit` and `-64bit`, one may use any target
271302
information or stage supported by `ignore-X` here as well (e.g.

src/tests/intro.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Here is a brief summary of the test suites as of this writing and what
2323
they mean. In some cases, the test suites are linked to parts of the manual
2424
that give more details.
2525

26-
- [`ui`](ui.html) -- tests that check the exact stdout/stderr from compilation
26+
- [`ui`](adding.html#) -- tests that check the exact stdout/stderr from compilation
2727
and/or running the test
2828
- `run-pass` -- tests that are expected to compile and execute successfully (no panics)
2929
- `run-pass-valgrind` -- tests that ought to run with valrind
@@ -34,15 +34,16 @@ that give more details.
3434
generates valid Rust code from the AST
3535
- `debuginfo` -- tests that run in gdb or lldb and query the debug info
3636
- `codegen` -- tests that compile and then test the generated LLVM
37-
code to make sure that optimizing we want are kicking in etc
38-
- `mir-opt` -- tests that check parts of the generated MIR to make sure we are optimizing
39-
etc.
37+
code to make sure that the optimizations we want are taking effect.
38+
- `mir-opt` -- tests that check parts of the generated MIR to make
39+
sure we are building things correctly or doing the optimizations we
40+
expect.
4041
- `incremental` -- tests for incremental compilation, checking that
4142
when certain modifications are performed, we are able to reuse the
4243
results from previous compilations.
43-
- `run-make` -- tests that basically just execute a `Makefile`; the ultimate in flexibility
44-
but annoying as all get out to write.
44+
- `run-make` -- tests that basically just execute a `Makefile`; the
45+
ultimate in flexibility but quite annoying to write.
4546
- `rustdoc` -- tests for rustdoc, making sure that the generated files contain
46-
documentation for various entities etc
47+
the expected documentation.
4748
- `*-fulldeps` -- same as above, but indicates that the test depends on things other
4849
than `libstd` (and hence those things must be built)

0 commit comments

Comments
 (0)