]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/backtrace.rs
Replace all ~"" with "".to_owned()
[rust.git] / src / test / run-pass / backtrace.rs
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
11 // ignore-win32 FIXME #13259
12 #![no_uv]
13
14 extern crate native;
15
16 use std::os;
17 use std::io::process::{Process, ProcessConfig};
18 use std::unstable::finally::Finally;
19 use std::str;
20
21 #[start]
22 fn start(argc: int, argv: **u8) -> int { native::start(argc, argv, main) }
23
24 #[inline(never)]
25 fn foo() {
26     fail!()
27 }
28
29 #[inline(never)]
30 fn double() {
31     (|| {
32         fail!("once");
33     }).finally(|| {
34         fail!("twice");
35     })
36 }
37
38 fn runtest(me: &str) {
39     let mut env = os::env().move_iter().collect::<Vec<(~str, ~str)>>();
40     match env.iter().position(|&(ref s, _)| "RUST_BACKTRACE" == *s) {
41         Some(i) => { env.remove(i); }
42         None => {}
43     }
44     env.push(("RUST_BACKTRACE".to_owned(), "1".to_owned()));
45
46     // Make sure that the stack trace is printed
47     let mut p = Process::configure(ProcessConfig {
48         program: me,
49         args: ["fail".to_owned()],
50         env: Some(env.as_slice()),
51         .. ProcessConfig::new()
52     }).unwrap();
53     let out = p.wait_with_output();
54     assert!(!out.status.success());
55     let s = str::from_utf8(out.error.as_slice()).unwrap();
56     assert!(s.contains("stack backtrace") && s.contains("foo::h"),
57             "bad output: {}", s);
58
59     // Make sure the stack trace is *not* printed
60     let mut p = Process::configure(ProcessConfig {
61         program: me,
62         args: ["fail".to_owned()],
63         .. ProcessConfig::new()
64     }).unwrap();
65     let out = p.wait_with_output();
66     assert!(!out.status.success());
67     let s = str::from_utf8(out.error.as_slice()).unwrap();
68     assert!(!s.contains("stack backtrace") && !s.contains("foo::h"),
69             "bad output2: {}", s);
70
71     // Make sure a stack trace is printed
72     let mut p = Process::configure(ProcessConfig {
73         program: me,
74         args: ["double-fail".to_owned()],
75         .. ProcessConfig::new()
76     }).unwrap();
77     let out = p.wait_with_output();
78     assert!(!out.status.success());
79     let s = str::from_utf8(out.error.as_slice()).unwrap();
80     assert!(s.contains("stack backtrace") && s.contains("double::h"),
81             "bad output3: {}", s);
82
83     // Make sure a stack trace isn't printed too many times
84     let mut p = Process::configure(ProcessConfig {
85         program: me,
86         args: ["double-fail".to_owned()],
87         env: Some(env.as_slice()),
88         .. ProcessConfig::new()
89     }).unwrap();
90     let out = p.wait_with_output();
91     assert!(!out.status.success());
92     let s = str::from_utf8(out.error.as_slice()).unwrap();
93     let mut i = 0;
94     for _ in range(0, 2) {
95         i += s.slice_from(i + 10).find_str("stack backtrace").unwrap() + 10;
96     }
97     assert!(s.slice_from(i + 10).find_str("stack backtrace").is_none(),
98             "bad output4: {}", s);
99 }
100
101 fn main() {
102     let args = os::args();
103     if args.len() >= 2 && args[1].as_slice() == "fail" {
104         foo();
105     } else if args.len() >= 2 && args[1].as_slice() == "double-fail" {
106         double();
107     } else {
108         runtest(args[0]);
109     }
110 }