better error reporting

This commit is contained in:
Ashwin Naren
2026-01-30 15:34:56 -08:00
parent ac20045a08
commit 01237ddf6f
3 changed files with 42 additions and 6 deletions

10
Cargo.lock generated
View File

@@ -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",

View File

@@ -4,6 +4,7 @@ version = "0.1.0"
edition = "2024"
[dependencies]
colored = "3"
inventory = "0.3"
libc = "0.2"
parking_lot = "0.12"

View File

@@ -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::<Test> {
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());