]>
Commit | Line | Data |
---|---|---|
a7813a04 XL |
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 std::env; | |
12 | use std::fs::File; | |
13 | use std::io::BufReader; | |
14 | use std::io::prelude::*; | |
15 | use std::path::{Path, PathBuf}; | |
16 | ||
17 | use common::Config; | |
18 | use common; | |
19 | use util; | |
20 | ||
c30ab7b3 SL |
21 | use extract_gdb_version; |
22 | ||
a7813a04 XL |
23 | /// Properties which must be known very early, before actually running |
24 | /// the test. | |
25 | pub struct EarlyProps { | |
26 | pub ignore: bool, | |
27 | pub should_fail: bool, | |
8bb4bdeb | 28 | pub aux: Vec<String>, |
a7813a04 XL |
29 | } |
30 | ||
31 | impl EarlyProps { | |
32 | pub fn from_file(config: &Config, testfile: &Path) -> Self { | |
33 | let mut props = EarlyProps { | |
34 | ignore: false, | |
35 | should_fail: false, | |
8bb4bdeb | 36 | aux: Vec::new(), |
a7813a04 XL |
37 | }; |
38 | ||
5bcae85e SL |
39 | iter_header(testfile, |
40 | None, | |
41 | &mut |ln| { | |
a7813a04 | 42 | props.ignore = |
5bcae85e | 43 | props.ignore || parse_name_directive(ln, "ignore-test") || |
a7813a04 XL |
44 | parse_name_directive(ln, &ignore_target(config)) || |
45 | parse_name_directive(ln, &ignore_architecture(config)) || | |
46 | parse_name_directive(ln, &ignore_stage(config)) || | |
47 | parse_name_directive(ln, &ignore_env(config)) || | |
5bcae85e | 48 | (config.mode == common::Pretty && parse_name_directive(ln, "ignore-pretty")) || |
a7813a04 XL |
49 | (config.target != config.host && |
50 | parse_name_directive(ln, "ignore-cross-compile")) || | |
9e0c209e SL |
51 | ignore_gdb(config, ln) || |
52 | ignore_lldb(config, ln) || | |
53 | ignore_llvm(config, ln); | |
a7813a04 | 54 | |
8bb4bdeb XL |
55 | if let Some(s) = parse_aux_build(ln) { |
56 | props.aux.push(s); | |
57 | } | |
58 | ||
5bcae85e | 59 | props.should_fail = props.should_fail || parse_name_directive(ln, "should-fail"); |
a7813a04 XL |
60 | }); |
61 | ||
62 | return props; | |
63 | ||
64 | fn ignore_target(config: &Config) -> String { | |
65 | format!("ignore-{}", util::get_os(&config.target)) | |
66 | } | |
67 | fn ignore_architecture(config: &Config) -> String { | |
68 | format!("ignore-{}", util::get_arch(&config.target)) | |
69 | } | |
70 | fn ignore_stage(config: &Config) -> String { | |
5bcae85e | 71 | format!("ignore-{}", config.stage_id.split('-').next().unwrap()) |
a7813a04 XL |
72 | } |
73 | fn ignore_env(config: &Config) -> String { | |
5bcae85e SL |
74 | format!("ignore-{}", |
75 | util::get_env(&config.target).unwrap_or("<unknown>")) | |
a7813a04 XL |
76 | } |
77 | fn ignore_gdb(config: &Config, line: &str) -> bool { | |
78 | if config.mode != common::DebugInfoGdb { | |
79 | return false; | |
80 | } | |
81 | ||
32a655c1 SL |
82 | if !line.contains("ignore-gdb-version") && |
83 | parse_name_directive(line, "ignore-gdb") { | |
a7813a04 XL |
84 | return true; |
85 | } | |
86 | ||
c30ab7b3 | 87 | if let Some(actual_version) = config.gdb_version { |
a7813a04 | 88 | if line.contains("min-gdb-version") { |
32a655c1 SL |
89 | let (start_ver, end_ver) = extract_gdb_version_range(line); |
90 | ||
91 | if start_ver != end_ver { | |
92 | panic!("Expected single GDB version") | |
93 | } | |
a7813a04 XL |
94 | // Ignore if actual version is smaller the minimum required |
95 | // version | |
32a655c1 SL |
96 | actual_version < start_ver |
97 | } else if line.contains("ignore-gdb-version") { | |
98 | let (min_version, max_version) = extract_gdb_version_range(line); | |
99 | ||
100 | if max_version < min_version { | |
101 | panic!("Malformed GDB version range: max < min") | |
102 | } | |
103 | ||
104 | actual_version >= min_version && actual_version <= max_version | |
a7813a04 XL |
105 | } else { |
106 | false | |
107 | } | |
108 | } else { | |
109 | false | |
110 | } | |
111 | } | |
112 | ||
32a655c1 SL |
113 | // Takes a directive of the form "ignore-gdb-version <version1> [- <version2>]", |
114 | // returns the numeric representation of <version1> and <version2> as | |
115 | // tuple: (<version1> as u32, <version2> as u32) | |
116 | // If the <version2> part is omitted, the second component of the tuple | |
117 | // is the same as <version1>. | |
118 | fn extract_gdb_version_range(line: &str) -> (u32, u32) { | |
119 | const ERROR_MESSAGE: &'static str = "Malformed GDB version directive"; | |
120 | ||
121 | let range_components = line.split(' ') | |
122 | .flat_map(|word| word.split('-')) | |
123 | .filter(|word| word.len() > 0) | |
124 | .skip_while(|word| extract_gdb_version(word).is_none()) | |
125 | .collect::<Vec<&str>>(); | |
126 | ||
127 | match range_components.len() { | |
128 | 1 => { | |
129 | let v = extract_gdb_version(range_components[0]).unwrap(); | |
130 | (v, v) | |
131 | } | |
132 | 2 => { | |
133 | let v_min = extract_gdb_version(range_components[0]).unwrap(); | |
134 | let v_max = extract_gdb_version(range_components[1]).expect(ERROR_MESSAGE); | |
135 | (v_min, v_max) | |
136 | } | |
137 | _ => panic!(ERROR_MESSAGE), | |
138 | } | |
139 | } | |
140 | ||
a7813a04 XL |
141 | fn ignore_lldb(config: &Config, line: &str) -> bool { |
142 | if config.mode != common::DebugInfoLldb { | |
143 | return false; | |
144 | } | |
145 | ||
146 | if parse_name_directive(line, "ignore-lldb") { | |
147 | return true; | |
148 | } | |
149 | ||
150 | if let Some(ref actual_version) = config.lldb_version { | |
151 | if line.contains("min-lldb-version") { | |
152 | let min_version = line.trim() | |
5bcae85e SL |
153 | .split(' ') |
154 | .last() | |
155 | .expect("Malformed lldb version directive"); | |
a7813a04 XL |
156 | // Ignore if actual version is smaller the minimum required |
157 | // version | |
5bcae85e | 158 | lldb_version_to_int(actual_version) < lldb_version_to_int(min_version) |
a7813a04 XL |
159 | } else { |
160 | false | |
161 | } | |
162 | } else { | |
163 | false | |
164 | } | |
165 | } | |
9e0c209e SL |
166 | |
167 | fn ignore_llvm(config: &Config, line: &str) -> bool { | |
168 | if let Some(ref actual_version) = config.llvm_version { | |
169 | if line.contains("min-llvm-version") { | |
170 | let min_version = line.trim() | |
171 | .split(' ') | |
172 | .last() | |
173 | .expect("Malformed llvm version directive"); | |
174 | // Ignore if actual version is smaller the minimum required | |
175 | // version | |
176 | &actual_version[..] < min_version | |
177 | } else { | |
178 | false | |
179 | } | |
180 | } else { | |
181 | false | |
182 | } | |
183 | } | |
a7813a04 XL |
184 | } |
185 | } | |
186 | ||
187 | #[derive(Clone, Debug)] | |
188 | pub struct TestProps { | |
189 | // Lines that should be expected, in order, on standard out | |
5bcae85e | 190 | pub error_patterns: Vec<String>, |
a7813a04 XL |
191 | // Extra flags to pass to the compiler |
192 | pub compile_flags: Vec<String>, | |
193 | // Extra flags to pass when the compiled code is run (such as --bench) | |
194 | pub run_flags: Option<String>, | |
195 | // If present, the name of a file that this test should match when | |
196 | // pretty-printed | |
197 | pub pp_exact: Option<PathBuf>, | |
198 | // Other crates that should be compiled (typically from the same | |
199 | // directory as the test, but for backwards compatibility reasons | |
200 | // we also check the auxiliary directory) | |
5bcae85e | 201 | pub aux_builds: Vec<String>, |
a7813a04 | 202 | // Environment settings to use for compiling |
5bcae85e | 203 | pub rustc_env: Vec<(String, String)>, |
a7813a04 | 204 | // Environment settings to use during execution |
5bcae85e | 205 | pub exec_env: Vec<(String, String)>, |
a7813a04 | 206 | // Lines to check if they appear in the expected debugger output |
5bcae85e | 207 | pub check_lines: Vec<String>, |
a7813a04 XL |
208 | // Build documentation for all specified aux-builds as well |
209 | pub build_aux_docs: bool, | |
210 | // Flag to force a crate to be built with the host architecture | |
211 | pub force_host: bool, | |
212 | // Check stdout for error-pattern output as well as stderr | |
213 | pub check_stdout: bool, | |
214 | // Don't force a --crate-type=dylib flag on the command line | |
215 | pub no_prefer_dynamic: bool, | |
216 | // Run --pretty expanded when running pretty printing tests | |
217 | pub pretty_expanded: bool, | |
218 | // Which pretty mode are we testing with, default to 'normal' | |
219 | pub pretty_mode: String, | |
220 | // Only compare pretty output and don't try compiling | |
221 | pub pretty_compare_only: bool, | |
222 | // Patterns which must not appear in the output of a cfail test. | |
223 | pub forbid_output: Vec<String>, | |
224 | // Revisions to test for incremental compilation. | |
225 | pub revisions: Vec<String>, | |
226 | // Directory (if any) to use for incremental compilation. This is | |
227 | // not set by end-users; rather it is set by the incremental | |
228 | // testing harness and used when generating compilation | |
229 | // arguments. (In particular, it propagates to the aux-builds.) | |
230 | pub incremental_dir: Option<PathBuf>, | |
9e0c209e SL |
231 | // Specifies that a cfail test must actually compile without errors. |
232 | pub must_compile_successfully: bool, | |
8bb4bdeb XL |
233 | // rustdoc will test the output of the `--test` option |
234 | pub check_test_line_numbers_match: bool, | |
a7813a04 XL |
235 | } |
236 | ||
237 | impl TestProps { | |
238 | pub fn new() -> Self { | |
a7813a04 | 239 | TestProps { |
9e0c209e | 240 | error_patterns: vec![], |
a7813a04 | 241 | compile_flags: vec![], |
9e0c209e SL |
242 | run_flags: None, |
243 | pp_exact: None, | |
244 | aux_builds: vec![], | |
a7813a04 XL |
245 | revisions: vec![], |
246 | rustc_env: vec![], | |
9e0c209e SL |
247 | exec_env: vec![], |
248 | check_lines: vec![], | |
249 | build_aux_docs: false, | |
250 | force_host: false, | |
251 | check_stdout: false, | |
252 | no_prefer_dynamic: false, | |
253 | pretty_expanded: false, | |
a7813a04 | 254 | pretty_mode: format!("normal"), |
9e0c209e SL |
255 | pretty_compare_only: false, |
256 | forbid_output: vec![], | |
a7813a04 | 257 | incremental_dir: None, |
9e0c209e | 258 | must_compile_successfully: false, |
8bb4bdeb | 259 | check_test_line_numbers_match: false, |
a7813a04 XL |
260 | } |
261 | } | |
262 | ||
263 | pub fn from_aux_file(&self, testfile: &Path, cfg: Option<&str>) -> Self { | |
264 | let mut props = TestProps::new(); | |
265 | ||
266 | // copy over select properties to the aux build: | |
267 | props.incremental_dir = self.incremental_dir.clone(); | |
268 | props.load_from(testfile, cfg); | |
269 | ||
270 | props | |
271 | } | |
272 | ||
273 | pub fn from_file(testfile: &Path) -> Self { | |
274 | let mut props = TestProps::new(); | |
275 | props.load_from(testfile, None); | |
276 | props | |
277 | } | |
278 | ||
279 | /// Load properties from `testfile` into `props`. If a property is | |
280 | /// tied to a particular revision `foo` (indicated by writing | |
281 | /// `//[foo]`), then the property is ignored unless `cfg` is | |
282 | /// `Some("foo")`. | |
5bcae85e SL |
283 | pub fn load_from(&mut self, testfile: &Path, cfg: Option<&str>) { |
284 | iter_header(testfile, | |
285 | cfg, | |
286 | &mut |ln| { | |
a7813a04 XL |
287 | if let Some(ep) = parse_error_pattern(ln) { |
288 | self.error_patterns.push(ep); | |
289 | } | |
290 | ||
291 | if let Some(flags) = parse_compile_flags(ln) { | |
5bcae85e SL |
292 | self.compile_flags.extend(flags.split_whitespace() |
293 | .map(|s| s.to_owned())); | |
a7813a04 XL |
294 | } |
295 | ||
296 | if let Some(r) = parse_revisions(ln) { | |
297 | self.revisions.extend(r); | |
298 | } | |
299 | ||
300 | if self.run_flags.is_none() { | |
301 | self.run_flags = parse_run_flags(ln); | |
302 | } | |
303 | ||
304 | if self.pp_exact.is_none() { | |
305 | self.pp_exact = parse_pp_exact(ln, testfile); | |
306 | } | |
307 | ||
308 | if !self.build_aux_docs { | |
309 | self.build_aux_docs = parse_build_aux_docs(ln); | |
310 | } | |
311 | ||
312 | if !self.force_host { | |
313 | self.force_host = parse_force_host(ln); | |
314 | } | |
315 | ||
316 | if !self.check_stdout { | |
317 | self.check_stdout = parse_check_stdout(ln); | |
318 | } | |
319 | ||
320 | if !self.no_prefer_dynamic { | |
321 | self.no_prefer_dynamic = parse_no_prefer_dynamic(ln); | |
322 | } | |
323 | ||
324 | if !self.pretty_expanded { | |
325 | self.pretty_expanded = parse_pretty_expanded(ln); | |
326 | } | |
327 | ||
328 | if let Some(m) = parse_pretty_mode(ln) { | |
329 | self.pretty_mode = m; | |
330 | } | |
331 | ||
332 | if !self.pretty_compare_only { | |
333 | self.pretty_compare_only = parse_pretty_compare_only(ln); | |
334 | } | |
335 | ||
5bcae85e | 336 | if let Some(ab) = parse_aux_build(ln) { |
a7813a04 XL |
337 | self.aux_builds.push(ab); |
338 | } | |
339 | ||
340 | if let Some(ee) = parse_env(ln, "exec-env") { | |
341 | self.exec_env.push(ee); | |
342 | } | |
343 | ||
344 | if let Some(ee) = parse_env(ln, "rustc-env") { | |
345 | self.rustc_env.push(ee); | |
346 | } | |
347 | ||
5bcae85e | 348 | if let Some(cl) = parse_check_line(ln) { |
a7813a04 XL |
349 | self.check_lines.push(cl); |
350 | } | |
351 | ||
352 | if let Some(of) = parse_forbid_output(ln) { | |
353 | self.forbid_output.push(of); | |
354 | } | |
9e0c209e SL |
355 | |
356 | if !self.must_compile_successfully { | |
357 | self.must_compile_successfully = parse_must_compile_successfully(ln); | |
358 | } | |
8bb4bdeb XL |
359 | |
360 | if !self.check_test_line_numbers_match { | |
361 | self.check_test_line_numbers_match = parse_check_test_line_numbers_match(ln); | |
362 | } | |
a7813a04 XL |
363 | }); |
364 | ||
365 | for key in vec!["RUST_TEST_NOCAPTURE", "RUST_TEST_THREADS"] { | |
366 | match env::var(key) { | |
5bcae85e | 367 | Ok(val) => { |
a7813a04 XL |
368 | if self.exec_env.iter().find(|&&(ref x, _)| *x == key).is_none() { |
369 | self.exec_env.push((key.to_owned(), val)) | |
5bcae85e SL |
370 | } |
371 | } | |
a7813a04 XL |
372 | Err(..) => {} |
373 | } | |
374 | } | |
375 | } | |
376 | } | |
377 | ||
5bcae85e | 378 | fn iter_header(testfile: &Path, cfg: Option<&str>, it: &mut FnMut(&str)) { |
a7813a04 | 379 | if testfile.is_dir() { |
5bcae85e | 380 | return; |
a7813a04 XL |
381 | } |
382 | let rdr = BufReader::new(File::open(testfile).unwrap()); | |
383 | for ln in rdr.lines() { | |
384 | // Assume that any directives will be found before the first | |
385 | // module or function. This doesn't seem to be an optimization | |
386 | // with a warm page cache. Maybe with a cold one. | |
387 | let ln = ln.unwrap(); | |
388 | let ln = ln.trim(); | |
389 | if ln.starts_with("fn") || ln.starts_with("mod") { | |
390 | return; | |
391 | } else if ln.starts_with("//[") { | |
392 | // A comment like `//[foo]` is specific to revision `foo` | |
393 | if let Some(close_brace) = ln.find("]") { | |
394 | let lncfg = &ln[3..close_brace]; | |
395 | let matches = match cfg { | |
396 | Some(s) => s == &lncfg[..], | |
397 | None => false, | |
398 | }; | |
399 | if matches { | |
5bcae85e | 400 | it(&ln[close_brace + 1..]); |
a7813a04 XL |
401 | } |
402 | } else { | |
403 | panic!("malformed condition directive: expected `//[foo]`, found `{}`", | |
404 | ln) | |
405 | } | |
406 | } else if ln.starts_with("//") { | |
407 | it(&ln[2..]); | |
408 | } | |
409 | } | |
410 | return; | |
411 | } | |
412 | ||
413 | fn parse_error_pattern(line: &str) -> Option<String> { | |
414 | parse_name_value_directive(line, "error-pattern") | |
415 | } | |
416 | ||
417 | fn parse_forbid_output(line: &str) -> Option<String> { | |
418 | parse_name_value_directive(line, "forbid-output") | |
419 | } | |
420 | ||
421 | fn parse_aux_build(line: &str) -> Option<String> { | |
422 | parse_name_value_directive(line, "aux-build") | |
423 | } | |
424 | ||
425 | fn parse_compile_flags(line: &str) -> Option<String> { | |
426 | parse_name_value_directive(line, "compile-flags") | |
427 | } | |
428 | ||
429 | fn parse_revisions(line: &str) -> Option<Vec<String>> { | |
430 | parse_name_value_directive(line, "revisions") | |
431 | .map(|r| r.split_whitespace().map(|t| t.to_string()).collect()) | |
432 | } | |
433 | ||
434 | fn parse_run_flags(line: &str) -> Option<String> { | |
435 | parse_name_value_directive(line, "run-flags") | |
436 | } | |
437 | ||
438 | fn parse_check_line(line: &str) -> Option<String> { | |
439 | parse_name_value_directive(line, "check") | |
440 | } | |
441 | ||
442 | fn parse_force_host(line: &str) -> bool { | |
443 | parse_name_directive(line, "force-host") | |
444 | } | |
445 | ||
446 | fn parse_build_aux_docs(line: &str) -> bool { | |
447 | parse_name_directive(line, "build-aux-docs") | |
448 | } | |
449 | ||
450 | fn parse_check_stdout(line: &str) -> bool { | |
451 | parse_name_directive(line, "check-stdout") | |
452 | } | |
453 | ||
454 | fn parse_no_prefer_dynamic(line: &str) -> bool { | |
455 | parse_name_directive(line, "no-prefer-dynamic") | |
456 | } | |
457 | ||
458 | fn parse_pretty_expanded(line: &str) -> bool { | |
459 | parse_name_directive(line, "pretty-expanded") | |
460 | } | |
461 | ||
462 | fn parse_pretty_mode(line: &str) -> Option<String> { | |
463 | parse_name_value_directive(line, "pretty-mode") | |
464 | } | |
465 | ||
466 | fn parse_pretty_compare_only(line: &str) -> bool { | |
467 | parse_name_directive(line, "pretty-compare-only") | |
468 | } | |
469 | ||
9e0c209e SL |
470 | fn parse_must_compile_successfully(line: &str) -> bool { |
471 | parse_name_directive(line, "must-compile-successfully") | |
472 | } | |
473 | ||
8bb4bdeb XL |
474 | fn parse_check_test_line_numbers_match(line: &str) -> bool { |
475 | parse_name_directive(line, "check-test-line-numbers-match") | |
476 | } | |
477 | ||
a7813a04 XL |
478 | fn parse_env(line: &str, name: &str) -> Option<(String, String)> { |
479 | parse_name_value_directive(line, name).map(|nv| { | |
480 | // nv is either FOO or FOO=BAR | |
5bcae85e SL |
481 | let mut strs: Vec<String> = nv.splitn(2, '=') |
482 | .map(str::to_owned) | |
483 | .collect(); | |
a7813a04 XL |
484 | |
485 | match strs.len() { | |
5bcae85e SL |
486 | 1 => (strs.pop().unwrap(), "".to_owned()), |
487 | 2 => { | |
488 | let end = strs.pop().unwrap(); | |
489 | (strs.pop().unwrap(), end) | |
490 | } | |
491 | n => panic!("Expected 1 or 2 strings, not {}", n), | |
a7813a04 XL |
492 | } |
493 | }) | |
494 | } | |
495 | ||
496 | fn parse_pp_exact(line: &str, testfile: &Path) -> Option<PathBuf> { | |
497 | if let Some(s) = parse_name_value_directive(line, "pp-exact") { | |
498 | Some(PathBuf::from(&s)) | |
499 | } else { | |
500 | if parse_name_directive(line, "pp-exact") { | |
501 | testfile.file_name().map(PathBuf::from) | |
502 | } else { | |
503 | None | |
504 | } | |
505 | } | |
506 | } | |
507 | ||
508 | fn parse_name_directive(line: &str, directive: &str) -> bool { | |
509 | // This 'no-' rule is a quick hack to allow pretty-expanded and no-pretty-expanded to coexist | |
510 | line.contains(directive) && !line.contains(&("no-".to_owned() + directive)) | |
511 | } | |
512 | ||
5bcae85e | 513 | pub fn parse_name_value_directive(line: &str, directive: &str) -> Option<String> { |
a7813a04 XL |
514 | let keycolon = format!("{}:", directive); |
515 | if let Some(colon) = line.find(&keycolon) { | |
5bcae85e | 516 | let value = line[(colon + keycolon.len())..line.len()].to_owned(); |
a7813a04 XL |
517 | debug!("{}: {}", directive, value); |
518 | Some(value) | |
519 | } else { | |
520 | None | |
521 | } | |
522 | } | |
523 | ||
a7813a04 | 524 | pub fn lldb_version_to_int(version_string: &str) -> isize { |
5bcae85e SL |
525 | let error_string = format!("Encountered LLDB version string with unexpected format: {}", |
526 | version_string); | |
a7813a04 XL |
527 | let error_string = error_string; |
528 | let major: isize = version_string.parse().ok().expect(&error_string); | |
529 | return major; | |
530 | } |