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