Skip to content

Commit 493d999

Browse files
committed
Auto merge of #32007 - nikomatsakis:compiletest-incremental, r=alexcrichton
This PR extends compiletest to support **test revisions** and with a preliminary **incremental testing harness**. run-pass, compile-fail, and run-fail tests may be tagged with ``` // revisions: a b c d ``` This will cause the test to be re-run four times with `--cfg {a,b,c,d}` in turn. This means you can write very closely related things using `cfg`. You can also configure the headers/expected-errors by writing `//[foo] header: value` or `//[foo]~ ERROR bar`, where `foo` is the name of your revision. See the changes to `coherence-cow.rs` as a proof of concept. The main point of this work is to support the incremental testing harness. This PR contains an initial, unused version. The code that uses it will land later. The incremental testing harness compiles each revision in turn, and requires that the revisions have particular names (e.g., `rpass2`, `cfail3`), which tell it whether a particular revision is expected to compile or not. Two questions: - Is there compiletest documentation anywhere I can update? - Should I hold off on landing the incremental testing harness until I have the code to exercise it? (That will come in a separate PR, still fixing a few details) r? @alexcrichton cc @rust-lang/compiler <-- new testing capabilities
2 parents f6e125f + fc4d0ec commit 493d999

10 files changed

+498
-228
lines changed

COMPILER_TESTS.md

+40
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,43 @@ whole, instead of just a few lines inside the test.
4242
* `ignore-test` always ignores the test
4343
* `ignore-lldb` and `ignore-gdb` will skip the debuginfo tests
4444
* `min-{gdb,lldb}-version`
45+
* `should-fail` indicates that the test should fail; used for "meta testing",
46+
where we test the compiletest program itself to check that it will generate
47+
errors in appropriate scenarios. This header is ignored for pretty-printer tests.
48+
49+
## Revisions
50+
51+
Certain classes of tests support "revisions" (as of the time of this
52+
writing, this includes run-pass, compile-fail, run-fail, and
53+
incremental, though incremental tests are somewhat
54+
different). Revisions allow a single test file to be used for multiple
55+
tests. This is done by adding a special header at the top of the file:
56+
57+
```
58+
// revisions: foo bar baz
59+
```
60+
61+
This will result in the test being compiled (and tested) three times,
62+
once with `--cfg foo`, once with `--cfg bar`, and once with `--cfg
63+
baz`. You can therefore use `#[cfg(foo)]` etc within the test to tweak
64+
each of these results.
65+
66+
You can also customize headers and expected error messages to a particular
67+
revision. To do this, add `[foo]` (or `bar`, `baz`, etc) after the `//`
68+
comment, like so:
69+
70+
```
71+
// A flag to pass in only for cfg `foo`:
72+
//[foo]compile-flags: -Z verbose
73+
74+
#[cfg(foo)]
75+
fn test_foo() {
76+
let x: usize = 32_u32; //[foo]~ ERROR mismatched types
77+
}
78+
```
79+
80+
Note that not all headers have meaning when customized too a revision.
81+
For example, the `ignore-test` header (and all "ignore" headers)
82+
currently only apply to the test as a whole, not to particular
83+
revisions. The only headers that are intended to really work when
84+
customized to a revision are error patterns and compiler flags.

src/compiletest/compiletest.rs

+16-2
Original file line numberDiff line numberDiff line change
@@ -354,11 +354,25 @@ pub fn is_test(config: &Config, testfile: &Path) -> bool {
354354
}
355355

