]> git.lizzy.rs Git - rust.git/blob - src/compiletest/header.rs
auto merge of #11601 : dguenther/rust/fix_test_summary, r=brson
[rust.git] / src / compiletest / header.rs
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.
4 //
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.
10
11 use common::config;
12 use common;
13 use util;
14
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
21     // pretty-printed
22     pp_exact: Option<Path>,
23     // Modules from aux directory that should be compiled
24     aux_builds: ~[~str],
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
30     check_lines: ~[~str],
31 }
32
33 // Load any test directives embedded in the file
34 pub fn load_props(testfile: &Path) -> TestProps {
35     let mut error_patterns = ~[];
36     let mut aux_builds = ~[];
37     let mut exec_env = ~[];
38     let mut compile_flags = None;
39     let mut pp_exact = None;
40     let mut debugger_cmds = ~[];
41     let mut check_lines = ~[];
42     iter_header(testfile, |ln| {
43         match parse_error_pattern(ln) {
44           Some(ep) => error_patterns.push(ep),
45           None => ()
46         };
47
48         if compile_flags.is_none() {
49             compile_flags = parse_compile_flags(ln);
50         }
51
52         if pp_exact.is_none() {
53             pp_exact = parse_pp_exact(ln, testfile);
54         }
55
56         match parse_aux_build(ln) {
57             Some(ab) => { aux_builds.push(ab); }
58             None => {}
59         }
60
61         match parse_exec_env(ln) {
62             Some(ee) => { exec_env.push(ee); }
63             None => {}
64         }
65
66         match parse_debugger_cmd(ln) {
67             Some(dc) => debugger_cmds.push(dc),
68             None => ()
69         };
70
71         match parse_check_line(ln) {
72             Some(cl) => check_lines.push(cl),
73             None => ()
74         };
75
76         true
77     });
78     return TestProps {
79         error_patterns: error_patterns,
80         compile_flags: compile_flags,
81         pp_exact: pp_exact,
82         aux_builds: aux_builds,
83         exec_env: exec_env,
84         debugger_cmds: debugger_cmds,
85         check_lines: check_lines
86     };
87 }
88
89 pub fn is_test_ignored(config: &config, testfile: &Path) -> bool {
90     fn xfail_target(config: &config) -> ~str {
91         ~"xfail-" + util::get_os(config.target)
92     }
93     fn xfail_stage(config: &config) -> ~str {
94         ~"xfail-" + config.stage_id.split('-').next().unwrap()
95     }
96
97     let val = iter_header(testfile, |ln| {
98         if parse_name_directive(ln, "xfail-test") { false }
99         else if parse_name_directive(ln, xfail_target(config)) { false }
100         else if parse_name_directive(ln, xfail_stage(config)) { false }
101         else if config.mode == common::mode_pretty &&
102             parse_name_directive(ln, "xfail-pretty") { false }
103         else { true }
104     });
105
106     !val
107 }
108
109 fn iter_header(testfile: &Path, it: |&str| -> bool) -> bool {
110     use std::io::buffered::BufferedReader;
111     use std::io::File;
112
113     let mut rdr = BufferedReader::new(File::open(testfile).unwrap());
114     for ln in rdr.lines() {
115         // Assume that any directives will be found before the first
116         // module or function. This doesn't seem to be an optimization
117         // with a warm page cache. Maybe with a cold one.
118         if ln.starts_with("fn") || ln.starts_with("mod") {
119             return true;
120         } else { if !(it(ln.trim())) { return false; } }
121     }
122     return true;
123 }
124
125 fn parse_error_pattern(line: &str) -> Option<~str> {
126     parse_name_value_directive(line, ~"error-pattern")
127 }
128
129 fn parse_aux_build(line: &str) -> Option<~str> {
130     parse_name_value_directive(line, ~"aux-build")
131 }
132
133 fn parse_compile_flags(line: &str) -> Option<~str> {
134     parse_name_value_directive(line, ~"compile-flags")
135 }
136
137 fn parse_debugger_cmd(line: &str) -> Option<~str> {
138     parse_name_value_directive(line, ~"debugger")
139 }
140
141 fn parse_check_line(line: &str) -> Option<~str> {
142     parse_name_value_directive(line, ~"check")
143 }
144
145 fn parse_exec_env(line: &str) -> Option<(~str, ~str)> {
146     parse_name_value_directive(line, ~"exec-env").map(|nv| {
147         // nv is either FOO or FOO=BAR
148         let mut strs: ~[~str] = nv.splitn('=', 1).map(|s| s.to_owned()).collect();
149
150         match strs.len() {
151           1u => (strs.pop(), ~""),
152           2u => {
153               let end = strs.pop();
154               (strs.pop(), end)
155           }
156           n => fail!("Expected 1 or 2 strings, not {}", n)
157         }
158     })
159 }
160
161 fn parse_pp_exact(line: &str, testfile: &Path) -> Option<Path> {
162     match parse_name_value_directive(line, ~"pp-exact") {
163       Some(s) => Some(Path::new(s)),
164       None => {
165         if parse_name_directive(line, "pp-exact") {
166             testfile.filename().map(|s| Path::new(s))
167         } else {
168             None
169         }
170       }
171     }
172 }
173
174 fn parse_name_directive(line: &str, directive: &str) -> bool {
175     line.contains(directive)
176 }
177
178 fn parse_name_value_directive(line: &str,
179                               directive: ~str) -> Option<~str> {
180     let keycolon = directive + ":";
181     match line.find_str(keycolon) {
182         Some(colon) => {
183             let value = line.slice(colon + keycolon.len(),
184                                    line.len()).to_owned();
185             debug!("{}: {}", directive,  value);
186             Some(value)
187         }
188         None => None
189     }
190 }