Skip to content

Commit 69d4a7e

Browse files
authored
Merge pull request #303 from cakebaker/clippy_fix_warnings_rust_1_84
tests: compile regexes outside of loop
2 parents 7c176ed + f292587 commit 69d4a7e

File tree

2 files changed

+25
-26
lines changed

2 files changed

+25
-26
lines changed

tests/by-util/test_pgrep.rs

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,10 @@ fn test_help() {
5555
#[test]
5656
#[cfg(target_os = "linux")]
5757
fn test_oldest() {
58+
let re = &Regex::new(SINGLE_PID).unwrap();
59+
5860
for arg in ["-o", "--oldest"] {
59-
new_ucmd!()
60-
.arg(arg)
61-
.succeeds()
62-
.stdout_matches(&Regex::new(SINGLE_PID).unwrap());
61+
new_ucmd!().arg(arg).succeeds().stdout_matches(re);
6362
}
6463
}
6564

@@ -77,11 +76,10 @@ fn test_oldest_non_matching_pattern() {
7776
#[test]
7877
#[cfg(target_os = "linux")]
7978
fn test_newest() {
79+
let re = &Regex::new(SINGLE_PID).unwrap();
80+
8081
for arg in ["-n", "--newest"] {
81-
new_ucmd!()
82-
.arg(arg)
83-
.succeeds()
84-
.stdout_matches(&Regex::new(SINGLE_PID).unwrap());
82+
new_ucmd!().arg(arg).succeeds().stdout_matches(re);
8583
}
8684
}
8785

@@ -99,12 +97,10 @@ fn test_newest_non_matching_pattern() {
9997
#[test]
10098
#[cfg(target_os = "linux")]
10199
fn test_older() {
100+
let re = &Regex::new(MULTIPLE_PIDS).unwrap();
101+
102102
for arg in ["-O", "--older"] {
103-
new_ucmd!()
104-
.arg(arg)
105-
.arg("0")
106-
.succeeds()
107-
.stdout_matches(&Regex::new(MULTIPLE_PIDS).unwrap());
103+
new_ucmd!().arg(arg).arg("0").succeeds().stdout_matches(re);
108104
}
109105
}
110106

@@ -199,13 +195,16 @@ fn test_ignore_case() {
199195
#[test]
200196
#[cfg(target_os = "linux")]
201197
fn test_list_full() {
198+
// (?m) enables multi-line mode
199+
let re = &Regex::new("(?m)^[1-9][0-9]* .+$").unwrap();
200+
202201
for arg in ["-a", "--list-full"] {
203202
new_ucmd!()
204203
.arg("sh")
205204
.arg(arg)
206205
.succeeds()
207206
// (?m) enables multi-line mode
208-
.stdout_matches(&Regex::new("(?m)^[1-9][0-9]* .+$").unwrap());
207+
.stdout_matches(re);
209208
}
210209
}
211210

@@ -246,13 +245,15 @@ fn test_count_with_non_matching_pattern() {
246245
#[test]
247246
#[cfg(target_os = "linux")]
248247
fn test_terminal() {
248+
let re = &Regex::new(MULTIPLE_PIDS).unwrap();
249+
249250
for arg in ["-t", "--terminal"] {
250251
new_ucmd!()
251252
.arg(arg)
252253
.arg("tty1")
253254
.arg("--inverse") // XXX hack to make test pass in CI
254255
.succeeds()
255-
.stdout_matches(&Regex::new(MULTIPLE_PIDS).unwrap());
256+
.stdout_matches(re);
256257
}
257258
}
258259

@@ -286,12 +287,10 @@ fn test_terminal_invalid_terminal() {
286287
#[test]
287288
#[cfg(target_os = "linux")]
288289
fn test_runstates() {
290+
let re = &Regex::new(MULTIPLE_PIDS).unwrap();
291+
289292
for arg in ["-r", "--runstates"] {
290-
new_ucmd!()
291-
.arg(arg)
292-
.arg("S")
293-
.succeeds()
294-
.stdout_matches(&Regex::new(MULTIPLE_PIDS).unwrap());
293+
new_ucmd!().arg(arg).arg("S").succeeds().stdout_matches(re);
295294
}
296295
}
297296

@@ -308,12 +307,10 @@ fn test_runstates_invalid_runstate() {
308307
#[test]
309308
#[cfg(target_os = "linux")]
310309
fn test_parent() {
310+
let re = &Regex::new(MULTIPLE_PIDS).unwrap();
311+
311312
for arg in ["-P", "--parent"] {
312-
new_ucmd!()
313-
.arg(arg)
314-
.arg("0")
315-
.succeeds()
316-
.stdout_matches(&Regex::new(MULTIPLE_PIDS).unwrap());
313+
new_ucmd!().arg(arg).arg("0").succeeds().stdout_matches(re);
317314
}
318315
}
319316

tests/by-util/test_pidof.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,12 @@ fn test_omit_pid() {
7272
fn test_separator() {
7373
use regex::Regex;
7474

75+
let re = &Regex::new("^[1-9][0-9]*separator[1-9][0-9]*\n$").unwrap();
76+
7577
for arg in ["-S", "-d", "--separator"] {
7678
new_ucmd!()
7779
.args(&[arg, "separator", "kthreadd", "kthreadd"])
7880
.succeeds()
79-
.stdout_matches(&Regex::new("^[1-9][0-9]*separator[1-9][0-9]*\n$").unwrap());
81+
.stdout_matches(re);
8082
}
8183
}

0 commit comments

Comments
 (0)