Skip to content

Commit 8dedf6c

Browse files
Use catch_unwind for panicking tests
Replace #[should_panic] expectations with explicit std::panic::catch_unwind checks in tests to assert on panic messages more robustly. Updated test_auth.rs and test_tray.rs to capture the panic, extract the message, and assert it contains the expected text. In test_signal_handler.rs, removed the #[should_panic] on the async runtime test, renamed it to async_thread_runtime_creation_success, and removed an artificial panic so the test validates the normal runtime creation path; the extreme failure path remains documented but not forced in tests. These changes make test failures clearer and avoid brittle should_panic usage.
1 parent c836e0e commit 8dedf6c

4 files changed

Lines changed: 33 additions & 21 deletions

File tree

.github/workflows/ci-rust.yml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,11 @@ jobs:
165165
if: inputs.run_tests
166166
run: |
167167
tarpaulin_args=()
168-
if [[ "${{ matrix.target }}" == "aarch64-unknown-linux-gnu" ]]; then
169-
tarpaulin_args+=(--jobs 1)
170-
fi
168+
case "${{ matrix.target }}" in
169+
aarch64-unknown-linux-gnu|aarch64-pc-windows-msvc)
170+
tarpaulin_args+=(--jobs 1)
171+
;;
172+
esac
171173
172174
cargo tarpaulin \
173175
--locked \

crates/server/tests/test_auth.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,20 @@ fn test_claims_struct_functionality() {
184184
}
185185

186186
#[test]
187-
#[should_panic(expected = "Invalid role constant")]
188187
fn test_auth_guard_invalid_role_constant() {
189188
// This should panic because role constant 99 is not valid
190189
// We test the panic by calling the role() method with an invalid const generic
191-
let _ = AuthGuard::<99>::role();
190+
let result = std::panic::catch_unwind(AuthGuard::<99>::role);
191+
let panic = result.expect_err("Invalid role constant should panic");
192+
let message = panic
193+
.downcast_ref::<&str>()
194+
.copied()
195+
.or_else(|| panic.downcast_ref::<String>().map(String::as_str))
196+
.unwrap_or("<non-string panic>");
197+
198+
assert!(
199+
message.contains("Invalid role constant"),
200+
"unexpected panic message: {}",
201+
message
202+
);
192203
}

crates/server/tests/test_signal_handler.rs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -604,17 +604,7 @@ mod shutdown_coordinator {
604604
}
605605

606606
#[test]
607-
#[should_panic(expected = "Failed to create tokio runtime")]
608-
fn async_thread_runtime_creation_failure() {
609-
// This test is tricky to trigger in practice, but we can document it
610-
// The panic path occurs when tokio runtime creation fails
611-
// In normal circumstances this should never happen, but the panic is there for safety
612-
613-
// Since we can't easily mock runtime creation failure, we'll create a separate test
614-
// that documents this behavior. The actual panic line will be covered when/if
615-
// runtime creation actually fails in extreme circumstances.
616-
617-
// For now, let's verify that normal async thread creation works fine
607+
fn async_thread_runtime_creation_success() {
618608
let mut coordinator = create_test_coordinator();
619609

620610
coordinator.register_async_thread("normal-async", |_| async move {
@@ -623,9 +613,7 @@ mod shutdown_coordinator {
623613

624614
coordinator.wait_for_completion();
625615

626-
// If we reach here, runtime creation worked fine
627-
// The panic path is for extreme error conditions that are hard to reproduce in tests
628-
panic!("Failed to create tokio runtime for test_panic_scenario");
616+
// If we reach here, runtime creation worked fine.
629617
}
630618

631619
#[test]

crates/server/tests/test_tray.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,23 @@ fn test_launch_with_shutdown_exits() {
3131
/// We expect a panic, because the code calls `image::open`
3232
/// and it should fail on a non-existent file.
3333
#[test]
34-
#[should_panic(expected = "Failed to open icon path")]
3534
fn test_load_icon_non_existent_path_panics() {
3635
use koko::tray::load_icon;
3736

3837
let non_existent_path = Path::new("non_existent_file.ico");
3938

4039
// This should panic based on the logic within `load_icon`.
41-
let _icon = load_icon(non_existent_path);
40+
let result = std::panic::catch_unwind(|| load_icon(non_existent_path));
41+
let panic = result.expect_err("Missing icon path should panic");
42+
let message = panic
43+
.downcast_ref::<&str>()
44+
.copied()
45+
.or_else(|| panic.downcast_ref::<String>().map(String::as_str))
46+
.unwrap_or("<non-string panic>");
47+
48+
assert!(
49+
message.contains("Failed to open icon path"),
50+
"unexpected panic message: {}",
51+
message
52+
);
4253
}

0 commit comments

Comments
 (0)