-
Notifications
You must be signed in to change notification settings - Fork 13
feat!: Set up human_panic in hugr-cli
#2511
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
//! Integration test for `human-panic`. | ||
//! | ||
//! Builds the release binary with the `panic-test` feature, runs it with a | ||
//! test-only panic trigger and an isolated temp dir, asserts the banner, | ||
//! and verifies a TOML report is written. Temp dir is removed at the end. | ||
|
||
//! Black-box integration test for `human-panic`. | ||
|
||
use predicates::str::contains; // for cargo_bin() | ||
use std::process::Command; | ||
use tempfile::TempDir; | ||
|
||
#[test] | ||
fn human_panic_writes_report() { | ||
// Isolated temp dir for the crash report. | ||
let tmp = TempDir::new().expect("create tempdir"); | ||
let tmp_path = tmp.path(); | ||
|
||
// Run the release CLI binary from the workspace root. | ||
// No features needed: main() installs human-panic in release builds. | ||
let mut cmd = Command::new("cargo"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've not used
Did you try these, is there some reason why they don't work here? If we can't use those, then (if I understand correctly) you can just create a assert_cmd::Command here and call |
||
cmd.args([ | ||
"run", | ||
"--release", | ||
"-p", | ||
"hugr-cli", | ||
"--bin", | ||
"hugr", | ||
"--", // end cargo args; program args would follow | ||
]); | ||
|
||
// Isolate temp location & trigger the test panic in the child process. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm of two minds about these environment variables. I don't see them documented anywhere on the On the plus side, it seems good for the test to clean up neatly after itself 😃. But an alternative would be - human-panic outputs the exact temporary-file path in it's So yeah, could go either way here, did you consider other approaches? |
||
if cfg!(windows) { | ||
cmd.env("TEMP", tmp_path).env("TMP", tmp_path); | ||
} else { | ||
cmd.env("TMPDIR", tmp_path); | ||
} | ||
cmd.env("PANIC_FOR_TESTS", "1"); | ||
|
||
// Expect non-zero exit and the banner on stderr (release only). | ||
assert_cmd::Command::from_std(cmd) | ||
.assert() | ||
.failure() | ||
.stderr(contains("Well, this is embarrassing.")); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: It'd be good to look for the string in the |
||
|
||
// Confirm a .toml report exists in our temp dir. | ||
let made_report = std::fs::read_dir(tmp_path) | ||
.unwrap() | ||
.filter_map(Result::ok) | ||
.any(|e| { | ||
e.path() | ||
.extension() | ||
.and_then(|ext| ext.to_str()) | ||
.is_some_and(|ext| ext.eq_ignore_ascii_case("toml")) | ||
}); | ||
assert!( | ||
made_report, | ||
"expected a human-panic report in {:?}", | ||
tmp_path | ||
); | ||
|
||
// Explicit cleanup; surface any removal errors. | ||
tmp.close().expect("failed to remove temp dir"); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The human-panic docs state that "It only displays a human-friendly panic message in release mode:" so while you have written some mildly complex logic here I wonder do we really need this?