rust/src/compiletest/header.rs

192 lines
5.6 KiB
Rust
Raw Normal View History

2013-02-09 12:09:19 -06:00
// Copyright 2012-2013 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.
use common::config;
use common;
use util;
pub struct TestProps {
// 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
2013-02-09 12:09:19 -06:00
exec_env: ~[(~str,~str)],
// Commands to be given to the debugger, when testing debug info
debugger_cmds: ~[~str],
// Lines to check if they appear in the expected debugger output
check_lines: ~[~str],
}
// Load any test directives embedded in the file
pub fn load_props(testfile: &Path) -> TestProps {
let mut error_patterns = ~[];
let mut aux_builds = ~[];
let mut exec_env = ~[];
let mut compile_flags = None;
let mut pp_exact = None;
2013-02-09 12:09:19 -06:00
let mut debugger_cmds = ~[];
let mut check_lines = ~[];
do iter_header(testfile) |ln| {
2012-08-06 14:34:08 -05:00
match parse_error_pattern(ln) {
Some(ep) => error_patterns.push(ep),
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);
}
match parse_aux_build(ln) {
Some(ab) => { aux_builds.push(ab); }
None => {}
}
match parse_exec_env(ln) {
Some(ee) => { exec_env.push(ee); }
None => {}
}
2013-02-09 12:09:19 -06:00
match parse_debugger_cmd(ln) {
Some(dc) => debugger_cmds.push(dc),
None => ()
};
match parse_check_line(ln) {
Some(cl) => check_lines.push(cl),
None => ()
};
true
};
return TestProps {
error_patterns: error_patterns,
compile_flags: compile_flags,
pp_exact: pp_exact,
aux_builds: aux_builds,
2013-02-09 12:09:19 -06:00
exec_env: exec_env,
debugger_cmds: debugger_cmds,
check_lines: check_lines
};
}
pub fn is_test_ignored(config: &config, testfile: &Path) -> bool {
fn xfail_target(config: &config) -> ~str {
~"xfail-" + util::get_os(config.target)
}
let val = do iter_header(testfile) |ln| {
if parse_name_directive(ln, "xfail-test") { false }
else if parse_name_directive(ln, xfail_target(config)) { false }
else if config.mode == common::mode_pretty &&
parse_name_directive(ln, "xfail-pretty") { false }
else { true }
};
!val
}
2013-10-06 18:08:56 -05:00
fn iter_header(testfile: &Path, it: &fn(&str) -> bool) -> bool {
use std::rt::io::Open;
use std::rt::io::file::FileInfo;
use std::rt::io::buffered::BufferedReader;
let mut rdr = BufferedReader::new(testfile.open_reader(Open).unwrap());
loop {
let ln = match rdr.read_line() {
Some(ln) => ln, None => break
};
// 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 ln.starts_with("fn") || ln.starts_with("mod") {
return true;
2013-10-06 18:08:56 -05:00
} else { if !(it(ln.trim())) { return false; } }
}
2012-08-01 19:30:05 -05:00
return true;
}
fn parse_error_pattern(line: &str) -> Option<~str> {
parse_name_value_directive(line, ~"error-pattern")
}
fn parse_aux_build(line: &str) -> Option<~str> {
parse_name_value_directive(line, ~"aux-build")
}
fn parse_compile_flags(line: &str) -> Option<~str> {
parse_name_value_directive(line, ~"compile-flags")
}
fn parse_debugger_cmd(line: &str) -> Option<~str> {
2013-02-09 12:09:19 -06:00
parse_name_value_directive(line, ~"debugger")
}
fn parse_check_line(line: &str) -> Option<~str> {
2013-02-09 12:09:19 -06:00
parse_name_value_directive(line, ~"check")
}
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 mut strs: ~[~str] = nv.splitn_iter('=', 1).map(|s| s.to_owned()).collect();
2012-08-06 14:34:08 -05:00
match strs.len() {
1u => (strs.pop(), ~""),
2u => {
let end = strs.pop();
(strs.pop(), end)
}
2013-09-29 15:18:51 -05:00
n => fail2!("Expected 1 or 2 strings, not {}", n)
}
}
}
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") {
Some(s) => Some(Path::new(s)),
None => {
if parse_name_directive(line, "pp-exact") {
testfile.filename().map(|s| Path::new(s))
} else {
None
}
}
}
}
fn parse_name_directive(line: &str, directive: &str) -> bool {
line.contains(directive)
}
fn parse_name_value_directive(line: &str,
directive: ~str) -> Option<~str> {
let keycolon = directive + ":";
match line.find_str(keycolon) {
Some(colon) => {
let value = line.slice(colon + keycolon.len(),
line.len()).to_owned();
2013-09-29 15:18:51 -05:00
debug2!("{}: {}", directive, value);
Some(value)
}
None => None
}
}