rust/src/libstd/test.rs

472 lines
13 KiB
Rust
Raw Normal View History

// 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.
import result::{ok, err};
import io::writer_util;
import core::ctypes;
export test_name;
export test_fn;
export test_desc;
export test_main;
export test_result;
export test_opts;
export tr_ok;
export tr_failed;
export tr_ignored;
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 21:01:09 -05:00
export run_tests_console;
#[abi = "cdecl"]
native mod rustrt {
fn sched_threads() -> ctypes::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.
2011-09-02 17:34:58 -05:00
type test_name = 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.
type test_fn = fn~();
// The definition of a single test. A test runner will run a list of
// these.
type test_desc = {
name: test_name,
fn: test_fn,
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).
fn test_main(args: [str], tests: [test_desc]) {
2011-08-15 18:38:23 -05:00
check (vec::is_not_empty(args));
2011-07-27 07:19:39 -05:00
let opts =
alt parse_opts(args) {
2011-07-27 07:19:39 -05:00
either::left(o) { o }
either::right(m) { fail m }
};
if !run_tests_console(opts, tests) { fail "Some tests failed"; }
}
2011-09-02 17:34:58 -05:00
type test_opts = {filter: option::t<str>, run_ignored: bool};
2011-09-02 17:34:58 -05:00
type opt_res = either::t<test_opts, str>;
// Parses command line arguments into test options
fn parse_opts(args: [str]) : vec::is_not_empty(args) -> opt_res {
2011-08-31 18:56:38 -05:00
let args_ = vec::tail(args);
2011-09-02 17:34:58 -05:00
let opts = [getopts::optflag("ignored")];
2011-07-27 07:19:39 -05:00
let match =
2011-08-12 01:27:32 -05:00
alt getopts::getopts(args_, opts) {
ok(m) { m }
err(f) { ret either::right(getopts::fail_str(f)) }
2011-07-27 07:19:39 -05:00
};
let filter =
2011-08-15 18:38:23 -05:00
if vec::len(match.free) > 0u {
2011-08-31 18:56:38 -05:00
option::some(match.free[0])
2011-07-27 07:19:39 -05:00
} else { option::none };
2011-09-02 17:34:58 -05:00
let run_ignored = getopts::opt_present(match, "ignored");
2011-07-27 07:19:39 -05:00
let test_opts = {filter: filter, run_ignored: run_ignored};
ret either::left(test_opts);
}
2012-01-19 17:20:57 -06:00
enum test_result { tr_ok; tr_failed; tr_ignored; }
// A simple console test runner
fn run_tests_console(opts: test_opts,
tests: [test_desc]) -> bool {
type test_state =
@{out: io::writer,
use_color: bool,
mutable total: uint,
mutable passed: uint,
mutable failed: uint,
mutable ignored: uint,
mutable failures: [test_desc]};
fn callback(event: testevent, st: test_state) {
alt event {
te_filtered(filtered_tests) {
2011-08-15 18:38:23 -05:00
st.total = vec::len(filtered_tests);
2011-09-02 17:34:58 -05:00
st.out.write_line(#fmt["\nrunning %u tests", st.total]);
}
2011-09-02 17:34:58 -05:00
te_wait(test) { st.out.write_str(#fmt["test %s ... ", test.name]); }
te_result(test, result) {
alt result {
tr_ok {
st.passed += 1u;
write_ok(st.out, st.use_color);
2011-09-02 17:34:58 -05:00
st.out.write_line("");
}
tr_failed {
st.failed += 1u;
write_failed(st.out, st.use_color);
2011-09-02 17:34:58 -05:00
st.out.write_line("");
st.failures += [test];
}
tr_ignored {
st.ignored += 1u;
write_ignored(st.out, st.use_color);
2011-09-02 17:34:58 -05:00
st.out.write_line("");
}
}
2011-07-27 07:19:39 -05:00
}
}
}
let st =
@{out: io::stdout(),
use_color: use_color(),
mutable total: 0u,
mutable passed: 0u,
mutable failed: 0u,
mutable ignored: 0u,
mutable failures: []};
run_tests(opts, tests, bind callback(_, st));
assert (st.passed + st.failed + st.ignored == st.total);
let success = st.failed == 0u;
2011-07-27 07:19:39 -05:00
if !success {
2011-09-02 17:34:58 -05:00
st.out.write_line("\nfailures:");
for test: test_desc in st.failures {
let testname = test.name; // Satisfy alias analysis
st.out.write_line(#fmt[" %s", testname]);
}
}
st.out.write_str(#fmt["\nresult: "]);
2011-07-27 07:19:39 -05:00
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); }
2011-09-02 17:34:58 -05:00
st.out.write_str(#fmt[". %u passed; %u failed; %u ignored\n\n", st.passed,
st.failed, st.ignored]);
ret success;
fn write_ok(out: io::writer, use_color: bool) {
2011-09-02 17:34:58 -05:00
write_pretty(out, "ok", term::color_green, use_color);
2011-07-27 07:19:39 -05:00
}
fn write_failed(out: io::writer, use_color: bool) {
2011-09-02 17:34:58 -05:00
write_pretty(out, "FAILED", term::color_red, use_color);
}
fn write_ignored(out: io::writer, use_color: bool) {
2011-09-02 17:34:58 -05:00
write_pretty(out, "ignored", term::color_yellow, use_color);
2011-07-15 02:31:00 -05:00
}
fn write_pretty(out: io::writer, word: str, color: u8, use_color: bool) {
2011-08-12 00:55:08 -05:00
if use_color && term::color_supported() {
term::fg(out, color);
}
out.write_str(word);
2011-08-12 00:55:08 -05:00
if use_color && term::color_supported() {
term::reset(out);
}
}
}
fn use_color() -> bool { ret get_concurrency() == 1u; }
2012-01-19 17:20:57 -06:00
enum testevent {
te_filtered([test_desc]);
te_wait(test_desc);
te_result(test_desc, test_result);
}
type monitor_msg = (test_desc, test_result);
fn run_tests(opts: test_opts, tests: [test_desc],
callback: fn@(testevent)) {
let filtered_tests = filter_tests(opts, tests);
callback(te_filtered(filtered_tests));
2012-01-19 15:44:07 -06: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();
#debug("using %u test tasks", concurrency);
2011-08-15 18:38:23 -05:00
let total = vec::len(filtered_tests);
let run_idx = 0u;
let wait_idx = 0u;
let done_idx = 0u;
let p = comm::port();
let ch = comm::chan(p);
while done_idx < total {
while wait_idx < concurrency && run_idx < total {
run_test(vec::shift(filtered_tests), ch);
wait_idx += 1u;
run_idx += 1u;
}
let (test, result) = comm::recv(p);
callback(te_wait(test));
callback(te_result(test, result));
wait_idx -= 1u;
done_idx += 1u;
}
}
fn get_concurrency() -> uint { rustrt::sched_threads() }
fn filter_tests(opts: test_opts,
tests: [test_desc]) -> [test_desc] {
2011-07-27 07:19:39 -05:00
let filtered = tests;
// Remove tests that don't match the test filter
filtered = if option::is_none(opts.filter) {
filtered
} else {
let filter_str =
alt opts.filter {
option::some(f) { f }
option::none { "" }
2011-07-27 07:19:39 -05:00
};
fn filter_fn(test: test_desc, filter_str: str) ->
option::t<test_desc> {
if str::find(test.name, filter_str) >= 0 {
ret option::some(test);
} else { ret option::none; }
}
let filter = bind filter_fn(_, filter_str);
vec::filter_map(filtered, filter)
};
// Maybe pull out the ignored test and unignore them
filtered = if !opts.run_ignored {
filtered
} else {
fn filter(test: test_desc) -> option::t<test_desc> {
if test.ignore {
ret option::some({name: test.name,
fn: test.fn,
ignore: false,
should_fail: test.should_fail});
} else { ret option::none; }
};
vec::filter_map(filtered, bind filter(_))
};
// Sort the tests alphabetically
2011-07-27 07:19:39 -05:00
filtered =
{
fn lteq(t1: test_desc, t2: test_desc) -> bool {
str::lteq(t1.name, t2.name)
2011-07-27 07:19:39 -05:00
}
sort::merge_sort(bind lteq(_, _), filtered)
2011-07-27 07:19:39 -05:00
};
ret filtered;
}
type test_future = {test: test_desc, wait: fn@() -> test_result};
fn run_test(+test: test_desc, monitor_ch: comm::chan<monitor_msg>) {
if test.ignore {
comm::send(monitor_ch, (test, tr_ignored));
ret;
}
task::spawn {||
let testfn = test.fn;
let test_task = task::spawn_joinable {||
configure_test_task();
testfn();
};
let task_result = task::join(test_task);
let test_result = calc_result(test, task_result == task::tr_success);
comm::send(monitor_ch, (test, test_result));
};
}
fn calc_result(test: test_desc, task_succeeded: bool) -> test_result {
if task_succeeded {
if test.should_fail { tr_failed }
else { tr_ok }
} else {
if test.should_fail { tr_ok }
else { tr_failed }
}
}
// Call from within a test task to make sure it's set up correctly
fn configure_test_task() {
// If this task fails we don't want that failure to propagate to the
// test runner or else we couldn't keep running tests
task::unsupervise();
}
2012-01-17 21:05:07 -06:00
#[cfg(test)]
mod tests {
#[test]
fn do_not_run_ignored_tests() {
fn f() { fail; }
let desc = {
name: "whatever",
fn: f,
ignore: true,
should_fail: false
};
let p = comm::port();
let ch = comm::chan(p);
run_test(desc, ch);
let (_, res) = comm::recv(p);
assert res != tr_ok;
2012-01-17 21:05:07 -06:00
}
#[test]
fn ignored_tests_result_in_ignored() {
fn f() { }
let desc = {
name: "whatever",
fn: f,
ignore: true,
should_fail: false
};
let p = comm::port();
let ch = comm::chan(p);
run_test(desc, ch);
let (_, res) = comm::recv(p);
assert res == tr_ignored;
2012-01-17 21:05:07 -06:00
}
#[test]
#[ignore(cfg(target_os = "win32"))]
fn test_should_fail() {
fn f() { fail; }
let desc = {
name: "whatever",
fn: f,
ignore: false,
should_fail: true
};
let p = comm::port();
let ch = comm::chan(p);
run_test(desc, ch);
let (_, res) = comm::recv(p);
assert res == tr_ok;
2012-01-17 21:05:07 -06:00
}
#[test]
fn test_should_fail_but_succeeds() {
fn f() { }
let desc = {
name: "whatever",
fn: f,
ignore: false,
should_fail: true
};
let p = comm::port();
let ch = comm::chan(p);
run_test(desc, ch);
let (_, res) = comm::recv(p);
assert res == tr_failed;
2012-01-17 21:05:07 -06:00
}
#[test]
fn first_free_arg_should_be_a_filter() {
let args = ["progname", "filter"];
check (vec::is_not_empty(args));
let opts = alt parse_opts(args) { either::left(o) { o } };
2012-01-17 21:05:07 -06:00
assert (str::eq("filter", option::get(opts.filter)));
}
#[test]
fn parse_ignored_flag() {
let args = ["progname", "filter", "--ignored"];
check (vec::is_not_empty(args));
let opts = alt parse_opts(args) { either::left(o) { o } };
2012-01-17 21:05:07 -06: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
let opts = {filter: option::none, run_ignored: true};
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 21:05:07 -06:00
assert (vec::len(filtered) == 1u);
assert (filtered[0].name == "1");
assert (filtered[0].ignore == false);
}
#[test]
fn sort_tests() {
let opts = {filter: option::none, run_ignored: false};
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"];
let tests =
{
let testfn = fn~() { };
2012-01-17 21:05:07 -06:00
let tests = [];
for name: str in names {
let test = {name: name, fn: testfn, ignore: false,
should_fail: false};
tests += [test];
}
tests
};
let filtered = filter_tests(opts, tests);
2012-01-17 21:05:07 -06: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"];
check (vec::same_length(expected, filtered));
let pairs = vec::zip(expected, filtered);
for (a, b) in pairs { assert (a == b.name); }
}
2012-01-17 21:05:07 -06:00
}
// Local Variables:
// mode: rust;
// fill-column: 78;
// indent-tabs-mode: nil
// c-basic-offset: 4
// buffer-file-coding-system: utf-8-unix
// End: