Skip to content

Commit

Permalink
Implementing #48
Browse files Browse the repository at this point in the history
  • Loading branch information
spacekookie committed Oct 4, 2018
1 parent e5cabc2 commit e82b6e8
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 24 deletions.
54 changes: 34 additions & 20 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,35 +94,49 @@ pub struct Metadata {
#[macro_export]
macro_rules! setup_panic {
($meta:expr) => {
#[allow(unused_imports)]
use $crate::{handle_dump, print_msg, Metadata};
#[allow(unused_imports)]
use std::panic::{self, PanicInfo};

panic::set_hook(Box::new(move |info: &PanicInfo| {
let file_path = handle_dump(&$meta, info);

print_msg(file_path, &$meta)
.expect("human-panic: printing error message to console failed");
}));
#[cfg(not(debug_assertions))]
match ::std::env::var("RUST_BACKTRACE") {
Err(_) => {
panic::set_hook(Box::new(move |info: &PanicInfo| {
let file_path = handle_dump(&$meta, info);
print_msg(file_path, &$meta)
.expect("human-panic: printing error message to console failed");
}));
},
Ok(_) => {},
}
};

() => {
#[allow(unused_imports)]
use $crate::{handle_dump, print_msg, Metadata};
#[allow(unused_imports)]
use std::panic::{self, PanicInfo};

let meta = Metadata {
version: env!("CARGO_PKG_VERSION").into(),
name: env!("CARGO_PKG_NAME").into(),
authors: env!("CARGO_PKG_AUTHORS").replace(":", ", ").into(),
homepage: env!("CARGO_PKG_HOMEPAGE").into(),
};

panic::set_hook(Box::new(move |info: &PanicInfo| {
let file_path = handle_dump(&meta, info);

print_msg(file_path, &meta)
.expect("human-panic: printing error message to console failed");
}));
};
#[cfg(not(debug_assertions))]
match ::std::env::var("RUST_BACKTRACE") {
Err(_) => {
let meta = Metadata {
version: env!("CARGO_PKG_VERSION").into(),
name: env!("CARGO_PKG_NAME").into(),
authors: env!("CARGO_PKG_AUTHORS").replace(":", ", ").into(),
homepage: env!("CARGO_PKG_HOMEPAGE").into(),
};

panic::set_hook(Box::new(move |info: &PanicInfo| {
let file_path = handle_dump(&meta, info);
print_msg(file_path, &meta)
.expect("human-panic: printing error message to console failed");
}));
},
Ok(_) => {},
}
}
}

/// Utility function that prints a message to our human users
Expand Down
14 changes: 12 additions & 2 deletions tests/custom-panic/tests/integration.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
extern crate assert_cli;

#[test]
fn integration() {
assert_cli::Assert::main_binary()
fn release() {
assert_cli::Assert::command(&["cargo", "run", "--release"])
.stderr()
.contains("custom-panic-test")
.stderr()
Expand All @@ -14,3 +14,13 @@ fn integration() {
.fails_with(101)
.unwrap();
}

#[test]
fn debug() {
assert_cli::Assert::command(&["cargo", "run"])
.stderr()
.contains("OMG EVERYTHING IS ON FIRE!!!")
.fails_with(101)
.unwrap();
}

14 changes: 12 additions & 2 deletions tests/single-panic/tests/integration.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
extern crate assert_cli;

#[test]
fn integration() {
assert_cli::Assert::main_binary()
fn release() {
assert_cli::Assert::command(&["cargo", "run", "--release"])
.stderr()
.contains("single-panic-test")
.stderr()
Expand All @@ -12,3 +12,13 @@ fn integration() {
.fails_with(101)
.unwrap();
}

#[test]
fn debug() {
assert_cli::Assert::command(&["cargo", "run"])
.stderr()
.contains("OMG EVERYTHING IS ON FIRE!!!")
.fails_with(101)
.unwrap();
}

0 comments on commit e82b6e8

Please sign in to comment.