]> git.proxmox.com Git - rustc.git/blame - src/test/run-pass/backtrace.rs
New upstream version 1.14.0+dfsg1
[rustc.git] / src / test / run-pass / backtrace.rs
CommitLineData
1a4d82fc
JJ
1// Copyright 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
d9579d0f 11// ignore-android FIXME #17520
5bcae85e 12// ignore-emscripten spawning processes is not supported
7453a54e 13// compile-flags:-g
1a4d82fc 14
85aaf69f 15use std::env;
9346a6ac 16use std::process::{Command, Stdio};
1a4d82fc
JJ
17use std::str;
18
19#[inline(never)]
20fn foo() {
85aaf69f
SL
21 let _v = vec![1, 2, 3];
22 if env::var_os("IS_TEST").is_some() {
1a4d82fc
JJ
23 panic!()
24 }
25}
26
27#[inline(never)]
28fn double() {
85aaf69f
SL
29 struct Double;
30
31 impl Drop for Double {
32 fn drop(&mut self) { panic!("twice") }
33 }
34
35 let _d = Double;
36
37 panic!("once");
1a4d82fc
JJ
38}
39
9346a6ac
AL
40fn template(me: &str) -> Command {
41 let mut m = Command::new(me);
42 m.env("IS_TEST", "1")
43 .stdout(Stdio::piped())
44 .stderr(Stdio::piped());
45 return m;
46}
1a4d82fc 47
54a0048b 48fn expected(fn_name: &str) -> String {
5bcae85e 49 format!(" - backtrace::{}", fn_name)
54a0048b
SL
50}
51
9346a6ac 52fn runtest(me: &str) {
1a4d82fc 53 // Make sure that the stack trace is printed
9346a6ac 54 let p = template(me).arg("fail").env("RUST_BACKTRACE", "1").spawn().unwrap();
1a4d82fc
JJ
55 let out = p.wait_with_output().unwrap();
56 assert!(!out.status.success());
9346a6ac 57 let s = str::from_utf8(&out.stderr).unwrap();
54a0048b 58 assert!(s.contains("stack backtrace") && s.contains(&expected("foo")),
1a4d82fc
JJ
59 "bad output: {}", s);
60
61 // Make sure the stack trace is *not* printed
c34b1796
AL
62 // (Remove RUST_BACKTRACE from our own environment, in case developer
63 // is running `make check` with it on.)
9346a6ac 64 let p = template(me).arg("fail").env_remove("RUST_BACKTRACE").spawn().unwrap();
1a4d82fc
JJ
65 let out = p.wait_with_output().unwrap();
66 assert!(!out.status.success());
9346a6ac 67 let s = str::from_utf8(&out.stderr).unwrap();
54a0048b 68 assert!(!s.contains("stack backtrace") && !s.contains(&expected("foo")),
1a4d82fc
JJ
69 "bad output2: {}", s);
70
54a0048b
SL
71 // Make sure the stack trace is *not* printed
72 // (RUST_BACKTRACE=0 acts as if it were unset from our own environment,
73 // in case developer is running `make check` with it set.)
74 let p = template(me).arg("fail").env("RUST_BACKTRACE","0").spawn().unwrap();
75 let out = p.wait_with_output().unwrap();
76 assert!(!out.status.success());
77 let s = str::from_utf8(&out.stderr).unwrap();
78 assert!(!s.contains("stack backtrace") && !s.contains(" - foo"),
79 "bad output3: {}", s);
80
1a4d82fc 81 // Make sure a stack trace is printed
9346a6ac 82 let p = template(me).arg("double-fail").spawn().unwrap();
1a4d82fc
JJ
83 let out = p.wait_with_output().unwrap();
84 assert!(!out.status.success());
9346a6ac 85 let s = str::from_utf8(&out.stderr).unwrap();
1a4d82fc
JJ
86 // loosened the following from double::h to double:: due to
87 // spurious failures on mac, 32bit, optimized
54a0048b 88 assert!(s.contains("stack backtrace") && s.contains(&expected("double")),
1a4d82fc
JJ
89 "bad output3: {}", s);
90
91 // Make sure a stack trace isn't printed too many times
9346a6ac 92 let p = template(me).arg("double-fail")
1a4d82fc
JJ
93 .env("RUST_BACKTRACE", "1").spawn().unwrap();
94 let out = p.wait_with_output().unwrap();
95 assert!(!out.status.success());
9346a6ac 96 let s = str::from_utf8(&out.stderr).unwrap();
1a4d82fc 97 let mut i = 0;
85aaf69f 98 for _ in 0..2 {
c34b1796 99 i += s[i + 10..].find("stack backtrace").unwrap() + 10;
1a4d82fc 100 }
c34b1796 101 assert!(s[i + 10..].find("stack backtrace").is_none(),
1a4d82fc
JJ
102 "bad output4: {}", s);
103}
104
105fn main() {
54a0048b 106 if cfg!(windows) && cfg!(target_env = "gnu") {
7453a54e
SL
107 return
108 }
109
85aaf69f
SL
110 let args: Vec<String> = env::args().collect();
111 if args.len() >= 2 && args[1] == "fail" {
1a4d82fc 112 foo();
85aaf69f 113 } else if args.len() >= 2 && args[1] == "double-fail" {
1a4d82fc
JJ
114 double();
115 } else {
85aaf69f 116 runtest(&args[0]);
1a4d82fc
JJ
117 }
118}