Skip to content

Commit d947efa

Browse files
committed
test_pgrep: Try to fix flaky tests
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 d947efa

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

tests/by-util/test_pgrep.rs

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

6+
use std::{
7+
array,
8+
process::{Child, Command},
9+
};
10+
611
use crate::common::util::TestScenario;
712
#[cfg(target_os = "linux")]
813
use regex::Regex;
@@ -151,37 +156,48 @@ fn test_valid_regex() {
151156
new_ucmd!().arg("a*").succeeds();
152157
}
153158

159+
#[cfg(target_os = "linux")]
160+
fn spawn_2_dummy_sleep_processes() -> [Child; 2] {
161+
array::from_fn(|_| Command::new("sleep").arg("2").spawn().unwrap())
162+
}
163+
154164
#[cfg(target_os = "linux")]
155165
#[test]
156166
fn test_delimiter() {
167+
let [mut sleep1, mut sleep2] = spawn_2_dummy_sleep_processes();
157168
for arg in ["-d", "--delimiter"] {
158169
new_ucmd!()
159-
.arg("sh")
170+
.arg("sleep")
160171
.arg(arg)
161172
.arg("|")
162173
.succeeds()
163174
.stdout_contains("|");
164175
}
176+
let _ = sleep1.kill();
177+
let _ = sleep2.kill();
165178
}
166179

167180
#[cfg(target_os = "linux")]
168181
#[test]
169182
fn test_delimiter_last_wins() {
183+
let [mut sleep1, mut sleep2] = 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+
let _ = sleep1.kill();
200+
let _ = sleep2.kill();
185201
}
186202

187203
#[test]

0 commit comments

Comments
 (0)