]> git.proxmox.com Git - rustc.git/blame - src/tools/compiletest/src/runtest.rs
Imported Upstream version 1.10.0+dfsg1
[rustc.git] / src / tools / compiletest / src / runtest.rs
CommitLineData
a7813a04
XL
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.
4//
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.
10
11use common::Config;
12use common::{CompileFail, ParseFail, Pretty, RunFail, RunPass, RunPassValgrind};
13use common::{Codegen, DebugInfoLldb, DebugInfoGdb, Rustdoc, CodegenUnits};
14use common::{Incremental, RunMake, Ui};
15use errors::{self, ErrorKind, Error};
16use json;
17use header::TestProps;
18use header;
19use procsrv;
20use test::TestPaths;
21use uidiff;
22use util::logv;
23
24use std::env;
25use std::collections::HashSet;
26use std::fmt;
27use std::fs::{self, File};
28use std::io::{self, BufReader};
29use std::io::prelude::*;
30use std::net::TcpStream;
31use std::path::{Path, PathBuf};
32use std::process::{Command, Output, ExitStatus};
33use std::str;
34
35pub fn run(config: Config, testpaths: &TestPaths) {
36 match &*config.target {
37
38 "arm-linux-androideabi" | "armv7-linux-androideabi" | "aarch64-linux-android" => {
39 if !config.adb_device_status {
40 panic!("android device not available");
41 }
42 }
43
44 _=> { }
45 }
46
47 if config.verbose {
48 // We're going to be dumping a lot of info. Start on a new line.
49 print!("\n\n");
50 }
51 debug!("running {:?}", testpaths.file.display());
52 let base_props = TestProps::from_file(&testpaths.file);
53
54 let base_cx = TestCx { config: &config,
55 props: &base_props,
56 testpaths: testpaths,
57 revision: None };
58 base_cx.init_all();
59
60 if base_props.revisions.is_empty() {
61 base_cx.run_revision()
62 } else {
63 for revision in &base_props.revisions {
64 let mut revision_props = base_props.clone();
65 revision_props.load_from(&testpaths.file, Some(&revision));
66 let rev_cx = TestCx {
67 config: &config,
68 props: &revision_props,
69 testpaths: testpaths,
70 revision: Some(revision)
71 };
72 rev_cx.run_revision();
73 }
74 }
75
76 base_cx.complete_all();
77}
78
79struct TestCx<'test> {
80 config: &'test Config,
81 props: &'test TestProps,
82 testpaths: &'test TestPaths,
83 revision: Option<&'test str>
84}
85
86struct DebuggerCommands {
87 commands: Vec<String>,
88 check_lines: Vec<String>,
89 breakpoint_lines: Vec<usize>,
90}
91
92impl<'test> TestCx<'test> {
93 /// invoked once before any revisions have been processed
94 fn init_all(&self) {
95 assert!(self.revision.is_none(), "init_all invoked for a revision");
96 match self.config.mode {
97 Incremental => self.init_incremental_test(),
98 _ => { }
99 }
100 }
101
102 /// Code executed for each revision in turn (or, if there are no
103 /// revisions, exactly once, with revision == None).
104 fn run_revision(&self) {
105 match self.config.mode {
106 CompileFail => self.run_cfail_test(),
107 ParseFail => self.run_cfail_test(),
108 RunFail => self.run_rfail_test(),
109 RunPass => self.run_rpass_test(),
110 RunPassValgrind => self.run_valgrind_test(),
111 Pretty => self.run_pretty_test(),
112 DebugInfoGdb => self.run_debuginfo_gdb_test(),
113 DebugInfoLldb => self.run_debuginfo_lldb_test(),
114 Codegen => self.run_codegen_test(),
115 Rustdoc => self.run_rustdoc_test(),
116 CodegenUnits => self.run_codegen_units_test(),
117 Incremental => self.run_incremental_test(),
118 RunMake => self.run_rmake_test(),
119 Ui => self.run_ui_test(),
120 }
121 }
122
123 /// Invoked after all revisions have executed.
124 fn complete_all(&self) {
125 assert!(self.revision.is_none(), "init_all invoked for a revision");
126 }
127
128 fn run_cfail_test(&self) {
129 let proc_res = self.compile_test();
130
131 if proc_res.status.success() {
132 self.fatal_proc_rec(
133 &format!("{} test compiled successfully!", self.config.mode)[..],
134 &proc_res);
135 }
136
137 self.check_correct_failure_status(&proc_res);
138
139 if proc_res.status.success() {
140 self.fatal("process did not return an error status");
141 }
142
143 let output_to_check = self.get_output(&proc_res);
144 let expected_errors = errors::load_errors(&self.testpaths.file, self.revision);
145 if !expected_errors.is_empty() {
146 if !self.props.error_patterns.is_empty() {
147 self.fatal("both error pattern and expected errors specified");
148 }
149 self.check_expected_errors(expected_errors, &proc_res);
150 } else {
151 self.check_error_patterns(&output_to_check, &proc_res);
152 }
153 self.check_no_compiler_crash(&proc_res);
154 self.check_forbid_output(&output_to_check, &proc_res);
155 }
156
157 fn run_rfail_test(&self) {
158 let proc_res = self.compile_test();
159
160 if !proc_res.status.success() {
161 self.fatal_proc_rec("compilation failed!", &proc_res);
162 }
163
164 let proc_res = self.exec_compiled_test();
165
166 // The value our Makefile configures valgrind to return on failure
167 const VALGRIND_ERR: i32 = 100;
168 if proc_res.status.code() == Some(VALGRIND_ERR) {
169 self.fatal_proc_rec("run-fail test isn't valgrind-clean!", &proc_res);
170 }
171
172 let output_to_check = self.get_output(&proc_res);
173 self.check_correct_failure_status(&proc_res);
174 self.check_error_patterns(&output_to_check, &proc_res);
175 }
176
177 fn get_output(&self, proc_res: &ProcRes) -> String {
178 if self.props.check_stdout {
179 format!("{}{}", proc_res.stdout, proc_res.stderr)
180 } else {
181 proc_res.stderr.clone()
182 }
183 }
184
185 fn check_correct_failure_status(&self, proc_res: &ProcRes) {
186 // The value the rust runtime returns on failure
187 const RUST_ERR: i32 = 101;
188 if proc_res.status.code() != Some(RUST_ERR) {
189 self.fatal_proc_rec(
190 &format!("failure produced the wrong error: {}",
191 proc_res.status),
192 proc_res);
193 }
194 }
195
196 fn run_rpass_test(&self) {
197 let proc_res = self.compile_test();
198
199 if !proc_res.status.success() {
200 self.fatal_proc_rec("compilation failed!", &proc_res);
201 }
202
203 let proc_res = self.exec_compiled_test();
204
205 if !proc_res.status.success() {
206 self.fatal_proc_rec("test run failed!", &proc_res);
207 }
208 }
209
210 fn run_valgrind_test(&self) {
211 assert!(self.revision.is_none(), "revisions not relevant here");
212
213 if self.config.valgrind_path.is_none() {
214 assert!(!self.config.force_valgrind);
215 return self.run_rpass_test();
216 }
217
218 let mut proc_res = self.compile_test();
219
220 if !proc_res.status.success() {
221 self.fatal_proc_rec("compilation failed!", &proc_res);
222 }
223
224 let mut new_config = self.config.clone();
225 new_config.runtool = new_config.valgrind_path.clone();
226 let new_cx = TestCx { config: &new_config, ..*self };
227 proc_res = new_cx.exec_compiled_test();
228
229 if !proc_res.status.success() {
230 self.fatal_proc_rec("test run failed!", &proc_res);
231 }
232 }
233
234 fn run_pretty_test(&self) {
235 if self.props.pp_exact.is_some() {
236 logv(self.config, "testing for exact pretty-printing".to_owned());
237 } else {
238 logv(self.config, "testing for converging pretty-printing".to_owned());
239 }
240
241 let rounds = match self.props.pp_exact { Some(_) => 1, None => 2 };
242
243 let mut src = String::new();
244 File::open(&self.testpaths.file).unwrap().read_to_string(&mut src).unwrap();
245 let mut srcs = vec!(src);
246
247 let mut round = 0;
248 while round < rounds {
249 logv(self.config, format!("pretty-printing round {} revision {:?}",
250 round, self.revision));
251 let proc_res = self.print_source(srcs[round].to_owned(), &self.props.pretty_mode);
252
253 if !proc_res.status.success() {
254 self.fatal_proc_rec(&format!("pretty-printing failed in round {} revision {:?}",
255 round, self.revision),
256 &proc_res);
257 }
258
259 let ProcRes{ stdout, .. } = proc_res;
260 srcs.push(stdout);
261 round += 1;
262 }
263
264 let mut expected = match self.props.pp_exact {
265 Some(ref file) => {
266 let filepath = self.testpaths.file.parent().unwrap().join(file);
267 let mut s = String::new();
268 File::open(&filepath).unwrap().read_to_string(&mut s).unwrap();
269 s
270 }
271 None => { srcs[srcs.len() - 2].clone() }
272 };
273 let mut actual = srcs[srcs.len() - 1].clone();
274
275 if self.props.pp_exact.is_some() {
276 // Now we have to care about line endings
277 let cr = "\r".to_owned();
278 actual = actual.replace(&cr, "").to_owned();
279 expected = expected.replace(&cr, "").to_owned();
280 }
281
282 self.compare_source(&expected, &actual);
283
284 // If we're only making sure that the output matches then just stop here
285 if self.props.pretty_compare_only { return; }
286
287 // Finally, let's make sure it actually appears to remain valid code
288 let proc_res = self.typecheck_source(actual);
289 if !proc_res.status.success() {
290 self.fatal_proc_rec("pretty-printed source does not typecheck", &proc_res);
291 }
292
293 if !self.props.pretty_expanded { return }
294
295 // additionally, run `--pretty expanded` and try to build it.
296 let proc_res = self.print_source(srcs[round].clone(), "expanded");
297 if !proc_res.status.success() {
298 self.fatal_proc_rec("pretty-printing (expanded) failed", &proc_res);
299 }
300
301 let ProcRes{ stdout: expanded_src, .. } = proc_res;
302 let proc_res = self.typecheck_source(expanded_src);
303 if !proc_res.status.success() {
304 self.fatal_proc_rec(
305 "pretty-printed source (expanded) does not typecheck",
306 &proc_res);
307 }
308 }
309
310 fn print_source(&self,
311 src: String,
312 pretty_type: &str)
313 -> ProcRes {
314 let aux_dir = self.aux_output_dir_name();
315 self.compose_and_run(self.make_pp_args(pretty_type.to_owned()),
316 self.props.exec_env.clone(),
317 self.config.compile_lib_path.to_str().unwrap(),
318 Some(aux_dir.to_str().unwrap()),
319 Some(src))
320 }
321
322 fn make_pp_args(&self,
323 pretty_type: String)
324 -> ProcArgs {
325 let aux_dir = self.aux_output_dir_name();
326 // FIXME (#9639): This needs to handle non-utf8 paths
327 let mut args = vec!("-".to_owned(),
328 "-Zunstable-options".to_owned(),
329 "--unpretty".to_owned(),
330 pretty_type,
331 format!("--target={}", self.config.target),
332 "-L".to_owned(),
333 aux_dir.to_str().unwrap().to_owned());
334 args.extend(self.split_maybe_args(&self.config.target_rustcflags));
335 args.extend(self.props.compile_flags.iter().cloned());
336 return ProcArgs {
337 prog: self.config.rustc_path.to_str().unwrap().to_owned(),
338 args: args,
339 };
340 }
341
342 fn compare_source(&self,
343 expected: &str,
344 actual: &str) {
345 if expected != actual {
346 self.error("pretty-printed source does not match expected source");
347 println!("\n\
348expected:\n\
349------------------------------------------\n\
350{}\n\
351------------------------------------------\n\
352actual:\n\
353------------------------------------------\n\
354{}\n\
355------------------------------------------\n\
356\n",
357 expected, actual);
358 panic!();
359 }
360 }
361
362 fn typecheck_source(&self, src: String) -> ProcRes {
363 let args = self.make_typecheck_args();
364 self.compose_and_run_compiler(args, Some(src))
365 }
366
367 fn make_typecheck_args(&self) -> ProcArgs {
368 let aux_dir = self.aux_output_dir_name();
369 let target = if self.props.force_host {
370 &*self.config.host
371 } else {
372 &*self.config.target
373 };
374 // FIXME (#9639): This needs to handle non-utf8 paths
375 let mut args = vec!("-".to_owned(),
376 "-Zno-trans".to_owned(),
377 format!("--target={}", target),
378 "-L".to_owned(),
379 self.config.build_base.to_str().unwrap().to_owned(),
380 "-L".to_owned(),
381 aux_dir.to_str().unwrap().to_owned());
382 if let Some(revision) = self.revision {
383 args.extend(vec![
384 format!("--cfg"),
385 format!("{}", revision),
386 ]);
387 }
388 args.extend(self.split_maybe_args(&self.config.target_rustcflags));
389 args.extend(self.props.compile_flags.iter().cloned());
390 // FIXME (#9639): This needs to handle non-utf8 paths
391 return ProcArgs {
392 prog: self.config.rustc_path.to_str().unwrap().to_owned(),
393 args: args,
394 };
395 }
396
397 fn run_debuginfo_gdb_test(&self) {
398 assert!(self.revision.is_none(), "revisions not relevant here");
399
400 let config = Config {
401 target_rustcflags: self.cleanup_debug_info_options(&self.config.target_rustcflags),
402 host_rustcflags: self.cleanup_debug_info_options(&self.config.host_rustcflags),
403 .. self.config.clone()
404 };
405
406 let test_cx = TestCx {
407 config: &config,
408 ..*self
409 };
410
411 test_cx.run_debuginfo_gdb_test_no_opt();
412 }
413
414 fn run_debuginfo_gdb_test_no_opt(&self) {
415 let DebuggerCommands {
416 commands,
417 check_lines,
418 breakpoint_lines
419 } = self.parse_debugger_commands("gdb");
420 let mut cmds = commands.join("\n");
421
422 // compile test file (it should have 'compile-flags:-g' in the header)
423 let compiler_run_result = self.compile_test();
424 if !compiler_run_result.status.success() {
425 self.fatal_proc_rec("compilation failed!", &compiler_run_result);
426 }
427
428 let exe_file = self.make_exe_name();
429
430 let debugger_run_result;
431 match &*self.config.target {
432 "arm-linux-androideabi" | "armv7-linux-androideabi" | "aarch64-linux-android" => {
433
434 cmds = cmds.replace("run", "continue");
435
436 let tool_path = match self.config.android_cross_path.to_str() {
437 Some(x) => x.to_owned(),
438 None => self.fatal("cannot find android cross path")
439 };
440
441 // write debugger script
442 let mut script_str = String::with_capacity(2048);
443 script_str.push_str(&format!("set charset {}\n", Self::charset()));
444 script_str.push_str(&format!("set sysroot {}\n", tool_path));
445 script_str.push_str(&format!("file {}\n", exe_file.to_str().unwrap()));
446 script_str.push_str("target remote :5039\n");
447 script_str.push_str(&format!("set solib-search-path \
448 ./{}/stage2/lib/rustlib/{}/lib/\n",
449 self.config.host, self.config.target));
450 for line in &breakpoint_lines {
451 script_str.push_str(&format!("break {:?}:{}\n",
452 self.testpaths.file.file_name()
453 .unwrap()
454 .to_string_lossy(),
455 *line)[..]);
456 }
457 script_str.push_str(&cmds);
458 script_str.push_str("\nquit\n");
459
460 debug!("script_str = {}", script_str);
461 self.dump_output_file(&script_str, "debugger.script");
462
463
464 procsrv::run("",
465 &self.config.adb_path,
466 None,
467 &[
468 "push".to_owned(),
469 exe_file.to_str().unwrap().to_owned(),
470 self.config.adb_test_dir.clone()
471 ],
472 vec!(("".to_owned(), "".to_owned())),
473 Some("".to_owned()))
474 .expect(&format!("failed to exec `{:?}`", self.config.adb_path));
475
476 procsrv::run("",
477 &self.config.adb_path,
478 None,
479 &[
480 "forward".to_owned(),
481 "tcp:5039".to_owned(),
482 "tcp:5039".to_owned()
483 ],
484 vec!(("".to_owned(), "".to_owned())),
485 Some("".to_owned()))
486 .expect(&format!("failed to exec `{:?}`", self.config.adb_path));
487
488 let adb_arg = format!("export LD_LIBRARY_PATH={}; \
489 gdbserver{} :5039 {}/{}",
490 self.config.adb_test_dir.clone(),
491 if self.config.target.contains("aarch64")
492 {"64"} else {""},
493 self.config.adb_test_dir.clone(),
494 exe_file.file_name().unwrap().to_str()
495 .unwrap());
496
497 let mut process = procsrv::run_background("",
498 &self.config.adb_path
499 ,
500 None,
501 &[
502 "shell".to_owned(),
503 adb_arg.clone()
504 ],
505 vec!(("".to_owned(),
506 "".to_owned())),
507 Some("".to_owned()))
508 .expect(&format!("failed to exec `{:?}`", self.config.adb_path));
509 loop {
510 //waiting 1 second for gdbserver start
511 ::std::thread::sleep(::std::time::Duration::new(1,0));
512 if TcpStream::connect("127.0.0.1:5039").is_ok() {
513 break
514 }
515 }
516
517 let debugger_script = self.make_out_name("debugger.script");
518 // FIXME (#9639): This needs to handle non-utf8 paths
519 let debugger_opts =
520 vec!("-quiet".to_owned(),
521 "-batch".to_owned(),
522 "-nx".to_owned(),
523 format!("-command={}", debugger_script.to_str().unwrap()));
524
525 let mut gdb_path = tool_path;
526 gdb_path.push_str(&format!("/bin/{}-gdb", self.config.target));
527 let procsrv::Result {
528 out,
529 err,
530 status
531 } = procsrv::run("",
532 &gdb_path,
533 None,
534 &debugger_opts,
535 vec!(("".to_owned(), "".to_owned())),
536 None)
537 .expect(&format!("failed to exec `{:?}`", gdb_path));
538 let cmdline = {
539 let cmdline = self.make_cmdline("",
540 &format!("{}-gdb", self.config.target),
541 &debugger_opts);
542 logv(self.config, format!("executing {}", cmdline));
543 cmdline
544 };
545
546 debugger_run_result = ProcRes {
547 status: Status::Normal(status),
548 stdout: out,
549 stderr: err,
550 cmdline: cmdline
551 };
552 if process.kill().is_err() {
553 println!("Adb process is already finished.");
554 }
555 }
556
557 _=> {
558 let rust_src_root = self.find_rust_src_root()
559 .expect("Could not find Rust source root");
560 let rust_pp_module_rel_path = Path::new("./src/etc");
561 let rust_pp_module_abs_path = rust_src_root.join(rust_pp_module_rel_path)
562 .to_str()
563 .unwrap()
564 .to_owned();
565 // write debugger script
566 let mut script_str = String::with_capacity(2048);
567 script_str.push_str(&format!("set charset {}\n", Self::charset()));
568 script_str.push_str("show version\n");
569
570 match self.config.gdb_version {
571 Some(ref version) => {
572 println!("NOTE: compiletest thinks it is using GDB version {}",
573 version);
574
575 if header::gdb_version_to_int(version) >
576 header::gdb_version_to_int("7.4") {
577 // Add the directory containing the pretty printers to
578 // GDB's script auto loading safe path
579 script_str.push_str(
580 &format!("add-auto-load-safe-path {}\n",
581 rust_pp_module_abs_path.replace(r"\", r"\\"))
582 );
583 }
584 }
585 _ => {
586 println!("NOTE: compiletest does not know which version of \
587 GDB it is using");
588 }
589 }
590
591 // The following line actually doesn't have to do anything with
592 // pretty printing, it just tells GDB to print values on one line:
593 script_str.push_str("set print pretty off\n");
594
595 // Add the pretty printer directory to GDB's source-file search path
596 script_str.push_str(&format!("directory {}\n",
597 rust_pp_module_abs_path));
598
599 // Load the target executable
600 script_str.push_str(&format!("file {}\n",
601 exe_file.to_str().unwrap()
602 .replace(r"\", r"\\")));
603
604 // Add line breakpoints
605 for line in &breakpoint_lines {
606 script_str.push_str(&format!("break '{}':{}\n",
607 self.testpaths.file.file_name().unwrap()
608 .to_string_lossy(),
609 *line));
610 }
611
612 script_str.push_str(&cmds);
613 script_str.push_str("\nquit\n");
614
615 debug!("script_str = {}", script_str);
616 self.dump_output_file(&script_str, "debugger.script");
617
618 // run debugger script with gdb
619 fn debugger() -> &'static str {
620 if cfg!(windows) {"gdb.exe"} else {"gdb"}
621 }
622
623 let debugger_script = self.make_out_name("debugger.script");
624
625 // FIXME (#9639): This needs to handle non-utf8 paths
626 let debugger_opts =
627 vec!("-quiet".to_owned(),
628 "-batch".to_owned(),
629 "-nx".to_owned(),
630 format!("-command={}", debugger_script.to_str().unwrap()));
631
632 let proc_args = ProcArgs {
633 prog: debugger().to_owned(),
634 args: debugger_opts,
635 };
636
637 let environment = vec![("PYTHONPATH".to_owned(), rust_pp_module_abs_path)];
638
639 debugger_run_result =
640 self.compose_and_run(proc_args,
641 environment,
642 self.config.run_lib_path.to_str().unwrap(),
643 None,
644 None);
645 }
646 }
647
648 if !debugger_run_result.status.success() {
649 self.fatal("gdb failed to execute");
650 }
651
652 self.check_debugger_output(&debugger_run_result, &check_lines);
653 }
654
655 fn find_rust_src_root(&self) -> Option<PathBuf> {
656 let mut path = self.config.src_base.clone();
657 let path_postfix = Path::new("src/etc/lldb_batchmode.py");
658
659 while path.pop() {
660 if path.join(&path_postfix).is_file() {
661 return Some(path);
662 }
663 }
664
665 return None;
666 }
667
668 fn run_debuginfo_lldb_test(&self) {
669 assert!(self.revision.is_none(), "revisions not relevant here");
670
671 if self.config.lldb_python_dir.is_none() {
672 self.fatal("Can't run LLDB test because LLDB's python path is not set.");
673 }
674
675 let config = Config {
676 target_rustcflags: self.cleanup_debug_info_options(&self.config.target_rustcflags),
677 host_rustcflags: self.cleanup_debug_info_options(&self.config.host_rustcflags),
678 .. self.config.clone()
679 };
680
681
682 let test_cx = TestCx {
683 config: &config,
684 ..*self
685 };
686
687 test_cx.run_debuginfo_lldb_test_no_opt();
688 }
689
690 fn run_debuginfo_lldb_test_no_opt(&self) {
691 // compile test file (it should have 'compile-flags:-g' in the header)
692 let compile_result = self.compile_test();
693 if !compile_result.status.success() {
694 self.fatal_proc_rec("compilation failed!", &compile_result);
695 }
696
697 let exe_file = self.make_exe_name();
698
699 match self.config.lldb_version {
700 Some(ref version) => {
701 println!("NOTE: compiletest thinks it is using LLDB version {}",
702 version);
703 }
704 _ => {
705 println!("NOTE: compiletest does not know which version of \
706 LLDB it is using");
707 }
708 }
709
710 // Parse debugger commands etc from test files
711 let DebuggerCommands {
712 commands,
713 check_lines,
714 breakpoint_lines,
715 ..
716 } = self.parse_debugger_commands("lldb");
717
718 // Write debugger script:
719 // We don't want to hang when calling `quit` while the process is still running
720 let mut script_str = String::from("settings set auto-confirm true\n");
721
722 // Make LLDB emit its version, so we have it documented in the test output
723 script_str.push_str("version\n");
724
725 // Switch LLDB into "Rust mode"
726 let rust_src_root = self.find_rust_src_root().expect("Could not find Rust source root");
727 let rust_pp_module_rel_path = Path::new("./src/etc/lldb_rust_formatters.py");
728 let rust_pp_module_abs_path = rust_src_root.join(rust_pp_module_rel_path)
729 .to_str()
730 .unwrap()
731 .to_owned();
732
733 script_str.push_str(&format!("command script import {}\n",
734 &rust_pp_module_abs_path[..])[..]);
735 script_str.push_str("type summary add --no-value ");
736 script_str.push_str("--python-function lldb_rust_formatters.print_val ");
737 script_str.push_str("-x \".*\" --category Rust\n");
738 script_str.push_str("type category enable Rust\n");
739
740 // Set breakpoints on every line that contains the string "#break"
741 let source_file_name = self.testpaths.file.file_name().unwrap().to_string_lossy();
742 for line in &breakpoint_lines {
743 script_str.push_str(&format!("breakpoint set --file '{}' --line {}\n",
744 source_file_name,
745 line));
746 }
747
748 // Append the other commands
749 for line in &commands {
750 script_str.push_str(line);
751 script_str.push_str("\n");
752 }
753
754 // Finally, quit the debugger
755 script_str.push_str("\nquit\n");
756
757 // Write the script into a file
758 debug!("script_str = {}", script_str);
759 self.dump_output_file(&script_str, "debugger.script");
760 let debugger_script = self.make_out_name("debugger.script");
761
762 // Let LLDB execute the script via lldb_batchmode.py
763 let debugger_run_result = self.run_lldb(&exe_file,
764 &debugger_script,
765 &rust_src_root);
766
767 if !debugger_run_result.status.success() {
768 self.fatal_proc_rec("Error while running LLDB", &debugger_run_result);
769 }
770
771 self.check_debugger_output(&debugger_run_result, &check_lines);
772 }
773
774 fn run_lldb(&self,
775 test_executable: &Path,
776 debugger_script: &Path,
777 rust_src_root: &Path)
778 -> ProcRes {
779 // Prepare the lldb_batchmode which executes the debugger script
780 let lldb_script_path = rust_src_root.join("src/etc/lldb_batchmode.py");
781 self.cmd2procres(Command::new(&self.config.lldb_python)
782 .arg(&lldb_script_path)
783 .arg(test_executable)
784 .arg(debugger_script)
785 .env("PYTHONPATH",
786 self.config.lldb_python_dir.as_ref().unwrap()))
787 }
788
789 fn cmd2procres(&self, cmd: &mut Command) -> ProcRes {
790 let (status, out, err) = match cmd.output() {
791 Ok(Output { status, stdout, stderr }) => {
792 (status,
793 String::from_utf8(stdout).unwrap(),
794 String::from_utf8(stderr).unwrap())
795 },
796 Err(e) => {
797 self.fatal(&format!("Failed to setup Python process for \
798 LLDB script: {}", e))
799 }
800 };
801
802 self.dump_output(&out, &err);
803 ProcRes {
804 status: Status::Normal(status),
805 stdout: out,
806 stderr: err,
807 cmdline: format!("{:?}", cmd)
808 }
809 }
810
811 fn parse_debugger_commands(&self, debugger_prefix: &str) -> DebuggerCommands {
812 let command_directive = format!("{}-command", debugger_prefix);
813 let check_directive = format!("{}-check", debugger_prefix);
814
815 let mut breakpoint_lines = vec!();
816 let mut commands = vec!();
817 let mut check_lines = vec!();
818 let mut counter = 1;
819 let reader = BufReader::new(File::open(&self.testpaths.file).unwrap());
820 for line in reader.lines() {
821 match line {
822 Ok(line) => {
823 if line.contains("#break") {
824 breakpoint_lines.push(counter);
825 }
826
827 header::parse_name_value_directive(
828 &line,
829 &command_directive).map(|cmd| {
830 commands.push(cmd)
831 });
832
833 header::parse_name_value_directive(
834 &line,
835 &check_directive).map(|cmd| {
836 check_lines.push(cmd)
837 });
838 }
839 Err(e) => {
840 self.fatal(&format!("Error while parsing debugger commands: {}", e))
841 }
842 }
843 counter += 1;
844 }
845
846 DebuggerCommands {
847 commands: commands,
848 check_lines: check_lines,
849 breakpoint_lines: breakpoint_lines,
850 }
851 }
852
853 fn cleanup_debug_info_options(&self, options: &Option<String>) -> Option<String> {
854 if options.is_none() {
855 return None;
856 }
857
858 // Remove options that are either unwanted (-O) or may lead to duplicates due to RUSTFLAGS.
859 let options_to_remove = [
860 "-O".to_owned(),
861 "-g".to_owned(),
862 "--debuginfo".to_owned()
863 ];
864 let new_options =
865 self.split_maybe_args(options).into_iter()
866 .filter(|x| !options_to_remove.contains(x))
867 .collect::<Vec<String>>();
868
869 Some(new_options.join(" "))
870 }
871
872 fn check_debugger_output(&self, debugger_run_result: &ProcRes, check_lines: &[String]) {
873 let num_check_lines = check_lines.len();
874
875 let mut check_line_index = 0;
876 for line in debugger_run_result.stdout.lines() {
877 if check_line_index >= num_check_lines {
878 break;
879 }
880
881 if check_single_line(line, &(check_lines[check_line_index])[..]) {
882 check_line_index += 1;
883 }
884 }
885 if check_line_index != num_check_lines && num_check_lines > 0 {
886 self.fatal_proc_rec(&format!("line not found in debugger output: {}",
887 check_lines[check_line_index]),
888 debugger_run_result);
889 }
890
891 fn check_single_line(line: &str, check_line: &str) -> bool {
892 // Allow check lines to leave parts unspecified (e.g., uninitialized
893 // bits in the wrong case of an enum) with the notation "[...]".
894 let line = line.trim();
895 let check_line = check_line.trim();
896 let can_start_anywhere = check_line.starts_with("[...]");
897 let can_end_anywhere = check_line.ends_with("[...]");
898
899 let check_fragments: Vec<&str> = check_line.split("[...]")
900 .filter(|frag| !frag.is_empty())
901 .collect();
902 if check_fragments.is_empty() {
903 return true;
904 }
905
906 let (mut rest, first_fragment) = if can_start_anywhere {
907 match line.find(check_fragments[0]) {
908 Some(pos) => (&line[pos + check_fragments[0].len() ..], 1),
909 None => return false
910 }
911 } else {
912 (line, 0)
913 };
914
915 for fragment_index in first_fragment .. check_fragments.len() {
916 let current_fragment = check_fragments[fragment_index];
917 match rest.find(current_fragment) {
918 Some(pos) => {
919 rest = &rest[pos + current_fragment.len() .. ];
920 }
921 None => return false
922 }
923 }
924
925 if !can_end_anywhere && !rest.is_empty() {
926 return false;
927 }
928
929 return true;
930 }
931 }
932
933 fn check_error_patterns(&self,
934 output_to_check: &str,
935 proc_res: &ProcRes) {
936 if self.props.error_patterns.is_empty() {
937 self.fatal(&format!("no error pattern specified in {:?}",
938 self.testpaths.file.display()));
939 }
940 let mut next_err_idx = 0;
941 let mut next_err_pat = self.props.error_patterns[next_err_idx].trim();
942 let mut done = false;
943 for line in output_to_check.lines() {
944 if line.contains(next_err_pat) {
945 debug!("found error pattern {}", next_err_pat);
946 next_err_idx += 1;
947 if next_err_idx == self.props.error_patterns.len() {
948 debug!("found all error patterns");
949 done = true;
950 break;
951 }
952 next_err_pat = self.props.error_patterns[next_err_idx].trim();
953 }
954 }
955 if done { return; }
956
957 let missing_patterns = &self.props.error_patterns[next_err_idx..];
958 if missing_patterns.len() == 1 {
959 self.fatal_proc_rec(
960 &format!("error pattern '{}' not found!", missing_patterns[0]),
961 proc_res);
962 } else {
963 for pattern in missing_patterns {
964 self.error(&format!("error pattern '{}' not found!", *pattern));
965 }
966 self.fatal_proc_rec("multiple error patterns not found", proc_res);
967 }
968 }
969
970 fn check_no_compiler_crash(&self, proc_res: &ProcRes) {
971 for line in proc_res.stderr.lines() {
972 if line.starts_with("error: internal compiler error:") {
973 self.fatal_proc_rec("compiler encountered internal error", proc_res);
974 }
975 }
976 }
977
978 fn check_forbid_output(&self,
979 output_to_check: &str,
980 proc_res: &ProcRes) {
981 for pat in &self.props.forbid_output {
982 if output_to_check.contains(pat) {
983 self.fatal_proc_rec("forbidden pattern found in compiler output", proc_res);
984 }
985 }
986 }
987
988 fn check_expected_errors(&self,
989 expected_errors: Vec<errors::Error>,
990 proc_res: &ProcRes) {
991 if proc_res.status.success() {
992 self.fatal_proc_rec("process did not return an error status", proc_res);
993 }
994
995 let file_name =
996 format!("{}", self.testpaths.file.display())
997 .replace(r"\", "/"); // on windows, translate all '\' path separators to '/'
998
999 // If the testcase being checked contains at least one expected "help"
1000 // message, then we'll ensure that all "help" messages are expected.
1001 // Otherwise, all "help" messages reported by the compiler will be ignored.
1002 // This logic also applies to "note" messages.
1003 let expect_help = expected_errors.iter().any(|ee| ee.kind == Some(ErrorKind::Help));
1004 let expect_note = expected_errors.iter().any(|ee| ee.kind == Some(ErrorKind::Note));
1005
1006 // Parse the JSON output from the compiler and extract out the messages.
1007 let actual_errors = json::parse_output(&file_name, &proc_res.stderr, &proc_res);
1008 let mut unexpected = 0;
1009 let mut not_found = 0;
1010 let mut found = vec![false; expected_errors.len()];
1011 for actual_error in &actual_errors {
1012 let opt_index =
1013 expected_errors
1014 .iter()
1015 .enumerate()
1016 .position(|(index, expected_error)| {
1017 !found[index] &&
1018 actual_error.line_num == expected_error.line_num &&
1019 (expected_error.kind.is_none() ||
1020 actual_error.kind == expected_error.kind) &&
1021 actual_error.msg.contains(&expected_error.msg)
1022 });
1023
1024 match opt_index {
1025 Some(index) => {
1026 // found a match, everybody is happy
1027 assert!(!found[index]);
1028 found[index] = true;
1029 }
1030
1031 None => {
1032 if self.is_unexpected_compiler_message(actual_error, expect_help, expect_note) {
1033 self.error(
1034 &format!("{}:{}: unexpected {:?}: '{}'",
1035 file_name,
1036 actual_error.line_num,
1037 actual_error.kind.as_ref()
1038 .map_or(String::from("message"),
1039 |k| k.to_string()),
1040 actual_error.msg));
1041 unexpected += 1;
1042 }
1043 }
1044 }
1045 }
1046
1047 // anything not yet found is a problem
1048 for (index, expected_error) in expected_errors.iter().enumerate() {
1049 if !found[index] {
1050 self.error(
1051 &format!("{}:{}: expected {} not found: {}",
1052 file_name,
1053 expected_error.line_num,
1054 expected_error.kind.as_ref()
1055 .map_or("message".into(),
1056 |k| k.to_string()),
1057 expected_error.msg));
1058 not_found += 1;
1059 }
1060 }
1061
1062 if unexpected > 0 || not_found > 0 {
1063 self.error(
1064 &format!("{} unexpected errors found, {} expected errors not found",
1065 unexpected, not_found));
1066 print!("status: {}\ncommand: {}\n",
1067 proc_res.status, proc_res.cmdline);
1068 println!("actual errors (from JSON output): {:#?}\n", actual_errors);
1069 println!("expected errors (from test file): {:#?}\n", expected_errors);
1070 panic!();
1071 }
1072 }
1073
1074 /// Returns true if we should report an error about `actual_error`,
1075 /// which did not match any of the expected error. We always require
1076 /// errors/warnings to be explicitly listed, but only require
1077 /// helps/notes if there are explicit helps/notes given.
1078 fn is_unexpected_compiler_message(&self,
1079 actual_error: &Error,
1080 expect_help: bool,
1081 expect_note: bool)
1082 -> bool {
1083 match actual_error.kind {
1084 Some(ErrorKind::Help) => expect_help,
1085 Some(ErrorKind::Note) => expect_note,
1086 Some(ErrorKind::Error) => true,
1087 Some(ErrorKind::Warning) => true,
1088 Some(ErrorKind::Suggestion) => false,
1089 None => false
1090 }
1091 }
1092
1093 fn compile_test(&self) -> ProcRes {
1094 let aux_dir = self.aux_output_dir_name();
1095 // FIXME (#9639): This needs to handle non-utf8 paths
1096 let link_args = vec!("-L".to_owned(),
1097 aux_dir.to_str().unwrap().to_owned());
1098 let args = self.make_compile_args(link_args,
1099 &self.testpaths.file,
1100 TargetLocation::ThisFile(self.make_exe_name()));
1101 self.compose_and_run_compiler(args, None)
1102 }
1103
1104 fn document(&self, out_dir: &Path) -> ProcRes {
1105 if self.props.build_aux_docs {
1106 for rel_ab in &self.props.aux_builds {
1107 let aux_testpaths = self.compute_aux_test_paths(rel_ab);
1108 let aux_props = self.props.from_aux_file(&aux_testpaths.file, self.revision);
1109 let aux_cx = TestCx {
1110 config: self.config,
1111 props: &aux_props,
1112 testpaths: &aux_testpaths,
1113 revision: self.revision
1114 };
1115 let auxres = aux_cx.document(out_dir);
1116 if !auxres.status.success() {
1117 return auxres;
1118 }
1119 }
1120 }
1121
1122 let aux_dir = self.aux_output_dir_name();
1123 let mut args = vec!["-L".to_owned(),
1124 aux_dir.to_str().unwrap().to_owned(),
1125 "-o".to_owned(),
1126 out_dir.to_str().unwrap().to_owned(),
1127 self.testpaths.file.to_str().unwrap().to_owned()];
1128 args.extend(self.props.compile_flags.iter().cloned());
1129 let args = ProcArgs {
1130 prog: self.config.rustdoc_path.to_str().unwrap().to_owned(),
1131 args: args,
1132 };
1133 self.compose_and_run_compiler(args, None)
1134 }
1135
1136 fn exec_compiled_test(&self) -> ProcRes {
1137 let env = self.props.exec_env.clone();
1138
1139 match &*self.config.target {
1140
1141 "arm-linux-androideabi" | "armv7-linux-androideabi" | "aarch64-linux-android" => {
1142 self._arm_exec_compiled_test(env)
1143 }
1144
1145 _=> {
1146 let aux_dir = self.aux_output_dir_name();
1147 self.compose_and_run(self.make_run_args(),
1148 env,
1149 self.config.run_lib_path.to_str().unwrap(),
1150 Some(aux_dir.to_str().unwrap()),
1151 None)
1152 }
1153 }
1154 }
1155
1156 /// For each `aux-build: foo/bar` annotation, we check to find the
1157 /// file in a `aux` directory relative to the test itself.
1158 fn compute_aux_test_paths(&self, rel_ab: &str) -> TestPaths {
1159 let test_ab = self.testpaths.file
1160 .parent()
1161 .expect("test file path has no parent")
1162 .join("auxiliary")
1163 .join(rel_ab);
1164 if !test_ab.exists() {
1165 self.fatal(&format!("aux-build `{}` source not found", test_ab.display()))
1166 }
1167
1168 TestPaths {
1169 file: test_ab,
1170 base: self.testpaths.base.clone(),
1171 relative_dir: self.testpaths.relative_dir
1172 .join("auxiliary")
1173 .join(rel_ab)
1174 .parent()
1175 .expect("aux-build path has no parent")
1176 .to_path_buf()
1177 }
1178 }
1179
1180 fn compose_and_run_compiler(&self, args: ProcArgs, input: Option<String>) -> ProcRes {
1181 if !self.props.aux_builds.is_empty() {
1182 self.create_dir_racy(&self.aux_output_dir_name());
1183 }
1184
1185 let aux_dir = self.aux_output_dir_name();
1186 // FIXME (#9639): This needs to handle non-utf8 paths
1187 let extra_link_args = vec!["-L".to_owned(),
1188 aux_dir.to_str().unwrap().to_owned()];
1189
1190 for rel_ab in &self.props.aux_builds {
1191 let aux_testpaths = self.compute_aux_test_paths(rel_ab);
1192 let aux_props = self.props.from_aux_file(&aux_testpaths.file, self.revision);
1193 let mut crate_type = if aux_props.no_prefer_dynamic {
1194 Vec::new()
1195 } else {
1196 // We primarily compile all auxiliary libraries as dynamic libraries
1197 // to avoid code size bloat and large binaries as much as possible
1198 // for the test suite (otherwise including libstd statically in all
1199 // executables takes up quite a bit of space).
1200 //
1201 // For targets like MUSL or Emscripten, however, there is no support for
1202 // dynamic libraries so we just go back to building a normal library. Note,
1203 // however, that for MUSL if the library is built with `force_host` then
1204 // it's ok to be a dylib as the host should always support dylibs.
1205 if (self.config.target.contains("musl") && !aux_props.force_host) ||
1206 self.config.target.contains("emscripten")
1207 {
1208 vec!("--crate-type=lib".to_owned())
1209 } else {
1210 vec!("--crate-type=dylib".to_owned())
1211 }
1212 };
1213 crate_type.extend(extra_link_args.clone());
1214 let aux_output = {
1215 let f = self.make_lib_name(&self.testpaths.file);
1216 let parent = f.parent().unwrap();
1217 TargetLocation::ThisDirectory(parent.to_path_buf())
1218 };
1219 let aux_cx = TestCx {
1220 config: self.config,
1221 props: &aux_props,
1222 testpaths: &aux_testpaths,
1223 revision: self.revision
1224 };
1225 let aux_args = aux_cx.make_compile_args(crate_type, &aux_testpaths.file, aux_output);
1226 let auxres = aux_cx.compose_and_run(aux_args,
1227 Vec::new(),
1228 aux_cx.config.compile_lib_path.to_str().unwrap(),
1229 Some(aux_dir.to_str().unwrap()),
1230 None);
1231 if !auxres.status.success() {
1232 self.fatal_proc_rec(
1233 &format!("auxiliary build of {:?} failed to compile: ",
1234 aux_testpaths.file.display()),
1235 &auxres);
1236 }
1237
1238 match &*self.config.target {
1239 "arm-linux-androideabi" | "armv7-linux-androideabi" | "aarch64-linux-android" => {
1240 self._arm_push_aux_shared_library();
1241 }
1242 _ => {}
1243 }
1244 }
1245
1246 self.compose_and_run(args,
1247 self.props.rustc_env.clone(),
1248 self.config.compile_lib_path.to_str().unwrap(),
1249 Some(aux_dir.to_str().unwrap()),
1250 input)
1251 }
1252
1253 // Like std::fs::create_dir_all, except handles concurrent calls among multiple
1254 // threads or processes.
1255 fn create_dir_racy(&self, path: &Path) {
1256 match fs::create_dir(path) {
1257 Ok(()) => return,
1258 Err(ref e) if e.kind() == io::ErrorKind::AlreadyExists => return,
1259 Err(ref e) if e.kind() == io::ErrorKind::NotFound => {}
1260 Err(e) => panic!("failed to create dir {:?}: {}", path, e),
1261 }
1262 self.create_dir_racy(path.parent().unwrap());
1263 match fs::create_dir(path) {
1264 Ok(()) => {}
1265 Err(ref e) if e.kind() == io::ErrorKind::AlreadyExists => {}
1266 Err(e) => panic!("failed to create dir {:?}: {}", path, e),
1267 }
1268 }
1269
1270 fn compose_and_run(&self,
1271 ProcArgs{ args, prog }: ProcArgs,
1272 procenv: Vec<(String, String)> ,
1273 lib_path: &str,
1274 aux_path: Option<&str>,
1275 input: Option<String>) -> ProcRes {
1276 return self.program_output(lib_path, prog, aux_path, args, procenv, input);
1277 }
1278
1279 fn make_compile_args(&self,
1280 extras: Vec<String> ,
1281 input_file: &Path,
1282 output_file: TargetLocation)
1283 -> ProcArgs
1284 {
1285 let target = if self.props.force_host {
1286 &*self.config.host
1287 } else {
1288 &*self.config.target
1289 };
1290
1291 // FIXME (#9639): This needs to handle non-utf8 paths
1292 let mut args = vec!(input_file.to_str().unwrap().to_owned(),
1293 "-L".to_owned(),
1294 self.config.build_base.to_str().unwrap().to_owned(),
1295 format!("--target={}", target));
1296
1297 if let Some(revision) = self.revision {
1298 args.extend(vec![
1299 format!("--cfg"),
1300 format!("{}", revision),
1301 ]);
1302 }
1303
1304 if let Some(ref incremental_dir) = self.props.incremental_dir {
1305 args.extend(vec![
1306 format!("-Z"),
1307 format!("incremental={}", incremental_dir.display()),
1308 ]);
1309 }
1310
1311
1312 match self.config.mode {
1313 CompileFail |
1314 ParseFail |
1315 Incremental => {
1316 // If we are extracting and matching errors in the new
1317 // fashion, then you want JSON mode. Old-skool error
1318 // patterns still match the raw compiler output.
1319 if self.props.error_patterns.is_empty() {
1320 args.extend(["--error-format",
1321 "json",
1322 "-Z",
1323 "unstable-options"]
1324 .iter()
1325 .map(|s| s.to_string()));
1326 }
1327 }
1328
1329 RunFail |
1330 RunPass |
1331 RunPassValgrind |
1332 Pretty |
1333 DebugInfoGdb |
1334 DebugInfoLldb |
1335 Codegen |
1336 Rustdoc |
1337 RunMake |
1338 Ui |
1339 CodegenUnits => {
1340 // do not use JSON output
1341 }
1342 }
1343
1344 args.extend_from_slice(&extras);
1345 if !self.props.no_prefer_dynamic {
1346 args.push("-C".to_owned());
1347 args.push("prefer-dynamic".to_owned());
1348 }
1349 let path = match output_file {
1350 TargetLocation::ThisFile(path) => {
1351 args.push("-o".to_owned());
1352 path
1353 }
1354 TargetLocation::ThisDirectory(path) => {
1355 args.push("--out-dir".to_owned());
1356 path
1357 }
1358 };
1359 args.push(path.to_str().unwrap().to_owned());
1360 if self.props.force_host {
1361 args.extend(self.split_maybe_args(&self.config.host_rustcflags));
1362 } else {
1363 args.extend(self.split_maybe_args(&self.config.target_rustcflags));
1364 }
1365 args.extend(self.props.compile_flags.iter().cloned());
1366 return ProcArgs {
1367 prog: self.config.rustc_path.to_str().unwrap().to_owned(),
1368 args: args,
1369 };
1370 }
1371
1372 fn make_lib_name(&self, auxfile: &Path) -> PathBuf {
1373 // what we return here is not particularly important, as it
1374 // happens; rustc ignores everything except for the directory.
1375 let auxname = self.output_testname(auxfile);
1376 self.aux_output_dir_name().join(&auxname)
1377 }
1378
1379 fn make_exe_name(&self) -> PathBuf {
1380 let mut f = self.output_base_name();
1381 // FIXME: This is using the host architecture exe suffix, not target!
1382 if self.config.target == "asmjs-unknown-emscripten" {
1383 let mut fname = f.file_name().unwrap().to_os_string();
1384 fname.push(".js");
1385 f.set_file_name(&fname);
1386 } else if !env::consts::EXE_SUFFIX.is_empty() {
1387 let mut fname = f.file_name().unwrap().to_os_string();
1388 fname.push(env::consts::EXE_SUFFIX);
1389 f.set_file_name(&fname);
1390 }
1391 f
1392 }
1393
1394 fn make_run_args(&self) -> ProcArgs {
1395 // If we've got another tool to run under (valgrind),
1396 // then split apart its command
1397 let mut args = self.split_maybe_args(&self.config.runtool);
1398
1399 // If this is emscripten, then run tests under nodejs
1400 if self.config.target == "asmjs-unknown-emscripten" {
1401 args.push("nodejs".to_owned());
1402 }
1403
1404 let exe_file = self.make_exe_name();
1405
1406 // FIXME (#9639): This needs to handle non-utf8 paths
1407 args.push(exe_file.to_str().unwrap().to_owned());
1408
1409 // Add the arguments in the run_flags directive
1410 args.extend(self.split_maybe_args(&self.props.run_flags));
1411
1412 let prog = args.remove(0);
1413 return ProcArgs {
1414 prog: prog,
1415 args: args,
1416 };
1417 }
1418
1419 fn split_maybe_args(&self, argstr: &Option<String>) -> Vec<String> {
1420 match *argstr {
1421 Some(ref s) => {
1422 s
1423 .split(' ')
1424 .filter_map(|s| {
1425 if s.chars().all(|c| c.is_whitespace()) {
1426 None
1427 } else {
1428 Some(s.to_owned())
1429 }
1430 }).collect()
1431 }
1432 None => Vec::new()
1433 }
1434 }
1435
1436 fn program_output(&self,
1437 lib_path: &str,
1438 prog: String,
1439 aux_path: Option<&str>,
1440 args: Vec<String>,
1441 env: Vec<(String, String)>,
1442 input: Option<String>)
1443 -> ProcRes {
1444 let cmdline =
1445 {
1446 let cmdline = self.make_cmdline(lib_path,
1447 &prog,
1448 &args);
1449 logv(self.config, format!("executing {}", cmdline));
1450 cmdline
1451 };
1452 let procsrv::Result {
1453 out,
1454 err,
1455 status
1456 } = procsrv::run(lib_path,
1457 &prog,
1458 aux_path,
1459 &args,
1460 env,
1461 input).expect(&format!("failed to exec `{}`", prog));
1462 self.dump_output(&out, &err);
1463 return ProcRes {
1464 status: Status::Normal(status),
1465 stdout: out,
1466 stderr: err,
1467 cmdline: cmdline,
1468 };
1469 }
1470
1471 fn make_cmdline(&self, libpath: &str, prog: &str, args: &[String]) -> String {
1472 use util;
1473
1474 // Linux and mac don't require adjusting the library search path
1475 if cfg!(unix) {
1476 format!("{} {}", prog, args.join(" "))
1477 } else {
1478 // Build the LD_LIBRARY_PATH variable as it would be seen on the command line
1479 // for diagnostic purposes
1480 fn lib_path_cmd_prefix(path: &str) -> String {
1481 format!("{}=\"{}\"", util::lib_path_env_var(), util::make_new_path(path))
1482 }
1483
1484 format!("{} {} {}", lib_path_cmd_prefix(libpath), prog, args.join(" "))
1485 }
1486 }
1487
1488 fn dump_output(&self, out: &str, err: &str) {
1489 self.dump_output_file(out, "out");
1490 self.dump_output_file(err, "err");
1491 self.maybe_dump_to_stdout(out, err);
1492 }
1493
1494 fn dump_output_file(&self,
1495 out: &str,
1496 extension: &str) {
1497 let outfile = self.make_out_name(extension);
1498 File::create(&outfile).unwrap().write_all(out.as_bytes()).unwrap();
1499 }
1500
1501 fn make_out_name(&self, extension: &str) -> PathBuf {
1502 self.output_base_name().with_extension(extension)
1503 }
1504
1505 fn aux_output_dir_name(&self) -> PathBuf {
1506 let f = self.output_base_name();
1507 let mut fname = f.file_name().unwrap().to_os_string();
1508 fname.push(&format!(".{}.libaux", self.config.mode));
1509 f.with_file_name(&fname)
1510 }
1511
1512 fn output_testname(&self, filepath: &Path) -> PathBuf {
1513 PathBuf::from(filepath.file_stem().unwrap())
1514 }
1515
1516 /// Given a test path like `compile-fail/foo/bar.rs` Returns a name like
1517 ///
1518 /// <output>/foo/bar-stage1
1519 fn output_base_name(&self) -> PathBuf {
1520 let dir = self.config.build_base.join(&self.testpaths.relative_dir);
1521
1522 // Note: The directory `dir` is created during `collect_tests_from_dir`
1523 dir
1524 .join(&self.output_testname(&self.testpaths.file))
1525 .with_extension(&self.config.stage_id)
1526 }
1527
1528 fn maybe_dump_to_stdout(&self, out: &str, err: &str) {
1529 if self.config.verbose {
1530 println!("------{}------------------------------", "stdout");
1531 println!("{}", out);
1532 println!("------{}------------------------------", "stderr");
1533 println!("{}", err);
1534 println!("------------------------------------------");
1535 }
1536 }
1537
1538 fn error(&self, err: &str) {
1539 match self.revision {
1540 Some(rev) => println!("\nerror in revision `{}`: {}", rev, err),
1541 None => println!("\nerror: {}", err)
1542 }
1543 }
1544
1545 fn fatal(&self, err: &str) -> ! {
1546 self.error(err); panic!();
1547 }
1548
1549 fn fatal_proc_rec(&self, err: &str, proc_res: &ProcRes) -> ! {
1550 self.error(err);
1551 proc_res.fatal(None);
1552 }
1553
1554 fn _arm_exec_compiled_test(&self, env: Vec<(String, String)>) -> ProcRes {
1555 let args = self.make_run_args();
1556 let cmdline = self.make_cmdline("", &args.prog, &args.args);
1557
1558 // get bare program string
1559 let mut tvec: Vec<String> = args.prog
1560 .split('/')
1561 .map(str::to_owned)
1562 .collect();
1563 let prog_short = tvec.pop().unwrap();
1564
1565 // copy to target
1566 let copy_result = procsrv::run("",
1567 &self.config.adb_path,
1568 None,
1569 &[
1570 "push".to_owned(),
1571 args.prog.clone(),
1572 self.config.adb_test_dir.clone()
1573 ],
1574 vec!(("".to_owned(), "".to_owned())),
1575 Some("".to_owned()))
1576 .expect(&format!("failed to exec `{}`", self.config.adb_path));
1577
1578 if self.config.verbose {
1579 println!("push ({}) {} {} {}",
1580 self.config.target,
1581 args.prog,
1582 copy_result.out,
1583 copy_result.err);
1584 }
1585
1586 logv(self.config, format!("executing ({}) {}", self.config.target, cmdline));
1587
1588 let mut runargs = Vec::new();
1589
1590 // run test via adb_run_wrapper
1591 runargs.push("shell".to_owned());
1592 for (key, val) in env {
1593 runargs.push(format!("{}={}", key, val));
1594 }
1595 runargs.push(format!("{}/../adb_run_wrapper.sh", self.config.adb_test_dir));
1596 runargs.push(format!("{}", self.config.adb_test_dir));
1597 runargs.push(format!("{}", prog_short));
1598
1599 for tv in &args.args {
1600 runargs.push(tv.to_owned());
1601 }
1602 procsrv::run("",
1603 &self.config.adb_path,
1604 None,
1605 &runargs,
1606 vec!(("".to_owned(), "".to_owned())), Some("".to_owned()))
1607 .expect(&format!("failed to exec `{}`", self.config.adb_path));
1608
1609 // get exitcode of result
1610 runargs = Vec::new();
1611 runargs.push("shell".to_owned());
1612 runargs.push("cat".to_owned());
1613 runargs.push(format!("{}/{}.exitcode", self.config.adb_test_dir, prog_short));
1614
1615 let procsrv::Result{ out: exitcode_out, err: _, status: _ } =
1616 procsrv::run("",
1617 &self.config.adb_path,
1618 None,
1619 &runargs,
1620 vec!(("".to_owned(), "".to_owned())),
1621 Some("".to_owned()))
1622 .expect(&format!("failed to exec `{}`", self.config.adb_path));
1623
1624 let mut exitcode: i32 = 0;
1625 for c in exitcode_out.chars() {
1626 if !c.is_numeric() { break; }
1627 exitcode = exitcode * 10 + match c {
1628 '0' ... '9' => c as i32 - ('0' as i32),
1629 _ => 101,
1630 }
1631 }
1632
1633 // get stdout of result
1634 runargs = Vec::new();
1635 runargs.push("shell".to_owned());
1636 runargs.push("cat".to_owned());
1637 runargs.push(format!("{}/{}.stdout", self.config.adb_test_dir, prog_short));
1638
1639 let procsrv::Result{ out: stdout_out, err: _, status: _ } =
1640 procsrv::run("",
1641 &self.config.adb_path,
1642 None,
1643 &runargs,
1644 vec!(("".to_owned(), "".to_owned())),
1645 Some("".to_owned()))
1646 .expect(&format!("failed to exec `{}`", self.config.adb_path));
1647
1648 // get stderr of result
1649 runargs = Vec::new();
1650 runargs.push("shell".to_owned());
1651 runargs.push("cat".to_owned());
1652 runargs.push(format!("{}/{}.stderr", self.config.adb_test_dir, prog_short));
1653
1654 let procsrv::Result{ out: stderr_out, err: _, status: _ } =
1655 procsrv::run("",
1656 &self.config.adb_path,
1657 None,
1658 &runargs,
1659 vec!(("".to_owned(), "".to_owned())),
1660 Some("".to_owned()))
1661 .expect(&format!("failed to exec `{}`", self.config.adb_path));
1662
1663 self.dump_output(&stdout_out, &stderr_out);
1664
1665 ProcRes {
1666 status: Status::Parsed(exitcode),
1667 stdout: stdout_out,
1668 stderr: stderr_out,
1669 cmdline: cmdline
1670 }
1671 }
1672
1673 fn _arm_push_aux_shared_library(&self) {
1674 let tdir = self.aux_output_dir_name();
1675
1676 let dirs = fs::read_dir(&tdir).unwrap();
1677 for file in dirs {
1678 let file = file.unwrap().path();
1679 if file.extension().and_then(|s| s.to_str()) == Some("so") {
1680 // FIXME (#9639): This needs to handle non-utf8 paths
1681 let copy_result = procsrv::run("",
1682 &self.config.adb_path,
1683 None,
1684 &[
1685 "push".to_owned(),
1686 file.to_str()
1687 .unwrap()
1688 .to_owned(),
1689 self.config.adb_test_dir.to_owned(),
1690 ],
1691 vec!(("".to_owned(),
1692 "".to_owned())),
1693 Some("".to_owned()))
1694 .expect(&format!("failed to exec `{}`", self.config.adb_path));
1695
1696 if self.config.verbose {
1697 println!("push ({}) {:?} {} {}",
1698 self.config.target, file.display(),
1699 copy_result.out, copy_result.err);
1700 }
1701 }
1702 }
1703 }
1704
1705 // codegen tests (using FileCheck)
1706
1707 fn compile_test_and_save_ir(&self) -> ProcRes {
1708 let aux_dir = self.aux_output_dir_name();
1709 // FIXME (#9639): This needs to handle non-utf8 paths
1710 let mut link_args = vec!("-L".to_owned(),
1711 aux_dir.to_str().unwrap().to_owned());
1712 let llvm_args = vec!("--emit=llvm-ir".to_owned(),);
1713 link_args.extend(llvm_args);
1714 let args = self.make_compile_args(link_args,
1715 &self.testpaths.file,
1716 TargetLocation::ThisDirectory(
1717 self.output_base_name().parent()
1718 .unwrap()
1719 .to_path_buf()));
1720 self.compose_and_run_compiler(args, None)
1721 }
1722
1723 fn check_ir_with_filecheck(&self) -> ProcRes {
1724 let irfile = self.output_base_name().with_extension("ll");
1725 let prog = self.config.llvm_filecheck.as_ref().unwrap();
1726 let proc_args = ProcArgs {
1727 // FIXME (#9639): This needs to handle non-utf8 paths
1728 prog: prog.to_str().unwrap().to_owned(),
1729 args: vec!(format!("-input-file={}", irfile.to_str().unwrap()),
1730 self.testpaths.file.to_str().unwrap().to_owned())
1731 };
1732 self.compose_and_run(proc_args, Vec::new(), "", None, None)
1733 }
1734
1735 fn run_codegen_test(&self) {
1736 assert!(self.revision.is_none(), "revisions not relevant here");
1737
1738 if self.config.llvm_filecheck.is_none() {
1739 self.fatal("missing --llvm-filecheck");
1740 }
1741
1742 let mut proc_res = self.compile_test_and_save_ir();
1743 if !proc_res.status.success() {
1744 self.fatal_proc_rec("compilation failed!", &proc_res);
1745 }
1746
1747 proc_res = self.check_ir_with_filecheck();
1748 if !proc_res.status.success() {
1749 self.fatal_proc_rec("verification with 'FileCheck' failed", &proc_res);
1750 }
1751 }
1752
1753 fn charset() -> &'static str {
1754 // FreeBSD 10.1 defaults to GDB 6.1.1 which doesn't support "auto" charset
1755 if cfg!(target_os = "bitrig") {
1756 "auto"
1757 } else if cfg!(target_os = "freebsd") {
1758 "ISO-8859-1"
1759 } else {
1760 "UTF-8"
1761 }
1762 }
1763
1764 fn run_rustdoc_test(&self) {
1765 assert!(self.revision.is_none(), "revisions not relevant here");
1766
1767 let out_dir = self.output_base_name();
1768 let _ = fs::remove_dir_all(&out_dir);
1769 self.create_dir_racy(&out_dir);
1770
1771 let proc_res = self.document(&out_dir);
1772 if !proc_res.status.success() {
1773 self.fatal_proc_rec("rustdoc failed!", &proc_res);
1774 }
1775 let root = self.find_rust_src_root().unwrap();
1776
1777 let res = self.cmd2procres(Command::new(&self.config.docck_python)
1778 .arg(root.join("src/etc/htmldocck.py"))
1779 .arg(out_dir)
1780 .arg(&self.testpaths.file));
1781 if !res.status.success() {
1782 self.fatal_proc_rec("htmldocck failed!", &res);
1783 }
1784 }
1785
1786 fn run_codegen_units_test(&self) {
1787 assert!(self.revision.is_none(), "revisions not relevant here");
1788
1789 let proc_res = self.compile_test();
1790
1791 if !proc_res.status.success() {
1792 self.fatal_proc_rec("compilation failed!", &proc_res);
1793 }
1794
1795 self.check_no_compiler_crash(&proc_res);
1796
1797 const PREFIX: &'static str = "TRANS_ITEM ";
1798 const CGU_MARKER: &'static str = "@@";
1799
1800 let actual: Vec<TransItem> = proc_res
1801 .stdout
1802 .lines()
1803 .filter(|line| line.starts_with(PREFIX))
1804 .map(str_to_trans_item)
1805 .collect();
1806
1807 let expected: Vec<TransItem> = errors::load_errors(&self.testpaths.file, None)
1808 .iter()
1809 .map(|e| str_to_trans_item(&e.msg[..]))
1810 .collect();
1811
1812 let mut missing = Vec::new();
1813 let mut wrong_cgus = Vec::new();
1814
1815 for expected_item in &expected {
1816 let actual_item_with_same_name = actual.iter()
1817 .find(|ti| ti.name == expected_item.name);
1818
1819 if let Some(actual_item) = actual_item_with_same_name {
1820 if !expected_item.codegen_units.is_empty() {
1821 // Also check for codegen units
1822 if expected_item.codegen_units != actual_item.codegen_units {
1823 wrong_cgus.push((expected_item.clone(), actual_item.clone()));
1824 }
1825 }
1826 } else {
1827 missing.push(expected_item.string.clone());
1828 }
1829 }
1830
1831 let unexpected: Vec<_> =
1832 actual.iter()
1833 .filter(|acgu| !expected.iter().any(|ecgu| acgu.name == ecgu.name))
1834 .map(|acgu| acgu.string.clone())
1835 .collect();
1836
1837 if !missing.is_empty() {
1838 missing.sort();
1839
1840 println!("\nThese items should have been contained but were not:\n");
1841
1842 for item in &missing {
1843 println!("{}", item);
1844 }
1845
1846 println!("\n");
1847 }
1848
1849 if !unexpected.is_empty() {
1850 let sorted = {
1851 let mut sorted = unexpected.clone();
1852 sorted.sort();
1853 sorted
1854 };
1855
1856 println!("\nThese items were contained but should not have been:\n");
1857
1858 for item in sorted {
1859 println!("{}", item);
1860 }
1861
1862 println!("\n");
1863 }
1864
1865 if !wrong_cgus.is_empty() {
1866 wrong_cgus.sort_by_key(|pair| pair.0.name.clone());
1867 println!("\nThe following items were assigned to wrong codegen units:\n");
1868
1869 for &(ref expected_item, ref actual_item) in &wrong_cgus {
1870 println!("{}", expected_item.name);
1871 println!(" expected: {}", codegen_units_to_str(&expected_item.codegen_units));
1872 println!(" actual: {}", codegen_units_to_str(&actual_item.codegen_units));
1873 println!("");
1874 }
1875 }
1876
1877 if !(missing.is_empty() && unexpected.is_empty() && wrong_cgus.is_empty())
1878 {
1879 panic!();
1880 }
1881
1882 #[derive(Clone, Eq, PartialEq)]
1883 struct TransItem {
1884 name: String,
1885 codegen_units: HashSet<String>,
1886 string: String,
1887 }
1888
1889 // [TRANS_ITEM] name [@@ (cgu)+]
1890 fn str_to_trans_item(s: &str) -> TransItem {
1891 let s = if s.starts_with(PREFIX) {
1892 (&s[PREFIX.len()..]).trim()
1893 } else {
1894 s.trim()
1895 };
1896
1897 let full_string = format!("{}{}", PREFIX, s.trim().to_owned());
1898
1899 let parts: Vec<&str> = s.split(CGU_MARKER)
1900 .map(str::trim)
1901 .filter(|s| !s.is_empty())
1902 .collect();
1903
1904 let name = parts[0].trim();
1905
1906 let cgus = if parts.len() > 1 {
1907 let cgus_str = parts[1];
1908
1909 cgus_str.split(" ")
1910 .map(str::trim)
1911 .filter(|s| !s.is_empty())
1912 .map(str::to_owned)
1913 .collect()
1914 }
1915 else {
1916 HashSet::new()
1917 };
1918
1919 TransItem {
1920 name: name.to_owned(),
1921 codegen_units: cgus,
1922 string: full_string,
1923 }
1924 }
1925
1926 fn codegen_units_to_str(cgus: &HashSet<String>) -> String
1927 {
1928 let mut cgus: Vec<_> = cgus.iter().collect();
1929 cgus.sort();
1930
1931 let mut string = String::new();
1932 for cgu in cgus {
1933 string.push_str(&cgu[..]);
1934 string.push_str(" ");
1935 }
1936
1937 string
1938 }
1939 }
1940
1941 fn init_incremental_test(&self) {
1942 // (See `run_incremental_test` for an overview of how incremental tests work.)
1943
1944 // Before any of the revisions have executed, create the
1945 // incremental workproduct directory. Delete any old
1946 // incremental work products that may be there from prior
1947 // runs.
1948 let incremental_dir = self.incremental_dir();
1949 if incremental_dir.exists() {
1950 fs::remove_dir_all(&incremental_dir).unwrap();
1951 }
1952 fs::create_dir_all(&incremental_dir).unwrap();
1953
1954 if self.config.verbose {
1955 print!("init_incremental_test: incremental_dir={}", incremental_dir.display());
1956 }
1957 }
1958
1959 fn run_incremental_test(&self) {
1960 // Basic plan for a test incremental/foo/bar.rs:
1961 // - load list of revisions rpass1, cfail2, rpass3
1962 // - each should begin with `rpass`, `cfail`, or `cfail`
1963 // - if `rpass`, expect compile and execution to succeed
1964 // - if `cfail`, expect compilation to fail
1965 // - if `rfail`, expect execution to fail
1966 // - create a directory build/foo/bar.incremental
1967 // - compile foo/bar.rs with -Z incremental=.../foo/bar.incremental and -C rpass1
1968 // - because name of revision starts with "rpass", expect success
1969 // - compile foo/bar.rs with -Z incremental=.../foo/bar.incremental and -C cfail2
1970 // - because name of revision starts with "cfail", expect an error
1971 // - load expected errors as usual, but filter for those that end in `[rfail2]`
1972 // - compile foo/bar.rs with -Z incremental=.../foo/bar.incremental and -C rpass3
1973 // - because name of revision starts with "rpass", expect success
1974 // - execute build/foo/bar.exe and save output
1975 //
1976 // FIXME -- use non-incremental mode as an oracle? That doesn't apply
1977 // to #[rustc_dirty] and clean tests I guess
1978
1979 let revision = self.revision.expect("incremental tests require a list of revisions");
1980
1981 // Incremental workproduct directory should have already been created.
1982 let incremental_dir = self.incremental_dir();
1983 assert!(incremental_dir.exists(), "init_incremental_test failed to create incremental dir");
1984
1985 // Add an extra flag pointing at the incremental directory.
1986 let mut revision_props = self.props.clone();
1987 revision_props.incremental_dir = Some(incremental_dir);
1988
1989 let revision_cx = TestCx {
1990 config: self.config,
1991 props: &revision_props,
1992 testpaths: self.testpaths,
1993 revision: self.revision,
1994 };
1995
1996 if self.config.verbose {
1997 print!("revision={:?} revision_props={:#?}", revision, revision_props);
1998 }
1999
2000 if revision.starts_with("rpass") {
2001 revision_cx.run_rpass_test();
2002 } else if revision.starts_with("rfail") {
2003 revision_cx.run_rfail_test();
2004 } else if revision.starts_with("cfail") {
2005 revision_cx.run_cfail_test();
2006 } else {
2007 revision_cx.fatal(
2008 "revision name must begin with rpass, rfail, or cfail");
2009 }
2010 }
2011
2012 /// Directory where incremental work products are stored.
2013 fn incremental_dir(&self) -> PathBuf {
2014 self.output_base_name().with_extension("incremental")
2015 }
2016
2017 fn run_rmake_test(&self) {
2018 // FIXME(#11094): we should fix these tests
2019 if self.config.host != self.config.target {
2020 return
2021 }
2022
2023 let cwd = env::current_dir().unwrap();
2024 let src_root = self.config.src_base.parent().unwrap()
2025 .parent().unwrap()
2026 .parent().unwrap();
2027 let src_root = cwd.join(&src_root);
2028
2029 let tmpdir = cwd.join(self.output_base_name());
2030 if tmpdir.exists() {
2031 self.aggressive_rm_rf(&tmpdir).unwrap();
2032 }
2033 self.create_dir_racy(&tmpdir);
2034
2035 let mut cmd = Command::new("make");
2036 cmd.current_dir(&self.testpaths.file)
2037 .env("TARGET", &self.config.target)
2038 .env("PYTHON", &self.config.docck_python)
2039 .env("S", src_root)
2040 .env("RUST_BUILD_STAGE", &self.config.stage_id)
2041 .env("RUSTC", cwd.join(&self.config.rustc_path))
2042 .env("RUSTDOC", cwd.join(&self.config.rustdoc_path))
2043 .env("TMPDIR", &tmpdir)
2044 .env("LD_LIB_PATH_ENVVAR", procsrv::dylib_env_var())
2045 .env("HOST_RPATH_DIR", cwd.join(&self.config.compile_lib_path))
2046 .env("TARGET_RPATH_DIR", cwd.join(&self.config.run_lib_path))
2047 .env("LLVM_COMPONENTS", &self.config.llvm_components)
2048 .env("LLVM_CXXFLAGS", &self.config.llvm_cxxflags);
2049
2050 if self.config.target.contains("msvc") {
2051 // We need to pass a path to `lib.exe`, so assume that `cc` is `cl.exe`
2052 // and that `lib.exe` lives next to it.
2053 let lib = Path::new(&self.config.cc).parent().unwrap().join("lib.exe");
2054
2055 // MSYS doesn't like passing flags of the form `/foo` as it thinks it's
2056 // a path and instead passes `C:\msys64\foo`, so convert all
2057 // `/`-arguments to MSVC here to `-` arguments.
2058 let cflags = self.config.cflags.split(' ').map(|s| s.replace("/", "-"))
2059 .collect::<Vec<_>>().join(" ");
2060
2061 cmd.env("IS_MSVC", "1")
2062 .env("MSVC_LIB", format!("'{}' -nologo", lib.display()))
2063 .env("CC", format!("'{}' {}", self.config.cc, cflags))
2064 .env("CXX", &self.config.cxx);
2065 } else {
2066 cmd.env("CC", format!("{} {}", self.config.cc, self.config.cflags))
2067 .env("CXX", format!("{} {}", self.config.cxx, self.config.cflags));
2068 }
2069
2070 let output = cmd.output().expect("failed to spawn `make`");
2071 if !output.status.success() {
2072 let res = ProcRes {
2073 status: Status::Normal(output.status),
2074 stdout: String::from_utf8_lossy(&output.stdout).into_owned(),
2075 stderr: String::from_utf8_lossy(&output.stderr).into_owned(),
2076 cmdline: format!("{:?}", cmd),
2077 };
2078 self.fatal_proc_rec("make failed", &res);
2079 }
2080 }
2081
2082 fn aggressive_rm_rf(&self, path: &Path) -> io::Result<()> {
2083 for e in try!(path.read_dir()) {
2084 let entry = try!(e);
2085 let path = entry.path();
2086 if try!(entry.file_type()).is_dir() {
2087 try!(self.aggressive_rm_rf(&path));
2088 } else {
2089 // Remove readonly files as well on windows (by default we can't)
2090 try!(fs::remove_file(&path).or_else(|e| {
2091 if cfg!(windows) && e.kind() == io::ErrorKind::PermissionDenied {
2092 let mut meta = try!(entry.metadata()).permissions();
2093 meta.set_readonly(false);
2094 try!(fs::set_permissions(&path, meta));
2095 fs::remove_file(&path)
2096 } else {
2097 Err(e)
2098 }
2099 }))
2100 }
2101 }
2102 fs::remove_dir(path)
2103 }
2104
2105 fn run_ui_test(&self) {
2106 println!("ui: {}", self.testpaths.file.display());
2107
2108 let proc_res = self.compile_test();
2109
2110 let expected_stderr_path = self.expected_output_path("stderr");
2111 let expected_stderr = self.load_expected_output(&expected_stderr_path);
2112
2113 let expected_stdout_path = self.expected_output_path("stdout");
2114 let expected_stdout = self.load_expected_output(&expected_stdout_path);
2115
2116 let normalized_stdout = self.normalize_output(&proc_res.stdout);
2117 let normalized_stderr = self.normalize_output(&proc_res.stderr);
2118
2119 let mut errors = 0;
2120 errors += self.compare_output("stdout", &normalized_stdout, &expected_stdout);
2121 errors += self.compare_output("stderr", &normalized_stderr, &expected_stderr);
2122
2123 if errors > 0 {
2124 println!("To update references, run this command from build directory:");
2125 let relative_path_to_file =
2126 self.testpaths.relative_dir
2127 .join(self.testpaths.file.file_name().unwrap());
2128 println!("{}/update-references.sh '{}' '{}'",
2129 self.config.src_base.display(),
2130 self.config.build_base.display(),
2131 relative_path_to_file.display());
2132 self.fatal_proc_rec(&format!("{} errors occurred comparing output.", errors),
2133 &proc_res);
2134 }
2135 }
2136
2137 fn normalize_output(&self, output: &str) -> String {
2138 let parent_dir = self.testpaths.file.parent().unwrap();
2139 let parent_dir_str = parent_dir.display().to_string();
2140 output.replace(&parent_dir_str, "$DIR")
2141 .replace("\\", "/") // normalize for paths on windows
2142 .replace("\r\n", "\n") // normalize for linebreaks on windows
2143 .replace("\t", "\\t") // makes tabs visible
2144 }
2145
2146 fn expected_output_path(&self, kind: &str) -> PathBuf {
2147 let extension = match self.revision {
2148 Some(r) => format!("{}.{}", r, kind),
2149 None => kind.to_string(),
2150 };
2151 self.testpaths.file.with_extension(extension)
2152 }
2153
2154 fn load_expected_output(&self, path: &Path) -> String {
2155 if !path.exists() {
2156 return String::new();
2157 }
2158
2159 let mut result = String::new();
2160 match File::open(path).and_then(|mut f| f.read_to_string(&mut result)) {
2161 Ok(_) => result,
2162 Err(e) => {
2163 self.fatal(&format!("failed to load expected output from `{}`: {}",
2164 path.display(), e))
2165 }
2166 }
2167 }
2168
2169 fn compare_output(&self, kind: &str, actual: &str, expected: &str) -> usize {
2170 if actual == expected {
2171 return 0;
2172 }
2173
2174 println!("normalized {}:\n{}\n", kind, actual);
2175 println!("expected {}:\n{}\n", kind, expected);
2176 println!("diff of {}:\n", kind);
2177 for line in uidiff::diff_lines(actual, expected) {
2178 println!("{}", line);
2179 }
2180
2181 let output_file = self.output_base_name().with_extension(kind);
2182 match File::create(&output_file).and_then(|mut f| f.write_all(actual.as_bytes())) {
2183 Ok(()) => { }
2184 Err(e) => {
2185 self.fatal(&format!("failed to write {} to `{}`: {}",
2186 kind, output_file.display(), e))
2187 }
2188 }
2189
2190 println!("\nThe actual {0} differed from the expected {0}.", kind);
2191 println!("Actual {} saved to {}", kind, output_file.display());
2192 1
2193 }
2194}
2195
2196struct ProcArgs {
2197 prog: String,
2198 args: Vec<String>,
2199}
2200
2201pub struct ProcRes {
2202 status: Status,
2203 stdout: String,
2204 stderr: String,
2205 cmdline: String,
2206}
2207
2208enum Status {
2209 Parsed(i32),
2210 Normal(ExitStatus),
2211}
2212
2213impl ProcRes {
2214 pub fn fatal(&self, err: Option<&str>) -> ! {
2215 if let Some(e) = err {
2216 println!("\nerror: {}", e);
2217 }
2218 print!("\
2219 status: {}\n\
2220 command: {}\n\
2221 stdout:\n\
2222 ------------------------------------------\n\
2223 {}\n\
2224 ------------------------------------------\n\
2225 stderr:\n\
2226 ------------------------------------------\n\
2227 {}\n\
2228 ------------------------------------------\n\
2229 \n",
2230 self.status, self.cmdline, self.stdout,
2231 self.stderr);
2232 panic!();
2233 }
2234}
2235
2236impl Status {
2237 fn code(&self) -> Option<i32> {
2238 match *self {
2239 Status::Parsed(i) => Some(i),
2240 Status::Normal(ref e) => e.code(),
2241 }
2242 }
2243
2244 fn success(&self) -> bool {
2245 match *self {
2246 Status::Parsed(i) => i == 0,
2247 Status::Normal(ref e) => e.success(),
2248 }
2249 }
2250}
2251
2252impl fmt::Display for Status {
2253 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2254 match *self {
2255 Status::Parsed(i) => write!(f, "exit code: {}", i),
2256 Status::Normal(ref e) => e.fmt(f),
2257 }
2258 }
2259}
2260
2261enum TargetLocation {
2262 ThisFile(PathBuf),
2263 ThisDirectory(PathBuf),
2264}
2265