From 316e1016ac79d77e187050233d6a7974979068a6 Mon Sep 17 00:00:00 2001 From: Katharina Fey Date: Thu, 4 Oct 2018 19:02:32 +0200 Subject: [PATCH 1/2] Implementing #48 --- src/lib.rs | 56 ++++++++++++++++--------- tests/custom-panic/tests/integration.rs | 14 ++++++- tests/single-panic/tests/integration.rs | 14 ++++++- 3 files changed, 60 insertions(+), 24 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index bc70e69..eb05dfd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -94,35 +94,51 @@ 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}; use $crate::{handle_dump, print_msg, Metadata}; - 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}; use $crate::{handle_dump, print_msg, Metadata}; - 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 diff --git a/tests/custom-panic/tests/integration.rs b/tests/custom-panic/tests/integration.rs index 7fc8e2b..68a7488 100644 --- a/tests/custom-panic/tests/integration.rs +++ b/tests/custom-panic/tests/integration.rs @@ -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() @@ -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(); +} + diff --git a/tests/single-panic/tests/integration.rs b/tests/single-panic/tests/integration.rs index b242e22..cd76045 100644 --- a/tests/single-panic/tests/integration.rs +++ b/tests/single-panic/tests/integration.rs @@ -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() @@ -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(); +} + From b42811884e98eb142c54709a27ba1ebaaf6b42d7 Mon Sep 17 00:00:00 2001 From: Katharina Fey Date: Thu, 4 Oct 2018 19:28:57 +0200 Subject: [PATCH 2/2] Running rustfmt on the new setup --- src/lib.rs | 16 +++++++--------- tests/custom-panic/tests/integration.rs | 1 - tests/single-panic/tests/integration.rs | 1 - 3 files changed, 7 insertions(+), 11 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index eb05dfd..32c996c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -94,10 +94,9 @@ 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}; + #[allow(unused_imports)] use $crate::{handle_dump, print_msg, Metadata}; #[cfg(not(debug_assertions))] @@ -108,16 +107,15 @@ macro_rules! setup_panic { print_msg(file_path, &$meta) .expect("human-panic: printing error message to console failed"); })); - }, - Ok(_) => {}, + } + Ok(_) => {} } }; () => { - #[allow(unused_imports)] - use $crate::{handle_dump, print_msg, Metadata}; #[allow(unused_imports)] use std::panic::{self, PanicInfo}; + #[allow(unused_imports)] use $crate::{handle_dump, print_msg, Metadata}; #[cfg(not(debug_assertions))] @@ -135,10 +133,10 @@ macro_rules! setup_panic { print_msg(file_path, &meta) .expect("human-panic: printing error message to console failed"); })); - }, - Ok(_) => {}, + } + Ok(_) => {} } - } + }; } /// Utility function that prints a message to our human users diff --git a/tests/custom-panic/tests/integration.rs b/tests/custom-panic/tests/integration.rs index 68a7488..199fc09 100644 --- a/tests/custom-panic/tests/integration.rs +++ b/tests/custom-panic/tests/integration.rs @@ -23,4 +23,3 @@ fn debug() { .fails_with(101) .unwrap(); } - diff --git a/tests/single-panic/tests/integration.rs b/tests/single-panic/tests/integration.rs index cd76045..e2b629d 100644 --- a/tests/single-panic/tests/integration.rs +++ b/tests/single-panic/tests/integration.rs @@ -21,4 +21,3 @@ fn debug() { .fails_with(101) .unwrap(); } -