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