1 // Copyright 2012-2014 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.
11 use common::CompareMode;
12 use common::{expected_output_path, UI_STDERR, UI_STDOUT, UI_FIXED};
13 use common::{output_base_dir, output_base_name, output_testname_unique};
14 use common::{Codegen, CodegenUnits, DebugInfoGdb, DebugInfoLldb, Rustdoc};
15 use common::{CompileFail, ParseFail, Pretty, RunFail, RunPass, RunPassValgrind};
16 use common::{Config, TestPaths};
17 use common::{Incremental, MirOpt, RunMake, Ui};
19 use errors::{self, Error, ErrorKind};
20 use filetime::FileTime;
21 use header::TestProps;
24 use rustfix::{apply_suggestions, get_suggestions_from_json};
25 use util::{logv, PathBufExt};
27 use std::collections::{HashMap, HashSet, VecDeque};
28 use std::collections::hash_map::DefaultHasher;
30 use std::ffi::OsString;
32 use std::fs::{self, create_dir_all, File};
33 use std::hash::{Hash, Hasher};
34 use std::io::prelude::*;
35 use std::io::{self, BufReader};
36 use std::path::{Path, PathBuf};
37 use std::process::{Child, Command, ExitStatus, Output, Stdio};
40 use extract_gdb_version;
43 fn disable_error_reporting<F: FnOnce() -> R, R>(f: F) -> R {
45 const SEM_NOGPFAULTERRORBOX: u32 = 0x0002;
47 fn SetErrorMode(mode: u32) -> u32;
51 static ref LOCK: Mutex<()> = {
55 // Error mode is a global variable, so lock it so only one thread will change it
56 let _lock = LOCK.lock().unwrap();
58 // Tell Windows to not show any UI on errors (such as terminating abnormally).
59 // This is important for running tests, since some of them use abnormal
60 // termination by design. This mode is inherited by all child processes.
62 let old_mode = SetErrorMode(SEM_NOGPFAULTERRORBOX); // read inherited flags
63 SetErrorMode(old_mode | SEM_NOGPFAULTERRORBOX);
65 SetErrorMode(old_mode);
71 fn disable_error_reporting<F: FnOnce() -> R, R>(f: F) -> R {
75 /// The name of the environment variable that holds dynamic library locations.
76 pub fn dylib_env_var() -> &'static str {
79 } else if cfg!(target_os = "macos") {
81 } else if cfg!(target_os = "haiku") {
88 #[derive(Debug, PartialEq)]
95 #[derive(Debug, PartialEq)]
98 pub lines: Vec<DiffLine>,
102 fn new(line_number: u32) -> Mismatch {
104 line_number: line_number,
110 // Produces a diff between the expected output and actual output.
111 pub fn make_diff(expected: &str, actual: &str, context_size: usize) -> Vec<Mismatch> {
112 let mut line_number = 1;
113 let mut context_queue: VecDeque<&str> = VecDeque::with_capacity(context_size);
114 let mut lines_since_mismatch = context_size + 1;
115 let mut results = Vec::new();
116 let mut mismatch = Mismatch::new(0);
118 for result in diff::lines(expected, actual) {
120 diff::Result::Left(str) => {
121 if lines_since_mismatch >= context_size && lines_since_mismatch > 0 {
122 results.push(mismatch);
123 mismatch = Mismatch::new(line_number - context_queue.len() as u32);
126 while let Some(line) = context_queue.pop_front() {
127 mismatch.lines.push(DiffLine::Context(line.to_owned()));
130 mismatch.lines.push(DiffLine::Expected(str.to_owned()));
132 lines_since_mismatch = 0;
134 diff::Result::Right(str) => {
135 if lines_since_mismatch >= context_size && lines_since_mismatch > 0 {
136 results.push(mismatch);
137 mismatch = Mismatch::new(line_number - context_queue.len() as u32);
140 while let Some(line) = context_queue.pop_front() {
141 mismatch.lines.push(DiffLine::Context(line.to_owned()));
144 mismatch.lines.push(DiffLine::Resulting(str.to_owned()));
145 lines_since_mismatch = 0;
147 diff::Result::Both(str, _) => {
148 if context_queue.len() >= context_size {
149 let _ = context_queue.pop_front();
152 if lines_since_mismatch < context_size {
153 mismatch.lines.push(DiffLine::Context(str.to_owned()));
154 } else if context_size > 0 {
155 context_queue.push_back(str);
159 lines_since_mismatch += 1;
164 results.push(mismatch);
170 pub fn run(config: Config, testpaths: &TestPaths, revision: Option<&str>) {
171 match &*config.target {
172 "arm-linux-androideabi" | "armv7-linux-androideabi" | "aarch64-linux-android" => {
173 if !config.adb_device_status {
174 panic!("android device not available");
179 // android has its own gdb handling
180 if config.mode == DebugInfoGdb && config.gdb.is_none() {
181 panic!("gdb not available but debuginfo gdb debuginfo test requested");
187 // We're going to be dumping a lot of info. Start on a new line.
190 debug!("running {:?}", testpaths.file.display());
191 let props = TestProps::from_file(&testpaths.file, revision, &config);
199 create_dir_all(&cx.output_base_dir()).unwrap();
201 if config.mode == Incremental {
202 // Incremental tests are special because they cannot be run in
205 !props.revisions.is_empty(),
206 "Incremental tests require revisions."
208 cx.init_incremental_test();
209 for revision in &props.revisions {
210 let revision_props = TestProps::from_file(&testpaths.file, Some(revision), &config);
211 let rev_cx = TestCx {
213 props: &revision_props,
215 revision: Some(revision),
217 rev_cx.run_revision();
226 pub fn compute_stamp_hash(config: &Config) -> String {
227 let mut hash = DefaultHasher::new();
228 config.stage_id.hash(&mut hash);
229 format!("{:x}", hash.finish())
232 struct TestCx<'test> {
233 config: &'test Config,
234 props: &'test TestProps,
235 testpaths: &'test TestPaths,
236 revision: Option<&'test str>,
239 struct DebuggerCommands {
240 commands: Vec<String>,
241 check_lines: Vec<String>,
242 breakpoint_lines: Vec<usize>,
245 impl<'test> TestCx<'test> {
246 /// Code executed for each revision in turn (or, if there are no
247 /// revisions, exactly once, with revision == None).
248 fn run_revision(&self) {
249 match self.config.mode {
250 CompileFail | ParseFail => self.run_cfail_test(),
251 RunFail => self.run_rfail_test(),
252 RunPass => self.run_rpass_test(),
253 RunPassValgrind => self.run_valgrind_test(),
254 Pretty => self.run_pretty_test(),
255 DebugInfoGdb => self.run_debuginfo_gdb_test(),
256 DebugInfoLldb => self.run_debuginfo_lldb_test(),
257 Codegen => self.run_codegen_test(),
258 Rustdoc => self.run_rustdoc_test(),
259 CodegenUnits => self.run_codegen_units_test(),
260 Incremental => self.run_incremental_test(),
261 RunMake => self.run_rmake_test(),
262 Ui => self.run_ui_test(),
263 MirOpt => self.run_mir_opt_test(),
267 fn check_if_test_should_compile(&self, proc_res: &ProcRes) {
268 if self.props.compile_pass {
269 if !proc_res.status.success() {
270 self.fatal_proc_rec("test compilation failed although it shouldn't!", proc_res);
273 if proc_res.status.success() {
275 &format!("{} test compiled successfully!", self.config.mode)[..],
280 self.check_correct_failure_status(proc_res);
284 fn run_cfail_test(&self) {
285 let proc_res = self.compile_test();
286 self.check_if_test_should_compile(&proc_res);
287 self.check_no_compiler_crash(&proc_res);
289 let output_to_check = self.get_output(&proc_res);
290 let expected_errors = errors::load_errors(&self.testpaths.file, self.revision);
291 if !expected_errors.is_empty() {
292 if !self.props.error_patterns.is_empty() {
293 self.fatal("both error pattern and expected errors specified");
295 self.check_expected_errors(expected_errors, &proc_res);
297 self.check_error_patterns(&output_to_check, &proc_res);
300 self.check_forbid_output(&output_to_check, &proc_res);
303 fn run_rfail_test(&self) {
304 let proc_res = self.compile_test();
306 if !proc_res.status.success() {
307 self.fatal_proc_rec("compilation failed!", &proc_res);
310 let proc_res = self.exec_compiled_test();
312 // The value our Makefile configures valgrind to return on failure
313 const VALGRIND_ERR: i32 = 100;
314 if proc_res.status.code() == Some(VALGRIND_ERR) {
315 self.fatal_proc_rec("run-fail test isn't valgrind-clean!", &proc_res);
318 let output_to_check = self.get_output(&proc_res);
319 self.check_correct_failure_status(&proc_res);
320 self.check_error_patterns(&output_to_check, &proc_res);
323 fn get_output(&self, proc_res: &ProcRes) -> String {
324 if self.props.check_stdout {
325 format!("{}{}", proc_res.stdout, proc_res.stderr)
327 proc_res.stderr.clone()
331 fn check_correct_failure_status(&self, proc_res: &ProcRes) {
332 let expected_status = Some(self.props.failure_status);
333 let received_status = proc_res.status.code();
335 if expected_status != received_status {
338 "Error: expected failure status ({:?}) but received status {:?}.",
339 expected_status, received_status
346 fn run_rpass_test(&self) {
347 let proc_res = self.compile_test();
349 if !proc_res.status.success() {
350 self.fatal_proc_rec("compilation failed!", &proc_res);
353 // FIXME(#41968): Move this check to tidy?
354 let expected_errors = errors::load_errors(&self.testpaths.file, self.revision);
356 expected_errors.is_empty(),
357 "run-pass tests with expected warnings should be moved to ui/"
360 if !self.props.skip_codegen {
361 let proc_res = self.exec_compiled_test();
362 if !proc_res.status.success() {
363 self.fatal_proc_rec("test run failed!", &proc_res);
368 fn run_valgrind_test(&self) {
369 assert!(self.revision.is_none(), "revisions not relevant here");
371 if self.config.valgrind_path.is_none() {
372 assert!(!self.config.force_valgrind);
373 return self.run_rpass_test();
376 let mut proc_res = self.compile_test();
378 if !proc_res.status.success() {
379 self.fatal_proc_rec("compilation failed!", &proc_res);
382 let mut new_config = self.config.clone();
383 new_config.runtool = new_config.valgrind_path.clone();
384 let new_cx = TestCx {
388 proc_res = new_cx.exec_compiled_test();
390 if !proc_res.status.success() {
391 self.fatal_proc_rec("test run failed!", &proc_res);
395 fn run_pretty_test(&self) {
396 if self.props.pp_exact.is_some() {
397 logv(self.config, "testing for exact pretty-printing".to_owned());
401 "testing for converging pretty-printing".to_owned(),
405 let rounds = match self.props.pp_exact {
410 let mut src = String::new();
411 File::open(&self.testpaths.file)
413 .read_to_string(&mut src)
415 let mut srcs = vec![src];
418 while round < rounds {
422 "pretty-printing round {} revision {:?}",
426 let proc_res = self.print_source(srcs[round].to_owned(), &self.props.pretty_mode);
428 if !proc_res.status.success() {
431 "pretty-printing failed in round {} revision {:?}",
438 let ProcRes { stdout, .. } = proc_res;
443 let mut expected = match self.props.pp_exact {
445 let filepath = self.testpaths.file.parent().unwrap().join(file);
446 let mut s = String::new();
447 File::open(&filepath)
449 .read_to_string(&mut s)
453 None => srcs[srcs.len() - 2].clone(),
455 let mut actual = srcs[srcs.len() - 1].clone();
457 if self.props.pp_exact.is_some() {
458 // Now we have to care about line endings
459 let cr = "\r".to_owned();
460 actual = actual.replace(&cr, "").to_owned();
461 expected = expected.replace(&cr, "").to_owned();
464 self.compare_source(&expected, &actual);
466 // If we're only making sure that the output matches then just stop here
467 if self.props.pretty_compare_only {
471 // Finally, let's make sure it actually appears to remain valid code
472 let proc_res = self.typecheck_source(actual);
473 if !proc_res.status.success() {
474 self.fatal_proc_rec("pretty-printed source does not typecheck", &proc_res);
477 if !self.props.pretty_expanded {
481 // additionally, run `--pretty expanded` and try to build it.
482 let proc_res = self.print_source(srcs[round].clone(), "expanded");
483 if !proc_res.status.success() {
484 self.fatal_proc_rec("pretty-printing (expanded) failed", &proc_res);
488 stdout: expanded_src,
491 let proc_res = self.typecheck_source(expanded_src);
492 if !proc_res.status.success() {
494 "pretty-printed source (expanded) does not typecheck",
500 fn print_source(&self, src: String, pretty_type: &str) -> ProcRes {
501 let aux_dir = self.aux_output_dir_name();
503 let mut rustc = Command::new(&self.config.rustc_path);
506 .args(&["-Z", &format!("unpretty={}", pretty_type)])
507 .args(&["--target", &self.config.target])
510 .args(self.split_maybe_args(&self.config.target_rustcflags))
511 .args(&self.props.compile_flags)
512 .envs(self.props.exec_env.clone());
514 self.compose_and_run(
516 self.config.compile_lib_path.to_str().unwrap(),
517 Some(aux_dir.to_str().unwrap()),
522 fn compare_source(&self, expected: &str, actual: &str) {
523 if expected != actual {
524 self.error("pretty-printed source does not match expected source");
528 ------------------------------------------\n\
530 ------------------------------------------\n\
532 ------------------------------------------\n\
534 ------------------------------------------\n\
541 fn typecheck_source(&self, src: String) -> ProcRes {
542 let mut rustc = Command::new(&self.config.rustc_path);
544 let out_dir = self.output_base_name().with_extension("pretty-out");
545 let _ = fs::remove_dir_all(&out_dir);
546 create_dir_all(&out_dir).unwrap();
548 let target = if self.props.force_host {
554 let aux_dir = self.aux_output_dir_name();
561 .arg(&format!("--target={}", target))
563 .arg(&self.config.build_base)
567 if let Some(revision) = self.revision {
568 rustc.args(&["--cfg", revision]);
571 rustc.args(self.split_maybe_args(&self.config.target_rustcflags));
572 rustc.args(&self.props.compile_flags);
574 self.compose_and_run_compiler(rustc, Some(src))
577 fn run_debuginfo_gdb_test(&self) {
578 assert!(self.revision.is_none(), "revisions not relevant here");
580 let config = Config {
581 target_rustcflags: self.cleanup_debug_info_options(&self.config.target_rustcflags),
582 host_rustcflags: self.cleanup_debug_info_options(&self.config.host_rustcflags),
583 ..self.config.clone()
586 let test_cx = TestCx {
591 test_cx.run_debuginfo_gdb_test_no_opt();
594 fn run_debuginfo_gdb_test_no_opt(&self) {
595 let prefixes = if self.config.gdb_native_rust {
597 static PREFIXES: &'static [&'static str] = &["gdb", "gdbr"];
598 println!("NOTE: compiletest thinks it is using GDB with native rust support");
602 static PREFIXES: &'static [&'static str] = &["gdb", "gdbg"];
603 println!("NOTE: compiletest thinks it is using GDB without native rust support");
607 let DebuggerCommands {
611 } = self.parse_debugger_commands(prefixes);
612 let mut cmds = commands.join("\n");
614 // compile test file (it should have 'compile-flags:-g' in the header)
615 let compiler_run_result = self.compile_test();
616 if !compiler_run_result.status.success() {
617 self.fatal_proc_rec("compilation failed!", &compiler_run_result);
620 let exe_file = self.make_exe_name();
622 let debugger_run_result;
623 match &*self.config.target {
624 "arm-linux-androideabi" | "armv7-linux-androideabi" | "aarch64-linux-android" => {
625 cmds = cmds.replace("run", "continue");
627 let tool_path = match self.config.android_cross_path.to_str() {
628 Some(x) => x.to_owned(),
629 None => self.fatal("cannot find android cross path"),
632 // write debugger script
633 let mut script_str = String::with_capacity(2048);
634 script_str.push_str(&format!("set charset {}\n", Self::charset()));
635 script_str.push_str(&format!("set sysroot {}\n", tool_path));
636 script_str.push_str(&format!("file {}\n", exe_file.to_str().unwrap()));
637 script_str.push_str("target remote :5039\n");
638 script_str.push_str(&format!(
639 "set solib-search-path \
640 ./{}/stage2/lib/rustlib/{}/lib/\n",
641 self.config.host, self.config.target
643 for line in &breakpoint_lines {
647 self.testpaths.file.file_name().unwrap().to_string_lossy(),
652 script_str.push_str(&cmds);
653 script_str.push_str("\nquit\n");
655 debug!("script_str = {}", script_str);
656 self.dump_output_file(&script_str, "debugger.script");
658 let adb_path = &self.config.adb_path;
660 Command::new(adb_path)
663 .arg(&self.config.adb_test_dir)
665 .expect(&format!("failed to exec `{:?}`", adb_path));
667 Command::new(adb_path)
668 .args(&["forward", "tcp:5039", "tcp:5039"])
670 .expect(&format!("failed to exec `{:?}`", adb_path));
672 let adb_arg = format!(
673 "export LD_LIBRARY_PATH={}; \
674 gdbserver{} :5039 {}/{}",
675 self.config.adb_test_dir.clone(),
676 if self.config.target.contains("aarch64") {
681 self.config.adb_test_dir.clone(),
682 exe_file.file_name().unwrap().to_str().unwrap()
685 debug!("adb arg: {}", adb_arg);
686 let mut adb = Command::new(adb_path)
687 .args(&["shell", &adb_arg])
688 .stdout(Stdio::piped())
689 .stderr(Stdio::inherit())
691 .expect(&format!("failed to exec `{:?}`", adb_path));
693 // Wait for the gdbserver to print out "Listening on port ..."
694 // at which point we know that it's started and then we can
695 // execute the debugger below.
696 let mut stdout = BufReader::new(adb.stdout.take().unwrap());
697 let mut line = String::new();
700 stdout.read_line(&mut line).unwrap();
701 if line.starts_with("Listening on port 5039") {
707 let debugger_script = self.make_out_name("debugger.script");
708 // FIXME (#9639): This needs to handle non-utf8 paths
709 let debugger_opts = vec![
713 format!("-command={}", debugger_script.to_str().unwrap()),
716 let mut gdb_path = tool_path;
717 gdb_path.push_str("/bin/gdb");
722 } = Command::new(&gdb_path)
723 .args(&debugger_opts)
725 .expect(&format!("failed to exec `{:?}`", gdb_path));
727 let mut gdb = Command::new(&format!("{}-gdb", self.config.target));
728 gdb.args(&debugger_opts);
729 let cmdline = self.make_cmdline(&gdb, "");
730 logv(self.config, format!("executing {}", cmdline));
734 debugger_run_result = ProcRes {
736 stdout: String::from_utf8(stdout).unwrap(),
737 stderr: String::from_utf8(stderr).unwrap(),
740 if adb.kill().is_err() {
741 println!("Adb process is already finished.");
746 let rust_src_root = self.config
747 .find_rust_src_root()
748 .expect("Could not find Rust source root");
749 let rust_pp_module_rel_path = Path::new("./src/etc");
750 let rust_pp_module_abs_path = rust_src_root
751 .join(rust_pp_module_rel_path)
755 // write debugger script
756 let mut script_str = String::with_capacity(2048);
757 script_str.push_str(&format!("set charset {}\n", Self::charset()));
758 script_str.push_str("show version\n");
760 match self.config.gdb_version {
763 "NOTE: compiletest thinks it is using GDB version {}",
767 if version > extract_gdb_version("7.4").unwrap() {
768 // Add the directory containing the pretty printers to
769 // GDB's script auto loading safe path
770 script_str.push_str(&format!(
771 "add-auto-load-safe-path {}\n",
772 rust_pp_module_abs_path.replace(r"\", r"\\")
778 "NOTE: compiletest does not know which version of \
784 // The following line actually doesn't have to do anything with
785 // pretty printing, it just tells GDB to print values on one line:
786 script_str.push_str("set print pretty off\n");
788 // Add the pretty printer directory to GDB's source-file search path
789 script_str.push_str(&format!("directory {}\n", rust_pp_module_abs_path));
791 // Load the target executable
792 script_str.push_str(&format!(
794 exe_file.to_str().unwrap().replace(r"\", r"\\")
797 // Force GDB to print values in the Rust format.
798 if self.config.gdb_native_rust {
799 script_str.push_str("set language rust\n");
802 // Add line breakpoints
803 for line in &breakpoint_lines {
804 script_str.push_str(&format!(
806 self.testpaths.file.file_name().unwrap().to_string_lossy(),
811 script_str.push_str(&cmds);
812 script_str.push_str("\nquit\n");
814 debug!("script_str = {}", script_str);
815 self.dump_output_file(&script_str, "debugger.script");
817 let debugger_script = self.make_out_name("debugger.script");
819 // FIXME (#9639): This needs to handle non-utf8 paths
820 let debugger_opts = vec![
824 format!("-command={}", debugger_script.to_str().unwrap()),
827 let mut gdb = Command::new(self.config.gdb.as_ref().unwrap());
828 gdb.args(&debugger_opts)
829 .env("PYTHONPATH", rust_pp_module_abs_path);
831 debugger_run_result = self.compose_and_run(
833 self.config.run_lib_path.to_str().unwrap(),
840 if !debugger_run_result.status.success() {
841 self.fatal_proc_rec("gdb failed to execute", &debugger_run_result);
844 self.check_debugger_output(&debugger_run_result, &check_lines);
847 fn run_debuginfo_lldb_test(&self) {
848 assert!(self.revision.is_none(), "revisions not relevant here");
850 if self.config.lldb_python_dir.is_none() {
851 self.fatal("Can't run LLDB test because LLDB's python path is not set.");
854 let config = Config {
855 target_rustcflags: self.cleanup_debug_info_options(&self.config.target_rustcflags),
856 host_rustcflags: self.cleanup_debug_info_options(&self.config.host_rustcflags),
857 ..self.config.clone()
860 let test_cx = TestCx {
865 test_cx.run_debuginfo_lldb_test_no_opt();
868 fn run_debuginfo_lldb_test_no_opt(&self) {
869 // compile test file (it should have 'compile-flags:-g' in the header)
870 let compile_result = self.compile_test();
871 if !compile_result.status.success() {
872 self.fatal_proc_rec("compilation failed!", &compile_result);
875 let exe_file = self.make_exe_name();
877 match self.config.lldb_version {
878 Some(ref version) => {
880 "NOTE: compiletest thinks it is using LLDB version {}",
886 "NOTE: compiletest does not know which version of \
892 // Parse debugger commands etc from test files
893 let DebuggerCommands {
898 } = self.parse_debugger_commands(&["lldb"]);
900 // Write debugger script:
901 // We don't want to hang when calling `quit` while the process is still running
902 let mut script_str = String::from("settings set auto-confirm true\n");
904 // Make LLDB emit its version, so we have it documented in the test output
905 script_str.push_str("version\n");
907 // Switch LLDB into "Rust mode"
908 let rust_src_root = self.config
909 .find_rust_src_root()
910 .expect("Could not find Rust source root");
911 let rust_pp_module_rel_path = Path::new("./src/etc/lldb_rust_formatters.py");
912 let rust_pp_module_abs_path = rust_src_root
913 .join(rust_pp_module_rel_path)
919 .push_str(&format!("command script import {}\n", &rust_pp_module_abs_path[..])[..]);
920 script_str.push_str("type summary add --no-value ");
921 script_str.push_str("--python-function lldb_rust_formatters.print_val ");
922 script_str.push_str("-x \".*\" --category Rust\n");
923 script_str.push_str("type category enable Rust\n");
925 // Set breakpoints on every line that contains the string "#break"
926 let source_file_name = self.testpaths.file.file_name().unwrap().to_string_lossy();
927 for line in &breakpoint_lines {
928 script_str.push_str(&format!(
929 "breakpoint set --file '{}' --line {}\n",
930 source_file_name, line
934 // Append the other commands
935 for line in &commands {
936 script_str.push_str(line);
937 script_str.push_str("\n");
940 // Finally, quit the debugger
941 script_str.push_str("\nquit\n");
943 // Write the script into a file
944 debug!("script_str = {}", script_str);
945 self.dump_output_file(&script_str, "debugger.script");
946 let debugger_script = self.make_out_name("debugger.script");
948 // Let LLDB execute the script via lldb_batchmode.py
949 let debugger_run_result = self.run_lldb(&exe_file, &debugger_script, &rust_src_root);
951 if !debugger_run_result.status.success() {
952 self.fatal_proc_rec("Error while running LLDB", &debugger_run_result);
955 self.check_debugger_output(&debugger_run_result, &check_lines);
960 test_executable: &Path,
961 debugger_script: &Path,
962 rust_src_root: &Path,
964 // Prepare the lldb_batchmode which executes the debugger script
965 let lldb_script_path = rust_src_root.join("src/etc/lldb_batchmode.py");
967 Command::new(&self.config.lldb_python)
968 .arg(&lldb_script_path)
969 .arg(test_executable)
970 .arg(debugger_script)
971 .env("PYTHONPATH", self.config.lldb_python_dir.as_ref().unwrap()),
975 fn cmd2procres(&self, cmd: &mut Command) -> ProcRes {
976 let (status, out, err) = match cmd.output() {
983 String::from_utf8(stdout).unwrap(),
984 String::from_utf8(stderr).unwrap(),
986 Err(e) => self.fatal(&format!(
987 "Failed to setup Python process for \
993 self.dump_output(&out, &err);
998 cmdline: format!("{:?}", cmd),
1002 fn parse_debugger_commands(&self, debugger_prefixes: &[&str]) -> DebuggerCommands {
1003 let directives = debugger_prefixes
1005 .map(|prefix| (format!("{}-command", prefix), format!("{}-check", prefix)))
1006 .collect::<Vec<_>>();
1008 let mut breakpoint_lines = vec![];
1009 let mut commands = vec![];
1010 let mut check_lines = vec![];
1011 let mut counter = 1;
1012 let reader = BufReader::new(File::open(&self.testpaths.file).unwrap());
1013 for line in reader.lines() {
1016 let line = if line.starts_with("//") {
1017 line[2..].trim_left()
1022 if line.contains("#break") {
1023 breakpoint_lines.push(counter);
1026 for &(ref command_directive, ref check_directive) in &directives {
1028 .parse_name_value_directive(&line, command_directive)
1029 .map(|cmd| commands.push(cmd));
1032 .parse_name_value_directive(&line, check_directive)
1033 .map(|cmd| check_lines.push(cmd));
1036 Err(e) => self.fatal(&format!("Error while parsing debugger commands: {}", e)),
1048 fn cleanup_debug_info_options(&self, options: &Option<String>) -> Option<String> {
1049 if options.is_none() {
1053 // Remove options that are either unwanted (-O) or may lead to duplicates due to RUSTFLAGS.
1054 let options_to_remove = ["-O".to_owned(), "-g".to_owned(), "--debuginfo".to_owned()];
1055 let new_options = self.split_maybe_args(options)
1057 .filter(|x| !options_to_remove.contains(x))
1058 .collect::<Vec<String>>();
1060 Some(new_options.join(" "))
1063 fn check_debugger_output(&self, debugger_run_result: &ProcRes, check_lines: &[String]) {
1064 let num_check_lines = check_lines.len();
1066 let mut check_line_index = 0;
1067 for line in debugger_run_result.stdout.lines() {
1068 if check_line_index >= num_check_lines {
1072 if check_single_line(line, &(check_lines[check_line_index])[..]) {
1073 check_line_index += 1;
1076 if check_line_index != num_check_lines && num_check_lines > 0 {
1077 self.fatal_proc_rec(
1079 "line not found in debugger output: {}",
1080 check_lines[check_line_index]
1082 debugger_run_result,
1086 fn check_single_line(line: &str, check_line: &str) -> bool {
1087 // Allow check lines to leave parts unspecified (e.g., uninitialized
1088 // bits in the wrong case of an enum) with the notation "[...]".
1089 let line = line.trim();
1090 let check_line = check_line.trim();
1091 let can_start_anywhere = check_line.starts_with("[...]");
1092 let can_end_anywhere = check_line.ends_with("[...]");
1094 let check_fragments: Vec<&str> = check_line
1096 .filter(|frag| !frag.is_empty())
1098 if check_fragments.is_empty() {
1102 let (mut rest, first_fragment) = if can_start_anywhere {
1103 match line.find(check_fragments[0]) {
1104 Some(pos) => (&line[pos + check_fragments[0].len()..], 1),
1105 None => return false,
1111 for current_fragment in &check_fragments[first_fragment..] {
1112 match rest.find(current_fragment) {
1114 rest = &rest[pos + current_fragment.len()..];
1116 None => return false,
1120 if !can_end_anywhere && !rest.is_empty() {
1128 fn check_error_patterns(&self, output_to_check: &str, proc_res: &ProcRes) {
1129 if self.props.error_patterns.is_empty() {
1130 if self.props.compile_pass {
1133 self.fatal(&format!(
1134 "no error pattern specified in {:?}",
1135 self.testpaths.file.display()
1139 let mut next_err_idx = 0;
1140 let mut next_err_pat = self.props.error_patterns[next_err_idx].trim();
1141 let mut done = false;
1142 for line in output_to_check.lines() {
1143 if line.contains(next_err_pat) {
1144 debug!("found error pattern {}", next_err_pat);
1146 if next_err_idx == self.props.error_patterns.len() {
1147 debug!("found all error patterns");
1151 next_err_pat = self.props.error_patterns[next_err_idx].trim();
1158 let missing_patterns = &self.props.error_patterns[next_err_idx..];
1159 if missing_patterns.len() == 1 {
1160 self.fatal_proc_rec(
1161 &format!("error pattern '{}' not found!", missing_patterns[0]),
1165 for pattern in missing_patterns {
1166 self.error(&format!("error pattern '{}' not found!", *pattern));
1168 self.fatal_proc_rec("multiple error patterns not found", proc_res);
1172 fn check_no_compiler_crash(&self, proc_res: &ProcRes) {
1173 for line in proc_res.stderr.lines() {
1174 if line.contains("error: internal compiler error") {
1175 self.fatal_proc_rec("compiler encountered internal error", proc_res);
1176 } else if line.contains(" panicked at ") {
1177 self.fatal_proc_rec("compiler panicked", proc_res);
1182 fn check_forbid_output(&self, output_to_check: &str, proc_res: &ProcRes) {
1183 for pat in &self.props.forbid_output {
1184 if output_to_check.contains(pat) {
1185 self.fatal_proc_rec("forbidden pattern found in compiler output", proc_res);
1190 fn check_expected_errors(&self, expected_errors: Vec<errors::Error>, proc_res: &ProcRes) {
1191 if proc_res.status.success()
1194 .any(|x| x.kind == Some(ErrorKind::Error))
1196 self.fatal_proc_rec("process did not return an error status", proc_res);
1199 // on windows, translate all '\' path separators to '/'
1200 let file_name = format!("{}", self.testpaths.file.display()).replace(r"\", "/");
1202 // If the testcase being checked contains at least one expected "help"
1203 // message, then we'll ensure that all "help" messages are expected.
1204 // Otherwise, all "help" messages reported by the compiler will be ignored.
1205 // This logic also applies to "note" messages.
1206 let expect_help = expected_errors
1208 .any(|ee| ee.kind == Some(ErrorKind::Help));
1209 let expect_note = expected_errors
1211 .any(|ee| ee.kind == Some(ErrorKind::Note));
1213 // Parse the JSON output from the compiler and extract out the messages.
1214 let actual_errors = json::parse_output(&file_name, &proc_res.stderr, proc_res);
1215 let mut unexpected = Vec::new();
1216 let mut found = vec![false; expected_errors.len()];
1217 for actual_error in &actual_errors {
1218 let opt_index = expected_errors.iter().enumerate().position(
1219 |(index, expected_error)| {
1220 !found[index] && actual_error.line_num == expected_error.line_num
1221 && (expected_error.kind.is_none()
1222 || actual_error.kind == expected_error.kind)
1223 && actual_error.msg.contains(&expected_error.msg)
1229 // found a match, everybody is happy
1230 assert!(!found[index]);
1231 found[index] = true;
1235 if self.is_unexpected_compiler_message(actual_error, expect_help, expect_note) {
1236 self.error(&format!(
1237 "{}:{}: unexpected {}: '{}'",
1239 actual_error.line_num,
1243 .map_or(String::from("message"), |k| k.to_string()),
1246 unexpected.push(actual_error);
1252 let mut not_found = Vec::new();
1253 // anything not yet found is a problem
1254 for (index, expected_error) in expected_errors.iter().enumerate() {
1256 self.error(&format!(
1257 "{}:{}: expected {} not found: {}",
1259 expected_error.line_num,
1263 .map_or("message".into(), |k| k.to_string()),
1266 not_found.push(expected_error);
1270 if !unexpected.is_empty() || !not_found.is_empty() {
1271 self.error(&format!(
1272 "{} unexpected errors found, {} expected errors not found",
1276 println!("status: {}\ncommand: {}", proc_res.status, proc_res.cmdline);
1277 if !unexpected.is_empty() {
1278 println!("unexpected errors (from JSON output): {:#?}\n", unexpected);
1280 if !not_found.is_empty() {
1281 println!("not found errors (from test file): {:#?}\n", not_found);
1287 /// Returns true if we should report an error about `actual_error`,
1288 /// which did not match any of the expected error. We always require
1289 /// errors/warnings to be explicitly listed, but only require
1290 /// helps/notes if there are explicit helps/notes given.
1291 fn is_unexpected_compiler_message(
1293 actual_error: &Error,
1297 match actual_error.kind {
1298 Some(ErrorKind::Help) => expect_help,
1299 Some(ErrorKind::Note) => expect_note,
1300 Some(ErrorKind::Error) | Some(ErrorKind::Warning) => true,
1301 Some(ErrorKind::Suggestion) | None => false,
1305 fn compile_test(&self) -> ProcRes {
1306 let mut rustc = self.make_compile_args(
1307 &self.testpaths.file,
1308 TargetLocation::ThisFile(self.make_exe_name()),
1311 rustc.arg("-L").arg(&self.aux_output_dir_name());
1313 match self.config.mode {
1314 CompileFail | Ui => {
1315 // compile-fail and ui tests tend to have tons of unused code as
1316 // it's just testing various pieces of the compile, but we don't
1317 // want to actually assert warnings about all this code. Instead
1318 // let's just ignore unused code warnings by defaults and tests
1319 // can turn it back on if needed.
1320 if !self.config.src_base.ends_with("rustdoc-ui") {
1321 rustc.args(&["-A", "unused"]);
1327 self.compose_and_run_compiler(rustc, None)
1330 fn document(&self, out_dir: &Path) -> ProcRes {
1331 if self.props.build_aux_docs {
1332 for rel_ab in &self.props.aux_builds {
1333 let aux_testpaths = self.compute_aux_test_paths(rel_ab);
1336 .from_aux_file(&aux_testpaths.file, self.revision, self.config);
1337 let aux_cx = TestCx {
1338 config: self.config,
1340 testpaths: &aux_testpaths,
1341 revision: self.revision,
1343 // Create the directory for the stdout/stderr files.
1344 create_dir_all(aux_cx.output_base_dir()).unwrap();
1345 let auxres = aux_cx.document(out_dir);
1346 if !auxres.status.success() {
1352 let aux_dir = self.aux_output_dir_name();
1354 let rustdoc_path = self.config
1357 .expect("--rustdoc-path passed");
1358 let mut rustdoc = Command::new(rustdoc_path);
1362 .arg(self.config.run_lib_path.to_str().unwrap())
1367 .arg(&self.testpaths.file)
1368 .args(&self.props.compile_flags);
1369 if let Some(ref linker) = self.config.linker {
1374 .arg("unstable-options");
1377 self.compose_and_run_compiler(rustdoc, None)
1380 fn exec_compiled_test(&self) -> ProcRes {
1381 let env = &self.props.exec_env;
1383 let proc_res = match &*self.config.target {
1384 // This is pretty similar to below, we're transforming:
1386 // program arg1 arg2
1390 // remote-test-client run program:support-lib.so arg1 arg2
1392 // The test-client program will upload `program` to the emulator
1393 // along with all other support libraries listed (in this case
1394 // `support-lib.so`. It will then execute the program on the
1395 // emulator with the arguments specified (in the environment we give
1396 // the process) and then report back the same result.
1397 _ if self.config.remote_test_client.is_some() => {
1398 let aux_dir = self.aux_output_dir_name();
1399 let ProcArgs { mut prog, args } = self.make_run_args();
1400 if let Ok(entries) = aux_dir.read_dir() {
1401 for entry in entries {
1402 let entry = entry.unwrap();
1403 if !entry.path().is_file() {
1407 prog.push_str(entry.path().to_str().unwrap());
1410 let mut test_client =
1411 Command::new(self.config.remote_test_client.as_ref().unwrap());
1413 .args(&["run", &prog])
1416 self.compose_and_run(
1418 self.config.run_lib_path.to_str().unwrap(),
1419 Some(aux_dir.to_str().unwrap()),
1424 let aux_dir = self.aux_output_dir_name();
1425 let ProcArgs { prog, args } = self.make_run_args();
1426 let mut program = Command::new(&prog);
1429 .current_dir(&self.output_base_dir())
1431 self.compose_and_run(
1433 self.config.run_lib_path.to_str().unwrap(),
1434 Some(aux_dir.to_str().unwrap()),
1440 if proc_res.status.success() {
1441 // delete the executable after running it to save space.
1442 // it is ok if the deletion failed.
1443 let _ = fs::remove_file(self.make_exe_name());
1449 /// For each `aux-build: foo/bar` annotation, we check to find the
1450 /// file in a `auxiliary` directory relative to the test itself.
1451 fn compute_aux_test_paths(&self, rel_ab: &str) -> TestPaths {
1452 let test_ab = self.testpaths
1455 .expect("test file path has no parent")
1458 if !test_ab.exists() {
1459 self.fatal(&format!(
1460 "aux-build `{}` source not found",
1467 relative_dir: self.testpaths
1469 .join(self.output_testname_unique())
1473 .expect("aux-build path has no parent")
1478 fn compose_and_run_compiler(&self, mut rustc: Command, input: Option<String>) -> ProcRes {
1479 let aux_dir = self.aux_output_dir_name();
1481 if !self.props.aux_builds.is_empty() {
1482 let _ = fs::remove_dir_all(&aux_dir);
1483 create_dir_all(&aux_dir).unwrap();
1486 for rel_ab in &self.props.aux_builds {
1487 let aux_testpaths = self.compute_aux_test_paths(rel_ab);
1490 .from_aux_file(&aux_testpaths.file, self.revision, self.config);
1491 let aux_output = TargetLocation::ThisDirectory(self.aux_output_dir_name());
1492 let aux_cx = TestCx {
1493 config: self.config,
1495 testpaths: &aux_testpaths,
1496 revision: self.revision,
1498 // Create the directory for the stdout/stderr files.
1499 create_dir_all(aux_cx.output_base_dir()).unwrap();
1500 let mut aux_rustc = aux_cx.make_compile_args(&aux_testpaths.file, aux_output);
1502 let crate_type = if aux_props.no_prefer_dynamic {
1504 } else if self.config.target.contains("cloudabi")
1505 || self.config.target.contains("emscripten")
1506 || (self.config.target.contains("musl") && !aux_props.force_host)
1507 || self.config.target.contains("wasm32")
1509 // We primarily compile all auxiliary libraries as dynamic libraries
1510 // to avoid code size bloat and large binaries as much as possible
1511 // for the test suite (otherwise including libstd statically in all
1512 // executables takes up quite a bit of space).
1514 // For targets like MUSL or Emscripten, however, there is no support for
1515 // dynamic libraries so we just go back to building a normal library. Note,
1516 // however, that for MUSL if the library is built with `force_host` then
1517 // it's ok to be a dylib as the host should always support dylibs.
1523 if let Some(crate_type) = crate_type {
1524 aux_rustc.args(&["--crate-type", crate_type]);
1527 aux_rustc.arg("-L").arg(&aux_dir);
1529 let auxres = aux_cx.compose_and_run(
1531 aux_cx.config.compile_lib_path.to_str().unwrap(),
1532 Some(aux_dir.to_str().unwrap()),
1535 if !auxres.status.success() {
1536 self.fatal_proc_rec(
1538 "auxiliary build of {:?} failed to compile: ",
1539 aux_testpaths.file.display()
1546 rustc.envs(self.props.rustc_env.clone());
1547 self.compose_and_run(
1549 self.config.compile_lib_path.to_str().unwrap(),
1550 Some(aux_dir.to_str().unwrap()),
1557 mut command: Command,
1559 aux_path: Option<&str>,
1560 input: Option<String>,
1563 let cmdline = self.make_cmdline(&command, lib_path);
1564 logv(self.config, format!("executing {}", cmdline));
1569 .stdout(Stdio::piped())
1570 .stderr(Stdio::piped())
1571 .stdin(Stdio::piped());
1573 // Need to be sure to put both the lib_path and the aux path in the dylib
1574 // search path for the child.
1575 let mut path = env::split_paths(&env::var_os(dylib_env_var()).unwrap_or(OsString::new()))
1576 .collect::<Vec<_>>();
1577 if let Some(p) = aux_path {
1578 path.insert(0, PathBuf::from(p))
1580 path.insert(0, PathBuf::from(lib_path));
1582 // Add the new dylib search path var
1583 let newpath = env::join_paths(&path).unwrap();
1584 command.env(dylib_env_var(), newpath);
1586 let mut child = disable_error_reporting(|| command.spawn())
1587 .expect(&format!("failed to exec `{:?}`", &command));
1588 if let Some(input) = input {
1593 .write_all(input.as_bytes())
1601 } = read2_abbreviated(child).expect("failed to read output");
1603 let result = ProcRes {
1605 stdout: String::from_utf8_lossy(&stdout).into_owned(),
1606 stderr: String::from_utf8_lossy(&stderr).into_owned(),
1610 self.dump_output(&result.stdout, &result.stderr);
1615 fn make_compile_args(&self, input_file: &Path, output_file: TargetLocation) -> Command {
1616 let is_rustdoc = self.config.src_base.ends_with("rustdoc-ui");
1617 let mut rustc = if !is_rustdoc {
1618 Command::new(&self.config.rustc_path)
1620 Command::new(&self.config
1623 .expect("no rustdoc built yet"))
1625 // FIXME Why is -L here?
1626 rustc.arg(input_file);//.arg("-L").arg(&self.config.build_base);
1628 // Optionally prevent default --target if specified in test compile-flags.
1629 let custom_target = self.props
1632 .any(|x| x.starts_with("--target"));
1635 let target = if self.props.force_host {
1638 &*self.config.target
1641 rustc.arg(&format!("--target={}", target));
1644 if let Some(revision) = self.revision {
1645 rustc.args(&["--cfg", revision]);
1649 if let Some(ref incremental_dir) = self.props.incremental_dir {
1650 rustc.args(&["-C", &format!("incremental={}", incremental_dir.display())]);
1651 rustc.args(&["-Z", "incremental-verify-ich"]);
1652 rustc.args(&["-Z", "incremental-queries"]);
1655 if self.config.mode == CodegenUnits {
1656 rustc.args(&["-Z", "human_readable_cgu_names"]);
1660 match self.config.mode {
1661 CompileFail | ParseFail | Incremental => {
1662 // If we are extracting and matching errors in the new
1663 // fashion, then you want JSON mode. Old-skool error
1664 // patterns still match the raw compiler output.
1665 if self.props.error_patterns.is_empty() {
1666 rustc.args(&["--error-format", "json"]);
1668 if !self.props.disable_ui_testing_normalization {
1669 rustc.arg("-Zui-testing");
1676 .any(|s| s.starts_with("--error-format"))
1678 rustc.args(&["--error-format", "json"]);
1680 if !self.props.disable_ui_testing_normalization {
1681 rustc.arg("-Zui-testing");
1687 "-Zmir-opt-level=3",
1688 "-Zdump-mir-exclude-pass-number",
1691 let mir_dump_dir = self.get_mir_dump_dir();
1692 let _ = fs::remove_dir_all(&mir_dump_dir);
1693 create_dir_all(mir_dump_dir.as_path()).unwrap();
1694 let mut dir_opt = "-Zdump-mir-dir=".to_string();
1695 dir_opt.push_str(mir_dump_dir.to_str().unwrap());
1696 debug!("dir_opt: {:?}", dir_opt);
1700 RunPass | RunFail | RunPassValgrind | Pretty | DebugInfoGdb | DebugInfoLldb
1701 | Codegen | Rustdoc | RunMake | CodegenUnits => {
1702 // do not use JSON output
1706 if self.props.skip_codegen {
1707 assert!(!self.props.compile_flags.iter().any(|s| s.starts_with("--emit")));
1708 rustc.args(&["--emit", "metadata"]);
1712 if self.config.target == "wasm32-unknown-unknown" {
1713 // rustc.arg("-g"); // get any backtrace at all on errors
1714 } else if !self.props.no_prefer_dynamic {
1715 rustc.args(&["-C", "prefer-dynamic"]);
1720 TargetLocation::ThisFile(path) => {
1721 rustc.arg("-o").arg(path);
1723 TargetLocation::ThisDirectory(path) => {
1724 rustc.arg("--out-dir").arg(path);
1728 match self.config.compare_mode {
1729 Some(CompareMode::Nll) => {
1730 rustc.args(&["-Zborrowck=mir", "-Ztwo-phase-borrows"]);
1735 if self.props.force_host {
1736 rustc.args(self.split_maybe_args(&self.config.host_rustcflags));
1738 rustc.args(self.split_maybe_args(&self.config.target_rustcflags));
1741 if let Some(ref linker) = self.config.linker {
1742 rustc.arg(format!("-Clinker={}", linker));
1746 rustc.args(&self.props.compile_flags);
1751 fn make_exe_name(&self) -> PathBuf {
1752 // Using a single letter here to keep the path length down for
1753 // Windows. Some test names get very long. rustc creates `rcgu`
1754 // files with the module name appended to it which can more than
1755 // double the length.
1756 let mut f = self.output_base_dir().join("a");
1757 // FIXME: This is using the host architecture exe suffix, not target!
1758 if self.config.target.contains("emscripten") {
1759 f = f.with_extra_extension("js");
1760 } else if self.config.target.contains("wasm32") {
1761 f = f.with_extra_extension("wasm");
1762 } else if !env::consts::EXE_SUFFIX.is_empty() {
1763 f = f.with_extra_extension(env::consts::EXE_SUFFIX);
1768 fn make_run_args(&self) -> ProcArgs {
1769 // If we've got another tool to run under (valgrind),
1770 // then split apart its command
1771 let mut args = self.split_maybe_args(&self.config.runtool);
1773 // If this is emscripten, then run tests under nodejs
1774 if self.config.target.contains("emscripten") {
1775 if let Some(ref p) = self.config.nodejs {
1776 args.push(p.clone());
1778 self.fatal("no NodeJS binary found (--nodejs)");
1782 // If this is otherwise wasm , then run tests under nodejs with our
1784 if self.config.target.contains("wasm32") {
1785 if let Some(ref p) = self.config.nodejs {
1786 args.push(p.clone());
1788 self.fatal("no NodeJS binary found (--nodejs)");
1791 let src = self.config.src_base
1792 .parent().unwrap() // chop off `run-pass`
1793 .parent().unwrap() // chop off `test`
1794 .parent().unwrap(); // chop off `src`
1795 args.push(src.join("src/etc/wasm32-shim.js").display().to_string());
1798 let exe_file = self.make_exe_name();
1800 // FIXME (#9639): This needs to handle non-utf8 paths
1801 args.push(exe_file.to_str().unwrap().to_owned());
1803 // Add the arguments in the run_flags directive
1804 args.extend(self.split_maybe_args(&self.props.run_flags));
1806 let prog = args.remove(0);
1807 ProcArgs { prog, args }
1810 fn split_maybe_args(&self, argstr: &Option<String>) -> Vec<String> {
1812 Some(ref s) => s.split(' ')
1814 if s.chars().all(|c| c.is_whitespace()) {
1825 fn make_cmdline(&self, command: &Command, libpath: &str) -> String {
1828 // Linux and mac don't require adjusting the library search path
1830 format!("{:?}", command)
1832 // Build the LD_LIBRARY_PATH variable as it would be seen on the command line
1833 // for diagnostic purposes
1834 fn lib_path_cmd_prefix(path: &str) -> String {
1837 util::lib_path_env_var(),
1838 util::make_new_path(path)
1842 format!("{} {:?}", lib_path_cmd_prefix(libpath), command)
1846 fn dump_output(&self, out: &str, err: &str) {
1847 let revision = if let Some(r) = self.revision {
1853 self.dump_output_file(out, &format!("{}out", revision));
1854 self.dump_output_file(err, &format!("{}err", revision));
1855 self.maybe_dump_to_stdout(out, err);
1858 fn dump_output_file(&self, out: &str, extension: &str) {
1859 let outfile = self.make_out_name(extension);
1860 File::create(&outfile)
1862 .write_all(out.as_bytes())
1866 /// Create a filename for output with the given extension. Example:
1867 /// /.../testname.revision.mode/testname.extension
1868 fn make_out_name(&self, extension: &str) -> PathBuf {
1869 self.output_base_name().with_extension(extension)
1872 /// Directory where auxiliary files are written. Example:
1873 /// /.../testname.revision.mode/auxiliary/
1874 fn aux_output_dir_name(&self) -> PathBuf {
1875 self.output_base_dir()
1877 .with_extra_extension(self.config.mode.disambiguator())
1880 /// Generates a unique name for the test, such as `testname.revision.mode`.
1881 fn output_testname_unique(&self) -> PathBuf {
1882 output_testname_unique(self.config, self.testpaths, self.safe_revision())
1885 /// The revision, ignored for Incremental since it wants all revisions in
1886 /// the same directory.
1887 fn safe_revision(&self) -> Option<&str> {
1888 if self.config.mode == Incremental {
1895 /// Absolute path to the directory where all output for the given
1896 /// test/revision should reside. Example:
1897 /// /path/to/build/host-triple/test/ui/relative/testname.revision.mode/
1898 fn output_base_dir(&self) -> PathBuf {
1899 output_base_dir(self.config, self.testpaths, self.safe_revision())
1902 /// Absolute path to the base filename used as output for the given
1903 /// test/revision. Example:
1904 /// /.../relative/testname.revision.mode/testname
1905 fn output_base_name(&self) -> PathBuf {
1906 output_base_name(self.config, self.testpaths, self.safe_revision())
1909 fn maybe_dump_to_stdout(&self, out: &str, err: &str) {
1910 if self.config.verbose {
1911 println!("------{}------------------------------", "stdout");
1912 println!("{}", out);
1913 println!("------{}------------------------------", "stderr");
1914 println!("{}", err);
1915 println!("------------------------------------------");
1919 fn error(&self, err: &str) {
1920 match self.revision {
1921 Some(rev) => println!("\nerror in revision `{}`: {}", rev, err),
1922 None => println!("\nerror: {}", err),
1926 fn fatal(&self, err: &str) -> ! {
1931 fn fatal_proc_rec(&self, err: &str, proc_res: &ProcRes) -> ! {
1932 self.try_print_open_handles();
1934 proc_res.fatal(None);
1937 // This function is a poor man's attempt to debug rust-lang/rust#38620, if
1938 // that's closed then this should be deleted
1940 // This is a very "opportunistic" debugging attempt, so we ignore all
1942 fn try_print_open_handles(&self) {
1946 if self.config.mode != Incremental {
1950 let filename = match self.testpaths.file.file_stem() {
1955 let mut cmd = Command::new("handle.exe");
1956 cmd.arg("-a").arg("-u");
1958 cmd.arg("-nobanner");
1959 cmd.stdout(Stdio::piped());
1960 cmd.stderr(Stdio::piped());
1961 let output = match cmd.spawn().and_then(read2_abbreviated) {
1962 Ok(output) => output,
1965 println!("---------------------------------------------------");
1966 println!("ran extra command to debug rust-lang/rust#38620: ");
1967 println!("{:?}", cmd);
1968 println!("result: {}", output.status);
1969 println!("--- stdout ----------------------------------------");
1970 println!("{}", String::from_utf8_lossy(&output.stdout));
1971 println!("--- stderr ----------------------------------------");
1972 println!("{}", String::from_utf8_lossy(&output.stderr));
1973 println!("---------------------------------------------------");
1976 // codegen tests (using FileCheck)
1978 fn compile_test_and_save_ir(&self) -> ProcRes {
1979 let aux_dir = self.aux_output_dir_name();
1981 let output_file = TargetLocation::ThisDirectory(self.output_base_dir());
1982 let mut rustc = self.make_compile_args(&self.testpaths.file, output_file);
1983 rustc.arg("-L").arg(aux_dir).arg("--emit=llvm-ir");
1985 self.compose_and_run_compiler(rustc, None)
1988 fn check_ir_with_filecheck(&self) -> ProcRes {
1989 let irfile = self.output_base_name().with_extension("ll");
1990 let mut filecheck = Command::new(self.config.llvm_filecheck.as_ref().unwrap());
1992 .arg("--input-file")
1994 .arg(&self.testpaths.file);
1995 self.compose_and_run(filecheck, "", None, None)
1998 fn run_codegen_test(&self) {
1999 assert!(self.revision.is_none(), "revisions not relevant here");
2001 if self.config.llvm_filecheck.is_none() {
2002 self.fatal("missing --llvm-filecheck");
2005 let mut proc_res = self.compile_test_and_save_ir();
2006 if !proc_res.status.success() {
2007 self.fatal_proc_rec("compilation failed!", &proc_res);
2010 proc_res = self.check_ir_with_filecheck();
2011 if !proc_res.status.success() {
2012 self.fatal_proc_rec("verification with 'FileCheck' failed", &proc_res);
2016 fn charset() -> &'static str {
2017 // FreeBSD 10.1 defaults to GDB 6.1.1 which doesn't support "auto" charset
2018 if cfg!(target_os = "bitrig") {
2020 } else if cfg!(target_os = "freebsd") {
2027 fn run_rustdoc_test(&self) {
2028 assert!(self.revision.is_none(), "revisions not relevant here");
2030 let out_dir = self.output_base_dir();
2031 let _ = fs::remove_dir_all(&out_dir);
2032 create_dir_all(&out_dir).unwrap();
2034 let proc_res = self.document(&out_dir);
2035 if !proc_res.status.success() {
2036 self.fatal_proc_rec("rustdoc failed!", &proc_res);
2039 if self.props.check_test_line_numbers_match {
2040 self.check_rustdoc_test_option(proc_res);
2042 let root = self.config.find_rust_src_root().unwrap();
2043 let res = self.cmd2procres(
2044 Command::new(&self.config.docck_python)
2045 .arg(root.join("src/etc/htmldocck.py"))
2047 .arg(&self.testpaths.file),
2049 if !res.status.success() {
2050 self.fatal_proc_rec("htmldocck failed!", &res);
2055 fn get_lines<P: AsRef<Path>>(
2058 mut other_files: Option<&mut Vec<String>>,
2061 fs::File::open(path).expect("markdown_test_output_check_entry File::open failed");
2062 let mut content = String::new();
2063 file.read_to_string(&mut content)
2064 .expect("markdown_test_output_check_entry read_to_string failed");
2065 let mut ignore = false;
2069 .filter_map(|(line_nb, line)| {
2070 if (line.trim_left().starts_with("pub mod ")
2071 || line.trim_left().starts_with("mod "))
2072 && line.ends_with(';')
2074 if let Some(ref mut other_files) = other_files {
2075 other_files.push(line.rsplit("mod ").next().unwrap().replace(";", ""));
2079 let sline = line.split("///").last().unwrap_or("");
2080 let line = sline.trim_left();
2081 if line.starts_with("```") {
2097 fn check_rustdoc_test_option(&self, res: ProcRes) {
2098 let mut other_files = Vec::new();
2099 let mut files: HashMap<String, Vec<usize>> = HashMap::new();
2100 let cwd = env::current_dir().unwrap();
2105 .unwrap_or(&self.testpaths.file)
2108 .replace('\\', "/"),
2109 self.get_lines(&self.testpaths.file, Some(&mut other_files)),
2111 for other_file in other_files {
2112 let mut path = self.testpaths.file.clone();
2113 path.set_file_name(&format!("{}.rs", other_file));
2115 path.strip_prefix(&cwd)
2119 .replace('\\', "/"),
2120 self.get_lines(&path, None),
2127 .filter(|s| s.starts_with("test "))
2129 let tmp: Vec<&str> = s.split(" - ").collect();
2131 let path = tmp[0].rsplit("test ").next().unwrap();
2132 if let Some(ref mut v) = files.get_mut(&path.replace('\\', "/")) {
2134 let mut iter = tmp[1].split("(line ");
2136 let line = iter.next()
2143 if let Ok(pos) = v.binary_search(&line) {
2146 self.fatal_proc_rec(
2147 &format!("Not found doc test: \"{}\" in \"{}\":{:?}", s, path, v),
2155 self.fatal_proc_rec(&format!("No test has been found... {:?}", files), &res);
2157 for (entry, v) in &files {
2159 self.fatal_proc_rec(
2161 "Not found test at line{} \"{}\":{:?}",
2162 if v.len() > 1 { "s" } else { "" },
2173 fn run_codegen_units_test(&self) {
2174 assert!(self.revision.is_none(), "revisions not relevant here");
2176 let proc_res = self.compile_test();
2178 if !proc_res.status.success() {
2179 self.fatal_proc_rec("compilation failed!", &proc_res);
2182 self.check_no_compiler_crash(&proc_res);
2184 const PREFIX: &'static str = "MONO_ITEM ";
2185 const CGU_MARKER: &'static str = "@@";
2187 let actual: Vec<MonoItem> = proc_res
2190 .filter(|line| line.starts_with(PREFIX))
2191 .map(str_to_mono_item)
2194 let expected: Vec<MonoItem> = errors::load_errors(&self.testpaths.file, None)
2196 .map(|e| str_to_mono_item(&e.msg[..]))
2199 let mut missing = Vec::new();
2200 let mut wrong_cgus = Vec::new();
2202 for expected_item in &expected {
2203 let actual_item_with_same_name = actual.iter().find(|ti| ti.name == expected_item.name);
2205 if let Some(actual_item) = actual_item_with_same_name {
2206 if !expected_item.codegen_units.is_empty() &&
2207 // Also check for codegen units
2208 expected_item.codegen_units != actual_item.codegen_units
2210 wrong_cgus.push((expected_item.clone(), actual_item.clone()));
2213 missing.push(expected_item.string.clone());
2217 let unexpected: Vec<_> = actual
2219 .filter(|acgu| !expected.iter().any(|ecgu| acgu.name == ecgu.name))
2220 .map(|acgu| acgu.string.clone())
2223 if !missing.is_empty() {
2226 println!("\nThese items should have been contained but were not:\n");
2228 for item in &missing {
2229 println!("{}", item);
2235 if !unexpected.is_empty() {
2237 let mut sorted = unexpected.clone();
2242 println!("\nThese items were contained but should not have been:\n");
2244 for item in sorted {
2245 println!("{}", item);
2251 if !wrong_cgus.is_empty() {
2252 wrong_cgus.sort_by_key(|pair| pair.0.name.clone());
2253 println!("\nThe following items were assigned to wrong codegen units:\n");
2255 for &(ref expected_item, ref actual_item) in &wrong_cgus {
2256 println!("{}", expected_item.name);
2259 codegen_units_to_str(&expected_item.codegen_units)
2263 codegen_units_to_str(&actual_item.codegen_units)
2269 if !(missing.is_empty() && unexpected.is_empty() && wrong_cgus.is_empty()) {
2273 #[derive(Clone, Eq, PartialEq)]
2276 codegen_units: HashSet<String>,
2280 // [MONO_ITEM] name [@@ (cgu)+]
2281 fn str_to_mono_item(s: &str) -> MonoItem {
2282 let s = if s.starts_with(PREFIX) {
2283 (&s[PREFIX.len()..]).trim()
2288 let full_string = format!("{}{}", PREFIX, s.trim().to_owned());
2290 let parts: Vec<&str> = s.split(CGU_MARKER)
2292 .filter(|s| !s.is_empty())
2295 let name = parts[0].trim();
2297 let cgus = if parts.len() > 1 {
2298 let cgus_str = parts[1];
2303 .filter(|s| !s.is_empty())
2311 name: name.to_owned(),
2312 codegen_units: cgus,
2313 string: full_string,
2317 fn codegen_units_to_str(cgus: &HashSet<String>) -> String {
2318 let mut cgus: Vec<_> = cgus.iter().collect();
2321 let mut string = String::new();
2323 string.push_str(&cgu[..]);
2324 string.push_str(" ");
2331 fn init_incremental_test(&self) {
2332 // (See `run_incremental_test` for an overview of how incremental tests work.)
2334 // Before any of the revisions have executed, create the
2335 // incremental workproduct directory. Delete any old
2336 // incremental work products that may be there from prior
2338 let incremental_dir = self.incremental_dir();
2339 if incremental_dir.exists() {
2340 // Canonicalizing the path will convert it to the //?/ format
2341 // on Windows, which enables paths longer than 260 character
2342 let canonicalized = incremental_dir.canonicalize().unwrap();
2343 fs::remove_dir_all(canonicalized).unwrap();
2345 fs::create_dir_all(&incremental_dir).unwrap();
2347 if self.config.verbose {
2349 "init_incremental_test: incremental_dir={}",
2350 incremental_dir.display()
2355 fn run_incremental_test(&self) {
2356 // Basic plan for a test incremental/foo/bar.rs:
2357 // - load list of revisions rpass1, cfail2, rpass3
2358 // - each should begin with `rpass`, `cfail`, or `rfail`
2359 // - if `rpass`, expect compile and execution to succeed
2360 // - if `cfail`, expect compilation to fail
2361 // - if `rfail`, expect execution to fail
2362 // - create a directory build/foo/bar.incremental
2363 // - compile foo/bar.rs with -Z incremental=.../foo/bar.incremental and -C rpass1
2364 // - because name of revision starts with "rpass", expect success
2365 // - compile foo/bar.rs with -Z incremental=.../foo/bar.incremental and -C cfail2
2366 // - because name of revision starts with "cfail", expect an error
2367 // - load expected errors as usual, but filter for those that end in `[rfail2]`
2368 // - compile foo/bar.rs with -Z incremental=.../foo/bar.incremental and -C rpass3
2369 // - because name of revision starts with "rpass", expect success
2370 // - execute build/foo/bar.exe and save output
2372 // FIXME -- use non-incremental mode as an oracle? That doesn't apply
2373 // to #[rustc_dirty] and clean tests I guess
2375 let revision = self.revision
2376 .expect("incremental tests require a list of revisions");
2378 // Incremental workproduct directory should have already been created.
2379 let incremental_dir = self.incremental_dir();
2381 incremental_dir.exists(),
2382 "init_incremental_test failed to create incremental dir"
2385 // Add an extra flag pointing at the incremental directory.
2386 let mut revision_props = self.props.clone();
2387 revision_props.incremental_dir = Some(incremental_dir);
2389 let revision_cx = TestCx {
2390 config: self.config,
2391 props: &revision_props,
2392 testpaths: self.testpaths,
2393 revision: self.revision,
2396 if self.config.verbose {
2398 "revision={:?} revision_props={:#?}",
2399 revision, revision_props
2403 if revision.starts_with("rpass") {
2404 revision_cx.run_rpass_test();
2405 } else if revision.starts_with("rfail") {
2406 revision_cx.run_rfail_test();
2407 } else if revision.starts_with("cfail") {
2408 revision_cx.run_cfail_test();
2410 revision_cx.fatal("revision name must begin with rpass, rfail, or cfail");
2414 /// Directory where incremental work products are stored.
2415 fn incremental_dir(&self) -> PathBuf {
2416 self.output_base_name().with_extension("inc")
2419 fn run_rmake_test(&self) {
2420 let cwd = env::current_dir().unwrap();
2421 let src_root = self.config
2429 let src_root = cwd.join(&src_root);
2431 let tmpdir = cwd.join(self.output_base_name());
2432 if tmpdir.exists() {
2433 self.aggressive_rm_rf(&tmpdir).unwrap();
2435 create_dir_all(&tmpdir).unwrap();
2437 let host = &self.config.host;
2438 let make = if host.contains("bitrig") || host.contains("dragonfly")
2439 || host.contains("freebsd") || host.contains("netbsd")
2440 || host.contains("openbsd")
2447 let mut cmd = Command::new(make);
2448 cmd.current_dir(&self.testpaths.file)
2449 .stdout(Stdio::piped())
2450 .stderr(Stdio::piped())
2451 .env("TARGET", &self.config.target)
2452 .env("PYTHON", &self.config.docck_python)
2454 .env("RUST_BUILD_STAGE", &self.config.stage_id)
2455 .env("RUSTC", cwd.join(&self.config.rustc_path))
2456 .env("TMPDIR", &tmpdir)
2457 .env("LD_LIB_PATH_ENVVAR", dylib_env_var())
2458 .env("HOST_RPATH_DIR", cwd.join(&self.config.compile_lib_path))
2459 .env("TARGET_RPATH_DIR", cwd.join(&self.config.run_lib_path))
2460 .env("LLVM_COMPONENTS", &self.config.llvm_components)
2461 .env("LLVM_CXXFLAGS", &self.config.llvm_cxxflags)
2463 // We for sure don't want these tests to run in parallel, so make
2464 // sure they don't have access to these vars if we we run via `make`
2466 .env_remove("MAKEFLAGS")
2467 .env_remove("MFLAGS")
2468 .env_remove("CARGO_MAKEFLAGS");
2470 if let Some(ref rustdoc) = self.config.rustdoc_path {
2471 cmd.env("RUSTDOC", cwd.join(rustdoc));
2474 if let Some(ref node) = self.config.nodejs {
2475 cmd.env("NODE", node);
2478 if let Some(ref linker) = self.config.linker {
2479 cmd.env("RUSTC_LINKER", linker);
2482 // We don't want RUSTFLAGS set from the outside to interfere with
2483 // compiler flags set in the test cases:
2484 cmd.env_remove("RUSTFLAGS");
2486 if self.config.target.contains("msvc") && self.config.cc != "" {
2487 // We need to pass a path to `lib.exe`, so assume that `cc` is `cl.exe`
2488 // and that `lib.exe` lives next to it.
2489 let lib = Path::new(&self.config.cc).parent().unwrap().join("lib.exe");
2491 // MSYS doesn't like passing flags of the form `/foo` as it thinks it's
2492 // a path and instead passes `C:\msys64\foo`, so convert all
2493 // `/`-arguments to MSVC here to `-` arguments.
2494 let cflags = self.config
2497 .map(|s| s.replace("/", "-"))
2498 .collect::<Vec<_>>()
2501 cmd.env("IS_MSVC", "1")
2502 .env("IS_WINDOWS", "1")
2503 .env("MSVC_LIB", format!("'{}' -nologo", lib.display()))
2504 .env("CC", format!("'{}' {}", self.config.cc, cflags))
2505 .env("CXX", &self.config.cxx);
2507 cmd.env("CC", format!("{} {}", self.config.cc, self.config.cflags))
2508 .env("CXX", format!("{} {}", self.config.cxx, self.config.cflags))
2509 .env("AR", &self.config.ar);
2511 if self.config.target.contains("windows") {
2512 cmd.env("IS_WINDOWS", "1");
2516 let output = cmd.spawn()
2517 .and_then(read2_abbreviated)
2518 .expect("failed to spawn `make`");
2519 if !output.status.success() {
2521 status: output.status,
2522 stdout: String::from_utf8_lossy(&output.stdout).into_owned(),
2523 stderr: String::from_utf8_lossy(&output.stderr).into_owned(),
2524 cmdline: format!("{:?}", cmd),
2526 self.fatal_proc_rec("make failed", &res);
2530 fn aggressive_rm_rf(&self, path: &Path) -> io::Result<()> {
2531 for e in path.read_dir()? {
2533 let path = entry.path();
2534 if entry.file_type()?.is_dir() {
2535 self.aggressive_rm_rf(&path)?;
2537 // Remove readonly files as well on windows (by default we can't)
2538 fs::remove_file(&path).or_else(|e| {
2539 if cfg!(windows) && e.kind() == io::ErrorKind::PermissionDenied {
2540 let mut meta = entry.metadata()?.permissions();
2541 meta.set_readonly(false);
2542 fs::set_permissions(&path, meta)?;
2543 fs::remove_file(&path)
2550 fs::remove_dir(path)
2553 fn run_ui_test(&self) {
2554 // if the user specified a format in the ui test
2555 // print the output to the stderr file, otherwise extract
2556 // the rendered error messages from json and print them
2557 let explicit = self.props
2560 .any(|s| s.contains("--error-format"));
2561 let proc_res = self.compile_test();
2562 self.check_if_test_should_compile(&proc_res);
2564 let expected_stderr = self.load_expected_output(UI_STDERR);
2565 let expected_stdout = self.load_expected_output(UI_STDOUT);
2566 let expected_fixed = self.load_expected_output(UI_FIXED);
2568 let normalized_stdout =
2569 self.normalize_output(&proc_res.stdout, &self.props.normalize_stdout);
2571 let stderr = if explicit {
2572 proc_res.stderr.clone()
2574 json::extract_rendered(&proc_res.stderr, &proc_res)
2577 let normalized_stderr = self.normalize_output(&stderr, &self.props.normalize_stderr);
2580 errors += self.compare_output("stdout", &normalized_stdout, &expected_stdout);
2581 errors += self.compare_output("stderr", &normalized_stderr, &expected_stderr);
2583 if self.config.compare_mode.is_some() {
2584 // don't test rustfix with nll right now
2585 } else if self.props.run_rustfix {
2586 // Apply suggestions from rustc to the code itself
2587 let unfixed_code = self.load_expected_output_from_path(&self.testpaths.file)
2589 let suggestions = get_suggestions_from_json(&proc_res.stderr, &HashSet::new()).unwrap();
2590 let fixed_code = apply_suggestions(&unfixed_code, &suggestions);
2592 errors += self.compare_output("fixed", &fixed_code, &expected_fixed);
2593 } else if !expected_fixed.is_empty() {
2594 panic!("the `// run-rustfix` directive wasn't found but a `*.fixed` \
2599 println!("To update references, rerun the tests and pass the `--bless` flag");
2600 let relative_path_to_file = self.testpaths
2602 .join(self.testpaths.file.file_name().unwrap());
2604 "To only update this specific test, also pass `--test-args {}`",
2605 relative_path_to_file.display(),
2607 self.fatal_proc_rec(
2608 &format!("{} errors occurred comparing output.", errors),
2613 let expected_errors = errors::load_errors(&self.testpaths.file, self.revision);
2615 if self.props.run_pass {
2616 let proc_res = self.exec_compiled_test();
2618 if !proc_res.status.success() {
2619 self.fatal_proc_rec("test run failed!", &proc_res);
2622 if !explicit && self.config.compare_mode.is_none() {
2623 if !expected_errors.is_empty() || !proc_res.status.success() {
2624 // "// error-pattern" comments
2625 self.check_expected_errors(expected_errors, &proc_res);
2626 } else if !self.props.error_patterns.is_empty() || !proc_res.status.success() {
2627 // "//~ERROR comments"
2628 self.check_error_patterns(&proc_res.stderr, &proc_res);
2632 if self.props.run_rustfix && self.config.compare_mode.is_none() {
2633 // And finally, compile the fixed code and make sure it both
2634 // succeeds and has no diagnostics.
2635 let mut rustc = self.make_compile_args(
2636 &self.testpaths.file.with_extension(UI_FIXED),
2637 TargetLocation::ThisFile(self.make_exe_name()),
2639 rustc.arg("-L").arg(&self.aux_output_dir_name());
2640 let res = self.compose_and_run_compiler(rustc, None);
2641 if !res.status.success() {
2642 self.fatal_proc_rec("failed to compile fixed code", &res);
2644 if !res.stderr.is_empty() {
2645 self.fatal_proc_rec("fixed code is still producing diagnostics", &res);
2650 fn run_mir_opt_test(&self) {
2651 let proc_res = self.compile_test();
2653 if !proc_res.status.success() {
2654 self.fatal_proc_rec("compilation failed!", &proc_res);
2657 let proc_res = self.exec_compiled_test();
2659 if !proc_res.status.success() {
2660 self.fatal_proc_rec("test run failed!", &proc_res);
2662 self.check_mir_dump();
2665 fn check_mir_dump(&self) {
2666 let mut test_file_contents = String::new();
2667 fs::File::open(self.testpaths.file.clone())
2669 .read_to_string(&mut test_file_contents)
2671 if let Some(idx) = test_file_contents.find("// END RUST SOURCE") {
2672 let (_, tests_text) = test_file_contents.split_at(idx + "// END_RUST SOURCE".len());
2673 let tests_text_str = String::from(tests_text);
2674 let mut curr_test: Option<&str> = None;
2675 let mut curr_test_contents = vec![ExpectedLine::Elision];
2676 for l in tests_text_str.lines() {
2677 debug!("line: {:?}", l);
2678 if l.starts_with("// START ") {
2679 let (_, t) = l.split_at("// START ".len());
2680 curr_test = Some(t);
2681 } else if l.starts_with("// END") {
2682 let (_, t) = l.split_at("// END ".len());
2683 if Some(t) != curr_test {
2684 panic!("mismatched START END test name");
2686 self.compare_mir_test_output(curr_test.unwrap(), &curr_test_contents);
2688 curr_test_contents.clear();
2689 curr_test_contents.push(ExpectedLine::Elision);
2690 } else if l.is_empty() {
2692 } else if l.starts_with("//") && l.split_at("//".len()).1.trim() == "..." {
2693 curr_test_contents.push(ExpectedLine::Elision)
2694 } else if l.starts_with("// ") {
2695 let (_, test_content) = l.split_at("// ".len());
2696 curr_test_contents.push(ExpectedLine::Text(test_content));
2702 fn check_mir_test_timestamp(&self, test_name: &str, output_file: &Path) {
2703 let t = |file| FileTime::from_last_modification_time(&fs::metadata(file).unwrap());
2704 let source_file = &self.testpaths.file;
2705 let output_time = t(output_file);
2706 let source_time = t(source_file);
2707 if source_time > output_time {
2709 "source file time: {:?} output file time: {:?}",
2710 source_time, output_time
2713 "test source file `{}` is newer than potentially stale output file `{}`.",
2714 source_file.display(),
2720 fn compare_mir_test_output(&self, test_name: &str, expected_content: &[ExpectedLine<&str>]) {
2721 let mut output_file = PathBuf::new();
2722 output_file.push(self.get_mir_dump_dir());
2723 output_file.push(test_name);
2724 debug!("comparing the contests of: {:?}", output_file);
2725 debug!("with: {:?}", expected_content);
2726 if !output_file.exists() {
2728 "Output file `{}` from test does not exist",
2729 output_file.into_os_string().to_string_lossy()
2732 self.check_mir_test_timestamp(test_name, &output_file);
2734 let mut dumped_file = fs::File::open(output_file.clone()).unwrap();
2735 let mut dumped_string = String::new();
2736 dumped_file.read_to_string(&mut dumped_string).unwrap();
2737 let mut dumped_lines = dumped_string
2739 .map(|l| nocomment_mir_line(l))
2740 .filter(|l| !l.is_empty());
2741 let mut expected_lines = expected_content
2744 if let &ExpectedLine::Text(l) = l {
2752 let compare = |expected_line, dumped_line| {
2753 let e_norm = normalize_mir_line(expected_line);
2754 let d_norm = normalize_mir_line(dumped_line);
2755 debug!("found: {:?}", d_norm);
2756 debug!("expected: {:?}", e_norm);
2760 let error = |expected_line, extra_msg| {
2761 let normalize_all = dumped_string
2763 .map(nocomment_mir_line)
2764 .filter(|l| !l.is_empty())
2765 .collect::<Vec<_>>()
2767 let f = |l: &ExpectedLine<_>| match l {
2768 &ExpectedLine::Elision => "... (elided)".into(),
2769 &ExpectedLine::Text(t) => t,
2771 let expected_content = expected_content
2774 .collect::<Vec<_>>()
2777 "Did not find expected line, error: {}\n\
2778 Expected Line: {:?}\n\
2790 // We expect each non-empty line to appear consecutively, non-consecutive lines
2791 // must be separated by at least one Elision
2792 let mut start_block_line = None;
2793 while let Some(dumped_line) = dumped_lines.next() {
2794 match expected_lines.next() {
2795 Some(&ExpectedLine::Text(expected_line)) => {
2796 let normalized_expected_line = normalize_mir_line(expected_line);
2797 if normalized_expected_line.contains(":{") {
2798 start_block_line = Some(expected_line);
2801 if !compare(expected_line, dumped_line) {
2802 error!("{:?}", start_block_line);
2806 "Mismatch in lines\n\
2807 Current block: {}\n\
2809 start_block_line.unwrap_or("None"),
2815 Some(&ExpectedLine::Elision) => {
2816 // skip any number of elisions in a row.
2817 while let Some(&&ExpectedLine::Elision) = expected_lines.peek() {
2818 expected_lines.next();
2820 if let Some(&ExpectedLine::Text(expected_line)) = expected_lines.next() {
2821 let mut found = compare(expected_line, dumped_line);
2825 while let Some(dumped_line) = dumped_lines.next() {
2826 found = compare(expected_line, dumped_line);
2832 error(expected_line, "ran out of mir dump to match against".into());
2841 fn get_mir_dump_dir(&self) -> PathBuf {
2842 let mut mir_dump_dir = PathBuf::from(self.config.build_base.as_path());
2843 debug!("input_file: {:?}", self.testpaths.file);
2844 mir_dump_dir.push(&self.testpaths.relative_dir);
2845 mir_dump_dir.push(self.testpaths.file.file_stem().unwrap());
2849 fn normalize_output(&self, output: &str, custom_rules: &[(String, String)]) -> String {
2850 let parent_dir = self.testpaths.file.parent().unwrap();
2851 let cflags = self.props.compile_flags.join(" ");
2852 let json = cflags.contains("--error-format json")
2853 || cflags.contains("--error-format pretty-json")
2854 || cflags.contains("--error-format=json")
2855 || cflags.contains("--error-format=pretty-json");
2856 let parent_dir_str = if json {
2857 parent_dir.display().to_string().replace("\\", "\\\\")
2859 parent_dir.display().to_string()
2862 let mut normalized = output.replace(&parent_dir_str, "$DIR");
2865 // escaped newlines in json strings should be readable
2866 // in the stderr files. There's no point int being correct,
2867 // since only humans process the stderr files.
2868 // Thus we just turn escaped newlines back into newlines.
2869 normalized = normalized.replace("\\n", "\n");
2872 normalized = normalized.replace("\\\\", "\\") // denormalize for paths on windows
2873 .replace("\\", "/") // normalize for paths on windows
2874 .replace("\r\n", "\n") // normalize for linebreaks on windows
2875 .replace("\t", "\\t"); // makes tabs visible
2876 for rule in custom_rules {
2877 let re = Regex::new(&rule.0).expect("bad regex in custom normalization rule");
2878 normalized = re.replace_all(&normalized, &rule.1[..]).into_owned();
2883 fn expected_output_path(&self, kind: &str) -> PathBuf {
2884 let mut path = expected_output_path(
2887 &self.config.compare_mode,
2890 if !path.exists() && self.config.compare_mode.is_some() {
2892 path = expected_output_path(&self.testpaths, self.revision, &None, kind);
2898 fn load_expected_output(&self, kind: &str) -> String {
2899 let path = self.expected_output_path(kind);
2901 match self.load_expected_output_from_path(&path) {
2903 Err(x) => self.fatal(&x),
2910 fn load_expected_output_from_path(&self, path: &Path) -> Result<String, String> {
2911 let mut result = String::new();
2912 match File::open(path).and_then(|mut f| f.read_to_string(&mut result)) {
2913 Ok(_) => Ok(result),
2914 Err(e) => Err(format!(
2915 "failed to load expected output from `{}`: {}",
2922 fn compare_output(&self, kind: &str, actual: &str, expected: &str) -> usize {
2923 if actual == expected {
2927 if !self.config.bless {
2928 if expected.is_empty() {
2929 println!("normalized {}:\n{}\n", kind, actual);
2931 println!("diff of {}:\n", kind);
2932 let diff_results = make_diff(expected, actual, 3);
2933 for result in diff_results {
2934 let mut line_number = result.line_number;
2935 for line in result.lines {
2937 DiffLine::Expected(e) => {
2938 println!("-\t{}", e);
2941 DiffLine::Context(c) => {
2942 println!("{}\t{}", line_number, c);
2945 DiffLine::Resulting(r) => {
2946 println!("+\t{}", r);
2955 let mode = self.config.compare_mode.as_ref().map_or("", |m| m.to_str());
2956 let output_file = self.output_base_name()
2957 .with_extra_extension(self.revision.unwrap_or(""))
2958 .with_extra_extension(mode)
2959 .with_extra_extension(kind);
2961 let mut files = vec![output_file];
2962 if self.config.bless {
2963 files.push(expected_output_path(
2966 &self.config.compare_mode,
2971 for output_file in &files {
2972 if actual.is_empty() {
2973 if let Err(e) = ::std::fs::remove_file(output_file) {
2974 self.fatal(&format!(
2975 "failed to delete `{}`: {}",
2976 output_file.display(),
2981 match File::create(&output_file).and_then(|mut f| f.write_all(actual.as_bytes())) {
2983 Err(e) => self.fatal(&format!(
2984 "failed to write {} to `{}`: {}",
2986 output_file.display(),
2993 println!("\nThe actual {0} differed from the expected {0}.", kind);
2994 for output_file in files {
2995 println!("Actual {} saved to {}", kind, output_file.display());
2997 if self.config.bless {
3004 fn create_stamp(&self) {
3005 let mut f = File::create(::stamp(&self.config, self.testpaths, self.revision)).unwrap();
3006 f.write_all(compute_stamp_hash(&self.config).as_bytes()).unwrap();
3015 pub struct ProcRes {
3023 pub fn fatal(&self, err: Option<&str>) -> ! {
3024 if let Some(e) = err {
3025 println!("\nerror: {}", e);
3032 ------------------------------------------\n\
3034 ------------------------------------------\n\
3036 ------------------------------------------\n\
3038 ------------------------------------------\n\
3040 self.status, self.cmdline, self.stdout, self.stderr
3046 enum TargetLocation {
3048 ThisDirectory(PathBuf),
3051 #[derive(Clone, PartialEq, Eq)]
3052 enum ExpectedLine<T: AsRef<str>> {
3057 impl<T> fmt::Debug for ExpectedLine<T>
3059 T: AsRef<str> + fmt::Debug,
3061 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
3062 if let &ExpectedLine::Text(ref t) = self {
3063 write!(formatter, "{:?}", t)
3065 write!(formatter, "\"...\" (Elision)")
3070 fn normalize_mir_line(line: &str) -> String {
3071 nocomment_mir_line(line).replace(char::is_whitespace, "")
3074 fn nocomment_mir_line(line: &str) -> &str {
3075 if let Some(idx) = line.find("//") {
3076 let (l, _) = line.split_at(idx);
3083 fn read2_abbreviated(mut child: Child) -> io::Result<Output> {
3085 use std::mem::replace;
3087 const HEAD_LEN: usize = 160 * 1024;
3088 const TAIL_LEN: usize = 256 * 1024;
3100 fn extend(&mut self, data: &[u8]) {
3101 let new_self = match *self {
3102 ProcOutput::Full(ref mut bytes) => {
3103 bytes.extend_from_slice(data);
3104 let new_len = bytes.len();
3105 if new_len <= HEAD_LEN + TAIL_LEN {
3108 let tail = bytes.split_off(new_len - TAIL_LEN).into_boxed_slice();
3109 let head = replace(bytes, Vec::new());
3110 let skipped = new_len - HEAD_LEN - TAIL_LEN;
3111 ProcOutput::Abbreviated {
3117 ProcOutput::Abbreviated {
3122 *skipped += data.len();
3123 if data.len() <= TAIL_LEN {
3124 tail[..data.len()].copy_from_slice(data);
3125 tail.rotate_left(data.len());
3127 tail.copy_from_slice(&data[(data.len() - TAIL_LEN)..]);
3135 fn into_bytes(self) -> Vec<u8> {
3137 ProcOutput::Full(bytes) => bytes,
3138 ProcOutput::Abbreviated {
3143 write!(&mut head, "\n\n<<<<<< SKIPPED {} BYTES >>>>>>\n\n", skipped).unwrap();
3144 head.extend_from_slice(&tail);
3151 let mut stdout = ProcOutput::Full(Vec::new());
3152 let mut stderr = ProcOutput::Full(Vec::new());
3154 drop(child.stdin.take());
3156 child.stdout.take().unwrap(),
3157 child.stderr.take().unwrap(),
3158 &mut |is_stdout, data, _| {
3159 if is_stdout { &mut stdout } else { &mut stderr }.extend(data);
3163 let status = child.wait()?;
3167 stdout: stdout.into_bytes(),
3168 stderr: stderr.into_bytes(),