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 pub error_patterns: Vec<~str> ,
18 // Extra flags to pass to the compiler
19 pub compile_flags: Option<~str>,
20 // If present, the name of a file that this test should match when
22 pub pp_exact: Option<Path>,
23 // Modules from aux directory that should be compiled
24 pub aux_builds: Vec<~str> ,
25 // Environment settings to use during execution
26 pub exec_env: Vec<(~str,~str)> ,
27 // Commands to be given to the debugger, when testing debug info
28 pub debugger_cmds: Vec<~str> ,
29 // Lines to check if they appear in the expected debugger output
30 pub check_lines: Vec<~str> ,
31 // Flag to force a crate to be built with the host architecture
33 // Check stdout for error-pattern output as well as stderr
34 pub check_stdout: bool,
35 // Don't force a --crate-type=dylib flag on the command line
36 pub no_prefer_dynamic: bool,
39 // Load any test directives embedded in the file
40 pub fn load_props(testfile: &Path) -> TestProps {
41 let mut error_patterns = Vec::new();
42 let mut aux_builds = Vec::new();
43 let mut exec_env = Vec::new();
44 let mut compile_flags = None;
45 let mut pp_exact = None;
46 let mut debugger_cmds = Vec::new();
47 let mut check_lines = Vec::new();
48 let mut force_host = false;
49 let mut check_stdout = false;
50 let mut no_prefer_dynamic = false;
51 iter_header(testfile, |ln| {
52 match parse_error_pattern(ln) {
53 Some(ep) => error_patterns.push(ep),
57 if compile_flags.is_none() {
58 compile_flags = parse_compile_flags(ln);
61 if pp_exact.is_none() {
62 pp_exact = parse_pp_exact(ln, testfile);
66 force_host = parse_force_host(ln);
70 check_stdout = parse_check_stdout(ln);
73 if !no_prefer_dynamic {
74 no_prefer_dynamic = parse_no_prefer_dynamic(ln);
77 match parse_aux_build(ln) {
78 Some(ab) => { aux_builds.push(ab); }
82 match parse_exec_env(ln) {
83 Some(ee) => { exec_env.push(ee); }
87 match parse_debugger_cmd(ln) {
88 Some(dc) => debugger_cmds.push(dc),
92 match parse_check_line(ln) {
93 Some(cl) => check_lines.push(cl),
100 error_patterns: error_patterns,
101 compile_flags: compile_flags,
103 aux_builds: aux_builds,
105 debugger_cmds: debugger_cmds,
106 check_lines: check_lines,
107 force_host: force_host,
108 check_stdout: check_stdout,
109 no_prefer_dynamic: no_prefer_dynamic,
113 pub fn is_test_ignored(config: &config, testfile: &Path) -> bool {
114 fn ignore_target(config: &config) -> ~str {
115 ~"ignore-" + util::get_os(config.target)
117 fn ignore_stage(config: &config) -> ~str {
118 ~"ignore-" + config.stage_id.split('-').next().unwrap()
121 let val = iter_header(testfile, |ln| {
122 if parse_name_directive(ln, "ignore-test") { false }
123 else if parse_name_directive(ln, ignore_target(config)) { false }
124 else if parse_name_directive(ln, ignore_stage(config)) { false }
125 else if config.mode == common::mode_pretty &&
126 parse_name_directive(ln, "ignore-pretty") { false }
127 else if config.target != config.host &&
128 parse_name_directive(ln, "ignore-cross-compile") { false }
135 fn iter_header(testfile: &Path, it: |&str| -> bool) -> bool {
136 use std::io::{BufferedReader, File};
138 let mut rdr = BufferedReader::new(File::open(testfile).unwrap());
139 for ln in rdr.lines() {
140 // Assume that any directives will be found before the first
141 // module or function. This doesn't seem to be an optimization
142 // with a warm page cache. Maybe with a cold one.
143 let ln = ln.unwrap();
144 if ln.starts_with("fn") || ln.starts_with("mod") {
146 } else { if !(it(ln.trim())) { return false; } }
151 fn parse_error_pattern(line: &str) -> Option<~str> {
152 parse_name_value_directive(line, ~"error-pattern")
155 fn parse_aux_build(line: &str) -> Option<~str> {
156 parse_name_value_directive(line, ~"aux-build")
159 fn parse_compile_flags(line: &str) -> Option<~str> {
160 parse_name_value_directive(line, ~"compile-flags")
163 fn parse_debugger_cmd(line: &str) -> Option<~str> {
164 parse_name_value_directive(line, ~"debugger")
167 fn parse_check_line(line: &str) -> Option<~str> {
168 parse_name_value_directive(line, ~"check")
171 fn parse_force_host(line: &str) -> bool {
172 parse_name_directive(line, "force-host")
175 fn parse_check_stdout(line: &str) -> bool {
176 parse_name_directive(line, "check-stdout")
179 fn parse_no_prefer_dynamic(line: &str) -> bool {
180 parse_name_directive(line, "no-prefer-dynamic")
183 fn parse_exec_env(line: &str) -> Option<(~str, ~str)> {
184 parse_name_value_directive(line, ~"exec-env").map(|nv| {
185 // nv is either FOO or FOO=BAR
186 let mut strs: Vec<~str> = nv.splitn('=', 1).map(|s| s.to_owned()).collect();
189 1u => (strs.pop().unwrap(), ~""),
191 let end = strs.pop().unwrap();
192 (strs.pop().unwrap(), end)
194 n => fail!("Expected 1 or 2 strings, not {}", n)
199 fn parse_pp_exact(line: &str, testfile: &Path) -> Option<Path> {
200 match parse_name_value_directive(line, ~"pp-exact") {
201 Some(s) => Some(Path::new(s)),
203 if parse_name_directive(line, "pp-exact") {
204 testfile.filename().map(|s| Path::new(s))
212 fn parse_name_directive(line: &str, directive: &str) -> bool {
213 line.contains(directive)
216 fn parse_name_value_directive(line: &str,
217 directive: ~str) -> Option<~str> {
218 let keycolon = directive + ":";
219 match line.find_str(keycolon) {
221 let value = line.slice(colon + keycolon.len(),
222 line.len()).to_owned();
223 debug!("{}: {}", directive, value);