Skip to content

Commit

Permalink
test_pgrep: Try to fix flaky tests (#309)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dezgeg authored Jan 18, 2025
1 parent ab6e081 commit eaf0ae2
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions tests/by-util/test_pgrep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -151,37 +157,46 @@ 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()
.stdout_does_not_contain("_")
.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]
Expand Down

0 comments on commit eaf0ae2

Please sign in to comment.