Skip to content

Commit eaf0ae2

Browse files
authored
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.
1 parent ab6e081 commit eaf0ae2

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

tests/by-util/test_pgrep.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
// For the full copyright and license information, please view the LICENSE
44
// file that was distributed with this source code.
55

6+
#[cfg(target_os = "linux")]
7+
use std::{
8+
array,
9+
process::{Child, Command},
10+
};
11+
612
use crate::common::util::TestScenario;
713
#[cfg(target_os = "linux")]
814
use regex::Regex;
@@ -151,37 +157,46 @@ fn test_valid_regex() {
151157
new_ucmd!().arg("a*").succeeds();
152158
}
153159

160+
#[cfg(target_os = "linux")]
161+
fn spawn_2_dummy_sleep_processes() -> [Child; 2] {
162+
array::from_fn(|_| Command::new("sleep").arg("2").spawn().unwrap())
163+
}
164+
154165
#[cfg(target_os = "linux")]
155166
#[test]
156167
fn test_delimiter() {
168+
let mut sleep_processes = spawn_2_dummy_sleep_processes();
157169
for arg in ["-d", "--delimiter"] {
158170
new_ucmd!()
159-
.arg("sh")
171+
.arg("sleep")
160172
.arg(arg)
161173
.arg("|")
162174
.succeeds()
163175
.stdout_contains("|");
164176
}
177+
sleep_processes.iter_mut().for_each(|p| drop(p.kill()));
165178
}
166179

167180
#[cfg(target_os = "linux")]
168181
#[test]
169182
fn test_delimiter_last_wins() {
183+
let mut sleep_processes = spawn_2_dummy_sleep_processes();
170184
new_ucmd!()
171-
.arg("sh")
185+
.arg("sleep")
172186
.arg("-d_")
173187
.arg("-d:")
174188
.succeeds()
175189
.stdout_does_not_contain("_")
176190
.stdout_contains(":");
177191

178192
new_ucmd!()
179-
.arg("sh")
193+
.arg("sleep")
180194
.arg("-d:")
181195
.arg("-d_")
182196
.succeeds()
183197
.stdout_does_not_contain(":")
184198
.stdout_contains("_");
199+
sleep_processes.iter_mut().for_each(|p| drop(p.kill()));
185200
}
186201

187202
#[test]

0 commit comments

Comments
 (0)