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
31 // Flag to force a crate to be built with the host architecture
35 // Load any test directives embedded in the file
36 pub fn load_props(testfile: &Path) -> TestProps {
37 let mut error_patterns = ~[];
38 let mut aux_builds = ~[];
39 let mut exec_env = ~[];
40 let mut compile_flags = None;
41 let mut pp_exact = None;
42 let mut debugger_cmds = ~[];
43 let mut check_lines = ~[];
44 let mut force_host = false;
45 iter_header(testfile, |ln| {
46 match parse_error_pattern(ln) {
47 Some(ep) => error_patterns.push(ep),
51 if compile_flags.is_none() {
52 compile_flags = parse_compile_flags(ln);
55 if pp_exact.is_none() {
56 pp_exact = parse_pp_exact(ln, testfile);
60 force_host = parse_force_host(ln);
63 match parse_aux_build(ln) {
64 Some(ab) => { aux_builds.push(ab); }
68 match parse_exec_env(ln) {
69 Some(ee) => { exec_env.push(ee); }
73 match parse_debugger_cmd(ln) {
74 Some(dc) => debugger_cmds.push(dc),
78 match parse_check_line(ln) {
79 Some(cl) => check_lines.push(cl),
86 error_patterns: error_patterns,
87 compile_flags: compile_flags,
89 aux_builds: aux_builds,
91 debugger_cmds: debugger_cmds,
92 check_lines: check_lines,
93 force_host: force_host,
97 pub fn is_test_ignored(config: &config, testfile: &Path) -> bool {
98 fn xfail_target(config: &config) -> ~str {
99 ~"xfail-" + util::get_os(config.target)
101 fn xfail_stage(config: &config) -> ~str {
102 ~"xfail-" + config.stage_id.split('-').next().unwrap()
105 let val = iter_header(testfile, |ln| {
106 if parse_name_directive(ln, "xfail-test") { false }
107 else if parse_name_directive(ln, xfail_target(config)) { false }
108 else if parse_name_directive(ln, xfail_stage(config)) { false }
109 else if config.mode == common::mode_pretty &&
110 parse_name_directive(ln, "xfail-pretty") { false }
117 fn iter_header(testfile: &Path, it: |&str| -> bool) -> bool {
118 use std::io::{BufferedReader, File};
120 let mut rdr = BufferedReader::new(File::open(testfile).unwrap());
121 for ln in rdr.lines() {
122 // Assume that any directives will be found before the first
123 // module or function. This doesn't seem to be an optimization
124 // with a warm page cache. Maybe with a cold one.
125 if ln.starts_with("fn") || ln.starts_with("mod") {
127 } else { if !(it(ln.trim())) { return false; } }
132 fn parse_error_pattern(line: &str) -> Option<~str> {
133 parse_name_value_directive(line, ~"error-pattern")
136 fn parse_aux_build(line: &str) -> Option<~str> {
137 parse_name_value_directive(line, ~"aux-build")
140 fn parse_compile_flags(line: &str) -> Option<~str> {
141 parse_name_value_directive(line, ~"compile-flags")
144 fn parse_debugger_cmd(line: &str) -> Option<~str> {
145 parse_name_value_directive(line, ~"debugger")
148 fn parse_check_line(line: &str) -> Option<~str> {
149 parse_name_value_directive(line, ~"check")
152 fn parse_force_host(line: &str) -> bool {
153 parse_name_directive(line, "force-host")
156 fn parse_exec_env(line: &str) -> Option<(~str, ~str)> {
157 parse_name_value_directive(line, ~"exec-env").map(|nv| {
158 // nv is either FOO or FOO=BAR
159 let mut strs: ~[~str] = nv.splitn('=', 1).map(|s| s.to_owned()).collect();
162 1u => (strs.pop().unwrap(), ~""),
164 let end = strs.pop().unwrap();
165 (strs.pop().unwrap(), end)
167 n => fail!("Expected 1 or 2 strings, not {}", n)
172 fn parse_pp_exact(line: &str, testfile: &Path) -> Option<Path> {
173 match parse_name_value_directive(line, ~"pp-exact") {
174 Some(s) => Some(Path::new(s)),
176 if parse_name_directive(line, "pp-exact") {
177 testfile.filename().map(|s| Path::new(s))
185 fn parse_name_directive(line: &str, directive: &str) -> bool {
186 line.contains(directive)
189 fn parse_name_value_directive(line: &str,
190 directive: ~str) -> Option<~str> {
191 let keycolon = directive + ":";
192 match line.find_str(keycolon) {
194 let value = line.slice(colon + keycolon.len(),
195 line.len()).to_owned();
196 debug!("{}: {}", directive, value);