]> git.lizzy.rs Git - rust.git/blob - src/compiletest/header.rs
auto merge of #13112 : ktt3ja/rust/issue-13058, r=pnkfelix
[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     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
21     // pretty-printed
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
32     pub force_host: bool,
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 }
38
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),
54           None => ()
55         };
56
57         if compile_flags.is_none() {
58             compile_flags = parse_compile_flags(ln);
59         }
60
61         if pp_exact.is_none() {
62             pp_exact = parse_pp_exact(ln, testfile);
63         }
64
65         if !force_host {
66             force_host = parse_force_host(ln);
67         }
68
69         if !check_stdout {
70             check_stdout = parse_check_stdout(ln);
71         }
72
73         if !no_prefer_dynamic {
74             no_prefer_dynamic = parse_no_prefer_dynamic(ln);
75         }
76
77         match parse_aux_build(ln) {
78             Some(ab) => { aux_builds.push(ab); }
79             None => {}
80         }
81
82         match parse_exec_env(ln) {
83             Some(ee) => { exec_env.push(ee); }
84             None => {}
85         }
86
87         match parse_debugger_cmd(ln) {
88             Some(dc) => debugger_cmds.push(dc),
89             None => ()
90         };
91
92         match parse_check_line(ln) {
93             Some(cl) => check_lines.push(cl),
94             None => ()
95         };
96
97         true
98     });
99     return TestProps {
100         error_patterns: error_patterns,
101         compile_flags: compile_flags,
102         pp_exact: pp_exact,
103         aux_builds: aux_builds,
104         exec_env: exec_env,
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,
110     };
111 }
112
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)
116     }
117     fn ignore_stage(config: &config) -> ~str {
118         ~"ignore-" + config.stage_id.split('-').next().unwrap()
119     }
120
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 }
129         else { true }
130     });
131
132     !val
133 }
134
135 fn iter_header(testfile: &Path, it: |&str| -> bool) -> bool {
136     use std::io::{BufferedReader, File};
137
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") {
145             return true;
146         } else { if !(it(ln.trim())) { return false; } }
147     }
148     return true;
149 }
150
151 fn parse_error_pattern(line: &str) -> Option<~str> {
152     parse_name_value_directive(line, ~"error-pattern")
153 }
154
155 fn parse_aux_build(line: &str) -> Option<~str> {
156     parse_name_value_directive(line, ~"aux-build")
157 }
158
159 fn parse_compile_flags(line: &str) -> Option<~str> {
160     parse_name_value_directive(line, ~"compile-flags")
161 }
162
163 fn parse_debugger_cmd(line: &str) -> Option<~str> {
164     parse_name_value_directive(line, ~"debugger")
165 }
166
167 fn parse_check_line(line: &str) -> Option<~str> {
168     parse_name_value_directive(line, ~"check")
169 }
170
171 fn parse_force_host(line: &str) -> bool {
172     parse_name_directive(line, "force-host")
173 }
174
175 fn parse_check_stdout(line: &str) -> bool {
176     parse_name_directive(line, "check-stdout")
177 }
178
179 fn parse_no_prefer_dynamic(line: &str) -> bool {
180     parse_name_directive(line, "no-prefer-dynamic")
181 }
182
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();
187
188         match strs.len() {
189           1u => (strs.pop().unwrap(), ~""),
190           2u => {
191               let end = strs.pop().unwrap();
192               (strs.pop().unwrap(), end)
193           }
194           n => fail!("Expected 1 or 2 strings, not {}", n)
195         }
196     })
197 }
198
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)),
202       None => {
203         if parse_name_directive(line, "pp-exact") {
204             testfile.filename().map(|s| Path::new(s))
205         } else {
206             None
207         }
208       }
209     }
210 }
211
212 fn parse_name_directive(line: &str, directive: &str) -> bool {
213     line.contains(directive)
214 }
215
216 fn parse_name_value_directive(line: &str,
217                               directive: ~str) -> Option<~str> {
218     let keycolon = directive + ":";
219     match line.find_str(keycolon) {
220         Some(colon) => {
221             let value = line.slice(colon + keycolon.len(),
222                                    line.len()).to_owned();
223             debug!("{}: {}", directive,  value);
224             Some(value)
225         }
226         None => None
227     }
228 }