diff --git a/crates/tsc-platform/src/crash.rs b/crates/tsc-platform/src/crash.rs index d8e5bf5..2a70a27 100644 --- a/crates/tsc-platform/src/crash.rs +++ b/crates/tsc-platform/src/crash.rs @@ -101,6 +101,15 @@ pub fn install(dir: impl Into, enabled: bool) { }); } +/// Panic if the `TSC_TEST_CRASH` environment variable is set, so testers can +/// check crash reports with a release build (docs/testing/manual-matrix.md). +/// Call right after [`install`]. +pub fn test_crash_if_requested() { + if std::env::var_os("TSC_TEST_CRASH").is_some_and(|v| !v.is_empty()) { + panic!("test crash requested with TSC_TEST_CRASH"); + } +} + /// Turn recording on or off (the settings switch). No effect before /// [`install`]. pub fn set_enabled(enabled: bool) { diff --git a/crates/tsc-platform/tests/crash.rs b/crates/tsc-platform/tests/crash.rs index ffa11fc..65d5e61 100644 --- a/crates/tsc-platform/tests/crash.rs +++ b/crates/tsc-platform/tests/crash.rs @@ -1,5 +1,5 @@ -//! The crash report panic hook, with real panics in threads. Its own test -//! binary: the hook is process-wide. +//! The crash report panic hook, with real panics in threads and in a child +//! process. Its own test binary: the hook is process-wide. use std::path::PathBuf; use std::thread; @@ -19,7 +19,8 @@ fn panic_in_thread(name: &str, message: String) { assert!(result.is_err(), "the thread should have panicked"); } -// One test: the hook and its settings are global to the process. +// The only test that installs the hook in this process: the hook and its +// settings are global. The child process test below only spawns. #[test] fn panic_hook_writes_reports() { let dir = temp_dir("hook"); @@ -78,3 +79,35 @@ fn panic_hook_writes_reports() { crash::set_enabled(false); std::fs::remove_dir_all(&dir).unwrap(); } + +/// Runs only as the child of `test_crash_in_child_process`: a start with +/// `TSC_TEST_CRASH` set, as a tester would do with the app. +#[test] +fn child_process_crash() { + let Some(dir) = std::env::var_os("TSC_CRASH_CHILD_DIR") else { return }; + crash::set_app_version("child"); + crash::install(PathBuf::from(dir), true); + crash::test_crash_if_requested(); + unreachable!("TSC_TEST_CRASH is set"); +} + +#[test] +fn test_crash_in_child_process() { + let dir = temp_dir("child"); + // Captured: the child's "FAILED" would look like a failure of this run. + let output = std::process::Command::new(std::env::current_exe().unwrap()) + .args(["--exact", "child_process_crash", "--test-threads=1"]) + .env("TSC_CRASH_CHILD_DIR", &dir) + .env("TSC_TEST_CRASH", "1") + .output() + .unwrap(); + let log = String::from_utf8_lossy(&output.stderr); + assert!(!output.status.success(), "the child should have panicked: {log}"); + // The next start finds the report. + let reports = crash::reports_in(&dir).unwrap(); + assert_eq!(reports.len(), 1, "{reports:?}"); + assert_eq!(reports[0].summary, "test crash requested with TSC_TEST_CRASH"); + let text = std::fs::read_to_string(&reports[0].path).unwrap(); + assert!(text.contains("Version: child"), "{text}"); + std::fs::remove_dir_all(&dir).unwrap(); +}