rust/src/libstd/test.rs

573 lines
16 KiB
Rust
Raw Normal View History

2012-03-07 18:17:30 -08:00
#[doc(hidden)];
// Support code for rustc's built in test runner generator. Currently,
// none of this is meant for users. It is intended to support the
// simplest interface possible for representing and running tests
// while providing a base that other test frameworks may build off of.
2012-09-04 11:23:53 -07:00
use core::cmp::Eq;
use either::Either;
use result::{Ok, Err};
use io::WriterUtil;
use libc::size_t;
use task::TaskBuilder;
use comm = core::comm;
2012-09-04 18:05:57 -07:00
export TestName;
export TestFn;
export TestDesc;
export test_main;
2012-09-04 18:05:57 -07:00
export TestResult;
export TestOpts;
export TrOk;
export TrFailed;
export TrIgnored;
The Big Test Suite Overhaul This replaces the make-based test runner with a set of Rust-based test runners. I believe that all existing functionality has been preserved. The primary objective is to dogfood the Rust test framework. A few main things happen here: 1) The run-pass/lib-* tests are all moved into src/test/stdtest. This is a standalone test crate intended for all standard library tests. It compiles to build/test/stdtest.stageN. 2) rustc now compiles into yet another build artifact, this one a test runner that runs any tests contained directly in the rustc crate. This allows much more fine-grained unit testing of the compiler. It compiles to build/test/rustctest.stageN. 3) There is a new custom test runner crate at src/test/compiletest that reproduces all the functionality for running the compile-fail, run-fail, run-pass and bench tests while integrating with Rust's test framework. It compiles to build/test/compiletest.stageN. 4) The build rules have been completely changed to use the new test runners, while also being less redundant, following the example of the recent stageN.mk rewrite. It adds two new features to the cfail/rfail/rpass/bench tests: 1) Tests can specify multiple 'error-pattern' directives which must be satisfied in order. 2) Tests can specify a 'compile-flags' directive which will make the test runner provide additional command line arguments to rustc. There are some downsides, the primary being that Rust has to be functioning pretty well just to run _any_ tests, which I imagine will be the source of some frustration when the entire test suite breaks. Will also cause some headaches during porting. Not having individual make rules, each rpass, etc test no longer remembers between runs whether it completed successfully. As a result, it's not possible to incrementally fix multiple tests by just running 'make check', fixing a test, and repeating without re-running all the tests contained in the test runner. Instead you can filter just the tests you want to run by using the TESTNAME environment variable. This also dispenses with the ability to run stage0 tests, but they tended to be broken more often than not anyway.
2011-07-12 19:01:09 -07:00
export run_tests_console;
#[abi = "cdecl"]
extern mod rustrt {
fn sched_threads() -> libc::size_t;
}
// The name of a test. By convention this follows the rules for rust
// paths; i.e. it should be a series of identifiers seperated by double
// colons. This way if some test runner wants to arrange the tests
// hierarchically it may.
2012-09-04 18:05:57 -07:00
type TestName = ~str;
// A function that runs a test. If the function returns successfully,
// the test succeeds; if the function fails then the test fails. We
// may need to come up with a more clever definition of test in order
// to support isolation of tests into tasks.
2012-09-04 18:05:57 -07:00
type TestFn = fn~();
// The definition of a single test. A test runner will run a list of
// these.
2012-09-04 18:05:57 -07:00
type TestDesc = {
name: TestName,
fn: TestFn,
ignore: bool,
should_fail: bool
};
// The default console test runner. It accepts the command line
// arguments and a vector of test_descs (generated at compile time).
2012-09-04 18:05:57 -07:00
fn test_main(args: ~[~str], tests: ~[TestDesc]) {
2011-07-27 14:19:39 +02:00
let opts =
2012-08-06 12:34:08 -07:00
match parse_opts(args) {
2012-08-14 16:54:13 -07:00
either::Left(o) => o,
either::Right(m) => fail m
2011-07-27 14:19:39 +02:00
};
if !run_tests_console(opts, tests) { fail ~"Some tests failed"; }
}
2012-09-04 18:05:57 -07:00
type TestOpts = {filter: Option<~str>, run_ignored: bool,
2012-08-20 12:23:37 -07:00
logfile: Option<~str>};
2012-09-04 18:05:57 -07:00
type OptRes = Either<TestOpts, ~str>;
// Parses command line arguments into test options
2012-09-04 18:05:57 -07:00
fn parse_opts(args: ~[~str]) -> OptRes {
2011-08-31 16:56:38 -07:00
let args_ = vec::tail(args);
let opts = ~[getopts::optflag(~"ignored"), getopts::optopt(~"logfile")];
let matches =
2012-08-06 12:34:08 -07:00
match getopts::getopts(args_, opts) {
2012-08-26 16:54:31 -07:00
Ok(m) => m,
Err(f) => return either::Right(getopts::fail_str(f))
2011-07-27 14:19:39 +02:00
};
let filter =
if vec::len(matches.free) > 0u {
2012-08-20 12:23:37 -07:00
option::Some(matches.free[0])
} else { option::None };
let run_ignored = getopts::opt_present(matches, ~"ignored");
let logfile = getopts::opt_maybe_str(matches, ~"logfile");
let test_opts = {filter: filter, run_ignored: run_ignored,
logfile: logfile};
2012-08-14 16:54:13 -07:00
return either::Left(test_opts);
}
2012-09-04 18:05:57 -07:00
enum TestResult { TrOk, TrFailed, TrIgnored, }
2012-09-04 18:05:57 -07:00
impl TestResult : Eq {
pure fn eq(&&other: TestResult) -> bool {
2012-08-30 12:25:48 -07:00
(self as uint) == (other as uint)
}
pure fn ne(&&other: TestResult) -> bool { !self.eq(other) }
2012-08-30 12:25:48 -07:00
}
2012-09-04 18:05:57 -07:00
type ConsoleTestState =
2012-08-14 13:38:35 -07:00
@{out: io::Writer,
2012-08-20 12:23:37 -07:00
log_out: Option<io::Writer>,
use_color: bool,
2012-03-26 18:35:18 -07:00
mut total: uint,
mut passed: uint,
mut failed: uint,
mut ignored: uint,
2012-09-04 18:05:57 -07:00
mut failures: ~[TestDesc]};
2012-03-12 17:31:03 -07:00
// A simple console test runner
2012-09-04 18:05:57 -07:00
fn run_tests_console(opts: TestOpts,
tests: ~[TestDesc]) -> bool {
2012-09-04 18:05:57 -07:00
fn callback(event: TestEvent, st: ConsoleTestState) {
debug!("callback(event=%?)", event);
2012-08-06 12:34:08 -07:00
match event {
2012-09-04 18:05:57 -07:00
TeFiltered(filtered_tests) => {
2011-08-15 16:38:23 -07:00
st.total = vec::len(filtered_tests);
let noun = if st.total != 1u { ~"tests" } else { ~"test" };
2012-08-22 17:24:52 -07:00
st.out.write_line(fmt!("\nrunning %u %s", st.total, noun));
}
2012-09-04 18:05:57 -07:00
TeWait(test) => st.out.write_str(fmt!("test %s ... ", test.name)),
TeResult(test, result) => {
2012-08-06 12:34:08 -07:00
match st.log_out {
2012-08-20 12:23:37 -07:00
Some(f) => write_log(f, result, test),
None => ()
}
2012-08-06 12:34:08 -07:00
match result {
2012-09-04 18:05:57 -07:00
TrOk => {
st.passed += 1u;
write_ok(st.out, st.use_color);
st.out.write_line(~"");
}
2012-09-04 18:05:57 -07:00
TrFailed => {
st.failed += 1u;
write_failed(st.out, st.use_color);
st.out.write_line(~"");
vec::push(st.failures, copy test);
}
2012-09-04 18:05:57 -07:00
TrIgnored => {
st.ignored += 1u;
write_ignored(st.out, st.use_color);
st.out.write_line(~"");
}
}
2011-07-27 14:19:39 +02:00
}
}
}
2012-08-06 12:34:08 -07:00
let log_out = match opts.logfile {
2012-08-20 12:23:37 -07:00
Some(path) => match io::file_writer(&Path(path),
2012-08-14 13:38:35 -07:00
~[io::Create, io::Truncate]) {
2012-08-26 16:54:31 -07:00
result::Ok(w) => Some(w),
result::Err(s) => {
2012-08-22 17:24:52 -07:00
fail(fmt!("can't open output file: %s", s))
2012-08-03 19:59:04 -07:00
}
},
2012-08-20 12:23:37 -07:00
None => None
};
let st =
@{out: io::stdout(),
log_out: log_out,
use_color: use_color(),
mut total: 0u,
mut passed: 0u,
mut failed: 0u,
mut ignored: 0u,
mut failures: ~[]};
2012-06-30 16:19:07 -07:00
run_tests(opts, tests, |x| callback(x, st));
assert (st.passed + st.failed + st.ignored == st.total);
let success = st.failed == 0u;
2011-07-27 14:19:39 +02:00
if !success {
2012-03-12 17:31:03 -07:00
print_failures(st);
}
2012-08-22 17:24:52 -07:00
st.out.write_str(fmt!("\nresult: "));
if success {
// There's no parallelism at this point so it's safe to use color
write_ok(st.out, true);
} else { write_failed(st.out, true); }
2012-08-22 17:24:52 -07:00
st.out.write_str(fmt!(". %u passed; %u failed; %u ignored\n\n", st.passed,
st.failed, st.ignored));
2012-08-01 17:30:05 -07:00
return success;
2012-09-04 18:05:57 -07:00
fn write_log(out: io::Writer, result: TestResult, test: TestDesc) {
2012-08-22 17:24:52 -07:00
out.write_line(fmt!("%s %s",
2012-08-06 12:34:08 -07:00
match result {
2012-09-04 18:05:57 -07:00
TrOk => ~"ok",
TrFailed => ~"failed",
TrIgnored => ~"ignored"
2012-08-22 17:24:52 -07:00
}, test.name));
}
2012-08-14 13:38:35 -07:00
fn write_ok(out: io::Writer, use_color: bool) {
write_pretty(out, ~"ok", term::color_green, use_color);
2011-07-27 14:19:39 +02:00
}
2012-08-14 13:38:35 -07:00
fn write_failed(out: io::Writer, use_color: bool) {
write_pretty(out, ~"FAILED", term::color_red, use_color);
}
2012-08-14 13:38:35 -07:00
fn write_ignored(out: io::Writer, use_color: bool) {
write_pretty(out, ~"ignored", term::color_yellow, use_color);
2011-07-15 00:31:00 -07:00
}
2012-08-14 13:38:35 -07:00
fn write_pretty(out: io::Writer, word: ~str, color: u8, use_color: bool) {
if use_color && term::color_supported() {
term::fg(out, color);
}
out.write_str(word);
if use_color && term::color_supported() {
term::reset(out);
}
}
}
2012-09-04 18:05:57 -07:00
fn print_failures(st: ConsoleTestState) {
st.out.write_line(~"\nfailures:");
let failures = copy st.failures;
2012-06-30 16:19:07 -07:00
let failures = vec::map(failures, |test| test.name);
let failures = sort::merge_sort(|x, y| str::le(*x, *y), failures);
2012-06-30 16:19:07 -07:00
for vec::each(failures) |name| {
2012-08-22 17:24:52 -07:00
st.out.write_line(fmt!(" %s", name));
2012-03-12 17:31:03 -07:00
}
}
#[test]
fn should_sort_failures_before_printing_them() {
let buffer = io::mem_buffer();
2012-03-12 17:31:03 -07:00
let writer = io::mem_buffer_writer(buffer);
let test_a = {
name: ~"a",
2012-03-12 17:31:03 -07:00
fn: fn~() { },
ignore: false,
should_fail: false
};
let test_b = {
name: ~"b",
2012-03-12 17:31:03 -07:00
fn: fn~() { },
ignore: false,
should_fail: false
};
let st =
@{out: writer,
2012-08-20 12:23:37 -07:00
log_out: option::None,
use_color: false,
2012-03-26 18:35:18 -07:00
mut total: 0u,
mut passed: 0u,
mut failed: 0u,
mut ignored: 0u,
mut failures: ~[test_b, test_a]};
2012-03-12 17:31:03 -07:00
print_failures(st);
let s = io::mem_buffer_str(buffer);
let apos = option::get(str::find_str(s, ~"a"));
let bpos = option::get(str::find_str(s, ~"b"));
2012-03-12 17:31:03 -07:00
assert apos < bpos;
}
2012-08-01 17:30:05 -07:00
fn use_color() -> bool { return get_concurrency() == 1u; }
2012-09-04 18:05:57 -07:00
enum TestEvent {
TeFiltered(~[TestDesc]),
TeWait(TestDesc),
TeResult(TestDesc, TestResult),
}
2012-09-04 18:05:57 -07:00
type MonitorMsg = (TestDesc, TestResult);
2012-09-04 18:05:57 -07:00
fn run_tests(opts: TestOpts, tests: ~[TestDesc],
callback: fn@(TestEvent)) {
let mut filtered_tests = filter_tests(opts, tests);
2012-09-04 18:05:57 -07:00
callback(TeFiltered(copy filtered_tests));
2012-01-19 13:44:07 -08:00
// It's tempting to just spawn all the tests at once, but since we have
// many tests that run in other processes we would be making a big mess.
let concurrency = get_concurrency();
2012-08-22 17:24:52 -07:00
debug!("using %u test tasks", concurrency);
2011-08-15 16:38:23 -07:00
let total = vec::len(filtered_tests);
let mut run_idx = 0;
let mut wait_idx = 0;
let mut done_idx = 0;
2012-08-27 14:22:25 -07:00
let p = core::comm::Port();
let ch = core::comm::Chan(p);
while done_idx < total {
while wait_idx < concurrency && run_idx < total {
let test = copy filtered_tests[run_idx];
if concurrency == 1 {
// We are doing one test at a time so we can print the name
// of the test before we run it. Useful for debugging tests
// that hang forever.
2012-09-04 18:05:57 -07:00
callback(TeWait(copy test));
}
run_test(move test, ch);
wait_idx += 1;
run_idx += 1;
}
2012-08-14 14:17:27 -07:00
let (test, result) = core::comm::recv(p);
if concurrency != 1 {
2012-09-04 18:05:57 -07:00
callback(TeWait(copy test));
}
callback(TeResult(move test, result));
wait_idx -= 1;
done_idx += 1;
}
}
// Windows tends to dislike being overloaded with threads.
#[cfg(windows)]
const sched_overcommit : uint = 1u;
#[cfg(unix)]
const sched_overcommit : uint = 4u;
fn get_concurrency() -> uint {
let threads = rustrt::sched_threads() as uint;
if threads == 1u { 1u }
else { threads * sched_overcommit }
}
#[allow(non_implicitly_copyable_typarams)]
2012-09-04 18:05:57 -07:00
fn filter_tests(opts: TestOpts,
tests: ~[TestDesc]) -> ~[TestDesc] {
let mut filtered = copy tests;
// Remove tests that don't match the test filter
filtered = if option::is_none(opts.filter) {
move filtered
} else {
let filter_str =
2012-08-06 12:34:08 -07:00
match opts.filter {
2012-08-20 12:23:37 -07:00
option::Some(f) => f,
option::None => ~""
2011-07-27 14:19:39 +02:00
};
2012-09-04 18:05:57 -07:00
fn filter_fn(test: TestDesc, filter_str: ~str) ->
Option<TestDesc> {
if str::contains(test.name, filter_str) {
2012-08-20 12:23:37 -07:00
return option::Some(copy test);
} else { return option::None; }
}
vec::filter_map(filtered, |x| filter_fn(x, filter_str))
};
// Maybe pull out the ignored test and unignore them
filtered = if !opts.run_ignored {
move filtered
} else {
2012-09-04 18:05:57 -07:00
fn filter(test: TestDesc) -> Option<TestDesc> {
if test.ignore {
2012-08-20 12:23:37 -07:00
return option::Some({name: test.name,
fn: copy test.fn,
ignore: false,
should_fail: test.should_fail});
2012-08-20 12:23:37 -07:00
} else { return option::None; }
};
2012-06-30 16:19:07 -07:00
vec::filter_map(filtered, |x| filter(x))
};
// Sort the tests alphabetically
filtered = {
2012-09-04 18:05:57 -07:00
pure fn lteq(t1: &TestDesc, t2: &TestDesc) -> bool {
str::le(t1.name, t2.name)
}
sort::merge_sort(lteq, filtered)
};
move filtered
}
2012-09-04 18:05:57 -07:00
type TestFuture = {test: TestDesc, wait: fn@() -> TestResult};
2012-09-04 18:05:57 -07:00
fn run_test(+test: TestDesc, monitor_ch: comm::Chan<MonitorMsg>) {
if test.ignore {
2012-09-04 18:05:57 -07:00
core::comm::send(monitor_ch, (copy test, TrIgnored));
2012-08-01 17:30:05 -07:00
return;
}
do task::spawn |move test| {
let testfn = copy test.fn;
2012-08-20 12:23:37 -07:00
let mut result_future = None; // task::future_result(builder);
task::task().unlinked().future_result(|+r| {
result_future = Some(move r);
}).spawn(move testfn);
2012-08-13 19:06:57 -07:00
let task_result = future::get(&option::unwrap(result_future));
2012-08-15 14:10:46 -07:00
let test_result = calc_result(test, task_result == task::Success);
comm::send(monitor_ch, (copy test, test_result));
};
}
2012-09-04 18:05:57 -07:00
fn calc_result(test: TestDesc, task_succeeded: bool) -> TestResult {
if task_succeeded {
2012-09-04 18:05:57 -07:00
if test.should_fail { TrFailed }
else { TrOk }
} else {
2012-09-04 18:05:57 -07:00
if test.should_fail { TrOk }
else { TrFailed }
}
}
2012-01-17 19:05:07 -08:00
#[cfg(test)]
mod tests {
#[test]
fn do_not_run_ignored_tests() {
fn f() { fail; }
let desc = {
name: ~"whatever",
2012-01-17 19:05:07 -08:00
fn: f,
ignore: true,
should_fail: false
};
2012-08-27 14:22:25 -07:00
let p = core::comm::Port();
let ch = core::comm::Chan(p);
run_test(desc, ch);
2012-08-14 14:17:27 -07:00
let (_, res) = core::comm::recv(p);
2012-09-04 18:05:57 -07:00
assert res != TrOk;
2012-01-17 19:05:07 -08:00
}
#[test]
fn ignored_tests_result_in_ignored() {
fn f() { }
let desc = {
name: ~"whatever",
2012-01-17 19:05:07 -08:00
fn: f,
ignore: true,
should_fail: false
};
2012-08-27 14:22:25 -07:00
let p = core::comm::Port();
let ch = core::comm::Chan(p);
run_test(desc, ch);
2012-08-14 14:17:27 -07:00
let (_, res) = core::comm::recv(p);
2012-09-04 18:05:57 -07:00
assert res == TrIgnored;
2012-01-17 19:05:07 -08:00
}
#[test]
#[ignore(cfg(windows))]
2012-01-17 19:05:07 -08:00
fn test_should_fail() {
fn f() { fail; }
let desc = {
name: ~"whatever",
2012-01-17 19:05:07 -08:00
fn: f,
ignore: false,
should_fail: true
};
2012-08-27 14:22:25 -07:00
let p = core::comm::Port();
let ch = core::comm::Chan(p);
run_test(desc, ch);
2012-08-14 14:17:27 -07:00
let (_, res) = core::comm::recv(p);
2012-09-04 18:05:57 -07:00
assert res == TrOk;
2012-01-17 19:05:07 -08:00
}
#[test]
fn test_should_fail_but_succeeds() {
fn f() { }
let desc = {
name: ~"whatever",
2012-01-17 19:05:07 -08:00
fn: f,
ignore: false,
should_fail: true
};
2012-08-27 14:22:25 -07:00
let p = core::comm::Port();
let ch = core::comm::Chan(p);
run_test(desc, ch);
2012-08-14 14:17:27 -07:00
let (_, res) = core::comm::recv(p);
2012-09-04 18:05:57 -07:00
assert res == TrFailed;
2012-01-17 19:05:07 -08:00
}
#[test]
fn first_free_arg_should_be_a_filter() {
let args = ~[~"progname", ~"filter"];
2012-08-06 12:34:08 -07:00
let opts = match parse_opts(args) {
2012-08-14 16:54:13 -07:00
either::Left(o) => o,
2012-08-03 19:59:04 -07:00
_ => fail ~"Malformed arg in first_free_arg_should_be_a_filter"
};
assert ~"filter" == option::get(opts.filter);
2012-01-17 19:05:07 -08:00
}
#[test]
fn parse_ignored_flag() {
let args = ~[~"progname", ~"filter", ~"--ignored"];
2012-08-06 12:34:08 -07:00
let opts = match parse_opts(args) {
2012-08-14 16:54:13 -07:00
either::Left(o) => o,
2012-08-03 19:59:04 -07:00
_ => fail ~"Malformed arg in parse_ignored_flag"
};
2012-01-17 19:05:07 -08:00
assert (opts.run_ignored);
}
#[test]
fn filter_for_ignored_option() {
// When we run ignored tests the test filter should filter out all the
// unignored tests and flip the ignore flag on the rest to false
2012-08-20 12:23:37 -07:00
let opts = {filter: option::None, run_ignored: true,
logfile: option::None};
2012-01-17 19:05:07 -08:00
let tests =
~[{name: ~"1", fn: fn~() { }, ignore: true, should_fail: false},
{name: ~"2", fn: fn~() { }, ignore: false, should_fail: false}];
let filtered = filter_tests(opts, tests);
2012-01-17 19:05:07 -08:00
assert (vec::len(filtered) == 1u);
assert (filtered[0].name == ~"1");
2012-01-17 19:05:07 -08:00
assert (filtered[0].ignore == false);
}
#[test]
fn sort_tests() {
2012-08-20 12:23:37 -07:00
let opts = {filter: option::None, run_ignored: false,
logfile: option::None};
2012-01-17 19:05:07 -08:00
let names =
~[~"sha1::test", ~"int::test_to_str", ~"int::test_pow",
~"test::do_not_run_ignored_tests",
~"test::ignored_tests_result_in_ignored",
~"test::first_free_arg_should_be_a_filter",
~"test::parse_ignored_flag", ~"test::filter_for_ignored_option",
~"test::sort_tests"];
2012-01-17 19:05:07 -08:00
let tests =
{
let testfn = fn~() { };
let mut tests = ~[];
2012-06-30 16:19:07 -07:00
for vec::each(names) |name| {
2012-06-01 11:10:27 -07:00
let test = {name: name, fn: copy testfn, ignore: false,
2012-01-17 19:05:07 -08:00
should_fail: false};
tests += ~[test];
2012-01-17 19:05:07 -08:00
}
tests
};
let filtered = filter_tests(opts, tests);
2012-01-17 19:05:07 -08:00
let expected =
~[~"int::test_pow", ~"int::test_to_str", ~"sha1::test",
~"test::do_not_run_ignored_tests",
~"test::filter_for_ignored_option",
~"test::first_free_arg_should_be_a_filter",
~"test::ignored_tests_result_in_ignored",
~"test::parse_ignored_flag",
~"test::sort_tests"];
2012-01-17 19:05:07 -08:00
let pairs = vec::zip(expected, filtered);
2012-06-30 16:19:07 -07:00
for vec::each(pairs) |p| { let (a, b) = copy p; assert (a == b.name); }
}
2012-01-17 19:05:07 -08:00
}
// Local Variables:
// mode: rust;
// fill-column: 78;
// indent-tabs-mode: nil
// c-basic-offset: 4
// buffer-file-coding-system: utf-8-unix
// End: