/
header.rs
144 lines (126 loc) · 4.2 KB
/
header.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
use common::config;
use io::ReaderUtil;
export test_props;
export load_props;
export is_test_ignored;
type test_props = {
// Lines that should be expected, in order, on standard out
error_patterns: ~[~str],
// Extra flags to pass to the compiler
compile_flags: Option<~str>,
// If present, the name of a file that this test should match when
// pretty-printed
pp_exact: Option<Path>,
// Modules from aux directory that should be compiled
aux_builds: ~[~str],
// Environment settings to use during execution
exec_env: ~[(~str,~str)]
};
// Load any test directives embedded in the file
fn load_props(testfile: &Path) -> test_props {
let mut error_patterns = ~[];
let mut aux_builds = ~[];
let mut exec_env = ~[];
let mut compile_flags = option::None;
let mut pp_exact = option::None;
for iter_header(testfile) |ln| {
match parse_error_pattern(ln) {
option::Some(ep) => vec::push(error_patterns, ep),
option::None => ()
};
if compile_flags.is_none() {
compile_flags = parse_compile_flags(ln);
}
if pp_exact.is_none() {
pp_exact = parse_pp_exact(ln, testfile);
}
do parse_aux_build(ln).iter |ab| {
vec::push(aux_builds, ab);
}
do parse_exec_env(ln).iter |ee| {
vec::push(exec_env, ee);
}
};
return {
error_patterns: error_patterns,
compile_flags: compile_flags,
pp_exact: pp_exact,
aux_builds: aux_builds,
exec_env: exec_env
};
}
fn is_test_ignored(config: config, testfile: &Path) -> bool {
let mut found = false;
for iter_header(testfile) |ln| {
if parse_name_directive(ln, ~"xfail-test") { return true; }
if parse_name_directive(ln, xfail_target()) { return true; }
if config.mode == common::mode_pretty &&
parse_name_directive(ln, ~"xfail-pretty") { return true; }
};
return found;
fn xfail_target() -> ~str {
~"xfail-" + os::sysname()
}
}
fn iter_header(testfile: &Path, it: fn(~str) -> bool) -> bool {
let rdr = io::file_reader(testfile).get();
while !rdr.eof() {
let ln = rdr.read_line();
// Assume that any directives will be found before the first
// module or function. This doesn't seem to be an optimization
// with a warm page cache. Maybe with a cold one.
if str::starts_with(ln, ~"fn")
|| str::starts_with(ln, ~"mod") {
return false;
} else { if !(it(ln)) { return false; } }
}
return true;
}
fn parse_error_pattern(line: ~str) -> Option<~str> {
parse_name_value_directive(line, ~"error-pattern")
}
fn parse_aux_build(line: ~str) -> Option<~str> {
parse_name_value_directive(line, ~"aux-build")
}
fn parse_compile_flags(line: ~str) -> Option<~str> {
parse_name_value_directive(line, ~"compile-flags")
}
fn parse_exec_env(line: ~str) -> Option<(~str, ~str)> {
do parse_name_value_directive(line, ~"exec-env").map |nv| {
// nv is either FOO or FOO=BAR
let strs = str::splitn_char(nv, '=', 1u);
match strs.len() {
1u => (strs[0], ~""),
2u => (strs[0], strs[1]),
n => fail fmt!("Expected 1 or 2 strings, not %u", n)
}
}
}
fn parse_pp_exact(line: ~str, testfile: &Path) -> Option<Path> {
match parse_name_value_directive(line, ~"pp-exact") {
option::Some(s) => option::Some(Path(s)),
option::None => {
if parse_name_directive(line, ~"pp-exact") {
option::Some(testfile.file_path())
} else {
option::None
}
}
}
}
fn parse_name_directive(line: ~str, directive: ~str) -> bool {
str::contains(line, directive)
}
fn parse_name_value_directive(line: ~str,
directive: ~str) -> Option<~str> unsafe {
let keycolon = directive + ~":";
match str::find_str(line, keycolon) {
option::Some(colon) => {
let value = str::slice(line, colon + str::len(keycolon),
str::len(line));
debug!("%s: %s", directive, value);
option::Some(value)
}
option::None => option::None
}
}