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,
31 pub fn from_file(config: &Config, testfile: &Path) -> Self {
32 let mut props = EarlyProps {
41 props.ignore || parse_name_directive(ln, "ignore-test") ||
42 parse_name_directive(ln, &ignore_target(config)) ||
43 parse_name_directive(ln, &ignore_architecture(config)) ||
44 parse_name_directive(ln, &ignore_stage(config)) ||
45 parse_name_directive(ln, &ignore_env(config)) ||
46 (config.mode == common::Pretty && parse_name_directive(ln, "ignore-pretty")) ||
47 (config.target != config.host &&
48 parse_name_directive(ln, "ignore-cross-compile")) ||
49 ignore_gdb(config, ln) ||
50 ignore_lldb(config, ln) ||
51 ignore_llvm(config, ln);
53 props.should_fail = props.should_fail || parse_name_directive(ln, "should-fail");
58 fn ignore_target(config: &Config) -> String {
59 format!("ignore-{}", util::get_os(&config.target))
61 fn ignore_architecture(config: &Config) -> String {
62 format!("ignore-{}", util::get_arch(&config.target))
64 fn ignore_stage(config: &Config) -> String {
65 format!("ignore-{}", config.stage_id.split('-').next().unwrap())
67 fn ignore_env(config: &Config) -> String {
69 util::get_env(&config.target).unwrap_or("<unknown>"))
71 fn ignore_gdb(config: &Config, line: &str) -> bool {
72 if config.mode != common::DebugInfoGdb {
76 if !line.contains("ignore-gdb-version") &&
77 parse_name_directive(line, "ignore-gdb") {
81 if let Some(actual_version) = config.gdb_version {
82 if line.contains("min-gdb-version") {
83 let (start_ver, end_ver) = extract_gdb_version_range(line);
85 if start_ver != end_ver {
86 panic!("Expected single GDB version")
88 // Ignore if actual version is smaller the minimum required
90 actual_version < start_ver
91 } else if line.contains("ignore-gdb-version") {
92 let (min_version, max_version) = extract_gdb_version_range(line);
94 if max_version < min_version {
95 panic!("Malformed GDB version range: max < min")
98 actual_version >= min_version && actual_version <= max_version
107 // Takes a directive of the form "ignore-gdb-version <version1> [- <version2>]",
108 // returns the numeric representation of <version1> and <version2> as
109 // tuple: (<version1> as u32, <version2> as u32)
110 // If the <version2> part is omitted, the second component of the tuple
111 // is the same as <version1>.
112 fn extract_gdb_version_range(line: &str) -> (u32, u32) {
113 const ERROR_MESSAGE: &'static str = "Malformed GDB version directive";
115 let range_components = line.split(' ')
116 .flat_map(|word| word.split('-'))
117 .filter(|word| word.len() > 0)
118 .skip_while(|word| extract_gdb_version(word).is_none())
119 .collect::<Vec<&str>>();
121 match range_components.len() {
123 let v = extract_gdb_version(range_components[0]).unwrap();
127 let v_min = extract_gdb_version(range_components[0]).unwrap();
128 let v_max = extract_gdb_version(range_components[1]).expect(ERROR_MESSAGE);
131 _ => panic!(ERROR_MESSAGE),
135 fn ignore_lldb(config: &Config, line: &str) -> bool {
136 if config.mode != common::DebugInfoLldb {
140 if parse_name_directive(line, "ignore-lldb") {
144 if let Some(ref actual_version) = config.lldb_version {
145 if line.contains("min-lldb-version") {
146 let min_version = line.trim()
149 .expect("Malformed lldb version directive");
150 // Ignore if actual version is smaller the minimum required
152 lldb_version_to_int(actual_version) < lldb_version_to_int(min_version)
161 fn ignore_llvm(config: &Config, line: &str) -> bool {
162 if let Some(ref actual_version) = config.llvm_version {
163 if line.contains("min-llvm-version") {
164 let min_version = line.trim()
167 .expect("Malformed llvm version directive");
168 // Ignore if actual version is smaller the minimum required
170 &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 cfail test must actually compile without errors.
226 pub must_compile_successfully: bool,
227 // rustdoc will test the output of the `--test` option
228 pub check_test_line_numbers_match: bool,
232 pub fn new() -> Self {
234 error_patterns: vec![],
235 compile_flags: vec![],
243 build_aux_docs: false,
246 no_prefer_dynamic: false,
247 pretty_expanded: false,
248 pretty_mode: format!("normal"),
249 pretty_compare_only: false,
250 forbid_output: vec![],
251 incremental_dir: None,
252 must_compile_successfully: false,
253 check_test_line_numbers_match: false,
257 pub fn from_aux_file(&self, testfile: &Path, cfg: Option<&str>) -> Self {
258 let mut props = TestProps::new();
260 // copy over select properties to the aux build:
261 props.incremental_dir = self.incremental_dir.clone();
262 props.load_from(testfile, cfg);
267 pub fn from_file(testfile: &Path) -> Self {
268 let mut props = TestProps::new();
269 props.load_from(testfile, None);
273 /// Load properties from `testfile` into `props`. If a property is
274 /// tied to a particular revision `foo` (indicated by writing
275 /// `//[foo]`), then the property is ignored unless `cfg` is
277 pub fn load_from(&mut self, testfile: &Path, cfg: Option<&str>) {
278 iter_header(testfile,
281 if let Some(ep) = parse_error_pattern(ln) {
282 self.error_patterns.push(ep);
285 if let Some(flags) = parse_compile_flags(ln) {
286 self.compile_flags.extend(flags.split_whitespace()
287 .map(|s| s.to_owned()));
290 if let Some(r) = parse_revisions(ln) {
291 self.revisions.extend(r);
294 if self.run_flags.is_none() {
295 self.run_flags = parse_run_flags(ln);
298 if self.pp_exact.is_none() {
299 self.pp_exact = parse_pp_exact(ln, testfile);
302 if !self.build_aux_docs {
303 self.build_aux_docs = parse_build_aux_docs(ln);
306 if !self.force_host {
307 self.force_host = parse_force_host(ln);
310 if !self.check_stdout {
311 self.check_stdout = parse_check_stdout(ln);
314 if !self.no_prefer_dynamic {
315 self.no_prefer_dynamic = parse_no_prefer_dynamic(ln);
318 if !self.pretty_expanded {
319 self.pretty_expanded = parse_pretty_expanded(ln);
322 if let Some(m) = parse_pretty_mode(ln) {
323 self.pretty_mode = m;
326 if !self.pretty_compare_only {
327 self.pretty_compare_only = parse_pretty_compare_only(ln);
330 if let Some(ab) = parse_aux_build(ln) {
331 self.aux_builds.push(ab);
334 if let Some(ee) = parse_env(ln, "exec-env") {
335 self.exec_env.push(ee);
338 if let Some(ee) = parse_env(ln, "rustc-env") {
339 self.rustc_env.push(ee);
342 if let Some(cl) = parse_check_line(ln) {
343 self.check_lines.push(cl);
346 if let Some(of) = parse_forbid_output(ln) {
347 self.forbid_output.push(of);
350 if !self.must_compile_successfully {
351 self.must_compile_successfully = parse_must_compile_successfully(ln);
354 if !self.check_test_line_numbers_match {
355 self.check_test_line_numbers_match = parse_check_test_line_numbers_match(ln);
359 for key in vec!["RUST_TEST_NOCAPTURE", "RUST_TEST_THREADS"] {
360 match env::var(key) {
362 if self.exec_env.iter().find(|&&(ref x, _)| *x == key).is_none() {
363 self.exec_env.push((key.to_owned(), val))
372 fn iter_header(testfile: &Path, cfg: Option<&str>, it: &mut FnMut(&str)) {
373 if testfile.is_dir() {
376 let rdr = BufReader::new(File::open(testfile).unwrap());
377 for ln in rdr.lines() {
378 // Assume that any directives will be found before the first
379 // module or function. This doesn't seem to be an optimization
380 // with a warm page cache. Maybe with a cold one.
381 let ln = ln.unwrap();
383 if ln.starts_with("fn") || ln.starts_with("mod") {
385 } else if ln.starts_with("//[") {
386 // A comment like `//[foo]` is specific to revision `foo`
387 if let Some(close_brace) = ln.find("]") {
388 let lncfg = &ln[3..close_brace];
389 let matches = match cfg {
390 Some(s) => s == &lncfg[..],
394 it(&ln[close_brace + 1..]);
397 panic!("malformed condition directive: expected `//[foo]`, found `{}`",
400 } else if ln.starts_with("//") {
407 fn parse_error_pattern(line: &str) -> Option<String> {
408 parse_name_value_directive(line, "error-pattern")
411 fn parse_forbid_output(line: &str) -> Option<String> {
412 parse_name_value_directive(line, "forbid-output")
415 fn parse_aux_build(line: &str) -> Option<String> {
416 parse_name_value_directive(line, "aux-build")
419 fn parse_compile_flags(line: &str) -> Option<String> {
420 parse_name_value_directive(line, "compile-flags")
423 fn parse_revisions(line: &str) -> Option<Vec<String>> {
424 parse_name_value_directive(line, "revisions")
425 .map(|r| r.split_whitespace().map(|t| t.to_string()).collect())
428 fn parse_run_flags(line: &str) -> Option<String> {
429 parse_name_value_directive(line, "run-flags")
432 fn parse_check_line(line: &str) -> Option<String> {
433 parse_name_value_directive(line, "check")
436 fn parse_force_host(line: &str) -> bool {
437 parse_name_directive(line, "force-host")
440 fn parse_build_aux_docs(line: &str) -> bool {
441 parse_name_directive(line, "build-aux-docs")
444 fn parse_check_stdout(line: &str) -> bool {
445 parse_name_directive(line, "check-stdout")
448 fn parse_no_prefer_dynamic(line: &str) -> bool {
449 parse_name_directive(line, "no-prefer-dynamic")
452 fn parse_pretty_expanded(line: &str) -> bool {
453 parse_name_directive(line, "pretty-expanded")
456 fn parse_pretty_mode(line: &str) -> Option<String> {
457 parse_name_value_directive(line, "pretty-mode")
460 fn parse_pretty_compare_only(line: &str) -> bool {
461 parse_name_directive(line, "pretty-compare-only")
464 fn parse_must_compile_successfully(line: &str) -> bool {
465 parse_name_directive(line, "must-compile-successfully")
468 fn parse_check_test_line_numbers_match(line: &str) -> bool {
469 parse_name_directive(line, "check-test-line-numbers-match")
472 fn parse_env(line: &str, name: &str) -> Option<(String, String)> {
473 parse_name_value_directive(line, name).map(|nv| {
474 // nv is either FOO or FOO=BAR
475 let mut strs: Vec<String> = nv.splitn(2, '=')
480 1 => (strs.pop().unwrap(), "".to_owned()),
482 let end = strs.pop().unwrap();
483 (strs.pop().unwrap(), end)
485 n => panic!("Expected 1 or 2 strings, not {}", n),
490 fn parse_pp_exact(line: &str, testfile: &Path) -> Option<PathBuf> {
491 if let Some(s) = parse_name_value_directive(line, "pp-exact") {
492 Some(PathBuf::from(&s))
494 if parse_name_directive(line, "pp-exact") {
495 testfile.file_name().map(PathBuf::from)
502 fn parse_name_directive(line: &str, directive: &str) -> bool {
503 // This 'no-' rule is a quick hack to allow pretty-expanded and no-pretty-expanded to coexist
504 line.contains(directive) && !line.contains(&("no-".to_owned() + directive))
507 pub fn parse_name_value_directive(line: &str, directive: &str) -> Option<String> {
508 let keycolon = format!("{}:", directive);
509 if let Some(colon) = line.find(&keycolon) {
510 let value = line[(colon + keycolon.len())..line.len()].to_owned();
511 debug!("{}: {}", directive, value);
518 pub fn lldb_version_to_int(version_string: &str) -> isize {
519 let error_string = format!("Encountered LLDB version string with unexpected format: {}",
521 let error_string = error_string;
522 let major: isize = version_string.parse().ok().expect(&error_string);