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