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