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
33 // Check stdout for error-pattern output as well as stderr
35 // Don't force a --crate-type=dylib flag on the command line
36 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 = ~[];
42 let mut aux_builds = ~[];
43 let mut exec_env = ~[];
44 let mut compile_flags = None;
45 let mut pp_exact = None;
46 let mut debugger_cmds = ~[];
47 let mut check_lines = ~[];
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 if ln.starts_with("fn") || ln.starts_with("mod") {
145 } else { if !(it(ln.trim())) { return false; } }
150 fn parse_error_pattern(line: &str) -> Option<~str> {
151 parse_name_value_directive(line, ~"error-pattern")
154 fn parse_aux_build(line: &str) -> Option<~str> {
155 parse_name_value_directive(line, ~"aux-build")
158 fn parse_compile_flags(line: &str) -> Option<~str> {
159 parse_name_value_directive(line, ~"compile-flags")
162 fn parse_debugger_cmd(line: &str) -> Option<~str> {
163 parse_name_value_directive(line, ~"debugger")
166 fn parse_check_line(line: &str) -> Option<~str> {
167 parse_name_value_directive(line, ~"check")
170 fn parse_force_host(line: &str) -> bool {
171 parse_name_directive(line, "force-host")
174 fn parse_check_stdout(line: &str) -> bool {
175 parse_name_directive(line, "check-stdout")
178 fn parse_no_prefer_dynamic(line: &str) -> bool {
179 parse_name_directive(line, "no-prefer-dynamic")
182 fn parse_exec_env(line: &str) -> Option<(~str, ~str)> {
183 parse_name_value_directive(line, ~"exec-env").map(|nv| {
184 // nv is either FOO or FOO=BAR
185 let mut strs: ~[~str] = nv.splitn('=', 1).map(|s| s.to_owned()).collect();
188 1u => (strs.pop().unwrap(), ~""),
190 let end = strs.pop().unwrap();
191 (strs.pop().unwrap(), end)
193 n => fail!("Expected 1 or 2 strings, not {}", n)
198 fn parse_pp_exact(line: &str, testfile: &Path) -> Option<Path> {
199 match parse_name_value_directive(line, ~"pp-exact") {
200 Some(s) => Some(Path::new(s)),
202 if parse_name_directive(line, "pp-exact") {
203 testfile.filename().map(|s| Path::new(s))
211 fn parse_name_directive(line: &str, directive: &str) -> bool {
212 line.contains(directive)
215 fn parse_name_value_directive(line: &str,
216 directive: ~str) -> Option<~str> {
217 let keycolon = directive + ":";
218 match line.find_str(keycolon) {
220 let value = line.slice(colon + keycolon.len(),
221 line.len()).to_owned();
222 debug!("{}: {}", directive, value);