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::prelude::*;
14 use std::io::BufReader;
15 use std::path::{Path, PathBuf};
17 use common::{self, Config, Mode};
20 use extract_gdb_version;
22 /// Properties which must be known very early, before actually running
24 pub struct EarlyProps {
26 pub should_fail: bool,
28 pub revisions: Vec<String>,
32 pub fn from_file(config: &Config, testfile: &Path) -> Self {
33 let mut props = EarlyProps {
40 iter_header(testfile, None, &mut |ln| {
41 // we should check if any only-<platform> exists and if it exists
42 // and does not matches the current platform, skip the test
43 props.ignore = props.ignore || config.parse_cfg_name_directive(ln, "ignore")
44 || (config.has_cfg_prefix(ln, "only")
45 && !config.parse_cfg_name_directive(ln, "only"))
46 || ignore_gdb(config, ln) || ignore_lldb(config, ln)
47 || ignore_llvm(config, ln);
49 if let Some(s) = config.parse_aux_build(ln) {
53 if let Some(r) = config.parse_revisions(ln) {
54 props.revisions.extend(r);
57 props.should_fail = props.should_fail || config.parse_name_directive(ln, "should-fail");
62 fn ignore_gdb(config: &Config, line: &str) -> bool {
63 if config.mode != common::DebugInfoGdb {
67 if let Some(actual_version) = config.gdb_version {
68 if line.starts_with("min-gdb-version") {
69 let (start_ver, end_ver) = extract_gdb_version_range(line);
71 if start_ver != end_ver {
72 panic!("Expected single GDB version")
74 // Ignore if actual version is smaller the minimum required
76 actual_version < start_ver
77 } else if line.starts_with("ignore-gdb-version") {
78 let (min_version, max_version) = extract_gdb_version_range(line);
80 if max_version < min_version {
81 panic!("Malformed GDB version range: max < min")
84 actual_version >= min_version && actual_version <= max_version
93 // Takes a directive of the form "ignore-gdb-version <version1> [- <version2>]",
94 // returns the numeric representation of <version1> and <version2> as
95 // tuple: (<version1> as u32, <version2> as u32)
96 // If the <version2> part is omitted, the second component of the tuple
97 // is the same as <version1>.
98 fn extract_gdb_version_range(line: &str) -> (u32, u32) {
99 const ERROR_MESSAGE: &'static str = "Malformed GDB version directive";
101 let range_components = line.split(&[' ', '-'][..])
102 .filter(|word| !word.is_empty())
103 .map(extract_gdb_version)
104 .skip_while(Option::is_none)
105 .take(3) // 3 or more = invalid, so take at most 3.
106 .collect::<Vec<Option<u32>>>();
108 match range_components.len() {
110 let v = range_components[0].unwrap();
114 let v_min = range_components[0].unwrap();
115 let v_max = range_components[1].expect(ERROR_MESSAGE);
118 _ => panic!(ERROR_MESSAGE),
122 fn ignore_lldb(config: &Config, line: &str) -> bool {
123 if config.mode != common::DebugInfoLldb {
127 if let Some(ref actual_version) = config.lldb_version {
128 if line.starts_with("min-lldb-version") {
129 let min_version = line.trim_right()
132 .expect("Malformed lldb version directive");
133 // Ignore if actual version is smaller the minimum required
135 lldb_version_to_int(actual_version) < lldb_version_to_int(min_version)
144 fn ignore_llvm(config: &Config, line: &str) -> bool {
145 if config.system_llvm && line.starts_with("no-system-llvm") {
148 if let Some(ref actual_version) = config.llvm_version {
149 if line.starts_with("min-llvm-version") {
150 let min_version = line.trim_right()
153 .expect("Malformed llvm version directive");
154 // Ignore if actual version is smaller the minimum required
156 &actual_version[..] < min_version
157 } else if line.starts_with("min-system-llvm-version") {
158 let min_version = line.trim_right()
161 .expect("Malformed llvm version directive");
162 // Ignore if using system LLVM and actual version
163 // is smaller the minimum required version
164 config.system_llvm && &actual_version[..] < min_version
175 #[derive(Clone, Debug)]
176 pub struct TestProps {
177 // Lines that should be expected, in order, on standard out
178 pub error_patterns: Vec<String>,
179 // Extra flags to pass to the compiler
180 pub compile_flags: Vec<String>,
181 // Extra flags to pass when the compiled code is run (such as --bench)
182 pub run_flags: Option<String>,
183 // If present, the name of a file that this test should match when
185 pub pp_exact: Option<PathBuf>,
186 // Other crates that should be compiled (typically from the same
187 // directory as the test, but for backwards compatibility reasons
188 // we also check the auxiliary directory)
189 pub aux_builds: Vec<String>,
190 // Environment settings to use for compiling
191 pub rustc_env: Vec<(String, String)>,
192 // Environment settings to use during execution
193 pub exec_env: Vec<(String, String)>,
194 // Lines to check if they appear in the expected debugger output
195 pub check_lines: Vec<String>,
196 // Build documentation for all specified aux-builds as well
197 pub build_aux_docs: bool,
198 // Flag to force a crate to be built with the host architecture
199 pub force_host: bool,
200 // Check stdout for error-pattern output as well as stderr
201 pub check_stdout: bool,
202 // Don't force a --crate-type=dylib flag on the command line
203 pub no_prefer_dynamic: bool,
204 // Run --pretty expanded when running pretty printing tests
205 pub pretty_expanded: bool,
206 // Which pretty mode are we testing with, default to 'normal'
207 pub pretty_mode: String,
208 // Only compare pretty output and don't try compiling
209 pub pretty_compare_only: bool,
210 // Patterns which must not appear in the output of a cfail test.
211 pub forbid_output: Vec<String>,
212 // Revisions to test for incremental compilation.
213 pub revisions: Vec<String>,
214 // Directory (if any) to use for incremental compilation. This is
215 // not set by end-users; rather it is set by the incremental
216 // testing harness and used when generating compilation
217 // arguments. (In particular, it propagates to the aux-builds.)
218 pub incremental_dir: Option<PathBuf>,
219 // Specifies that a test must actually compile without errors.
220 pub compile_pass: bool,
221 // rustdoc will test the output of the `--test` option
222 pub check_test_line_numbers_match: bool,
223 // The test must be compiled and run successfully. Only used in UI tests for now.
225 // Skip any codegen step and running the executable. Only for run-pass.
226 pub skip_codegen: bool,
227 // Do not pass `-Z ui-testing` to UI tests
228 pub disable_ui_testing_normalization: bool,
229 // customized normalization rules
230 pub normalize_stdout: Vec<(String, String)>,
231 pub normalize_stderr: Vec<(String, String)>,
232 pub failure_status: i32,
233 pub run_rustfix: bool,
237 pub fn new() -> Self {
239 error_patterns: vec![],
240 compile_flags: vec![],
248 build_aux_docs: false,
251 no_prefer_dynamic: false,
252 pretty_expanded: false,
253 pretty_mode: "normal".to_string(),
254 pretty_compare_only: false,
255 forbid_output: vec![],
256 incremental_dir: None,
258 check_test_line_numbers_match: false,
261 disable_ui_testing_normalization: false,
262 normalize_stdout: vec![],
263 normalize_stderr: vec![],
269 pub fn from_aux_file(&self, testfile: &Path, cfg: Option<&str>, config: &Config) -> Self {
270 let mut props = TestProps::new();
272 // copy over select properties to the aux build:
273 props.incremental_dir = self.incremental_dir.clone();
274 props.load_from(testfile, cfg, config);
279 pub fn from_file(testfile: &Path, cfg: Option<&str>, config: &Config) -> Self {
280 let mut props = TestProps::new();
281 props.load_from(testfile, cfg, config);
285 /// Load properties from `testfile` into `props`. If a property is
286 /// tied to a particular revision `foo` (indicated by writing
287 /// `//[foo]`), then the property is ignored unless `cfg` is
289 fn load_from(&mut self, testfile: &Path, cfg: Option<&str>, config: &Config) {
290 iter_header(testfile, cfg, &mut |ln| {
291 if let Some(ep) = config.parse_error_pattern(ln) {
292 self.error_patterns.push(ep);
295 if let Some(flags) = config.parse_compile_flags(ln) {
297 .extend(flags.split_whitespace().map(|s| s.to_owned()));
300 if let Some(edition) = config.parse_edition(ln) {
301 self.compile_flags.push(format!("--edition={}", edition));
304 if let Some(r) = config.parse_revisions(ln) {
305 self.revisions.extend(r);
308 if self.run_flags.is_none() {
309 self.run_flags = config.parse_run_flags(ln);
312 if self.pp_exact.is_none() {
313 self.pp_exact = config.parse_pp_exact(ln, testfile);
316 if !self.build_aux_docs {
317 self.build_aux_docs = config.parse_build_aux_docs(ln);
320 if !self.force_host {
321 self.force_host = config.parse_force_host(ln);
324 if !self.check_stdout {
325 self.check_stdout = config.parse_check_stdout(ln);
328 if !self.no_prefer_dynamic {
329 self.no_prefer_dynamic = config.parse_no_prefer_dynamic(ln);
332 if !self.pretty_expanded {
333 self.pretty_expanded = config.parse_pretty_expanded(ln);
336 if let Some(m) = config.parse_pretty_mode(ln) {
337 self.pretty_mode = m;
340 if !self.pretty_compare_only {
341 self.pretty_compare_only = config.parse_pretty_compare_only(ln);
344 if let Some(ab) = config.parse_aux_build(ln) {
345 self.aux_builds.push(ab);
348 if let Some(ee) = config.parse_env(ln, "exec-env") {
349 self.exec_env.push(ee);
352 if let Some(ee) = config.parse_env(ln, "rustc-env") {
353 self.rustc_env.push(ee);
356 if let Some(cl) = config.parse_check_line(ln) {
357 self.check_lines.push(cl);
360 if let Some(of) = config.parse_forbid_output(ln) {
361 self.forbid_output.push(of);
364 if !self.check_test_line_numbers_match {
365 self.check_test_line_numbers_match = config.parse_check_test_line_numbers_match(ln);
369 self.run_pass = config.parse_run_pass(ln);
372 if !self.compile_pass {
373 // run-pass implies must_compile_sucessfully
374 self.compile_pass = config.parse_compile_pass(ln) || self.run_pass;
377 if !self.skip_codegen {
378 self.skip_codegen = config.parse_skip_codegen(ln);
381 if !self.disable_ui_testing_normalization {
382 self.disable_ui_testing_normalization =
383 config.parse_disable_ui_testing_normalization(ln);
386 if let Some(rule) = config.parse_custom_normalization(ln, "normalize-stdout") {
387 self.normalize_stdout.push(rule);
389 if let Some(rule) = config.parse_custom_normalization(ln, "normalize-stderr") {
390 self.normalize_stderr.push(rule);
393 if let Some(code) = config.parse_failure_status(ln) {
394 self.failure_status = code;
397 if !self.run_rustfix {
398 self.run_rustfix = config.parse_run_rustfix(ln);
402 if self.failure_status == -1 {
403 self.failure_status = match config.mode {
404 Mode::RunFail => 101,
409 for key in &["RUST_TEST_NOCAPTURE", "RUST_TEST_THREADS"] {
410 if let Ok(val) = env::var(key) {
411 if self.exec_env.iter().find(|&&(ref x, _)| x == key).is_none() {
412 self.exec_env.push(((*key).to_owned(), val))
419 fn iter_header(testfile: &Path, cfg: Option<&str>, it: &mut FnMut(&str)) {
420 if testfile.is_dir() {
424 let comment = if testfile.to_string_lossy().ends_with(".rs") {
430 let comment_with_brace = comment.to_string() + "[";
432 let rdr = BufReader::new(File::open(testfile).unwrap());
433 for ln in rdr.lines() {
434 // Assume that any directives will be found before the first
435 // module or function. This doesn't seem to be an optimization
436 // with a warm page cache. Maybe with a cold one.
437 let ln = ln.unwrap();
439 if ln.starts_with("fn") || ln.starts_with("mod") {
441 } else if ln.starts_with(&comment_with_brace) {
442 // A comment like `//[foo]` is specific to revision `foo`
443 if let Some(close_brace) = ln.find(']') {
444 let open_brace = ln.find('[').unwrap();
445 let lncfg = &ln[open_brace + 1 .. close_brace];
446 let matches = match cfg {
447 Some(s) => s == &lncfg[..],
451 it(ln[(close_brace + 1)..].trim_left());
454 panic!("malformed condition directive: expected `{}foo]`, found `{}`",
455 comment_with_brace, ln)
457 } else if ln.starts_with(comment) {
458 it(ln[comment.len() ..].trim_left());
465 fn parse_error_pattern(&self, line: &str) -> Option<String> {
466 self.parse_name_value_directive(line, "error-pattern")
469 fn parse_forbid_output(&self, line: &str) -> Option<String> {
470 self.parse_name_value_directive(line, "forbid-output")
473 fn parse_aux_build(&self, line: &str) -> Option<String> {
474 self.parse_name_value_directive(line, "aux-build")
477 fn parse_compile_flags(&self, line: &str) -> Option<String> {
478 self.parse_name_value_directive(line, "compile-flags")
481 fn parse_revisions(&self, line: &str) -> Option<Vec<String>> {
482 self.parse_name_value_directive(line, "revisions")
483 .map(|r| r.split_whitespace().map(|t| t.to_string()).collect())
486 fn parse_run_flags(&self, line: &str) -> Option<String> {
487 self.parse_name_value_directive(line, "run-flags")
490 fn parse_check_line(&self, line: &str) -> Option<String> {
491 self.parse_name_value_directive(line, "check")
494 fn parse_force_host(&self, line: &str) -> bool {
495 self.parse_name_directive(line, "force-host")
498 fn parse_build_aux_docs(&self, line: &str) -> bool {
499 self.parse_name_directive(line, "build-aux-docs")
502 fn parse_check_stdout(&self, line: &str) -> bool {
503 self.parse_name_directive(line, "check-stdout")
506 fn parse_no_prefer_dynamic(&self, line: &str) -> bool {
507 self.parse_name_directive(line, "no-prefer-dynamic")
510 fn parse_pretty_expanded(&self, line: &str) -> bool {
511 self.parse_name_directive(line, "pretty-expanded")
514 fn parse_pretty_mode(&self, line: &str) -> Option<String> {
515 self.parse_name_value_directive(line, "pretty-mode")
518 fn parse_pretty_compare_only(&self, line: &str) -> bool {
519 self.parse_name_directive(line, "pretty-compare-only")
522 fn parse_failure_status(&self, line: &str) -> Option<i32> {
523 match self.parse_name_value_directive(line, "failure-status") {
524 Some(code) => code.trim().parse::<i32>().ok(),
529 fn parse_compile_pass(&self, line: &str) -> bool {
530 self.parse_name_directive(line, "compile-pass")
533 fn parse_disable_ui_testing_normalization(&self, line: &str) -> bool {
534 self.parse_name_directive(line, "disable-ui-testing-normalization")
537 fn parse_check_test_line_numbers_match(&self, line: &str) -> bool {
538 self.parse_name_directive(line, "check-test-line-numbers-match")
541 fn parse_run_pass(&self, line: &str) -> bool {
542 self.parse_name_directive(line, "run-pass")
545 fn parse_skip_codegen(&self, line: &str) -> bool {
546 self.parse_name_directive(line, "skip-codegen")
549 fn parse_env(&self, line: &str, name: &str) -> Option<(String, String)> {
550 self.parse_name_value_directive(line, name).map(|nv| {
551 // nv is either FOO or FOO=BAR
552 let mut strs: Vec<String> = nv.splitn(2, '=').map(str::to_owned).collect();
555 1 => (strs.pop().unwrap(), "".to_owned()),
557 let end = strs.pop().unwrap();
558 (strs.pop().unwrap(), end)
560 n => panic!("Expected 1 or 2 strings, not {}", n),
565 fn parse_pp_exact(&self, line: &str, testfile: &Path) -> Option<PathBuf> {
566 if let Some(s) = self.parse_name_value_directive(line, "pp-exact") {
567 Some(PathBuf::from(&s))
568 } else if self.parse_name_directive(line, "pp-exact") {
569 testfile.file_name().map(PathBuf::from)
575 fn parse_custom_normalization(&self, mut line: &str, prefix: &str) -> Option<(String, String)> {
576 if self.parse_cfg_name_directive(line, prefix) {
577 let from = match parse_normalization_string(&mut line) {
581 let to = match parse_normalization_string(&mut line) {
591 /// Parses a name-value directive which contains config-specific information, e.g. `ignore-x86`
592 /// or `normalize-stderr-32bit`. Returns `true` if the line matches it.
593 fn parse_cfg_name_directive(&self, line: &str, prefix: &str) -> bool {
594 if line.starts_with(prefix) && line.as_bytes().get(prefix.len()) == Some(&b'-') {
595 let name = line[prefix.len() + 1..]
596 .split(&[':', ' '][..])
601 util::matches_os(&self.target, name) || // target
602 name == util::get_arch(&self.target) || // architecture
603 name == util::get_pointer_width(&self.target) || // pointer width
604 name == self.stage_id.split('-').next().unwrap() || // stage
605 Some(name) == util::get_env(&self.target) || // env
607 common::DebugInfoGdb => name == "gdb",
608 common::DebugInfoLldb => name == "lldb",
609 common::Pretty => name == "pretty",
611 } || (self.target != self.host && name == "cross-compile")
617 fn has_cfg_prefix(&self, line: &str, prefix: &str) -> bool {
618 // returns whether this line contains this prefix or not. For prefix
619 // "ignore", returns true if line says "ignore-x86_64", "ignore-arch",
620 // "ignore-android" etc.
621 line.starts_with(prefix) && line.as_bytes().get(prefix.len()) == Some(&b'-')
624 fn parse_name_directive(&self, line: &str, directive: &str) -> bool {
625 // Ensure the directive is a whole word. Do not match "ignore-x86" when
626 // the line says "ignore-x86_64".
627 line.starts_with(directive) && match line.as_bytes().get(directive.len()) {
628 None | Some(&b' ') | Some(&b':') => true,
633 pub fn parse_name_value_directive(&self, line: &str, directive: &str) -> Option<String> {
634 let colon = directive.len();
635 if line.starts_with(directive) && line.as_bytes().get(colon) == Some(&b':') {
636 let value = line[(colon + 1)..].to_owned();
637 debug!("{}: {}", directive, value);
638 Some(expand_variables(value, self))
644 pub fn find_rust_src_root(&self) -> Option<PathBuf> {
645 let mut path = self.src_base.clone();
646 let path_postfix = Path::new("src/etc/lldb_batchmode.py");
649 if path.join(&path_postfix).is_file() {
657 fn parse_run_rustfix(&self, line: &str) -> bool {
658 self.parse_name_directive(line, "run-rustfix")
661 fn parse_edition(&self, line: &str) -> Option<String> {
662 self.parse_name_value_directive(line, "edition")
666 pub fn lldb_version_to_int(version_string: &str) -> isize {
667 let error_string = format!(
668 "Encountered LLDB version string with unexpected format: {}",
671 version_string.parse().expect(&error_string)
674 fn expand_variables(mut value: String, config: &Config) -> String {
675 const CWD: &'static str = "{{cwd}}";
676 const SRC_BASE: &'static str = "{{src-base}}";
677 const BUILD_BASE: &'static str = "{{build-base}}";
679 if value.contains(CWD) {
680 let cwd = env::current_dir().unwrap();
681 value = value.replace(CWD, &cwd.to_string_lossy());
684 if value.contains(SRC_BASE) {
685 value = value.replace(SRC_BASE, &config.src_base.to_string_lossy());
688 if value.contains(BUILD_BASE) {
689 value = value.replace(BUILD_BASE, &config.build_base.to_string_lossy());
695 /// Finds the next quoted string `"..."` in `line`, and extract the content from it. Move the `line`
696 /// variable after the end of the quoted string.
701 /// let mut s = "normalize-stderr-32bit: \"something (32 bits)\" -> \"something ($WORD bits)\".";
702 /// let first = parse_normalization_string(&mut s);
703 /// assert_eq!(first, Some("something (32 bits)".to_owned()));
704 /// assert_eq!(s, " -> \"something ($WORD bits)\".");
706 fn parse_normalization_string(line: &mut &str) -> Option<String> {
707 // FIXME support escapes in strings.
708 let begin = match line.find('"') {
712 let end = match line[begin..].find('"') {
713 Some(i) => i + begin,
716 let result = line[begin..end].to_owned();
717 *line = &line[end + 1..];