rust/src/compiletest/header.rs

145 lines
4.2 KiB
Rust
Raw Normal View History

use common::config;
use io::ReaderUtil;
export test_props;
export load_props;
export is_test_ignored;
type test_props = {
// Lines that should be expected, in order, on standard out
error_patterns: ~[~str],
// Extra flags to pass to the compiler
2012-08-20 14:23:37 -05:00
compile_flags: Option<~str>,
// If present, the name of a file that this test should match when
// pretty-printed
2012-08-20 14:23:37 -05:00
pp_exact: Option<Path>,
// Modules from aux directory that should be compiled
aux_builds: ~[~str],
// Environment settings to use during execution
exec_env: ~[(~str,~str)]
};
// Load any test directives embedded in the file
fn load_props(testfile: &Path) -> test_props {
let mut error_patterns = ~[];
let mut aux_builds = ~[];
let mut exec_env = ~[];
2012-08-20 14:23:37 -05:00
let mut compile_flags = option::None;
let mut pp_exact = option::None;
2012-06-30 18:19:07 -05:00
for iter_header(testfile) |ln| {
2012-08-06 14:34:08 -05:00
match parse_error_pattern(ln) {
option::Some(ep) => error_patterns.push(ep),
2012-08-20 14:23:37 -05:00
option::None => ()
};
2012-09-21 21:37:57 -05:00
if compile_flags.is_none() {
compile_flags = parse_compile_flags(ln);
}
2012-09-21 21:37:57 -05:00
if pp_exact.is_none() {
pp_exact = parse_pp_exact(ln, testfile);
}
2012-09-21 21:37:57 -05:00
do parse_aux_build(ln).iter |ab| {
aux_builds.push(ab);
}
2012-09-21 21:37:57 -05:00
do parse_exec_env(ln).iter |ee| {
exec_env.push(ee);
}
};
2012-08-01 19:30:05 -05:00
return {
error_patterns: error_patterns,
compile_flags: compile_flags,
pp_exact: pp_exact,
aux_builds: aux_builds,
exec_env: exec_env
};
}
fn is_test_ignored(config: config, testfile: &Path) -> bool {
let mut found = false;
2012-06-30 18:19:07 -05:00
for iter_header(testfile) |ln| {
2012-08-01 19:30:05 -05:00
if parse_name_directive(ln, ~"xfail-test") { return true; }
if parse_name_directive(ln, xfail_target()) { return true; }
if config.mode == common::mode_pretty &&
2012-08-01 19:30:05 -05:00
parse_name_directive(ln, ~"xfail-pretty") { return true; }
};
2012-08-01 19:30:05 -05:00
return found;
fn xfail_target() -> ~str {
~"xfail-" + os::sysname()
}
}
fn iter_header(testfile: &Path, it: fn(~str) -> bool) -> bool {
2012-09-25 18:23:04 -05:00
let rdr = io::file_reader(testfile).get();
while !rdr.eof() {
let ln = rdr.read_line();
// Assume that any directives will be found before the first
// module or function. This doesn't seem to be an optimization
// with a warm page cache. Maybe with a cold one.
if str::starts_with(ln, ~"fn")
|| str::starts_with(ln, ~"mod") {
2012-08-01 19:30:05 -05:00
return false;
} else { if !(it(ln)) { return false; } }
}
2012-08-01 19:30:05 -05:00
return true;
}
2012-08-20 14:23:37 -05:00
fn parse_error_pattern(line: ~str) -> Option<~str> {
parse_name_value_directive(line, ~"error-pattern")
}
2012-08-20 14:23:37 -05:00
fn parse_aux_build(line: ~str) -> Option<~str> {
parse_name_value_directive(line, ~"aux-build")
}
2012-08-20 14:23:37 -05:00
fn parse_compile_flags(line: ~str) -> Option<~str> {
parse_name_value_directive(line, ~"compile-flags")
}
2012-08-20 14:23:37 -05:00
fn parse_exec_env(line: ~str) -> Option<(~str, ~str)> {
do parse_name_value_directive(line, ~"exec-env").map |nv| {
// nv is either FOO or FOO=BAR
let strs = str::splitn_char(nv, '=', 1u);
2012-08-06 14:34:08 -05:00
match strs.len() {
2012-08-03 21:59:04 -05:00
1u => (strs[0], ~""),
2u => (strs[0], strs[1]),
2012-08-22 19:24:52 -05:00
n => fail fmt!("Expected 1 or 2 strings, not %u", n)
}
}
}
2012-08-20 14:23:37 -05:00
fn parse_pp_exact(line: ~str, testfile: &Path) -> Option<Path> {
2012-08-06 14:34:08 -05:00
match parse_name_value_directive(line, ~"pp-exact") {
2012-08-20 14:23:37 -05:00
option::Some(s) => option::Some(Path(s)),
option::None => {
if parse_name_directive(line, ~"pp-exact") {
2012-08-20 14:23:37 -05:00
option::Some(testfile.file_path())
} else {
2012-08-20 14:23:37 -05:00
option::None
}
}
}
}
fn parse_name_directive(line: ~str, directive: ~str) -> bool {
str::contains(line, directive)
}
fn parse_name_value_directive(line: ~str,
2012-08-20 14:23:37 -05:00
directive: ~str) -> Option<~str> unsafe {
let keycolon = directive + ~":";
2012-08-06 14:34:08 -05:00
match str::find_str(line, keycolon) {
2012-08-20 14:23:37 -05:00
option::Some(colon) => {
2012-02-23 03:44:04 -06:00
let value = str::slice(line, colon + str::len(keycolon),
str::len(line));
2012-08-22 19:24:52 -05:00
debug!("%s: %s", directive, value);
2012-08-20 14:23:37 -05:00
option::Some(value)
}
2012-08-20 14:23:37 -05:00
option::None => option::None
}
}