1
1
# Adding new tests
2
2
3
3
** 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.
7
7
8
8
To add a new test, the first thing you generally do is to create a
9
9
file, typically a Rust source file. Test files have a particular
@@ -20,18 +20,36 @@ structure:
20
20
21
21
Depending on the test suite, there may be some other details to be aware of:
22
22
- 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
+
24
40
## Naming your test
25
41
26
42
We have not traditionally had a lot of structure in the names of
27
43
tests. Moreover, for a long time, the rustc test runner did not
28
44
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
30
46
considered an ideal setup.
31
47
48
+ [ `src/test/run-pass` ] : https://github.com/rust-lang/rust/tree/master/src/test/run-pass/
49
+
32
50
For regression tests -- basically, some random snippet of code that
33
51
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,
35
53
though, it is better if you can put the test into a directory that
36
54
helps identify what piece of code is being tested here (e.g.,
37
55
` 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.
204
222
205
223
The UI tests are intended to capture the compiler's complete output,
206
224
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
213
233
` 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.
216
236
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
222
250
223
251
### Editing and updating the reference files
224
252
@@ -265,7 +293,10 @@ The corresponding reference file will use the normalized output to test both
265
293
...
266
294
```
267
295
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
269
300
270
301
Besides ` normalize-stderr-32bit ` and ` -64bit ` , one may use any target
271
302
information or stage supported by ` ignore-X ` here as well (e.g.
0 commit comments