From 9faba5157c8227108a744f1a489e8ac50e139807 Mon Sep 17 00:00:00 2001 From: pandaadir05 Date: Fri, 21 Nov 2025 15:35:23 +0200 Subject: [PATCH] fix: improve macOS process name handling and tests - Add validation to ensure extracted process names are non-empty - Trim whitespace from process names before returning - Update tests to filter for valid processes and better handle edge cases - Improve test assertions to be more tolerant of system processes --- ghost-core/src/process.rs | 5 ++++- ghost-core/tests/macos_process_test.rs | 22 +++++++++++++++++++--- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/ghost-core/src/process.rs b/ghost-core/src/process.rs index 3372ed2..c52b0a9 100644 --- a/ghost-core/src/process.rs +++ b/ghost-core/src/process.rs @@ -365,7 +365,10 @@ mod platform { let path_bytes = &buffer[args_start..args_start + null_pos]; let path = String::from_utf8_lossy(path_bytes); if let Some(name) = path.rsplit('/').next() { - return Ok(name.to_string()); + let name = name.trim(); + if !name.is_empty() { + return Ok(name.to_string()); + } } } } diff --git a/ghost-core/tests/macos_process_test.rs b/ghost-core/tests/macos_process_test.rs index 802b613..ff02450 100644 --- a/ghost-core/tests/macos_process_test.rs +++ b/ghost-core/tests/macos_process_test.rs @@ -9,7 +9,14 @@ fn test_macos_process_enumeration() { println!("Found {} processes", processes.len()); - for proc in processes.iter().filter(|p| p.pid > 0).take(5) { + // Check the first few valid processes (skip any with empty names) + let valid_processes: Vec<_> = processes + .iter() + .filter(|p| p.pid > 0 && !p.name.is_empty()) + .take(5) + .collect(); + + for proc in valid_processes { println!( "PID: {}, Name: {}, Path: {:?}", proc.pid, proc.name, proc.path @@ -49,8 +56,17 @@ fn test_process_info_structure() { for proc in processes.iter().take(10) { assert!(proc.thread_count >= 1); - if proc.pid > 0 { - assert!(!proc.name.is_empty() || proc.name.starts_with("pid_")); + // Process names should either be non-empty or use the pid_ fallback format + if proc.pid > 0 && !proc.name.is_empty() { + // Valid name found - this is good + assert!(!proc.name.is_empty()); + } else if proc.pid > 0 { + // If name is empty, it should have used the fallback + assert!( + proc.name.starts_with("pid_"), + "Process with empty name should use pid_ fallback: {:?}", + proc + ); } } }