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<String> ,
18 // Extra flags to pass to the compiler
19 pub compile_flags: Option<String>,
20 // Extra flags to pass when the compiled code is run (such as --bench)
21 pub run_flags: Option<String>,
22 // If present, the name of a file that this test should match when
24 pub pp_exact: Option<Path>,
25 // Modules from aux directory that should be compiled
26 pub aux_builds: Vec<String> ,
27 // Environment settings to use during execution
28 pub exec_env: Vec<(String,String)> ,
29 // Lines to check if they appear in the expected debugger output
30 pub check_lines: Vec<String> ,
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,
37 // Don't run --pretty expanded when running pretty printing tests
38 pub no_pretty_expanded: bool,
39 // Which pretty mode are we testing with, default to 'normal'
40 pub pretty_mode: String,
41 // Only compare pretty output and don't try compiling
42 pub pretty_compare_only: bool,
43 // Patterns which must not appear in the output of a cfail test.
44 pub forbid_output: Vec<String>,
47 // Load any test directives embedded in the file
48 pub fn load_props(testfile: &Path) -> TestProps {
49 let mut error_patterns = Vec::new();
50 let mut aux_builds = Vec::new();
51 let mut exec_env = Vec::new();
52 let mut compile_flags = None;
53 let mut run_flags = None;
54 let mut pp_exact = None;
55 let mut check_lines = Vec::new();
56 let mut force_host = false;
57 let mut check_stdout = false;
58 let mut no_prefer_dynamic = false;
59 let mut no_pretty_expanded = false;
60 let mut pretty_mode = None;
61 let mut pretty_compare_only = false;
62 let mut forbid_output = Vec::new();
63 iter_header(testfile, |ln| {
64 match parse_error_pattern(ln) {
65 Some(ep) => error_patterns.push(ep),
69 if compile_flags.is_none() {
70 compile_flags = parse_compile_flags(ln);
73 if run_flags.is_none() {
74 run_flags = parse_run_flags(ln);
77 if pp_exact.is_none() {
78 pp_exact = parse_pp_exact(ln, testfile);
82 force_host = parse_force_host(ln);
86 check_stdout = parse_check_stdout(ln);
89 if !no_prefer_dynamic {
90 no_prefer_dynamic = parse_no_prefer_dynamic(ln);
93 if !no_pretty_expanded {
94 no_pretty_expanded = parse_no_pretty_expanded(ln);
97 if pretty_mode.is_none() {
98 pretty_mode = parse_pretty_mode(ln);
101 if !pretty_compare_only {
102 pretty_compare_only = parse_pretty_compare_only(ln);
105 match parse_aux_build(ln) {
106 Some(ab) => { aux_builds.push(ab); }
110 match parse_exec_env(ln) {
111 Some(ee) => { exec_env.push(ee); }
115 match parse_check_line(ln) {
116 Some(cl) => check_lines.push(cl),
120 match parse_forbid_output(ln) {
121 Some(of) => forbid_output.push(of),
129 error_patterns: error_patterns,
130 compile_flags: compile_flags,
131 run_flags: run_flags,
133 aux_builds: aux_builds,
135 check_lines: check_lines,
136 force_host: force_host,
137 check_stdout: check_stdout,
138 no_prefer_dynamic: no_prefer_dynamic,
139 no_pretty_expanded: no_pretty_expanded,
140 pretty_mode: pretty_mode.unwrap_or("normal".to_string()),
141 pretty_compare_only: pretty_compare_only,
142 forbid_output: forbid_output,
146 pub fn is_test_ignored(config: &Config, testfile: &Path) -> bool {
147 fn ignore_target(config: &Config) -> String {
148 format!("ignore-{}", util::get_os(config.target.as_slice()))
150 fn ignore_stage(config: &Config) -> String {
152 config.stage_id.as_slice().split('-').next().unwrap())
154 fn ignore_gdb(config: &Config, line: &str) -> bool {
155 if config.mode != common::DebugInfoGdb {
159 if parse_name_directive(line, "ignore-gdb") {
163 match config.gdb_version {
164 Some(ref actual_version) => {
165 if line.contains("min-gdb-version") {
166 let min_version = line.trim()
169 .expect("Malformed GDB version directive");
170 // Ignore if actual version is smaller the minimum required
172 gdb_version_to_int(actual_version.as_slice()) <
173 gdb_version_to_int(min_version.as_slice())
182 fn ignore_lldb(config: &Config, line: &str) -> bool {
183 if config.mode != common::DebugInfoLldb {
187 if parse_name_directive(line, "ignore-lldb") {
191 match config.lldb_version {
192 Some(ref actual_version) => {
193 if line.contains("min-lldb-version") {
194 let min_version = line.trim()
197 .expect("Malformed lldb version directive");
198 // Ignore if actual version is smaller the minimum required
200 lldb_version_to_int(actual_version.as_slice()) <
201 lldb_version_to_int(min_version.as_slice())
210 let val = iter_header(testfile, |ln| {
211 !parse_name_directive(ln, "ignore-test") &&
212 !parse_name_directive(ln, ignore_target(config).as_slice()) &&
213 !parse_name_directive(ln, ignore_stage(config).as_slice()) &&
214 !(config.mode == common::Pretty && parse_name_directive(ln, "ignore-pretty")) &&
215 !(config.target != config.host && parse_name_directive(ln, "ignore-cross-compile")) &&
216 !ignore_gdb(config, ln) &&
217 !ignore_lldb(config, ln)
223 fn iter_header(testfile: &Path, it: |&str| -> bool) -> bool {
224 use std::io::{BufferedReader, File};
226 let mut rdr = BufferedReader::new(File::open(testfile).unwrap());
227 for ln in rdr.lines() {
228 // Assume that any directives will be found before the first
229 // module or function. This doesn't seem to be an optimization
230 // with a warm page cache. Maybe with a cold one.
231 let ln = ln.unwrap();
232 if ln.as_slice().starts_with("fn") ||
233 ln.as_slice().starts_with("mod") {
236 if !(it(ln.as_slice().trim())) {
244 fn parse_error_pattern(line: &str) -> Option<String> {
245 parse_name_value_directive(line, "error-pattern")
248 fn parse_forbid_output(line: &str) -> Option<String> {
249 parse_name_value_directive(line, "forbid-output")
252 fn parse_aux_build(line: &str) -> Option<String> {
253 parse_name_value_directive(line, "aux-build")
256 fn parse_compile_flags(line: &str) -> Option<String> {
257 parse_name_value_directive(line, "compile-flags")
260 fn parse_run_flags(line: &str) -> Option<String> {
261 parse_name_value_directive(line, "run-flags")
264 fn parse_check_line(line: &str) -> Option<String> {
265 parse_name_value_directive(line, "check")
268 fn parse_force_host(line: &str) -> bool {
269 parse_name_directive(line, "force-host")
272 fn parse_check_stdout(line: &str) -> bool {
273 parse_name_directive(line, "check-stdout")
276 fn parse_no_prefer_dynamic(line: &str) -> bool {
277 parse_name_directive(line, "no-prefer-dynamic")
280 fn parse_no_pretty_expanded(line: &str) -> bool {
281 parse_name_directive(line, "no-pretty-expanded")
284 fn parse_pretty_mode(line: &str) -> Option<String> {
285 parse_name_value_directive(line, "pretty-mode")
288 fn parse_pretty_compare_only(line: &str) -> bool {
289 parse_name_directive(line, "pretty-compare-only")
292 fn parse_exec_env(line: &str) -> Option<(String, String)> {
293 parse_name_value_directive(line, "exec-env").map(|nv| {
294 // nv is either FOO or FOO=BAR
295 let mut strs: Vec<String> = nv.as_slice()
297 .map(|s| s.to_string())
301 1u => (strs.pop().unwrap(), "".to_string()),
303 let end = strs.pop().unwrap();
304 (strs.pop().unwrap(), end)
306 n => panic!("Expected 1 or 2 strings, not {}", n)
311 fn parse_pp_exact(line: &str, testfile: &Path) -> Option<Path> {
312 match parse_name_value_directive(line, "pp-exact") {
313 Some(s) => Some(Path::new(s)),
315 if parse_name_directive(line, "pp-exact") {
316 testfile.filename().map(|s| Path::new(s))
324 fn parse_name_directive(line: &str, directive: &str) -> bool {
325 line.contains(directive)
328 pub fn parse_name_value_directive(line: &str, directive: &str)
330 let keycolon = format!("{}:", directive);
331 match line.find_str(keycolon.as_slice()) {
333 let value = line.slice(colon + keycolon.len(),
334 line.len()).to_string();
335 debug!("{}: {}", directive, value);
342 pub fn gdb_version_to_int(version_string: &str) -> int {
343 let error_string = format!(
344 "Encountered GDB version string with unexpected format: {}",
346 let error_string = error_string.as_slice();
348 let components: Vec<&str> = version_string.trim().split('.').collect();
350 if components.len() != 2 {
351 panic!("{}", error_string);
354 let major: int = components[0].parse().expect(error_string);
355 let minor: int = components[1].parse().expect(error_string);
357 return major * 1000 + minor;
360 pub fn lldb_version_to_int(version_string: &str) -> int {
361 let error_string = format!(
362 "Encountered LLDB version string with unexpected format: {}",
364 let error_string = error_string.as_slice();
365 let major: int = version_string.parse().expect(error_string);