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
37 // Load any test directives embedded in the file
38 pub fn load_props(testfile: &Path) -> TestProps {
39 let mut error_patterns = ~[];
40 let mut aux_builds = ~[];
41 let mut exec_env = ~[];
42 let mut compile_flags = None;
43 let mut pp_exact = None;
44 let mut debugger_cmds = ~[];
45 let mut check_lines = ~[];
46 let mut force_host = false;
47 let mut check_stdout = false;
48 iter_header(testfile, |ln| {
49 match parse_error_pattern(ln) {
50 Some(ep) => error_patterns.push(ep),
54 if compile_flags.is_none() {
55 compile_flags = parse_compile_flags(ln);
58 if pp_exact.is_none() {
59 pp_exact = parse_pp_exact(ln, testfile);
63 force_host = parse_force_host(ln);
67 check_stdout = parse_check_stdout(ln);
70 match parse_aux_build(ln) {
71 Some(ab) => { aux_builds.push(ab); }
75 match parse_exec_env(ln) {
76 Some(ee) => { exec_env.push(ee); }
80 match parse_debugger_cmd(ln) {
81 Some(dc) => debugger_cmds.push(dc),
85 match parse_check_line(ln) {
86 Some(cl) => check_lines.push(cl),
93 error_patterns: error_patterns,
94 compile_flags: compile_flags,
96 aux_builds: aux_builds,
98 debugger_cmds: debugger_cmds,
99 check_lines: check_lines,
100 force_host: force_host,
101 check_stdout: check_stdout,
105 pub fn is_test_ignored(config: &config, testfile: &Path) -> bool {
106 fn ignore_target(config: &config) -> ~str {
107 ~"ignore-" + util::get_os(config.target)
109 fn ignore_stage(config: &config) -> ~str {
110 ~"ignore-" + config.stage_id.split('-').next().unwrap()
113 let val = iter_header(testfile, |ln| {
114 if parse_name_directive(ln, "ignore-test") { false }
115 else if parse_name_directive(ln, ignore_target(config)) { false }
116 else if parse_name_directive(ln, ignore_stage(config)) { false }
117 else if config.mode == common::mode_pretty &&
118 parse_name_directive(ln, "ignore-pretty") { false }
119 else if config.target != config.host &&
120 parse_name_directive(ln, "ignore-cross-compile") { false }
127 fn iter_header(testfile: &Path, it: |&str| -> bool) -> bool {
128 use std::io::{BufferedReader, File};
130 let mut rdr = BufferedReader::new(File::open(testfile).unwrap());
131 for ln in rdr.lines() {
132 // Assume that any directives will be found before the first
133 // module or function. This doesn't seem to be an optimization
134 // with a warm page cache. Maybe with a cold one.
135 if ln.starts_with("fn") || ln.starts_with("mod") {
137 } else { if !(it(ln.trim())) { return false; } }
142 fn parse_error_pattern(line: &str) -> Option<~str> {
143 parse_name_value_directive(line, ~"error-pattern")
146 fn parse_aux_build(line: &str) -> Option<~str> {
147 parse_name_value_directive(line, ~"aux-build")
150 fn parse_compile_flags(line: &str) -> Option<~str> {
151 parse_name_value_directive(line, ~"compile-flags")
154 fn parse_debugger_cmd(line: &str) -> Option<~str> {
155 parse_name_value_directive(line, ~"debugger")
158 fn parse_check_line(line: &str) -> Option<~str> {
159 parse_name_value_directive(line, ~"check")
162 fn parse_force_host(line: &str) -> bool {
163 parse_name_directive(line, "force-host")
166 fn parse_check_stdout(line: &str) -> bool {
167 parse_name_directive(line, "check-stdout")
170 fn parse_exec_env(line: &str) -> Option<(~str, ~str)> {
171 parse_name_value_directive(line, ~"exec-env").map(|nv| {
172 // nv is either FOO or FOO=BAR
173 let mut strs: ~[~str] = nv.splitn('=', 1).map(|s| s.to_owned()).collect();
176 1u => (strs.pop().unwrap(), ~""),
178 let end = strs.pop().unwrap();
179 (strs.pop().unwrap(), end)
181 n => fail!("Expected 1 or 2 strings, not {}", n)
186 fn parse_pp_exact(line: &str, testfile: &Path) -> Option<Path> {
187 match parse_name_value_directive(line, ~"pp-exact") {
188 Some(s) => Some(Path::new(s)),
190 if parse_name_directive(line, "pp-exact") {
191 testfile.filename().map(|s| Path::new(s))
199 fn parse_name_directive(line: &str, directive: &str) -> bool {
200 line.contains(directive)
203 fn parse_name_value_directive(line: &str,
204 directive: ~str) -> Option<~str> {
205 let keycolon = directive + ":";
206 match line.find_str(keycolon) {
208 let value = line.slice(colon + keycolon.len(),
209 line.len()).to_owned();
210 debug!("{}: {}", directive, value);