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.
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.
13 use std::io::BufReader;
14 use std::io::prelude::*;
15 use std::path::{Path, PathBuf};
21 use extract_gdb_version;
23 /// Properties which must be known very early, before actually running
25 pub struct EarlyProps {
27 pub should_fail: bool,
32 pub fn from_file(config: &Config, testfile: &Path) -> Self {
33 let mut props = EarlyProps {
43 props.ignore || parse_name_directive(ln, "ignore-test") ||
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)) ||
48 (config.mode == common::Pretty && parse_name_directive(ln, "ignore-pretty")) ||
49 (config.target != config.host &&
50 parse_name_directive(ln, "ignore-cross-compile")) ||
51 ignore_gdb(config, ln) ||
52 ignore_lldb(config, ln) ||
53 ignore_llvm(config, ln);
55 if let Some(s) = parse_aux_build(ln) {
59 props.should_fail = props.should_fail || parse_name_directive(ln, "should-fail");
64 fn ignore_target(config: &Config) -> String {
65 format!("ignore-{}", util::get_os(&config.target))
67 fn ignore_architecture(config: &Config) -> String {
68 format!("ignore-{}", util::get_arch(&config.target))
70 fn ignore_stage(config: &Config) -> String {
71 format!("ignore-{}", config.stage_id.split('-').next().unwrap())
73 fn ignore_env(config: &Config) -> String {
75 util::get_env(&config.target).unwrap_or("<unknown>"))
77 fn ignore_gdb(config: &Config, line: &str) -> bool {
78 if config.mode != common::DebugInfoGdb {
82 if !line.contains("ignore-gdb-version") &&
83 parse_name_directive(line, "ignore-gdb") {
87 if let Some(actual_version) = config.gdb_version {
88 if line.contains("min-gdb-version") {
89 let (start_ver, end_ver) = extract_gdb_version_range(line);
91 if start_ver != end_ver {
92 panic!("Expected single GDB version")
94 // Ignore if actual version is smaller the minimum required
96 actual_version < start_ver
97 } else if line.contains("ignore-gdb-version") {
98 let (min_version, max_version) = extract_gdb_version_range(line);
100 if max_version < min_version {
101 panic!("Malformed GDB version range: max < min")
104 actual_version >= min_version && actual_version <= max_version
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";
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>>();
127 match range_components.len() {
129 let v = extract_gdb_version(range_components[0]).unwrap();
133 let v_min = extract_gdb_version(range_components[0]).unwrap();
134 let v_max = extract_gdb_version(range_components[1]).expect(ERROR_MESSAGE);
137 _ => panic!(ERROR_MESSAGE),
141 fn ignore_lldb(config: &Config, line: &str) -> bool {
142 if config.mode != common::DebugInfoLldb {
146 if parse_name_directive(line, "ignore-lldb") {
150 if let Some(ref actual_version) = config.lldb_version {
151 if line.contains("min-lldb-version") {
152 let min_version = line.trim()
155 .expect("Malformed lldb version directive");
156 // Ignore if actual version is smaller the minimum required
158 lldb_version_to_int(actual_version) < lldb_version_to_int(min_version)
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()
173 .expect("Malformed llvm version directive");
174 // Ignore if actual version is smaller the minimum required
176 &actual_version[..] < min_version
187 #[derive(Clone, Debug)]
188 pub struct TestProps {
189 // Lines that should be expected, in order, on standard out
190 pub error_patterns: Vec<String>,
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
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)
201 pub aux_builds: Vec<String>,
202 // Environment settings to use for compiling
203 pub rustc_env: Vec<(String, String)>,
204 // Environment settings to use during execution
205 pub exec_env: Vec<(String, String)>,
206 // Lines to check if they appear in the expected debugger output
207 pub check_lines: Vec<String>,
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>,
231 // Specifies that a cfail test must actually compile without errors.
232 pub must_compile_successfully: bool,
233 // rustdoc will test the output of the `--test` option
234 pub check_test_line_numbers_match: bool,
238 pub fn new() -> Self {
240 error_patterns: vec![],
241 compile_flags: vec![],
249 build_aux_docs: false,
252 no_prefer_dynamic: false,
253 pretty_expanded: false,
254 pretty_mode: format!("normal"),
255 pretty_compare_only: false,
256 forbid_output: vec![],
257 incremental_dir: None,
258 must_compile_successfully: false,
259 check_test_line_numbers_match: false,
263 pub fn from_aux_file(&self, testfile: &Path, cfg: Option<&str>) -> Self {
264 let mut props = TestProps::new();
266 // copy over select properties to the aux build:
267 props.incremental_dir = self.incremental_dir.clone();
268 props.load_from(testfile, cfg);
273 pub fn from_file(testfile: &Path) -> Self {
274 let mut props = TestProps::new();
275 props.load_from(testfile, None);
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
283 pub fn load_from(&mut self, testfile: &Path, cfg: Option<&str>) {
284 iter_header(testfile,
287 if let Some(ep) = parse_error_pattern(ln) {
288 self.error_patterns.push(ep);
291 if let Some(flags) = parse_compile_flags(ln) {
292 self.compile_flags.extend(flags.split_whitespace()
293 .map(|s| s.to_owned()));
296 if let Some(r) = parse_revisions(ln) {
297 self.revisions.extend(r);
300 if self.run_flags.is_none() {
301 self.run_flags = parse_run_flags(ln);
304 if self.pp_exact.is_none() {
305 self.pp_exact = parse_pp_exact(ln, testfile);
308 if !self.build_aux_docs {
309 self.build_aux_docs = parse_build_aux_docs(ln);
312 if !self.force_host {
313 self.force_host = parse_force_host(ln);
316 if !self.check_stdout {
317 self.check_stdout = parse_check_stdout(ln);
320 if !self.no_prefer_dynamic {
321 self.no_prefer_dynamic = parse_no_prefer_dynamic(ln);
324 if !self.pretty_expanded {
325 self.pretty_expanded = parse_pretty_expanded(ln);
328 if let Some(m) = parse_pretty_mode(ln) {
329 self.pretty_mode = m;
332 if !self.pretty_compare_only {
333 self.pretty_compare_only = parse_pretty_compare_only(ln);
336 if let Some(ab) = parse_aux_build(ln) {
337 self.aux_builds.push(ab);
340 if let Some(ee) = parse_env(ln, "exec-env") {
341 self.exec_env.push(ee);
344 if let Some(ee) = parse_env(ln, "rustc-env") {
345 self.rustc_env.push(ee);
348 if let Some(cl) = parse_check_line(ln) {
349 self.check_lines.push(cl);
352 if let Some(of) = parse_forbid_output(ln) {
353 self.forbid_output.push(of);
356 if !self.must_compile_successfully {
357 self.must_compile_successfully = parse_must_compile_successfully(ln);
360 if !self.check_test_line_numbers_match {
361 self.check_test_line_numbers_match = parse_check_test_line_numbers_match(ln);
365 for key in vec!["RUST_TEST_NOCAPTURE", "RUST_TEST_THREADS"] {
366 match env::var(key) {
368 if self.exec_env.iter().find(|&&(ref x, _)| *x == key).is_none() {
369 self.exec_env.push((key.to_owned(), val))
378 fn iter_header(testfile: &Path, cfg: Option<&str>, it: &mut FnMut(&str)) {
379 if testfile.is_dir() {
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();
389 if ln.starts_with("fn") || ln.starts_with("mod") {
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[..],
400 it(&ln[close_brace + 1..]);
403 panic!("malformed condition directive: expected `//[foo]`, found `{}`",
406 } else if ln.starts_with("//") {
413 fn parse_error_pattern(line: &str) -> Option<String> {
414 parse_name_value_directive(line, "error-pattern")
417 fn parse_forbid_output(line: &str) -> Option<String> {
418 parse_name_value_directive(line, "forbid-output")
421 fn parse_aux_build(line: &str) -> Option<String> {
422 parse_name_value_directive(line, "aux-build")
425 fn parse_compile_flags(line: &str) -> Option<String> {
426 parse_name_value_directive(line, "compile-flags")
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())
434 fn parse_run_flags(line: &str) -> Option<String> {
435 parse_name_value_directive(line, "run-flags")
438 fn parse_check_line(line: &str) -> Option<String> {
439 parse_name_value_directive(line, "check")
442 fn parse_force_host(line: &str) -> bool {
443 parse_name_directive(line, "force-host")
446 fn parse_build_aux_docs(line: &str) -> bool {
447 parse_name_directive(line, "build-aux-docs")
450 fn parse_check_stdout(line: &str) -> bool {
451 parse_name_directive(line, "check-stdout")
454 fn parse_no_prefer_dynamic(line: &str) -> bool {
455 parse_name_directive(line, "no-prefer-dynamic")
458 fn parse_pretty_expanded(line: &str) -> bool {
459 parse_name_directive(line, "pretty-expanded")
462 fn parse_pretty_mode(line: &str) -> Option<String> {
463 parse_name_value_directive(line, "pretty-mode")
466 fn parse_pretty_compare_only(line: &str) -> bool {
467 parse_name_directive(line, "pretty-compare-only")
470 fn parse_must_compile_successfully(line: &str) -> bool {
471 parse_name_directive(line, "must-compile-successfully")
474 fn parse_check_test_line_numbers_match(line: &str) -> bool {
475 parse_name_directive(line, "check-test-line-numbers-match")
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
481 let mut strs: Vec<String> = nv.splitn(2, '=')
486 1 => (strs.pop().unwrap(), "".to_owned()),
488 let end = strs.pop().unwrap();
489 (strs.pop().unwrap(), end)
491 n => panic!("Expected 1 or 2 strings, not {}", n),
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))
500 if parse_name_directive(line, "pp-exact") {
501 testfile.file_name().map(PathBuf::from)
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))
513 pub fn parse_name_value_directive(line: &str, directive: &str) -> Option<String> {
514 let keycolon = format!("{}:", directive);
515 if let Some(colon) = line.find(&keycolon) {
516 let value = line[(colon + keycolon.len())..line.len()].to_owned();
517 debug!("{}: {}", directive, value);
524 pub fn lldb_version_to_int(version_string: &str) -> isize {
525 let error_string = format!("Encountered LLDB version string with unexpected format: {}",
527 let error_string = error_string;
528 let major: isize = version_string.parse().ok().expect(&error_string);