]> git.lizzy.rs Git - rust.git/blob - src/compiletest/header.rs
libstd: implement From<&Path|PathBuf> for Cow<Path>
[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 std::env;
12 use std::fs::File;
13 use std::io::BufReader;
14 use std::io::prelude::*;
15 use std::path::{Path, PathBuf};
16
17 use common::Config;
18 use common;
19 use util;
20
21 pub struct TestProps {
22     // Lines that should be expected, in order, on standard out
23     pub error_patterns: Vec<String> ,
24     // Extra flags to pass to the compiler
25     pub compile_flags: Option<String>,
26     // Extra flags to pass when the compiled code is run (such as --bench)
27     pub run_flags: Option<String>,
28     // If present, the name of a file that this test should match when
29     // pretty-printed
30     pub pp_exact: Option<PathBuf>,
31     // Modules from aux directory that should be compiled
32     pub aux_builds: Vec<String> ,
33     // Environment settings to use during execution
34     pub exec_env: Vec<(String,String)> ,
35     // Lines to check if they appear in the expected debugger output
36     pub check_lines: Vec<String> ,
37     // Flag to force a crate to be built with the host architecture
38     pub force_host: bool,
39     // Check stdout for error-pattern output as well as stderr
40     pub check_stdout: bool,
41     // Don't force a --crate-type=dylib flag on the command line
42     pub no_prefer_dynamic: bool,
43     // Run --pretty expanded when running pretty printing tests
44     pub pretty_expanded: bool,
45     // Which pretty mode are we testing with, default to 'normal'
46     pub pretty_mode: String,
47     // Only compare pretty output and don't try compiling
48     pub pretty_compare_only: bool,
49     // Patterns which must not appear in the output of a cfail test.
50     pub forbid_output: Vec<String>,
51 }
52
53 // Load any test directives embedded in the file
54 pub fn load_props(testfile: &Path) -> TestProps {
55     let mut error_patterns = Vec::new();
56     let mut aux_builds = Vec::new();
57     let mut exec_env = Vec::new();
58     let mut compile_flags = None;
59     let mut run_flags = None;
60     let mut pp_exact = None;
61     let mut check_lines = Vec::new();
62     let mut force_host = false;
63     let mut check_stdout = false;
64     let mut no_prefer_dynamic = false;
65     let mut pretty_expanded = false;
66     let mut pretty_mode = None;
67     let mut pretty_compare_only = false;
68     let mut forbid_output = Vec::new();
69     iter_header(testfile, &mut |ln| {
70         if let Some(ep) = parse_error_pattern(ln) {
71            error_patterns.push(ep);
72         }
73
74         if compile_flags.is_none() {
75             compile_flags = parse_compile_flags(ln);
76         }
77
78         if run_flags.is_none() {
79             run_flags = parse_run_flags(ln);
80         }
81
82         if pp_exact.is_none() {
83             pp_exact = parse_pp_exact(ln, testfile);
84         }
85
86         if !force_host {
87             force_host = parse_force_host(ln);
88         }
89
90         if !check_stdout {
91             check_stdout = parse_check_stdout(ln);
92         }
93
94         if !no_prefer_dynamic {
95             no_prefer_dynamic = parse_no_prefer_dynamic(ln);
96         }
97
98         if !pretty_expanded {
99             pretty_expanded = parse_pretty_expanded(ln);
100         }
101
102         if pretty_mode.is_none() {
103             pretty_mode = parse_pretty_mode(ln);
104         }
105
106         if !pretty_compare_only {
107             pretty_compare_only = parse_pretty_compare_only(ln);
108         }
109
110         if let  Some(ab) = parse_aux_build(ln) {
111             aux_builds.push(ab);
112         }
113
114         if let Some(ee) = parse_exec_env(ln) {
115             exec_env.push(ee);
116         }
117
118         if let Some(cl) =  parse_check_line(ln) {
119             check_lines.push(cl);
120         }
121
122         if let Some(of) = parse_forbid_output(ln) {
123             forbid_output.push(of);
124         }
125
126         true
127     });
128
129     for key in vec!["RUST_TEST_NOCAPTURE", "RUST_TEST_THREADS"] {
130         match env::var(key) {
131             Ok(val) =>
132                 if exec_env.iter().find(|&&(ref x, _)| *x == key).is_none() {
133                     exec_env.push((key.to_owned(), val))
134                 },
135             Err(..) => {}
136         }
137     }
138
139     TestProps {
140         error_patterns: error_patterns,
141         compile_flags: compile_flags,
142         run_flags: run_flags,
143         pp_exact: pp_exact,
144         aux_builds: aux_builds,
145         exec_env: exec_env,
146         check_lines: check_lines,
147         force_host: force_host,
148         check_stdout: check_stdout,
149         no_prefer_dynamic: no_prefer_dynamic,
150         pretty_expanded: pretty_expanded,
151         pretty_mode: pretty_mode.unwrap_or("normal".to_owned()),
152         pretty_compare_only: pretty_compare_only,
153         forbid_output: forbid_output,
154     }
155 }
156
157 pub fn is_test_ignored(config: &Config, testfile: &Path) -> bool {
158     fn ignore_target(config: &Config) -> String {
159         format!("ignore-{}", util::get_os(&config.target))
160     }
161     fn ignore_architecture(config: &Config) -> String {
162         format!("ignore-{}", util::get_arch(&config.target))
163     }
164     fn ignore_stage(config: &Config) -> String {
165         format!("ignore-{}",
166                 config.stage_id.split('-').next().unwrap())
167     }
168     fn ignore_env(config: &Config) -> String {
169         format!("ignore-{}", util::get_env(&config.target).unwrap_or("<unknown>"))
170     }
171     fn ignore_gdb(config: &Config, line: &str) -> bool {
172         if config.mode != common::DebugInfoGdb {
173             return false;
174         }
175
176         if parse_name_directive(line, "ignore-gdb") {
177             return true;
178         }
179
180         if let Some(ref actual_version) = config.gdb_version {
181             if line.contains("min-gdb-version") {
182                 let min_version = line.trim()
183                                       .split(' ')
184                                       .last()
185                                       .expect("Malformed GDB version directive");
186                 // Ignore if actual version is smaller the minimum required
187                 // version
188                 gdb_version_to_int(actual_version) <
189                     gdb_version_to_int(min_version)
190             } else {
191                 false
192             }
193         } else {
194             false
195         }
196     }
197
198     fn ignore_lldb(config: &Config, line: &str) -> bool {
199         if config.mode != common::DebugInfoLldb {
200             return false;
201         }
202
203         if parse_name_directive(line, "ignore-lldb") {
204             return true;
205         }
206
207         if let Some(ref actual_version) = config.lldb_version {
208             if line.contains("min-lldb-version") {
209                 let min_version = line.trim()
210                                       .split(' ')
211                                       .last()
212                                       .expect("Malformed lldb version directive");
213                 // Ignore if actual version is smaller the minimum required
214                 // version
215                 lldb_version_to_int(actual_version) <
216                     lldb_version_to_int(min_version)
217             } else {
218                 false
219             }
220         } else {
221             false
222         }
223     }
224
225     let val = iter_header(testfile, &mut |ln| {
226         !parse_name_directive(ln, "ignore-test") &&
227         !parse_name_directive(ln, &ignore_target(config)) &&
228         !parse_name_directive(ln, &ignore_architecture(config)) &&
229         !parse_name_directive(ln, &ignore_stage(config)) &&
230         !parse_name_directive(ln, &ignore_env(config)) &&
231         !(config.mode == common::Pretty && parse_name_directive(ln, "ignore-pretty")) &&
232         !(config.target != config.host && parse_name_directive(ln, "ignore-cross-compile")) &&
233         !ignore_gdb(config, ln) &&
234         !ignore_lldb(config, ln)
235     });
236
237     !val
238 }
239
240 fn iter_header(testfile: &Path, it: &mut FnMut(&str) -> bool) -> bool {
241     let rdr = BufReader::new(File::open(testfile).unwrap());
242     for ln in rdr.lines() {
243         // Assume that any directives will be found before the first
244         // module or function. This doesn't seem to be an optimization
245         // with a warm page cache. Maybe with a cold one.
246         let ln = ln.unwrap();
247         if ln.starts_with("fn") ||
248                 ln.starts_with("mod") {
249             return true;
250         } else {
251             if !(it(ln.trim())) {
252                 return false;
253             }
254         }
255     }
256     return true;
257 }
258
259 fn parse_error_pattern(line: &str) -> Option<String> {
260     parse_name_value_directive(line, "error-pattern")
261 }
262
263 fn parse_forbid_output(line: &str) -> Option<String> {
264     parse_name_value_directive(line, "forbid-output")
265 }
266
267 fn parse_aux_build(line: &str) -> Option<String> {
268     parse_name_value_directive(line, "aux-build")
269 }
270
271 fn parse_compile_flags(line: &str) -> Option<String> {
272     parse_name_value_directive(line, "compile-flags")
273 }
274
275 fn parse_run_flags(line: &str) -> Option<String> {
276     parse_name_value_directive(line, "run-flags")
277 }
278
279 fn parse_check_line(line: &str) -> Option<String> {
280     parse_name_value_directive(line, "check")
281 }
282
283 fn parse_force_host(line: &str) -> bool {
284     parse_name_directive(line, "force-host")
285 }
286
287 fn parse_check_stdout(line: &str) -> bool {
288     parse_name_directive(line, "check-stdout")
289 }
290
291 fn parse_no_prefer_dynamic(line: &str) -> bool {
292     parse_name_directive(line, "no-prefer-dynamic")
293 }
294
295 fn parse_pretty_expanded(line: &str) -> bool {
296     parse_name_directive(line, "pretty-expanded")
297 }
298
299 fn parse_pretty_mode(line: &str) -> Option<String> {
300     parse_name_value_directive(line, "pretty-mode")
301 }
302
303 fn parse_pretty_compare_only(line: &str) -> bool {
304     parse_name_directive(line, "pretty-compare-only")
305 }
306
307 fn parse_exec_env(line: &str) -> Option<(String, String)> {
308     parse_name_value_directive(line, "exec-env").map(|nv| {
309         // nv is either FOO or FOO=BAR
310         let mut strs: Vec<String> = nv
311                                       .splitn(2, '=')
312                                       .map(str::to_owned)
313                                       .collect();
314
315         match strs.len() {
316           1 => (strs.pop().unwrap(), "".to_owned()),
317           2 => {
318               let end = strs.pop().unwrap();
319               (strs.pop().unwrap(), end)
320           }
321           n => panic!("Expected 1 or 2 strings, not {}", n)
322         }
323     })
324 }
325
326 fn parse_pp_exact(line: &str, testfile: &Path) -> Option<PathBuf> {
327     if let Some(s) = parse_name_value_directive(line, "pp-exact") {
328         Some(PathBuf::from(&s))
329     } else {
330         if parse_name_directive(line, "pp-exact") {
331             testfile.file_name().map(PathBuf::from)
332         } else {
333             None
334         }
335     }
336 }
337
338 fn parse_name_directive(line: &str, directive: &str) -> bool {
339     // This 'no-' rule is a quick hack to allow pretty-expanded and no-pretty-expanded to coexist
340     line.contains(directive) && !line.contains(&("no-".to_owned() + directive))
341 }
342
343 pub fn parse_name_value_directive(line: &str, directive: &str)
344                                   -> Option<String> {
345     let keycolon = format!("{}:", directive);
346     if let Some(colon) = line.find(&keycolon) {
347         let value = line[(colon + keycolon.len()) .. line.len()].to_owned();
348         debug!("{}: {}", directive, value);
349         Some(value)
350     } else {
351         None
352     }
353 }
354
355 pub fn gdb_version_to_int(version_string: &str) -> isize {
356     let error_string = format!(
357         "Encountered GDB version string with unexpected format: {}",
358         version_string);
359     let error_string = error_string;
360
361     let components: Vec<&str> = version_string.trim().split('.').collect();
362
363     if components.len() != 2 {
364         panic!("{}", error_string);
365     }
366
367     let major: isize = components[0].parse().ok().expect(&error_string);
368     let minor: isize = components[1].parse().ok().expect(&error_string);
369
370     return major * 1000 + minor;
371 }
372
373 pub fn lldb_version_to_int(version_string: &str) -> isize {
374     let error_string = format!(
375         "Encountered LLDB version string with unexpected format: {}",
376         version_string);
377     let error_string = error_string;
378     let major: isize = version_string.parse().ok().expect(&error_string);
379     return major;
380 }