]> git.lizzy.rs Git - rust.git/blob - src/compiletest/header.rs
auto merge of #12184 : JeremyLetang/rust/rustpkg-link, r=alexcrichton
[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 { true }
112     });
113
114     !val
115 }
116
117 fn iter_header(testfile: &Path, it: |&str| -> bool) -> bool {
118     use std::io::{BufferedReader, File};
119
120     let mut rdr = BufferedReader::new(File::open(testfile).unwrap());
121     for ln in rdr.lines() {
122         // Assume that any directives will be found before the first
123         // module or function. This doesn't seem to be an optimization
124         // with a warm page cache. Maybe with a cold one.
125         if ln.starts_with("fn") || ln.starts_with("mod") {
126             return true;
127         } else { if !(it(ln.trim())) { return false; } }
128     }
129     return true;
130 }
131
132 fn parse_error_pattern(line: &str) -> Option<~str> {
133     parse_name_value_directive(line, ~"error-pattern")
134 }
135
136 fn parse_aux_build(line: &str) -> Option<~str> {
137     parse_name_value_directive(line, ~"aux-build")
138 }
139
140 fn parse_compile_flags(line: &str) -> Option<~str> {
141     parse_name_value_directive(line, ~"compile-flags")
142 }
143
144 fn parse_debugger_cmd(line: &str) -> Option<~str> {
145     parse_name_value_directive(line, ~"debugger")
146 }
147
148 fn parse_check_line(line: &str) -> Option<~str> {
149     parse_name_value_directive(line, ~"check")
150 }
151
152 fn parse_force_host(line: &str) -> bool {
153     parse_name_directive(line, "force-host")
154 }
155
156 fn parse_exec_env(line: &str) -> Option<(~str, ~str)> {
157     parse_name_value_directive(line, ~"exec-env").map(|nv| {
158         // nv is either FOO or FOO=BAR
159         let mut strs: ~[~str] = nv.splitn('=', 1).map(|s| s.to_owned()).collect();
160
161         match strs.len() {
162           1u => (strs.pop().unwrap(), ~""),
163           2u => {
164               let end = strs.pop().unwrap();
165               (strs.pop().unwrap(), end)
166           }
167           n => fail!("Expected 1 or 2 strings, not {}", n)
168         }
169     })
170 }
171
172 fn parse_pp_exact(line: &str, testfile: &Path) -> Option<Path> {
173     match parse_name_value_directive(line, ~"pp-exact") {
174       Some(s) => Some(Path::new(s)),
175       None => {
176         if parse_name_directive(line, "pp-exact") {
177             testfile.filename().map(|s| Path::new(s))
178         } else {
179             None
180         }
181       }
182     }
183 }
184
185 fn parse_name_directive(line: &str, directive: &str) -> bool {
186     line.contains(directive)
187 }
188
189 fn parse_name_value_directive(line: &str,
190                               directive: ~str) -> Option<~str> {
191     let keycolon = directive + ":";
192     match line.find_str(keycolon) {
193         Some(colon) => {
194             let value = line.slice(colon + keycolon.len(),
195                                    line.len()).to_owned();
196             debug!("{}: {}", directive,  value);
197             Some(value)
198         }
199         None => None
200     }
201 }