356356
pub fn make_test(config: &Config, testpaths: &TestPaths) -> test::TestDescAndFn {
357+
let early_props = header::early_props(config, &testpaths.file);
358+
359+
// The `should-fail` annotation doesn't apply to pretty tests,
360+
// since we run the pretty printer across all tests by default.
361+
// If desired, we could add a `should-fail-pretty` annotation.
362+
let should_panic = match config.mode {
363+
Pretty => test::ShouldPanic::No,
364+
_ => if early_props.should_fail {
365+
test::ShouldPanic::Yes
366+
} else {
367+
test::ShouldPanic::No
368+
}
369+
};
370+
357371
test::TestDescAndFn {
358372
desc: test::TestDesc {
359373
name: make_test_name(config, testpaths),
360-
ignore: header::is_test_ignored(config, &testpaths.file),
361-
should_panic: test::ShouldPanic::No,
374+
ignore: early_props.ignore,
375+
should_panic: should_panic,
362376
},
363377
testfn: make_test_closure(config, testpaths),
364378
}

src/compiletest/errors.rs

+35-20
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,10 @@ enum WhichLine { ThisLine, FollowPrevious(usize), AdjustBackward(usize) }
3030
/// Goal is to enable tests both like: //~^^^ ERROR go up three
3131
/// and also //~^ ERROR message one for the preceding line, and
3232
/// //~| ERROR message two for that same line.
33-
// Load any test directives embedded in the file
34-
pub fn load_errors(testfile: &Path) -> Vec<ExpectedError> {
33+
///
34+
/// If cfg is not None (i.e., in an incremental test), then we look
35+
/// for `//[X]~` instead, where `X` is the current `cfg`.
36+
pub fn load_errors(testfile: &Path, cfg: Option<&str>) -> Vec<ExpectedError> {
3537
let rdr = BufReader::new(File::open(testfile).unwrap());
3638

3739
// `last_nonfollow_error` tracks the most recently seen
@@ -44,30 +46,41 @@ pub fn load_errors(testfile: &Path) -> Vec<ExpectedError> {
4446
// updating it in the map callback below.)
4547
let mut last_nonfollow_error = None;
4648

47-
rdr.lines().enumerate().filter_map(|(line_no, ln)| {
48-
parse_expected(last_nonfollow_error,
49-
line_no + 1,
50-
&ln.unwrap())
51-
.map(|(which, error)| {
52-
match which {
53-
FollowPrevious(_) => {}
54-
_ => last_nonfollow_error = Some(error.line),
55-
}
56-
error
57-
})
58-
}).collect()
49+
let tag = match cfg {
50+
Some(rev) => format!("//[{}]~", rev),
51+
None => format!("//~")
52+
};
53+
54+
rdr.lines()
55+
.enumerate()
56+
.filter_map(|(line_no, ln)| {
57+
parse_expected(last_nonfollow_error,
58+
line_no + 1,
59+
&ln.unwrap(),
60+
&tag)
61+
.map(|(which, error)| {
62+
match which {
63+
FollowPrevious(_) => {}
64+
_ => last_nonfollow_error = Some(error.line),
65+
}
66+
error
67+
})
68+
})
69+
.collect()
5970
}
6071

6172
fn parse_expected(last_nonfollow_error: Option<usize>,
6273
line_num: usize,
63-
line: &str) -> Option<(WhichLine, ExpectedError)> {
64-
let start = match line.find("//~") { Some(i) => i, None => return None };
65-
let (follow, adjusts) = if line.char_at(start + 3) == '|' {
74+
line: &str,
75+
tag: &str)
76+
-> Option<(WhichLine, ExpectedError)> {
77+
let start = match line.find(tag) { Some(i) => i, None => return None };
78+
let (follow, adjusts) = if line.char_at(start + tag.len()) == '|' {
6679
(true, 0)
6780
} else {
68-
(false, line[start + 3..].chars().take_while(|c| *c == '^').count())
81+
(false, line[start + tag.len()..].chars().take_while(|c| *c == '^').count())
6982
};
70-
let kind_start = start + 3 + adjusts + (follow as usize);
83+
let kind_start = start + tag.len() + adjusts + (follow as usize);
7184
let letters = line[kind_start..].chars();
7285
let kind = letters.skip_while(|c| c.is_whitespace())
7386
.take_while(|c| !c.is_whitespace())
@@ -91,7 +104,9 @@ fn parse_expected(last_nonfollow_error: Option<usize>,
91104
(which, line)
92105
};
93106

94-
debug!("line={} which={:?} kind={:?} msg={:?}", line_num, which, kind, msg);
107+
debug!("line={} tag={:?} which={:?} kind={:?} msg={:?}",
108+
line_num, tag, which, kind, msg);
109+
95110
Some((which, ExpectedError { line: line,
96111
kind: kind,
97112
msg: msg, }))

0 commit comments

Comments
 (0)