1 // Copyright (c) 2018 Toby Smith <toby@tismith.id.au>
2 //
3 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4 // http://www.apache.org/license/LICENSE-2.0> or the MIT license
5 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6 // option. This file may not be copied, modified, or distributed
7 // except according to those terms.
8 
9 extern crate assert_cmd;
10 extern crate predicates;
11 use assert_cmd::prelude::*;
12 use predicates::prelude::*;
13 
get_cwd() -> String14 fn get_cwd() -> String {
15     std::env::current_dir()
16         .unwrap()
17         .to_str()
18         .unwrap()
19         .to_string()
20 }
21 
22 macro_rules! test_body {
23     ($name:expr, $matcher:expr) => {
24         let bin = format!("{}/target/debug/examples/{}", get_cwd(), $name);
25         let pred = predicates::str::contains($matcher).from_utf8();
26         std::process::Command::new(&bin)
27             .assert()
28             .failure()
29             .stderr(pred);
30     };
31     ($name:expr) => {
32         test_body!(
33             $name,
34             "Error: this is some context\nInfo: caused by root cause failure"
35         )
36     };
37 }
38 
39 #[test]
test_example()40 fn test_example() {
41     test_body!("example");
42 }
43 
44 #[test]
test_context()45 fn test_context() {
46     test_body!("context");
47 }
48 
49 #[test]
test_display()50 fn test_display() {
51     test_body!("display", "Error: this is an error message");
52 }
53 
54 #[test]
test_no_backtrace()55 fn test_no_backtrace() {
56     let bin = format!("{}/target/debug/examples/example", get_cwd());
57     let pred = predicates::str::contains("stack backtrace").from_utf8();
58     std::process::Command::new(&bin)
59         .env_remove("RUST_BACKTRACE")
60         .assert()
61         .failure()
62         .stderr(pred.not());
63 }
64 
65 #[test]
test_backtrace()66 fn test_backtrace() {
67     let bin = format!("{}/target/debug/examples/example", get_cwd());
68     let pred = predicates::str::contains("stack backtrace").from_utf8();
69     std::process::Command::new(&bin)
70         .env("RUST_BACKTRACE", "1")
71         .assert()
72         .failure()
73         .stderr(pred);
74 }
75