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