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,
29 pub revisions: Vec<String>,
33 pub fn from_file(config: &Config, testfile: &Path) -> Self {
34 let mut props = EarlyProps {
44 // we should check if any only-<platform> exists and if it exists
45 // and does not matches the current platform, skip the test
48 config.parse_cfg_name_directive(ln, "ignore") ||
49 (config.has_cfg_prefix(ln, "only") &&
50 !config.parse_cfg_name_directive(ln, "only")) ||
51 ignore_gdb(config, ln) ||
52 ignore_lldb(config, ln) ||
53 ignore_llvm(config, ln);
55 if let Some(s) = config.parse_aux_build(ln) {
59 if let Some(r) = config.parse_revisions(ln) {
60 props.revisions.extend(r);
63 props.should_fail = props.should_fail || config.parse_name_directive(ln, "should-fail");
68 fn ignore_gdb(config: &Config, line: &str) -> bool {
69 if config.mode != common::DebugInfoGdb {
73 if let Some(actual_version) = config.gdb_version {
74 if line.starts_with("min-gdb-version") {
75 let (start_ver, end_ver) = extract_gdb_version_range(line);
77 if start_ver != end_ver {
78 panic!("Expected single GDB version")
80 // Ignore if actual version is smaller the minimum required
82 actual_version < start_ver
83 } else if line.starts_with("ignore-gdb-version") {
84 let (min_version, max_version) = extract_gdb_version_range(line);
86 if max_version < min_version {
87 panic!("Malformed GDB version range: max < min")
90 actual_version >= min_version && actual_version <= max_version
99 // Takes a directive of the form "ignore-gdb-version <version1> [- <version2>]",
100 // returns the numeric representation of <version1> and <version2> as
101 // tuple: (<version1> as u32, <version2> as u32)
102 // If the <version2> part is omitted, the second component of the tuple
103 // is the same as <version1>.
104 fn extract_gdb_version_range(line: &str) -> (u32, u32) {
105 const ERROR_MESSAGE: &'static str = "Malformed GDB version directive";
107 let range_components = line.split(&[' ', '-'][..])
108 .filter(|word| !word.is_empty())
109 .map(extract_gdb_version)
110 .skip_while(Option::is_none)
111 .take(3) // 3 or more = invalid, so take at most 3.
112 .collect::<Vec<Option<u32>>>();
114 match range_components.len() {
116 let v = range_components[0].unwrap();
120 let v_min = range_components[0].unwrap();
121 let v_max = range_components[1].expect(ERROR_MESSAGE);
124 _ => panic!(ERROR_MESSAGE),
128 fn ignore_lldb(config: &Config, line: &str) -> bool {
129 if config.mode != common::DebugInfoLldb {
133 if let Some(ref actual_version) = config.lldb_version {
134 if line.starts_with("min-lldb-version") {
135 let min_version = line.trim_right()
138 .expect("Malformed lldb version directive");
139 // Ignore if actual version is smaller the minimum required
141 lldb_version_to_int(actual_version) < lldb_version_to_int(min_version)
150 fn ignore_llvm(config: &Config, line: &str) -> bool {
151 if config.system_llvm && line.starts_with("no-system-llvm") {
154 if let Some(ref actual_version) = config.llvm_version {
155 if line.starts_with("min-llvm-version") {
156 let min_version = line.trim_right()
159 .expect("Malformed llvm version directive");
160 // Ignore if actual version is smaller the minimum required
162 &actual_version[..] < min_version
163 } else if line.starts_with("min-system-llvm-version") {
164 let min_version = line.trim_right()
167 .expect("Malformed llvm version directive");
168 // Ignore if using system LLVM and actual version
169 // is smaller the minimum required version
170 config.system_llvm && &actual_version[..] < min_version
181 #[derive(Clone, Debug)]
182 pub struct TestProps {
183 // Lines that should be expected, in order, on standard out
184 pub error_patterns: Vec<String>,
185 // Extra flags to pass to the compiler
186 pub compile_flags: Vec<String>,
187 // Extra flags to pass when the compiled code is run (such as --bench)
188 pub run_flags: Option<String>,
189 // If present, the name of a file that this test should match when
191 pub pp_exact: Option<PathBuf>,
192 // Other crates that should be compiled (typically from the same
193 // directory as the test, but for backwards compatibility reasons
194 // we also check the auxiliary directory)
195 pub aux_builds: Vec<String>,
196 // Environment settings to use for compiling
197 pub rustc_env: Vec<(String, String)>,
198 // Environment settings to use during execution
199 pub exec_env: Vec<(String, String)>,
200 // Lines to check if they appear in the expected debugger output
201 pub check_lines: Vec<String>,
202 // Build documentation for all specified aux-builds as well
203 pub build_aux_docs: bool,
204 // Flag to force a crate to be built with the host architecture
205 pub force_host: bool,
206 // Check stdout for error-pattern output as well as stderr
207 pub check_stdout: bool,
208 // Don't force a --crate-type=dylib flag on the command line
209 pub no_prefer_dynamic: bool,
210 // Run --pretty expanded when running pretty printing tests
211 pub pretty_expanded: bool,
212 // Which pretty mode are we testing with, default to 'normal'
213 pub pretty_mode: String,
214 // Only compare pretty output and don't try compiling
215 pub pretty_compare_only: bool,
216 // Patterns which must not appear in the output of a cfail test.
217 pub forbid_output: Vec<String>,
218 // Revisions to test for incremental compilation.
219 pub revisions: Vec<String>,
220 // Directory (if any) to use for incremental compilation. This is
221 // not set by end-users; rather it is set by the incremental
222 // testing harness and used when generating compilation
223 // arguments. (In particular, it propagates to the aux-builds.)
224 pub incremental_dir: Option<PathBuf>,
225 // Specifies that a test must actually compile without errors.
226 pub compile_pass: bool,
227 // rustdoc will test the output of the `--test` option
228 pub check_test_line_numbers_match: bool,
229 // The test must be compiled and run successfully. Only used in UI tests for now.
231 // Skip any codegen step and running the executable. Only for run-pass.
232 pub skip_trans: bool,
233 // Do not pass `-Z ui-testing` to UI tests
234 pub disable_ui_testing_normalization: bool,
235 // customized normalization rules
236 pub normalize_stdout: Vec<(String, String)>,
237 pub normalize_stderr: Vec<(String, String)>,
238 pub failure_status: i32,
239 pub run_rustfix: bool,
243 pub fn new() -> Self {
245 error_patterns: vec![],
246 compile_flags: vec![],
254 build_aux_docs: false,
257 no_prefer_dynamic: false,
258 pretty_expanded: false,
259 pretty_mode: "normal".to_string(),
260 pretty_compare_only: false,
261 forbid_output: vec![],
262 incremental_dir: None,
264 check_test_line_numbers_match: false,
267 disable_ui_testing_normalization: false,
268 normalize_stdout: vec![],
269 normalize_stderr: vec![],
275 pub fn from_aux_file(&self,
280 let mut props = TestProps::new();
282 // copy over select properties to the aux build:
283 props.incremental_dir = self.incremental_dir.clone();
284 props.load_from(testfile, cfg, config);
289 pub fn from_file(testfile: &Path, cfg: Option<&str>, config: &Config) -> Self {
290 let mut props = TestProps::new();
291 props.load_from(testfile, cfg, config);
295 /// Load properties from `testfile` into `props`. If a property is
296 /// tied to a particular revision `foo` (indicated by writing
297 /// `//[foo]`), then the property is ignored unless `cfg` is
299 fn load_from(&mut self,
303 iter_header(testfile,
306 if let Some(ep) = config.parse_error_pattern(ln) {
307 self.error_patterns.push(ep);
310 if let Some(flags) = config.parse_compile_flags(ln) {
311 self.compile_flags.extend(flags.split_whitespace()
312 .map(|s| s.to_owned()));
315 if let Some(r) = config.parse_revisions(ln) {
316 self.revisions.extend(r);
319 if self.run_flags.is_none() {
320 self.run_flags = config.parse_run_flags(ln);
323 if self.pp_exact.is_none() {
324 self.pp_exact = config.parse_pp_exact(ln, testfile);
327 if !self.build_aux_docs {
328 self.build_aux_docs = config.parse_build_aux_docs(ln);
331 if !self.force_host {
332 self.force_host = config.parse_force_host(ln);
335 if !self.check_stdout {
336 self.check_stdout = config.parse_check_stdout(ln);
339 if !self.no_prefer_dynamic {
340 self.no_prefer_dynamic = config.parse_no_prefer_dynamic(ln);
343 if !self.pretty_expanded {
344 self.pretty_expanded = config.parse_pretty_expanded(ln);
347 if let Some(m) = config.parse_pretty_mode(ln) {
348 self.pretty_mode = m;
351 if !self.pretty_compare_only {
352 self.pretty_compare_only = config.parse_pretty_compare_only(ln);
355 if let Some(ab) = config.parse_aux_build(ln) {
356 self.aux_builds.push(ab);
359 if let Some(ee) = config.parse_env(ln, "exec-env") {
360 self.exec_env.push(ee);
363 if let Some(ee) = config.parse_env(ln, "rustc-env") {
364 self.rustc_env.push(ee);
367 if let Some(cl) = config.parse_check_line(ln) {
368 self.check_lines.push(cl);
371 if let Some(of) = config.parse_forbid_output(ln) {
372 self.forbid_output.push(of);
375 if !self.check_test_line_numbers_match {
376 self.check_test_line_numbers_match = config.parse_check_test_line_numbers_match(ln);
380 self.run_pass = config.parse_run_pass(ln);
383 if !self.compile_pass {
384 // run-pass implies must_compile_sucessfully
386 config.parse_compile_pass(ln) || self.run_pass;
389 if !self.skip_trans {
390 self.skip_trans = config.parse_skip_trans(ln);
393 if !self.disable_ui_testing_normalization {
394 self.disable_ui_testing_normalization =
395 config.parse_disable_ui_testing_normalization(ln);
398 if let Some(rule) = config.parse_custom_normalization(ln, "normalize-stdout") {
399 self.normalize_stdout.push(rule);
401 if let Some(rule) = config.parse_custom_normalization(ln, "normalize-stderr") {
402 self.normalize_stderr.push(rule);
405 if let Some(code) = config.parse_failure_status(ln) {
406 self.failure_status = code;
409 if !self.run_rustfix {
410 self.run_rustfix = config.parse_run_rustfix(ln);
414 for key in &["RUST_TEST_NOCAPTURE", "RUST_TEST_THREADS"] {
415 if let Ok(val) = env::var(key) {
416 if self.exec_env.iter().find(|&&(ref x, _)| x == key).is_none() {
417 self.exec_env.push(((*key).to_owned(), val))
424 fn iter_header(testfile: &Path, cfg: Option<&str>, it: &mut FnMut(&str)) {
425 if testfile.is_dir() {
428 let rdr = BufReader::new(File::open(testfile).unwrap());
429 for ln in rdr.lines() {
430 // Assume that any directives will be found before the first
431 // module or function. This doesn't seem to be an optimization
432 // with a warm page cache. Maybe with a cold one.
433 let ln = ln.unwrap();
435 if ln.starts_with("fn") || ln.starts_with("mod") {
437 } else if ln.starts_with("//[") {
438 // A comment like `//[foo]` is specific to revision `foo`
439 if let Some(close_brace) = ln.find(']') {
440 let lncfg = &ln[3..close_brace];
441 let matches = match cfg {
442 Some(s) => s == &lncfg[..],
446 it(ln[(close_brace + 1) ..].trim_left());
449 panic!("malformed condition directive: expected `//[foo]`, found `{}`",
452 } else if ln.starts_with("//") {
453 it(ln[2..].trim_left());
460 fn parse_error_pattern(&self, line: &str) -> Option<String> {
461 self.parse_name_value_directive(line, "error-pattern")
464 fn parse_forbid_output(&self, line: &str) -> Option<String> {
465 self.parse_name_value_directive(line, "forbid-output")
468 fn parse_aux_build(&self, line: &str) -> Option<String> {
469 self.parse_name_value_directive(line, "aux-build")
472 fn parse_compile_flags(&self, line: &str) -> Option<String> {
473 self.parse_name_value_directive(line, "compile-flags")
476 fn parse_revisions(&self, line: &str) -> Option<Vec<String>> {
477 self.parse_name_value_directive(line, "revisions")
478 .map(|r| r.split_whitespace().map(|t| t.to_string()).collect())
481 fn parse_run_flags(&self, line: &str) -> Option<String> {
482 self.parse_name_value_directive(line, "run-flags")
485 fn parse_check_line(&self, line: &str) -> Option<String> {
486 self.parse_name_value_directive(line, "check")
489 fn parse_force_host(&self, line: &str) -> bool {
490 self.parse_name_directive(line, "force-host")
493 fn parse_build_aux_docs(&self, line: &str) -> bool {
494 self.parse_name_directive(line, "build-aux-docs")
497 fn parse_check_stdout(&self, line: &str) -> bool {
498 self.parse_name_directive(line, "check-stdout")
501 fn parse_no_prefer_dynamic(&self, line: &str) -> bool {
502 self.parse_name_directive(line, "no-prefer-dynamic")
505 fn parse_pretty_expanded(&self, line: &str) -> bool {
506 self.parse_name_directive(line, "pretty-expanded")
509 fn parse_pretty_mode(&self, line: &str) -> Option<String> {
510 self.parse_name_value_directive(line, "pretty-mode")
513 fn parse_pretty_compare_only(&self, line: &str) -> bool {
514 self.parse_name_directive(line, "pretty-compare-only")
517 fn parse_failure_status(&self, line: &str) -> Option<i32> {
518 match self.parse_name_value_directive(line, "failure-status") {
519 Some(code) => code.trim().parse::<i32>().ok(),
524 fn parse_compile_pass(&self, line: &str) -> bool {
525 self.parse_name_directive(line, "compile-pass")
528 fn parse_disable_ui_testing_normalization(&self, line: &str) -> bool {
529 self.parse_name_directive(line, "disable-ui-testing-normalization")
532 fn parse_check_test_line_numbers_match(&self, line: &str) -> bool {
533 self.parse_name_directive(line, "check-test-line-numbers-match")
536 fn parse_run_pass(&self, line: &str) -> bool {
537 self.parse_name_directive(line, "run-pass")
540 fn parse_skip_trans(&self, line: &str) -> bool {
541 self.parse_name_directive(line, "skip-trans")
544 fn parse_env(&self, line: &str, name: &str) -> Option<(String, String)> {
545 self.parse_name_value_directive(line, name).map(|nv| {
546 // nv is either FOO or FOO=BAR
547 let mut strs: Vec<String> = nv.splitn(2, '=')
552 1 => (strs.pop().unwrap(), "".to_owned()),
554 let end = strs.pop().unwrap();
555 (strs.pop().unwrap(), end)
557 n => panic!("Expected 1 or 2 strings, not {}", n),
562 fn parse_pp_exact(&self, line: &str, testfile: &Path) -> Option<PathBuf> {
563 if let Some(s) = self.parse_name_value_directive(line, "pp-exact") {
564 Some(PathBuf::from(&s))
565 } else if self.parse_name_directive(line, "pp-exact") {
566 testfile.file_name().map(PathBuf::from)
572 fn parse_custom_normalization(&self, mut line: &str, prefix: &str) -> Option<(String, String)> {
573 if self.parse_cfg_name_directive(line, prefix) {
574 let from = match parse_normalization_string(&mut line) {
578 let to = match parse_normalization_string(&mut line) {
588 /// Parses a name-value directive which contains config-specific information, e.g. `ignore-x86`
589 /// or `normalize-stderr-32bit`. Returns `true` if the line matches it.
590 fn parse_cfg_name_directive(&self, line: &str, prefix: &str) -> bool {
591 if line.starts_with(prefix) && line.as_bytes().get(prefix.len()) == Some(&b'-') {
592 let name = line[prefix.len()+1 ..].split(&[':', ' '][..]).next().unwrap();
595 util::matches_os(&self.target, name) || // target
596 name == util::get_arch(&self.target) || // architecture
597 name == util::get_pointer_width(&self.target) || // pointer width
598 name == self.stage_id.split('-').next().unwrap() || // stage
599 Some(name) == util::get_env(&self.target) || // env
601 common::DebugInfoGdb => name == "gdb",
602 common::DebugInfoLldb => name == "lldb",
603 common::Pretty => name == "pretty",
606 (self.target != self.host && name == "cross-compile")
612 fn has_cfg_prefix(&self, line: &str, prefix: &str) -> bool {
613 // returns whether this line contains this prefix or not. For prefix
614 // "ignore", returns true if line says "ignore-x86_64", "ignore-arch",
615 // "ignore-android" etc.
616 line.starts_with(prefix) && line.as_bytes().get(prefix.len()) == Some(&b'-')
619 fn parse_name_directive(&self, line: &str, directive: &str) -> bool {
620 // Ensure the directive is a whole word. Do not match "ignore-x86" when
621 // the line says "ignore-x86_64".
622 line.starts_with(directive) && match line.as_bytes().get(directive.len()) {
623 None | Some(&b' ') | Some(&b':') => true,
628 pub fn parse_name_value_directive(&self, line: &str, directive: &str) -> Option<String> {
629 let colon = directive.len();
630 if line.starts_with(directive) && line.as_bytes().get(colon) == Some(&b':') {
631 let value = line[(colon + 1) ..].to_owned();
632 debug!("{}: {}", directive, value);
633 Some(expand_variables(value, self))
639 pub fn find_rust_src_root(&self) -> Option<PathBuf> {
640 let mut path = self.src_base.clone();
641 let path_postfix = Path::new("src/etc/lldb_batchmode.py");
644 if path.join(&path_postfix).is_file() {
652 fn parse_run_rustfix(&self, line: &str) -> bool {
653 self.parse_name_directive(line, "run-rustfix")
657 pub fn lldb_version_to_int(version_string: &str) -> isize {
658 let error_string = format!("Encountered LLDB version string with unexpected format: {}",
660 version_string.parse().expect(&error_string)
663 fn expand_variables(mut value: String, config: &Config) -> String {
664 const CWD: &'static str = "{{cwd}}";
665 const SRC_BASE: &'static str = "{{src-base}}";
666 const BUILD_BASE: &'static str = "{{build-base}}";
668 if value.contains(CWD) {
669 let cwd = env::current_dir().unwrap();
670 value = value.replace(CWD, &cwd.to_string_lossy());
673 if value.contains(SRC_BASE) {
674 value = value.replace(SRC_BASE, &config.src_base.to_string_lossy());
677 if value.contains(BUILD_BASE) {
678 value = value.replace(BUILD_BASE, &config.build_base.to_string_lossy());
684 /// Finds the next quoted string `"..."` in `line`, and extract the content from it. Move the `line`
685 /// variable after the end of the quoted string.
690 /// let mut s = "normalize-stderr-32bit: \"something (32 bits)\" -> \"something ($WORD bits)\".";
691 /// let first = parse_normalization_string(&mut s);
692 /// assert_eq!(first, Some("something (32 bits)".to_owned()));
693 /// assert_eq!(s, " -> \"something ($WORD bits)\".");
695 fn parse_normalization_string(line: &mut &str) -> Option<String> {
696 // FIXME support escapes in strings.
697 let begin = match line.find('"') {
701 let end = match line[begin..].find('"') {
702 Some(i) => i + begin,
705 let result = line[begin..end].to_owned();
706 *line = &line[end+1..];