test_pgrep: Try to fix flaky tests (#309)

test_delimiter and test_delimiter_last_wins seem to rely on the system
running the test having 2 or more 'sh' processes running, but that
doesn't seem to be reliably the case in GitHub pipelines. Explicitly
spawn two sleep processes instead.
This commit is contained in:
Tuomas Tynkkynen
2025-01-18 20:59:05 +02:00
committed by GitHub
parent ab6e081faa
commit eaf0ae2bd2
+18 -3
View File
@@ -3,6 +3,12 @@
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.
#[cfg(target_os = "linux")]
use std::{
array,
process::{Child, Command},
};
use crate::common::util::TestScenario;
#[cfg(target_os = "linux")]
use regex::Regex;
@@ -151,24 +157,32 @@ fn test_valid_regex() {
new_ucmd!().arg("a*").succeeds();
}
#[cfg(target_os = "linux")]
fn spawn_2_dummy_sleep_processes() -> [Child; 2] {
array::from_fn(|_| Command::new("sleep").arg("2").spawn().unwrap())
}
#[cfg(target_os = "linux")]
#[test]
fn test_delimiter() {
let mut sleep_processes = spawn_2_dummy_sleep_processes();
for arg in ["-d", "--delimiter"] {
new_ucmd!()
.arg("sh")
.arg("sleep")
.arg(arg)
.arg("|")
.succeeds()
.stdout_contains("|");
}
sleep_processes.iter_mut().for_each(|p| drop(p.kill()));
}
#[cfg(target_os = "linux")]
#[test]
fn test_delimiter_last_wins() {
let mut sleep_processes = spawn_2_dummy_sleep_processes();
new_ucmd!()
.arg("sh")
.arg("sleep")
.arg("-d_")
.arg("-d:")
.succeeds()
@@ -176,12 +190,13 @@ fn test_delimiter_last_wins() {
.stdout_contains(":");
new_ucmd!()
.arg("sh")
.arg("sleep")
.arg("-d:")
.arg("-d_")
.succeeds()
.stdout_does_not_contain(":")
.stdout_contains("_");
sleep_processes.iter_mut().for_each(|p| drop(p.kill()));
}
#[test]