From 76aa0bfa254a69ddf803511c5d440625829c229b Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sat, 4 Nov 2023 22:54:36 +0100 Subject: [PATCH] Fix test issue when checking environment on mac: if you run a "system" binary, you cannot access its environment, so better run a non-system one --- tests/process.rs | 105 +++++++++++++++++++++++++++++------------------ 1 file changed, 66 insertions(+), 39 deletions(-) diff --git a/tests/process.rs b/tests/process.rs index 91078958c..3332ef339 100644 --- a/tests/process.rs +++ b/tests/process.rs @@ -1,5 +1,6 @@ // Take a look at the license at the top of the repository in the LICENSE file. +use std::path::Path; use sysinfo::{Pid, System}; #[test] @@ -95,25 +96,51 @@ fn test_cmd() { } } +fn build_example() { + std::process::Command::new("cargo") + .arg("build") + .arg("--example") + .arg("simple") + .stdout(std::process::Stdio::null()) + .spawn() + .unwrap() + .wait() + .unwrap(); +} + +fn find_executable>(path: P) -> Option { + let entries = std::fs::read_dir(path).unwrap(); + for entry in entries { + let entry = entry.unwrap(); + if entry.file_type().unwrap().is_dir() { + if let Some(path) = find_executable(&entry.path()) { + return Some(path); + } + } else if entry.file_name().into_string().unwrap().as_str() == "simple" { + return Some(entry.path().display().to_string()); + } + } + None +} + #[test] fn test_environ() { if !sysinfo::IS_SUPPORTED || cfg!(feature = "apple-sandbox") { return; } - let mut p = if cfg!(target_os = "windows") { - std::process::Command::new("waitfor") - .arg("/t") + // For some reason, it doesn't work on freebsd so let's just use "sleep"... + let mut p = if cfg!(target_os = "freebsd") { + std::process::Command::new("sleep") .arg("3") - .arg("EnvironSignal") .stdout(std::process::Stdio::null()) .env("FOO", "BAR") .env("OTHER", "VALUE") .spawn() .unwrap() } else { - std::process::Command::new("sleep") - .arg("3") - .stdout(std::process::Stdio::null()) + build_example(); + std::process::Command::new(find_executable("./target").unwrap()) + .stdin(std::process::Stdio::piped()) .env("FOO", "BAR") .env("OTHER", "VALUE") .spawn() @@ -121,24 +148,24 @@ fn test_environ() { }; let pid = Pid::from_u32(p.id() as _); - std::thread::sleep(std::time::Duration::from_secs(1)); let mut s = System::new(); - s.refresh_processes(); - p.kill().expect("Unable to kill process."); - let processes = s.processes(); - let p = processes.get(&pid); + for _ in 0..3 { + s.refresh_process_specifics(pid, sysinfo::ProcessRefreshKind::everything()); - if let Some(p) = p { - assert_eq!(p.pid(), pid); - // FIXME: instead of ignoring the test on CI, try to find out what's wrong... - if std::env::var("APPLE_CI").is_err() { - assert!(p.environ().iter().any(|e| e == "FOO=BAR")); - assert!(p.environ().iter().any(|e| e == "OTHER=VALUE")); + let processes = s.processes(); + let proc_ = processes.get(&pid); + + if let Some(proc_) = proc_ { + p.kill().expect("Unable to kill process."); + assert_eq!(proc_.pid(), pid); + assert!(proc_.environ().iter().any(|e| e == "FOO=BAR")); + assert!(proc_.environ().iter().any(|e| e == "OTHER=VALUE")); + return; } - } else { - panic!("Process not found!"); + std::thread::sleep(std::time::Duration::from_secs(1)); } + panic!("Process not found!"); } // Test to ensure that a process with a lot of environment variables doesn't get truncated. @@ -153,43 +180,43 @@ fn test_big_environ() { for _ in 0..SIZE { big_env.push('a'); } - let mut p = if cfg!(target_os = "windows") { - std::process::Command::new("waitfor") - .arg("/t") + // For some reason, it doesn't work on freebsd so let's just use "sleep"... + let mut p = if cfg!(target_os = "freebsd") { + std::process::Command::new("sleep") .arg("3") - .arg("EnvironSignal") .stdout(std::process::Stdio::null()) .env("FOO", &big_env) .spawn() .unwrap() } else { - std::process::Command::new("sleep") - .arg("3") - .stdout(std::process::Stdio::null()) + build_example(); + std::process::Command::new(find_executable("./target").unwrap()) + .stdin(std::process::Stdio::piped()) .env("FOO", &big_env) .spawn() .unwrap() }; let pid = Pid::from_u32(p.id() as _); - std::thread::sleep(std::time::Duration::from_secs(1)); let mut s = System::new(); - s.refresh_processes(); - p.kill().expect("Unable to kill process."); - let processes = s.processes(); - let p = processes.get(&pid); + for _ in 0..3 { + s.refresh_processes(); - if let Some(p) = p { - assert_eq!(p.pid(), pid); - // FIXME: instead of ignoring the test on CI, try to find out what's wrong... - if std::env::var("APPLE_CI").is_err() { + let processes = s.processes(); + let proc_ = processes.get(&pid); + + if let Some(proc_) = proc_ { + p.kill().expect("Unable to kill process."); + assert_eq!(proc_.pid(), pid); let env = format!("FOO={big_env}"); - assert!(p.environ().iter().any(|e| *e == env)); + assert!(proc_.environ().iter().any(|e| *e == env)); + return; } - } else { - panic!("Process not found!"); + std::thread::sleep(std::time::Duration::from_secs(1)); } + + panic!("Process not found!"); } #[test]