2012-12-03 16:48:01 -08:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2012-03-07 18:17:30 -08:00
|
|
|
#[doc(hidden)];
|
|
|
|
|
2011-07-09 16:08:03 -07:00
|
|
|
// 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-12-23 17:41:37 -05:00
|
|
|
use getopts;
|
|
|
|
use sort;
|
|
|
|
use term;
|
|
|
|
|
2012-09-04 11:23:53 -07:00
|
|
|
use core::cmp::Eq;
|
2012-12-23 17:41:37 -05:00
|
|
|
use core::either::Either;
|
|
|
|
use core::either;
|
|
|
|
use core::io::WriterUtil;
|
|
|
|
use core::io;
|
|
|
|
use core::libc::size_t;
|
2013-01-25 00:52:50 -08:00
|
|
|
use core::pipes::{stream, Chan, Port, SharedChan};
|
2012-12-23 17:41:37 -05:00
|
|
|
use core::option;
|
2013-01-08 19:37:25 -08:00
|
|
|
use core::prelude::*;
|
2012-12-23 17:41:37 -05:00
|
|
|
use core::result;
|
|
|
|
use core::str;
|
|
|
|
use core::task::TaskBuilder;
|
|
|
|
use core::task;
|
|
|
|
use core::vec;
|
2011-07-16 17:04:20 -07:00
|
|
|
|
2011-11-16 22:49:38 -06:00
|
|
|
#[abi = "cdecl"]
|
2012-07-03 16:11:00 -07:00
|
|
|
extern mod rustrt {
|
2013-01-29 12:06:09 -08:00
|
|
|
pub unsafe fn rust_sched_threads() -> size_t;
|
2011-07-25 18:07:25 -07:00
|
|
|
}
|
|
|
|
|
2011-07-09 16:08:03 -07:00
|
|
|
// The name of a test. By convention this follows the rules for rust
|
2011-09-26 10:51:23 -07:00
|
|
|
// paths; i.e. it should be a series of identifiers seperated by double
|
2011-07-09 16:08:03 -07:00
|
|
|
// colons. This way if some test runner wants to arrange the tests
|
2011-09-26 10:51:23 -07:00
|
|
|
// hierarchically it may.
|
2012-10-01 17:32:50 -07:00
|
|
|
pub type TestName = ~str;
|
2011-07-09 16:08:03 -07:00
|
|
|
|
|
|
|
// 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.
|
2013-01-31 17:12:29 -08:00
|
|
|
pub type TestFn = ~fn();
|
2011-07-09 16:08:03 -07:00
|
|
|
|
|
|
|
// The definition of a single test. A test runner will run a list of
|
|
|
|
// these.
|
2013-01-08 14:00:45 -08:00
|
|
|
pub struct TestDesc {
|
2012-09-04 18:05:57 -07:00
|
|
|
name: TestName,
|
2011-11-01 10:31:23 -07:00
|
|
|
ignore: bool,
|
|
|
|
should_fail: bool
|
2013-01-08 14:00:45 -08:00
|
|
|
}
|
2011-07-09 16:08:03 -07:00
|
|
|
|
2013-01-31 17:12:29 -08:00
|
|
|
pub struct TestDescAndFn {
|
|
|
|
desc: TestDesc,
|
|
|
|
testfn: TestFn,
|
|
|
|
}
|
|
|
|
|
2011-07-09 16:08:03 -07:00
|
|
|
// The default console test runner. It accepts the command line
|
|
|
|
// arguments and a vector of test_descs (generated at compile time).
|
2013-01-31 17:12:29 -08:00
|
|
|
pub fn test_main(args: &[~str], tests: ~[TestDescAndFn]) {
|
2011-07-27 14:19:39 +02:00
|
|
|
let opts =
|
2012-08-06 12:34:08 -07:00
|
|
|
match parse_opts(args) {
|
2012-09-28 00:22:18 -07:00
|
|
|
either::Left(move o) => o,
|
2013-01-31 17:51:01 -08:00
|
|
|
either::Right(move m) => die!(m)
|
2011-07-27 14:19:39 +02:00
|
|
|
};
|
2013-01-31 17:51:01 -08:00
|
|
|
if !run_tests_console(&opts, tests) { die!(~"Some tests failed"); }
|
2011-07-09 16:08:03 -07:00
|
|
|
}
|
|
|
|
|
2013-01-22 08:44:24 -08:00
|
|
|
pub struct TestOpts {
|
|
|
|
filter: Option<~str>,
|
|
|
|
run_ignored: bool,
|
|
|
|
logfile: Option<~str>,
|
|
|
|
}
|
2011-07-14 16:05:33 -07:00
|
|
|
|
2012-09-04 18:05:57 -07:00
|
|
|
type OptRes = Either<TestOpts, ~str>;
|
2011-07-14 16:05:33 -07:00
|
|
|
|
|
|
|
// Parses command line arguments into test options
|
2013-01-08 19:37:25 -08:00
|
|
|
pub fn parse_opts(args: &[~str]) -> OptRes {
|
2011-08-31 16:56:38 -07:00
|
|
|
let args_ = vec::tail(args);
|
2012-07-13 22:57:48 -07:00
|
|
|
let opts = ~[getopts::optflag(~"ignored"), getopts::optopt(~"logfile")];
|
2012-07-31 16:38:41 -07:00
|
|
|
let matches =
|
2012-08-06 12:34:08 -07:00
|
|
|
match getopts::getopts(args_, opts) {
|
2012-09-28 00:22:18 -07:00
|
|
|
Ok(move m) => m,
|
|
|
|
Err(move f) => return either::Right(getopts::fail_str(f))
|
2011-07-27 14:19:39 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
let filter =
|
2013-02-04 16:48:52 -08:00
|
|
|
if vec::len(matches.free) > 0 {
|
2012-08-20 12:23:37 -07:00
|
|
|
option::Some(matches.free[0])
|
|
|
|
} else { option::None };
|
2011-07-14 16:05:33 -07:00
|
|
|
|
2012-11-24 12:49:31 -08:00
|
|
|
let run_ignored = getopts::opt_present(&matches, ~"ignored");
|
|
|
|
let logfile = getopts::opt_maybe_str(&matches, ~"logfile");
|
2011-07-14 16:05:33 -07:00
|
|
|
|
2013-01-22 08:44:24 -08:00
|
|
|
let test_opts = TestOpts {
|
|
|
|
filter: filter,
|
|
|
|
run_ignored: run_ignored,
|
|
|
|
logfile: logfile,
|
|
|
|
};
|
2011-07-11 16:33:21 -07:00
|
|
|
|
2013-01-22 08:44:24 -08:00
|
|
|
either::Left(test_opts)
|
2011-07-11 16:33:21 -07:00
|
|
|
}
|
|
|
|
|
2012-12-11 15:16:36 -08:00
|
|
|
#[deriving_eq]
|
2012-10-01 17:32:50 -07:00
|
|
|
pub enum TestResult { TrOk, TrFailed, TrIgnored, }
|
2011-07-14 11:29:54 -07:00
|
|
|
|
2013-02-04 16:48:52 -08:00
|
|
|
struct ConsoleTestState {
|
|
|
|
out: io::Writer,
|
|
|
|
log_out: Option<io::Writer>,
|
|
|
|
use_color: bool,
|
|
|
|
mut total: uint,
|
|
|
|
mut passed: uint,
|
|
|
|
mut failed: uint,
|
|
|
|
mut ignored: uint,
|
|
|
|
mut failures: ~[TestDesc]
|
|
|
|
}
|
2012-03-12 17:31:03 -07:00
|
|
|
|
2011-07-11 16:33:21 -07:00
|
|
|
// A simple console test runner
|
2012-10-01 17:32:50 -07:00
|
|
|
pub fn run_tests_console(opts: &TestOpts,
|
2013-01-31 17:12:29 -08:00
|
|
|
tests: ~[TestDescAndFn]) -> bool {
|
2011-07-11 16:33:21 -07:00
|
|
|
|
2013-02-04 16:48:52 -08:00
|
|
|
fn callback(event: &TestEvent, st: @ConsoleTestState) {
|
2012-08-28 19:31:56 -07:00
|
|
|
debug!("callback(event=%?)", event);
|
2012-09-19 18:51:35 -07:00
|
|
|
match *event {
|
2012-09-28 00:22:18 -07:00
|
|
|
TeFiltered(ref filtered_tests) => {
|
|
|
|
st.total = filtered_tests.len();
|
2013-02-04 16:48:52 -08:00
|
|
|
let noun = if st.total != 1 { ~"tests" } else { ~"test" };
|
2012-08-22 17:24:52 -07:00
|
|
|
st.out.write_line(fmt!("\nrunning %u %s", st.total, noun));
|
2011-08-01 11:26:29 -07:00
|
|
|
}
|
2012-09-28 02:26:20 -07:00
|
|
|
TeWait(ref test) => st.out.write_str(
|
|
|
|
fmt!("test %s ... ", test.name)),
|
2012-09-28 00:22:18 -07:00
|
|
|
TeResult(copy test, result) => {
|
2012-08-06 12:34:08 -07:00
|
|
|
match st.log_out {
|
2012-09-19 18:51:35 -07:00
|
|
|
Some(f) => write_log(f, result, &test),
|
2012-08-20 12:23:37 -07:00
|
|
|
None => ()
|
2012-04-03 23:27:51 +08:00
|
|
|
}
|
2012-08-06 12:34:08 -07:00
|
|
|
match result {
|
2012-09-04 18:05:57 -07:00
|
|
|
TrOk => {
|
2013-02-04 16:48:52 -08:00
|
|
|
st.passed += 1;
|
2012-04-13 18:34:41 +08:00
|
|
|
write_ok(st.out, st.use_color);
|
2012-07-13 22:57:48 -07:00
|
|
|
st.out.write_line(~"");
|
2011-07-29 19:54:05 -07:00
|
|
|
}
|
2012-09-04 18:05:57 -07:00
|
|
|
TrFailed => {
|
2013-02-04 16:48:52 -08:00
|
|
|
st.failed += 1;
|
2012-04-13 18:34:41 +08:00
|
|
|
write_failed(st.out, st.use_color);
|
2012-07-13 22:57:48 -07:00
|
|
|
st.out.write_line(~"");
|
2012-09-18 22:35:42 -07:00
|
|
|
st.failures.push(move test);
|
2011-07-29 19:54:05 -07:00
|
|
|
}
|
2012-09-04 18:05:57 -07:00
|
|
|
TrIgnored => {
|
2013-02-04 16:48:52 -08:00
|
|
|
st.ignored += 1;
|
2012-04-13 18:34:41 +08:00
|
|
|
write_ignored(st.out, st.use_color);
|
2012-07-13 22:57:48 -07:00
|
|
|
st.out.write_line(~"");
|
2011-07-29 19:54:05 -07:00
|
|
|
}
|
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2011-07-11 11:19:32 -07:00
|
|
|
}
|
2011-07-09 16:08:03 -07:00
|
|
|
}
|
|
|
|
|
2012-08-06 12:34:08 -07:00
|
|
|
let log_out = match opts.logfile {
|
2012-09-28 00:22:18 -07:00
|
|
|
Some(ref 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),
|
2012-09-28 00:22:18 -07:00
|
|
|
result::Err(ref s) => {
|
2013-01-31 17:51:01 -08:00
|
|
|
die!(fmt!("can't open output file: %s", *s))
|
2012-08-03 19:59:04 -07:00
|
|
|
}
|
2012-08-06 17:14:32 -07:00
|
|
|
},
|
2012-08-20 12:23:37 -07:00
|
|
|
None => None
|
2012-04-13 18:34:45 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
let st =
|
2013-02-04 16:48:52 -08:00
|
|
|
@ConsoleTestState{out: io::stdout(),
|
2012-04-13 18:34:45 +08:00
|
|
|
log_out: log_out,
|
|
|
|
use_color: use_color(),
|
2013-02-04 16:48:52 -08:00
|
|
|
mut total: 0,
|
|
|
|
mut passed: 0,
|
|
|
|
mut failed: 0,
|
|
|
|
mut ignored: 0,
|
2012-06-29 16:26:56 -07:00
|
|
|
mut failures: ~[]};
|
2012-04-13 18:34:45 +08:00
|
|
|
|
2012-09-19 18:51:35 -07:00
|
|
|
run_tests(opts, tests, |x| callback(&x, st));
|
2012-04-13 18:34:45 +08:00
|
|
|
|
2011-08-19 15:16:48 -07:00
|
|
|
assert (st.passed + st.failed + st.ignored == st.total);
|
2013-02-04 16:48:52 -08:00
|
|
|
let success = st.failed == 0;
|
2011-07-11 11:19:32 -07:00
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
if !success {
|
2012-03-12 17:31:03 -07:00
|
|
|
print_failures(st);
|
2011-07-21 22:26:53 -07:00
|
|
|
}
|
|
|
|
|
2012-08-22 17:24:52 -07:00
|
|
|
st.out.write_str(fmt!("\nresult: "));
|
2012-04-13 18:34:41 +08:00
|
|
|
if success {
|
2012-04-13 18:34:45 +08:00
|
|
|
// There's no parallelism at this point so it's safe to use color
|
2012-04-13 18:34:41 +08:00
|
|
|
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));
|
2011-07-11 11:19:32 -07:00
|
|
|
|
2012-08-01 17:30:05 -07:00
|
|
|
return success;
|
2011-07-11 11:19:32 -07:00
|
|
|
|
2012-09-19 18:51:35 -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-04-03 23:27:51 +08:00
|
|
|
}
|
|
|
|
|
2012-08-14 13:38:35 -07:00
|
|
|
fn write_ok(out: io::Writer, use_color: bool) {
|
2012-07-13 22:57:48 -07:00
|
|
|
write_pretty(out, ~"ok", term::color_green, use_color);
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2011-07-11 11:19:32 -07:00
|
|
|
|
2012-08-14 13:38:35 -07:00
|
|
|
fn write_failed(out: io::Writer, use_color: bool) {
|
2012-07-13 22:57:48 -07:00
|
|
|
write_pretty(out, ~"FAILED", term::color_red, use_color);
|
2011-07-11 11:19:32 -07:00
|
|
|
}
|
2011-07-14 11:29:54 -07:00
|
|
|
|
2012-08-14 13:38:35 -07:00
|
|
|
fn write_ignored(out: io::Writer, use_color: bool) {
|
2012-07-13 22:57:48 -07:00
|
|
|
write_pretty(out, ~"ignored", term::color_yellow, use_color);
|
2011-07-15 00:31:00 -07:00
|
|
|
}
|
|
|
|
|
2012-09-19 18:51:35 -07:00
|
|
|
fn write_pretty(out: io::Writer, word: &str, color: u8, use_color: bool) {
|
2012-04-13 18:34:41 +08:00
|
|
|
if use_color && term::color_supported() {
|
2012-01-11 15:15:54 +01:00
|
|
|
term::fg(out, color);
|
2011-07-14 11:29:54 -07:00
|
|
|
}
|
2011-08-31 23:46:44 -07:00
|
|
|
out.write_str(word);
|
2012-04-13 18:34:41 +08:00
|
|
|
if use_color && term::color_supported() {
|
2012-01-11 15:15:54 +01:00
|
|
|
term::reset(out);
|
2011-07-14 11:29:54 -07:00
|
|
|
}
|
|
|
|
}
|
2011-07-09 16:08:03 -07:00
|
|
|
}
|
|
|
|
|
2013-02-04 16:48:52 -08:00
|
|
|
fn print_failures(st: @ConsoleTestState) {
|
2012-07-13 22:57:48 -07:00
|
|
|
st.out.write_line(~"\nfailures:");
|
2012-05-18 10:40:54 -07:00
|
|
|
let failures = copy st.failures;
|
2012-06-30 16:19:07 -07:00
|
|
|
let failures = vec::map(failures, |test| test.name);
|
2012-09-27 19:05:13 -05:00
|
|
|
let failures = do sort::merge_sort(failures) |x, y| { str::le(*x, *y) };
|
2012-09-18 21:41:13 -07:00
|
|
|
for vec::each(failures) |name| {
|
2012-09-18 21:41:37 -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() {
|
2013-01-31 17:12:29 -08:00
|
|
|
fn dummy() {}
|
|
|
|
|
2012-09-14 09:40:28 -07:00
|
|
|
let s = do io::with_str_writer |wr| {
|
2013-01-08 14:00:45 -08:00
|
|
|
let test_a = TestDesc {
|
2012-09-14 09:40:28 -07:00
|
|
|
name: ~"a",
|
|
|
|
ignore: false,
|
|
|
|
should_fail: false
|
|
|
|
};
|
2012-03-12 17:31:03 -07:00
|
|
|
|
2013-01-08 14:00:45 -08:00
|
|
|
let test_b = TestDesc {
|
2012-09-14 09:40:28 -07:00
|
|
|
name: ~"b",
|
|
|
|
ignore: false,
|
|
|
|
should_fail: false
|
|
|
|
};
|
2012-03-12 17:31:03 -07:00
|
|
|
|
2012-09-14 09:40:28 -07:00
|
|
|
let st =
|
2013-02-04 16:48:52 -08:00
|
|
|
@ConsoleTestState{out: wr,
|
2012-09-14 09:40:28 -07:00
|
|
|
log_out: option::None,
|
|
|
|
use_color: false,
|
2013-02-04 16:48:52 -08:00
|
|
|
mut total: 0,
|
|
|
|
mut passed: 0,
|
|
|
|
mut failed: 0,
|
|
|
|
mut ignored: 0,
|
2012-09-18 22:35:42 -07:00
|
|
|
mut failures: ~[move test_b, move test_a]};
|
2012-03-12 17:31:03 -07:00
|
|
|
|
2012-09-14 09:40:28 -07:00
|
|
|
print_failures(st);
|
|
|
|
};
|
2012-03-12 17:31:03 -07:00
|
|
|
|
2012-09-21 19:37:57 -07:00
|
|
|
let apos = str::find_str(s, ~"a").get();
|
|
|
|
let bpos = str::find_str(s, ~"b").get();
|
2012-03-12 17:31:03 -07:00
|
|
|
assert apos < bpos;
|
|
|
|
}
|
|
|
|
|
2013-02-04 16:48:52 -08:00
|
|
|
fn use_color() -> bool { return get_concurrency() == 1; }
|
2012-04-13 18:34:41 +08:00
|
|
|
|
2012-09-04 18:05:57 -07:00
|
|
|
enum TestEvent {
|
|
|
|
TeFiltered(~[TestDesc]),
|
|
|
|
TeWait(TestDesc),
|
|
|
|
TeResult(TestDesc, TestResult),
|
2011-07-29 19:54:05 -07:00
|
|
|
}
|
|
|
|
|
2012-09-04 18:05:57 -07:00
|
|
|
type MonitorMsg = (TestDesc, TestResult);
|
2012-01-19 14:36:11 -08:00
|
|
|
|
2013-01-08 19:37:25 -08:00
|
|
|
fn run_tests(opts: &TestOpts,
|
2013-01-31 17:12:29 -08:00
|
|
|
tests: ~[TestDescAndFn],
|
2012-10-03 12:21:48 -07:00
|
|
|
callback: fn@(e: TestEvent)) {
|
2012-03-14 14:03:56 -04:00
|
|
|
let mut filtered_tests = filter_tests(opts, tests);
|
2013-01-31 17:12:29 -08:00
|
|
|
|
|
|
|
let filtered_descs = filtered_tests.map(|t| t.desc);
|
|
|
|
callback(TeFiltered(filtered_descs));
|
2011-07-29 19:54:05 -07:00
|
|
|
|
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.
|
2011-07-29 19:54:05 -07:00
|
|
|
let concurrency = get_concurrency();
|
2012-08-22 17:24:52 -07:00
|
|
|
debug!("using %u test tasks", concurrency);
|
2012-01-19 14:36:11 -08:00
|
|
|
|
2013-01-31 17:12:29 -08:00
|
|
|
let mut remaining = filtered_tests;
|
|
|
|
vec::reverse(remaining);
|
|
|
|
let mut pending = 0;
|
2011-07-29 19:54:05 -07:00
|
|
|
|
2013-01-25 00:52:50 -08:00
|
|
|
let (p, ch) = stream();
|
|
|
|
let ch = SharedChan(ch);
|
2012-01-19 14:36:11 -08:00
|
|
|
|
2013-01-31 17:12:29 -08:00
|
|
|
while pending > 0 || !remaining.is_empty() {
|
|
|
|
while pending < concurrency && !remaining.is_empty() {
|
|
|
|
let test = remaining.pop();
|
2012-09-10 17:50:48 -07:00
|
|
|
if concurrency == 1 {
|
2012-02-18 16:30:07 -08:00
|
|
|
// 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.
|
2013-01-31 17:12:29 -08:00
|
|
|
callback(TeWait(test.desc));
|
2012-02-18 16:30:07 -08:00
|
|
|
}
|
2013-01-31 17:12:29 -08:00
|
|
|
run_test(test, ch.clone());
|
|
|
|
pending += 1;
|
2011-07-29 19:54:05 -07:00
|
|
|
}
|
|
|
|
|
2013-01-31 17:12:29 -08:00
|
|
|
let (desc, result) = p.recv();
|
2012-09-10 17:50:48 -07:00
|
|
|
if concurrency != 1 {
|
2013-01-31 17:12:29 -08:00
|
|
|
callback(TeWait(desc));
|
2012-02-18 16:30:07 -08:00
|
|
|
}
|
2013-01-31 17:12:29 -08:00
|
|
|
callback(TeResult(desc, result));
|
|
|
|
pending -= 1;
|
2011-07-29 19:54:05 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-07 18:55:02 -08:00
|
|
|
// Windows tends to dislike being overloaded with threads.
|
2012-06-07 21:38:25 -07:00
|
|
|
#[cfg(windows)]
|
2013-02-04 16:48:52 -08:00
|
|
|
const sched_overcommit : uint = 1;
|
2012-02-07 18:55:02 -08:00
|
|
|
|
2012-06-07 21:38:25 -07:00
|
|
|
#[cfg(unix)]
|
2012-02-07 18:55:02 -08:00
|
|
|
const sched_overcommit : uint = 4u;
|
|
|
|
|
2012-01-19 14:43:56 -08:00
|
|
|
fn get_concurrency() -> uint {
|
2013-01-10 21:23:07 -08:00
|
|
|
unsafe {
|
|
|
|
let threads = rustrt::rust_sched_threads() as uint;
|
2013-02-04 16:48:52 -08:00
|
|
|
if threads == 1 { 1 }
|
2013-01-10 21:23:07 -08:00
|
|
|
else { threads * sched_overcommit }
|
|
|
|
}
|
2012-01-19 14:43:56 -08:00
|
|
|
}
|
2011-07-25 15:21:36 -07:00
|
|
|
|
Nomenclature fixes in the lint checker. Fewer double-negatives.
New style is allow(foo), warn(foo), deny(foo) and forbid(foo),
mirrored by -A foo, -W foo, -D foo and -F foo on command line.
These replace -W no-foo, -W foo, -W err-foo, respectively.
Forbid is new, and means "deny, and you can't override it".
2012-07-26 17:08:21 -07:00
|
|
|
#[allow(non_implicitly_copyable_typarams)]
|
2013-01-31 17:12:29 -08:00
|
|
|
pub fn filter_tests(
|
|
|
|
opts: &TestOpts,
|
|
|
|
tests: ~[TestDescAndFn]) -> ~[TestDescAndFn]
|
|
|
|
{
|
|
|
|
let mut filtered = tests;
|
2011-07-11 16:33:21 -07:00
|
|
|
|
2011-07-16 17:04:20 -07:00
|
|
|
// Remove tests that don't match the test filter
|
2012-09-21 19:37:57 -07:00
|
|
|
filtered = if opts.filter.is_none() {
|
2012-09-10 17:50:48 -07:00
|
|
|
move filtered
|
2011-10-13 16:42:43 -07:00
|
|
|
} else {
|
|
|
|
let filter_str =
|
2012-08-06 12:34:08 -07:00
|
|
|
match opts.filter {
|
2012-09-28 00:22:18 -07:00
|
|
|
option::Some(copy f) => f,
|
2012-08-20 12:23:37 -07:00
|
|
|
option::None => ~""
|
2011-07-27 14:19:39 +02:00
|
|
|
};
|
2011-07-14 16:05:33 -07:00
|
|
|
|
2013-01-31 17:12:29 -08:00
|
|
|
fn filter_fn(test: TestDescAndFn, filter_str: &str) ->
|
|
|
|
Option<TestDescAndFn> {
|
|
|
|
if str::contains(test.desc.name, filter_str) {
|
|
|
|
return option::Some(test);
|
2012-08-20 12:23:37 -07:00
|
|
|
} else { return option::None; }
|
2011-10-13 16:42:43 -07:00
|
|
|
}
|
|
|
|
|
2012-09-27 22:20:47 -07:00
|
|
|
vec::filter_map(filtered, |x| filter_fn(x, filter_str))
|
2011-10-13 16:42:43 -07:00
|
|
|
};
|
|
|
|
|
2011-07-16 17:04:20 -07:00
|
|
|
// Maybe pull out the ignored test and unignore them
|
2011-10-13 16:42:43 -07:00
|
|
|
filtered = if !opts.run_ignored {
|
2012-09-10 17:50:48 -07:00
|
|
|
move filtered
|
2011-10-13 16:42:43 -07:00
|
|
|
} else {
|
2013-01-31 17:12:29 -08:00
|
|
|
fn filter(test: TestDescAndFn) -> Option<TestDescAndFn> {
|
|
|
|
if test.desc.ignore {
|
|
|
|
let TestDescAndFn {desc, testfn} = test;
|
|
|
|
Some(TestDescAndFn {
|
|
|
|
desc: TestDesc {ignore: false, ..desc},
|
|
|
|
testfn: testfn
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2011-07-14 16:05:33 -07:00
|
|
|
};
|
|
|
|
|
2012-09-27 22:20:47 -07:00
|
|
|
vec::filter_map(filtered, |x| filter(x))
|
2011-10-13 16:42:43 -07:00
|
|
|
};
|
|
|
|
|
2011-07-16 17:04:20 -07:00
|
|
|
// Sort the tests alphabetically
|
2013-01-31 17:12:29 -08:00
|
|
|
pure fn lteq(t1: &TestDescAndFn, t2: &TestDescAndFn) -> bool {
|
|
|
|
str::le(t1.desc.name, t2.desc.name)
|
|
|
|
}
|
|
|
|
sort::quick_sort(filtered, lteq);
|
2011-07-11 16:33:21 -07:00
|
|
|
|
2012-09-10 17:50:48 -07:00
|
|
|
move filtered
|
2011-07-11 16:33:21 -07:00
|
|
|
}
|
2011-07-09 16:08:03 -07:00
|
|
|
|
2013-01-22 08:44:24 -08:00
|
|
|
struct TestFuture {
|
|
|
|
test: TestDesc,
|
|
|
|
wait: fn@() -> TestResult,
|
|
|
|
}
|
2011-07-25 15:21:36 -07:00
|
|
|
|
2013-01-31 17:12:29 -08:00
|
|
|
pub fn run_test(test: TestDescAndFn, monitor_ch: SharedChan<MonitorMsg>) {
|
|
|
|
let TestDescAndFn {desc, testfn} = test;
|
|
|
|
|
|
|
|
if desc.ignore {
|
|
|
|
monitor_ch.send((desc, TrIgnored));
|
2012-08-01 17:30:05 -07:00
|
|
|
return;
|
2011-11-01 10:31:23 -07:00
|
|
|
}
|
|
|
|
|
2013-01-31 17:12:29 -08:00
|
|
|
let testfn_cell = ::cell::Cell(testfn);
|
|
|
|
do task::spawn {
|
2012-08-20 12:23:37 -07:00
|
|
|
let mut result_future = None; // task::future_result(builder);
|
2012-08-07 14:26:03 -04:00
|
|
|
task::task().unlinked().future_result(|+r| {
|
2012-09-10 17:50:48 -07:00
|
|
|
result_future = Some(move r);
|
2013-01-31 17:12:29 -08:00
|
|
|
}).spawn(testfn_cell.take());
|
2012-10-22 19:01:37 -07:00
|
|
|
let task_result = option::unwrap(move result_future).recv();
|
2013-01-31 17:12:29 -08:00
|
|
|
let test_result = calc_result(&desc, task_result == task::Success);
|
|
|
|
monitor_ch.send((desc, test_result));
|
2012-01-19 14:36:11 -08:00
|
|
|
};
|
2011-07-14 11:29:54 -07:00
|
|
|
}
|
|
|
|
|
2013-01-31 17:12:29 -08:00
|
|
|
fn calc_result(desc: &TestDesc, task_succeeded: bool) -> TestResult {
|
2012-01-19 14:36:11 -08:00
|
|
|
if task_succeeded {
|
2013-01-31 17:12:29 -08:00
|
|
|
if desc.should_fail { TrFailed }
|
2012-09-04 18:05:57 -07:00
|
|
|
else { TrOk }
|
2012-01-19 14:36:11 -08:00
|
|
|
} else {
|
2013-01-31 17:12:29 -08:00
|
|
|
if desc.should_fail { TrOk }
|
2012-09-04 18:05:57 -07:00
|
|
|
else { TrFailed }
|
2012-01-19 14:36:11 -08:00
|
|
|
}
|
2011-07-14 22:24:19 -07:00
|
|
|
}
|
|
|
|
|
2012-01-17 19:05:07 -08:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2013-01-31 17:12:29 -08:00
|
|
|
use test::{TrFailed, TrIgnored, TrOk, filter_tests, parse_opts,
|
|
|
|
TestDesc, TestDescAndFn};
|
2013-01-22 08:44:24 -08:00
|
|
|
use test::{TestOpts, run_test};
|
2013-01-08 19:37:25 -08:00
|
|
|
|
2012-12-27 18:24:18 -08:00
|
|
|
use core::either;
|
2013-01-25 00:52:50 -08:00
|
|
|
use core::pipes::{stream, SharedChan};
|
2012-12-27 18:24:18 -08:00
|
|
|
use core::option;
|
|
|
|
use core::vec;
|
|
|
|
|
2012-01-17 19:05:07 -08:00
|
|
|
#[test]
|
2013-01-29 12:06:09 -08:00
|
|
|
pub fn do_not_run_ignored_tests() {
|
2013-01-31 17:51:01 -08:00
|
|
|
fn f() { die!(); }
|
2013-01-31 17:12:29 -08:00
|
|
|
let desc = TestDescAndFn {
|
|
|
|
desc: TestDesc {
|
|
|
|
name: ~"whatever",
|
|
|
|
ignore: true,
|
|
|
|
should_fail: false
|
|
|
|
},
|
2012-09-12 14:51:38 -07:00
|
|
|
testfn: f,
|
2012-01-17 19:05:07 -08:00
|
|
|
};
|
2013-01-25 00:52:50 -08:00
|
|
|
let (p, ch) = stream();
|
|
|
|
let ch = SharedChan(ch);
|
2012-01-19 14:36:11 -08:00
|
|
|
run_test(desc, ch);
|
2013-01-25 00:52:50 -08:00
|
|
|
let (_, res) = p.recv();
|
2012-09-04 18:05:57 -07:00
|
|
|
assert res != TrOk;
|
2012-01-17 19:05:07 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2013-01-29 12:06:09 -08:00
|
|
|
pub fn ignored_tests_result_in_ignored() {
|
2012-01-17 19:05:07 -08:00
|
|
|
fn f() { }
|
2013-01-31 17:12:29 -08:00
|
|
|
let desc = TestDescAndFn {
|
|
|
|
desc: TestDesc {
|
|
|
|
name: ~"whatever",
|
|
|
|
ignore: true,
|
|
|
|
should_fail: false
|
|
|
|
},
|
2012-09-12 14:51:38 -07:00
|
|
|
testfn: f,
|
2012-01-17 19:05:07 -08:00
|
|
|
};
|
2013-01-25 00:52:50 -08:00
|
|
|
let (p, ch) = stream();
|
|
|
|
let ch = SharedChan(ch);
|
2012-01-19 14:36:11 -08:00
|
|
|
run_test(desc, ch);
|
2013-01-25 00:52:50 -08:00
|
|
|
let (_, res) = p.recv();
|
2012-09-04 18:05:57 -07:00
|
|
|
assert res == TrIgnored;
|
2012-01-17 19:05:07 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2012-06-07 21:38:25 -07:00
|
|
|
#[ignore(cfg(windows))]
|
2013-01-29 12:06:09 -08:00
|
|
|
pub fn test_should_fail() {
|
2013-01-31 17:51:01 -08:00
|
|
|
fn f() { die!(); }
|
2013-01-31 17:12:29 -08:00
|
|
|
let desc = TestDescAndFn {
|
|
|
|
desc: TestDesc {
|
|
|
|
name: ~"whatever",
|
|
|
|
ignore: false,
|
|
|
|
should_fail: true
|
|
|
|
},
|
2012-09-12 14:51:38 -07:00
|
|
|
testfn: f,
|
2012-01-17 19:05:07 -08:00
|
|
|
};
|
2013-01-25 00:52:50 -08:00
|
|
|
let (p, ch) = stream();
|
|
|
|
let ch = SharedChan(ch);
|
2012-01-19 14:36:11 -08:00
|
|
|
run_test(desc, ch);
|
2013-01-25 00:52:50 -08:00
|
|
|
let (_, res) = p.recv();
|
2012-09-04 18:05:57 -07:00
|
|
|
assert res == TrOk;
|
2012-01-17 19:05:07 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2013-01-29 12:06:09 -08:00
|
|
|
pub fn test_should_fail_but_succeeds() {
|
2012-01-17 19:05:07 -08:00
|
|
|
fn f() { }
|
2013-01-31 17:12:29 -08:00
|
|
|
let desc = TestDescAndFn {
|
|
|
|
desc: TestDesc {
|
|
|
|
name: ~"whatever",
|
|
|
|
ignore: false,
|
|
|
|
should_fail: true
|
|
|
|
},
|
2012-09-12 14:51:38 -07:00
|
|
|
testfn: f,
|
2012-01-17 19:05:07 -08:00
|
|
|
};
|
2013-01-25 00:52:50 -08:00
|
|
|
let (p, ch) = stream();
|
|
|
|
let ch = SharedChan(ch);
|
2012-01-19 14:36:11 -08:00
|
|
|
run_test(desc, ch);
|
2013-01-25 00:52:50 -08:00
|
|
|
let (_, res) = p.recv();
|
2012-09-04 18:05:57 -07:00
|
|
|
assert res == TrFailed;
|
2012-01-17 19:05:07 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2013-01-29 12:06:09 -08:00
|
|
|
pub fn first_free_arg_should_be_a_filter() {
|
2012-07-13 22:57:48 -07:00
|
|
|
let args = ~[~"progname", ~"filter"];
|
2012-08-06 12:34:08 -07:00
|
|
|
let opts = match parse_opts(args) {
|
2012-09-28 02:26:20 -07:00
|
|
|
either::Left(copy o) => o,
|
2013-01-31 17:51:01 -08:00
|
|
|
_ => die!(~"Malformed arg in first_free_arg_should_be_a_filter")
|
2012-08-03 19:59:04 -07:00
|
|
|
};
|
2012-09-21 19:37:57 -07:00
|
|
|
assert ~"filter" == opts.filter.get();
|
2012-01-17 19:05:07 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2013-01-29 12:06:09 -08:00
|
|
|
pub fn parse_ignored_flag() {
|
2012-07-13 22:57:48 -07:00
|
|
|
let args = ~[~"progname", ~"filter", ~"--ignored"];
|
2012-08-06 12:34:08 -07:00
|
|
|
let opts = match parse_opts(args) {
|
2012-09-28 02:26:20 -07:00
|
|
|
either::Left(copy o) => o,
|
2013-01-31 17:51:01 -08:00
|
|
|
_ => die!(~"Malformed arg in parse_ignored_flag")
|
2012-08-03 19:59:04 -07:00
|
|
|
};
|
2012-01-17 19:05:07 -08:00
|
|
|
assert (opts.run_ignored);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2013-01-29 12:06:09 -08:00
|
|
|
pub fn filter_for_ignored_option() {
|
2013-01-31 17:12:29 -08:00
|
|
|
fn dummy() {}
|
|
|
|
|
2012-01-17 19:05:07 -08:00
|
|
|
// 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
|
|
|
|
|
2013-01-22 08:44:24 -08:00
|
|
|
let opts = TestOpts {
|
|
|
|
filter: option::None,
|
|
|
|
run_ignored: true,
|
|
|
|
logfile: option::None,
|
|
|
|
};
|
|
|
|
|
|
|
|
let tests = ~[
|
2013-01-31 17:12:29 -08:00
|
|
|
TestDescAndFn {
|
|
|
|
desc: TestDesc {
|
|
|
|
name: ~"1",
|
|
|
|
ignore: true,
|
|
|
|
should_fail: false,
|
|
|
|
},
|
|
|
|
testfn: dummy,
|
2013-01-22 08:44:24 -08:00
|
|
|
},
|
2013-01-31 17:12:29 -08:00
|
|
|
TestDescAndFn {
|
|
|
|
desc: TestDesc {
|
|
|
|
name: ~"2",
|
|
|
|
ignore: false,
|
|
|
|
should_fail: false
|
|
|
|
},
|
|
|
|
testfn: dummy,
|
2013-01-22 08:44:24 -08:00
|
|
|
},
|
|
|
|
];
|
2012-09-19 18:51:35 -07:00
|
|
|
let filtered = filter_tests(&opts, tests);
|
2012-01-17 19:05:07 -08:00
|
|
|
|
2013-02-04 16:48:52 -08:00
|
|
|
assert (vec::len(filtered) == 1);
|
2013-01-31 17:12:29 -08:00
|
|
|
assert (filtered[0].desc.name == ~"1");
|
|
|
|
assert (filtered[0].desc.ignore == false);
|
2012-01-17 19:05:07 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2013-01-29 12:06:09 -08:00
|
|
|
pub fn sort_tests() {
|
2013-01-22 08:44:24 -08:00
|
|
|
let opts = TestOpts {
|
|
|
|
filter: option::None,
|
|
|
|
run_ignored: false,
|
|
|
|
logfile: option::None,
|
|
|
|
};
|
2012-01-17 19:05:07 -08:00
|
|
|
|
|
|
|
let names =
|
2012-07-13 22:57:48 -07:00
|
|
|
~[~"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 =
|
|
|
|
{
|
2013-01-31 17:12:29 -08:00
|
|
|
fn testfn() { }
|
2012-09-18 21:41:37 -07:00
|
|
|
let mut tests = ~[];
|
2012-09-18 21:41:13 -07:00
|
|
|
for vec::each(names) |name| {
|
2013-01-31 17:12:29 -08:00
|
|
|
let test = TestDescAndFn {
|
|
|
|
desc: TestDesc {
|
|
|
|
name: *name, ignore: false,
|
|
|
|
should_fail: false
|
|
|
|
},
|
|
|
|
testfn: testfn,
|
|
|
|
};
|
2012-09-18 22:35:42 -07:00
|
|
|
tests.push(move test);
|
2012-09-18 21:41:37 -07:00
|
|
|
}
|
2012-09-18 22:35:42 -07:00
|
|
|
move tests
|
2012-09-18 21:41:37 -07:00
|
|
|
};
|
2012-09-19 18:51:35 -07:00
|
|
|
let filtered = filter_tests(&opts, tests);
|
2012-01-17 19:05:07 -08:00
|
|
|
|
2012-09-18 21:41:37 -07: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
|
|
|
|
2012-09-18 22:35:42 -07:00
|
|
|
let pairs = vec::zip(expected, move filtered);
|
2012-01-17 19:05:07 -08:00
|
|
|
|
2012-09-18 21:41:37 -07:00
|
|
|
for vec::each(pairs) |p| {
|
|
|
|
match *p {
|
2013-01-31 17:12:29 -08:00
|
|
|
(ref a, ref b) => { assert (*a == b.desc.name); }
|
2012-09-18 21:41:37 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-01-17 19:05:07 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-07-09 16:08:03 -07:00
|
|
|
// Local Variables:
|
|
|
|
// mode: rust;
|
|
|
|
// fill-column: 78;
|
|
|
|
// indent-tabs-mode: nil
|
|
|
|
// c-basic-offset: 4
|
|
|
|
// buffer-file-coding-system: utf-8-unix
|
|
|
|
// End:
|