From c65b7e83af115cf9737a38f805119d1e0665be32 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 | 50 ++++++++++++++++++++++++++---------------------- 1 file changed, 27 insertions(+), 23 deletions(-) diff --git a/tests/process.rs b/tests/process.rs index 91078958c..bf2f462eb 100644 --- a/tests/process.rs +++ b/tests/process.rs @@ -95,24 +95,35 @@ 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(); +} + #[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") + build_example(); + std::process::Command::new("./target/debug/examples/simple") .stdout(std::process::Stdio::null()) .env("FOO", "BAR") .env("OTHER", "VALUE") @@ -123,7 +134,7 @@ 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(); + s.refresh_process_specifics(pid, sysinfo::ProcessRefreshKind::everything()); p.kill().expect("Unable to kill process."); let processes = s.processes(); @@ -131,11 +142,8 @@ fn test_environ() { 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")); - } + assert!(p.environ().iter().any(|e| e == "FOO=BAR")); + assert!(p.environ().iter().any(|e| e == "OTHER=VALUE")); } else { panic!("Process not found!"); } @@ -153,18 +161,17 @@ 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") + build_example(); + std::process::Command::new("./target/debug/examples/simple") .stdout(std::process::Stdio::null()) .env("FOO", &big_env) .spawn() @@ -182,11 +189,8 @@ fn test_big_environ() { 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 env = format!("FOO={big_env}"); - assert!(p.environ().iter().any(|e| *e == env)); - } + let env = format!("FOO={big_env}"); + assert!(p.environ().iter().any(|e| *e == env)); } else { panic!("Process not found!"); }