1 // Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
15 pub struct TestProps {
16 // Lines that should be expected, in order, on standard out
17 error_patterns: ~[~str],
18 // Extra flags to pass to the compiler
19 compile_flags: Option<~str>,
20 // If present, the name of a file that this test should match when
22 pp_exact: Option<Path>,
23 // Modules from aux directory that should be compiled
25 // Environment settings to use during execution
26 exec_env: ~[(~str,~str)],
27 // Commands to be given to the debugger, when testing debug info
28 debugger_cmds: ~[~str],
29 // Lines to check if they appear in the expected debugger output
33 // Load any test directives embedded in the file
34 pub fn load_props(testfile: &Path) -> TestProps {
35 let mut error_patterns = ~[];
36 let mut aux_builds = ~[];
37 let mut exec_env = ~[];
38 let mut compile_flags = None;
39 let mut pp_exact = None;
40 let mut debugger_cmds = ~[];
41 let mut check_lines = ~[];
42 iter_header(testfile, |ln| {
43 match parse_error_pattern(ln) {
44 Some(ep) => error_patterns.push(ep),
48 if compile_flags.is_none() {
49 compile_flags = parse_compile_flags(ln);
52 if pp_exact.is_none() {
53 pp_exact = parse_pp_exact(ln, testfile);
56 match parse_aux_build(ln) {
57 Some(ab) => { aux_builds.push(ab); }
61 match parse_exec_env(ln) {
62 Some(ee) => { exec_env.push(ee); }
66 match parse_debugger_cmd(ln) {
67 Some(dc) => debugger_cmds.push(dc),
71 match parse_check_line(ln) {
72 Some(cl) => check_lines.push(cl),
79 error_patterns: error_patterns,
80 compile_flags: compile_flags,
82 aux_builds: aux_builds,
84 debugger_cmds: debugger_cmds,
85 check_lines: check_lines
89 pub fn is_test_ignored(config: &config, testfile: &Path) -> bool {
90 fn xfail_target(config: &config) -> ~str {
91 ~"xfail-" + util::get_os(config.target)
93 fn xfail_stage(config: &config) -> ~str {
94 ~"xfail-" + config.stage_id.split('-').next().unwrap()
97 let val = iter_header(testfile, |ln| {
98 if parse_name_directive(ln, "xfail-test") { false }
99 else if parse_name_directive(ln, xfail_target(config)) { false }
100 else if parse_name_directive(ln, xfail_stage(config)) { false }
101 else if config.mode == common::mode_pretty &&
102 parse_name_directive(ln, "xfail-pretty") { false }
109 fn iter_header(testfile: &Path, it: |&str| -> bool) -> bool {
110 use std::io::{BufferedReader, File};
112 let mut rdr = BufferedReader::new(File::open(testfile).unwrap());
113 for ln in rdr.lines() {
114 // Assume that any directives will be found before the first
115 // module or function. This doesn't seem to be an optimization
116 // with a warm page cache. Maybe with a cold one.
117 if ln.starts_with("fn") || ln.starts_with("mod") {
119 } else { if !(it(ln.trim())) { return false; } }
124 fn parse_error_pattern(line: &str) -> Option<~str> {
125 parse_name_value_directive(line, ~"error-pattern")
128 fn parse_aux_build(line: &str) -> Option<~str> {
129 parse_name_value_directive(line, ~"aux-build")
132 fn parse_compile_flags(line: &str) -> Option<~str> {
133 parse_name_value_directive(line, ~"compile-flags")
136 fn parse_debugger_cmd(line: &str) -> Option<~str> {
137 parse_name_value_directive(line, ~"debugger")
140 fn parse_check_line(line: &str) -> Option<~str> {
141 parse_name_value_directive(line, ~"check")
144 fn parse_exec_env(line: &str) -> Option<(~str, ~str)> {
145 parse_name_value_directive(line, ~"exec-env").map(|nv| {
146 // nv is either FOO or FOO=BAR
147 let mut strs: ~[~str] = nv.splitn('=', 1).map(|s| s.to_owned()).collect();
150 1u => (strs.pop(), ~""),
152 let end = strs.pop();
155 n => fail!("Expected 1 or 2 strings, not {}", n)
160 fn parse_pp_exact(line: &str, testfile: &Path) -> Option<Path> {
161 match parse_name_value_directive(line, ~"pp-exact") {
162 Some(s) => Some(Path::new(s)),
164 if parse_name_directive(line, "pp-exact") {
165 testfile.filename().map(|s| Path::new(s))
173 fn parse_name_directive(line: &str, directive: &str) -> bool {
174 line.contains(directive)
177 fn parse_name_value_directive(line: &str,
178 directive: ~str) -> Option<~str> {
179 let keycolon = directive + ":";
180 match line.find_str(keycolon) {
182 let value = line.slice(colon + keycolon.len(),
183 line.len()).to_owned();
184 debug!("{}: {}", directive, value);