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
This commit is contained in:
@@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user