Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

extend compiletest to support revisions, incremental tests #32007

Merged
merged 14 commits into from
Mar 3, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions COMPILER_TESTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,43 @@ whole, instead of just a few lines inside the test.
* `ignore-test` always ignores the test
* `ignore-lldb` and `ignore-gdb` will skip the debuginfo tests
* `min-{gdb,lldb}-version`
* `should-fail` indicates that the test should fail; used for "meta testing",
where we test the compiletest program itself to check that it will generate
errors in appropriate scenarios. This header is ignored for pretty-printer tests.

## Revisions

Certain classes of tests support "revisions" (as of the time of this
writing, this includes run-pass, compile-fail, run-fail, and
incremental, though incremental tests are somewhat
different). Revisions allow a single test file to be used for multiple
tests. This is done by adding a special header at the top of the file:

```
// revisions: foo bar baz
```

This will result in the test being compiled (and tested) three times,
once with `--cfg foo`, once with `--cfg bar`, and once with `--cfg
baz`. You can therefore use `#[cfg(foo)]` etc within the test to tweak
each of these results.

You can also customize headers and expected error messages to a particular
revision. To do this, add `[foo]` (or `bar`, `baz`, etc) after the `//`
comment, like so:

```
// A flag to pass in only for cfg `foo`:
//[foo]compile-flags: -Z verbose

#[cfg(foo)]
fn test_foo() {
let x: usize = 32_u32; //[foo]~ ERROR mismatched types
}
```

Note that not all headers have meaning when customized too a revision.
For example, the `ignore-test` header (and all "ignore" headers)
currently only apply to the test as a whole, not to particular
revisions. The only headers that are intended to really work when
customized to a revision are error patterns and compiler flags.
18 changes: 16 additions & 2 deletions src/compiletest/compiletest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,11 +354,25 @@ pub fn is_test(config: &Config, testfile: &Path) -> bool {
}

pub fn make_test(config: &Config, testpaths: &TestPaths) -> test::TestDescAndFn {
let early_props = header::early_props(config, &testpaths.file);

// The `should-fail` annotation doesn't apply to pretty tests,
// since we run the pretty printer across all tests by default.
// If desired, we could add a `should-fail-pretty` annotation.
let should_panic = match config.mode {
Pretty => test::ShouldPanic::No,
_ => if early_props.should_fail {
test::ShouldPanic::Yes
} else {
test::ShouldPanic::No
}
};

test::TestDescAndFn {
desc: test::TestDesc {
name: make_test_name(config, testpaths),
ignore: header::is_test_ignored(config, &testpaths.file),
should_panic: test::ShouldPanic::No,
ignore: early_props.ignore,
should_panic: should_panic,
},
testfn: make_test_closure(config, testpaths),
}
Expand Down
55 changes: 35 additions & 20 deletions src/compiletest/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ enum WhichLine { ThisLine, FollowPrevious(usize), AdjustBackward(usize) }
/// Goal is to enable tests both like: //~^^^ ERROR go up three
/// and also //~^ ERROR message one for the preceding line, and
/// //~| ERROR message two for that same line.
// Load any test directives embedded in the file
pub fn load_errors(testfile: &Path) -> Vec<ExpectedError> {
///
/// If cfg is not None (i.e., in an incremental test), then we look
/// for `//[X]~` instead, where `X` is the current `cfg`.
pub fn load_errors(testfile: &Path, cfg: Option<&str>) -> Vec<ExpectedError> {
let rdr = BufReader::new(File::open(testfile).unwrap());

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

rdr.lines().enumerate().filter_map(|(line_no, ln)| {
parse_expected(last_nonfollow_error,
line_no + 1,
&ln.unwrap())
.map(|(which, error)| {
match which {
FollowPrevious(_) => {}
_ => last_nonfollow_error = Some(error.line),
}
error
})
}).collect()
let tag = match cfg {
Some(rev) => format!("//[{}]~", rev),
None => format!("//~")
};

rdr.lines()
.enumerate()
.filter_map(|(line_no, ln)| {
parse_expected(last_nonfollow_error,
line_no + 1,
&ln.unwrap(),
&tag)
.map(|(which, error)| {
match which {
FollowPrevious(_) => {}
_ => last_nonfollow_error = Some(error.line),
}
error
})
})
.collect()
}

fn parse_expected(last_nonfollow_error: Option<usize>,
line_num: usize,
line: &str) -> Option<(WhichLine, ExpectedError)> {
let start = match line.find("//~") { Some(i) => i, None => return None };
let (follow, adjusts) = if line.char_at(start + 3) == '|' {
line: &str,
tag: &str)
-> Option<(WhichLine, ExpectedError)> {
let start = match line.find(tag) { Some(i) => i, None => return None };
let (follow, adjusts) = if line.char_at(start + tag.len()) == '|' {
(true, 0)
} else {
(false, line[start + 3..].chars().take_while(|c| *c == '^').count())
(false, line[start + tag.len()..].chars().take_while(|c| *c == '^').count())
};
let kind_start = start + 3 + adjusts + (follow as usize);
let kind_start = start + tag.len() + adjusts + (follow as usize);
let letters = line[kind_start..].chars();
let kind = letters.skip_while(|c| c.is_whitespace())
.take_while(|c| !c.is_whitespace())
Expand All @@ -91,7 +104,9 @@ fn parse_expected(last_nonfollow_error: Option<usize>,
(which, line)
};

debug!("line={} which={:?} kind={:?} msg={:?}", line_num, which, kind, msg);
debug!("line={} tag={:?} which={:?} kind={:?} msg={:?}",
line_num, tag, which, kind, msg);

Some((which, ExpectedError { line: line,
kind: kind,
msg: msg, }))
Expand Down
Loading