From 01237ddf6f71c85c11fcdf76bfff49c8bfef4970 Mon Sep 17 00:00:00 2001 From: Ashwin Naren Date: Fri, 30 Jan 2026 15:34:56 -0800 Subject: [PATCH] better error reporting --- Cargo.lock | 10 ++++++++++ usertest/Cargo.toml | 1 + usertest/src/main.rs | 37 +++++++++++++++++++++++++++++++------ 3 files changed, 42 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9103dd8..01eb39c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -58,6 +58,15 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801" +[[package]] +name = "colored" +version = "3.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "faf9468729b8cbcea668e36183cb69d317348c2e08e994829fb56ebfdfbaac34" +dependencies = [ + "windows-sys 0.61.2", +] + [[package]] name = "crc" version = "3.4.0" @@ -600,6 +609,7 @@ checksum = "9312f7c4f6ff9069b165498234ce8be658059c6728633667c526e27dc2cf1df5" name = "usertest" version = "0.1.0" dependencies = [ + "colored", "inventory", "libc", "parking_lot", diff --git a/usertest/Cargo.toml b/usertest/Cargo.toml index e27af84..21a7f47 100644 --- a/usertest/Cargo.toml +++ b/usertest/Cargo.toml @@ -4,6 +4,7 @@ version = "0.1.0" edition = "2024" [dependencies] +colored = "3" inventory = "0.3" libc = "0.2" parking_lot = "0.12" diff --git a/usertest/src/main.rs b/usertest/src/main.rs index 3e7c16d..64b04a6 100644 --- a/usertest/src/main.rs +++ b/usertest/src/main.rs @@ -3,6 +3,7 @@ use std::{ sync::{Arc, Barrier, Mutex}, thread, }; +use colored::Colorize; mod fs; mod futex; @@ -193,7 +194,13 @@ fn test_mincore() { register_test!(test_mincore); -fn run_test(test_fn: fn()) { +fn failing_test() { + panic!("This test is supposed to fail"); +} + +register_test!(failing_test); + +fn run_test(test_fn: fn()) -> Result<(), i32> { // Fork a new process to run the test unsafe { let pid = libc::fork(); @@ -201,14 +208,27 @@ fn run_test(test_fn: fn()) { panic!("fork failed"); } else if pid == 0 { // Child process - test_fn(); - libc::_exit(0); + let result = std::panic::catch_unwind(|| { + test_fn(); + }); + let exit_code = if let Err(e) = result { + // Get the panic info + eprintln!("Test panicked: {:?}", e); + let error = std::io::Error::last_os_error(); + eprintln!("Last OS error: {}", error); + 1 + } else { + 0 + }; + libc::_exit(exit_code); } else { // Parent process let mut status = 0; libc::waitpid(pid, &mut status, 0); if !libc::WIFEXITED(status) || libc::WEXITSTATUS(status) != 0 { - panic!("Test failed in child process with status {status}"); + Err(status) + } else { + Ok(()) } } } @@ -220,8 +240,13 @@ fn main() { for test in inventory::iter:: { print!("{} ...", test.test_text); let _ = stdout().flush(); - run_test(test.test_fn); - println!(" OK"); + match run_test(test.test_fn) { + Ok(()) => println!("{}", " OK".green()), + Err(code) => { + println!(" {}", "FAILED".red()); + eprintln!("Test '{}' failed with exit code {}", test.test_text, code); + } + } } let end = std::time::Instant::now(); println!("All tests passed in {} ms", (end - start).as_millis());