Skip to content

Commit 48121da

Browse files
committed
whoami/id: refactor tests for #1982
1 parent 045eb00 commit 48121da

File tree

2 files changed

+116
-79
lines changed

2 files changed

+116
-79
lines changed

tests/by-util/test_id.rs

Lines changed: 76 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,32 @@
11
use crate::common::util::*;
22

3+
// Apparently some CI environments have configuration issues, e.g. with 'whoami' and 'id'.
4+
// If we are running inside the CI and "needle" is in "stderr" skipping this test is
5+
// considered okay. If we are not inside the CI this calls assert!(result.success).
6+
//
7+
// From the Logs: "Build (ubuntu-18.04, x86_64-unknown-linux-gnu, feat_os_unix, use-cross)"
8+
// stderr: "whoami: cannot find name for user ID 1001"
9+
// Maybe: "adduser --uid 1001 username" can put things right?
10+
// stderr = id: error: Could not find uid 1001: No such id: 1001
11+
fn skipping_test_is_okay(result: &CmdResult, needle: &str) -> bool {
12+
if !result.succeeded() {
13+
println!("result.stdout = {}", result.stdout_str());
14+
println!("result.stderr = {}", result.stderr_str());
15+
if is_ci() && result.stderr_str().contains(needle) {
16+
println!("test skipped:");
17+
return true;
18+
} else {
19+
result.success();
20+
}
21+
}
22+
false
23+
}
24+
325
fn return_whoami_username() -> String {
426
let scene = TestScenario::new("whoami");
527
let result = scene.cmd("whoami").run();
6-
if is_ci() && result.stderr.contains("cannot find name for user ID") {
7-
// In the CI, some server are failing to return whoami.
8-
// As seems to be a configuration issue, ignoring it
28+
if skipping_test_is_okay(&result, "whoami: cannot find name for user ID") {
29+
println!("test skipped:");
930
return String::from("");
1031
}
1132

@@ -14,40 +35,41 @@ fn return_whoami_username() -> String {
1435

1536
#[test]
1637
fn test_id() {
17-
let result = new_ucmd!().arg("-u").run();
18-
if result.stderr.contains("cannot find name for user ID") {
19-
// In the CI, some server are failing to return whoami.
20-
// As seems to be a configuration issue, ignoring it
21-
return;
22-
}
38+
let scene = TestScenario::new(util_name!());
39+
40+
let result = scene.ucmd().arg("-u").succeeds();
41+
let uid = result.stdout_str().trim();
2342

24-
let uid = result.success().stdout_str().trim();
25-
let result = new_ucmd!().run();
26-
if is_ci() && result.stderr.contains("cannot find name for user ID") {
27-
// In the CI, some server are failing to return whoami.
28-
// As seems to be a configuration issue, ignoring it
43+
let result = scene.ucmd().run();
44+
if skipping_test_is_okay(&result, "Could not find uid") {
2945
return;
3046
}
3147

32-
if !result.stderr_str().contains("Could not find uid") {
33-
// Verify that the id found by --user/-u exists in the list
34-
result.success().stdout_contains(&uid);
35-
}
48+
// Verify that the id found by --user/-u exists in the list
49+
result.stdout_contains(uid);
3650
}
3751

3852
#[test]
3953
fn test_id_from_name() {
4054
let username = return_whoami_username();
41-
if username == "" {
42-
// Sometimes, the CI is failing here
55+
if username.is_empty() {
56+
return;
57+
}
58+
59+
let scene = TestScenario::new(util_name!());
60+
let result = scene.ucmd().arg(&username).run();
61+
if skipping_test_is_okay(&result, "Could not find uid") {
4362
return;
4463
}
4564

46-
let result = new_ucmd!().arg(&username).succeeds();
4765
let uid = result.stdout_str().trim();
4866

49-
new_ucmd!()
50-
.succeeds()
67+
let result = scene.ucmd().run();
68+
if skipping_test_is_okay(&result, "Could not find uid") {
69+
return;
70+
}
71+
72+
result
5173
// Verify that the id found by --user/-u exists in the list
5274
.stdout_contains(uid)
5375
// Verify that the username found by whoami exists in the list
@@ -56,48 +78,42 @@ fn test_id_from_name() {
5678

5779
#[test]
5880
fn test_id_name_from_id() {
59-
let result = new_ucmd!().arg("-u").succeeds();
60-
let uid = result.stdout_str().trim();
81+
let result = new_ucmd!().arg("-nu").run();
82+
83+
let username_id = result.stdout_str().trim();
6184

62-
let result = new_ucmd!().arg("-nu").arg(uid).run();
63-
if is_ci() && result.stderr.contains("No such user/group") {
64-
// In the CI, some server are failing to return whoami.
65-
// As seems to be a configuration issue, ignoring it
85+
let username_whoami = return_whoami_username();
86+
if username_whoami.is_empty() {
6687
return;
6788
}
6889

69-
let username_id = result.success().stdout_str().trim();
70-
71-
let scene = TestScenario::new("whoami");
72-
let result = scene.cmd("whoami").succeeds();
73-
74-
let username_whoami = result.stdout_str().trim();
75-
7690
assert_eq!(username_id, username_whoami);
7791
}
7892

7993
#[test]
8094
fn test_id_group() {
81-
let mut result = new_ucmd!().arg("-g").succeeds();
95+
let scene = TestScenario::new(util_name!());
96+
97+
let mut result = scene.ucmd().arg("-g").succeeds();
8298
let s1 = result.stdout_str().trim();
8399
assert!(s1.parse::<f64>().is_ok());
84100

85-
result = new_ucmd!().arg("--group").succeeds();
101+
result = scene.ucmd().arg("--group").succeeds();
86102
let s1 = result.stdout_str().trim();
87103
assert!(s1.parse::<f64>().is_ok());
88104
}
89105

90106
#[test]
91107
fn test_id_groups() {
92-
let result = new_ucmd!().arg("-G").succeeds();
93-
assert!(result.success);
108+
let scene = TestScenario::new(util_name!());
109+
110+
let result = scene.ucmd().arg("-G").succeeds();
94111
let groups = result.stdout_str().trim().split_whitespace();
95112
for s in groups {
96113
assert!(s.parse::<f64>().is_ok());
97114
}
98115

99-
let result = new_ucmd!().arg("--groups").succeeds();
100-
assert!(result.success);
116+
let result = scene.ucmd().arg("--groups").succeeds();
101117
let groups = result.stdout_str().trim().split_whitespace();
102118
for s in groups {
103119
assert!(s.parse::<f64>().is_ok());
@@ -106,40 +122,48 @@ fn test_id_groups() {
106122

107123
#[test]
108124
fn test_id_user() {
109-
let mut result = new_ucmd!().arg("-u").succeeds();
125+
let scene = TestScenario::new(util_name!());
126+
127+
let result = scene.ucmd().arg("-u").succeeds();
110128
let s1 = result.stdout_str().trim();
111129
assert!(s1.parse::<f64>().is_ok());
112130

113-
result = new_ucmd!().arg("--user").succeeds();
131+
let result = scene.ucmd().arg("--user").succeeds();
114132
let s1 = result.stdout_str().trim();
115133
assert!(s1.parse::<f64>().is_ok());
116134
}
117135

118136
#[test]
119137
fn test_id_pretty_print() {
120138
let username = return_whoami_username();
121-
if username == "" {
122-
// Sometimes, the CI is failing here
139+
if username.is_empty() {
123140
return;
124141
}
125142

126-
let result = new_ucmd!().arg("-p").run();
127-
if result.stdout_str().trim() == "" {
128-
// Sometimes, the CI is failing here with
129-
// old rust versions on Linux
143+
let scene = TestScenario::new(util_name!());
144+
let result = scene.ucmd().arg("-p").run();
145+
if result.stdout_str().trim().is_empty() {
146+
// this fails only on: "MinRustV (ubuntu-latest, feat_os_unix)"
147+
// `rustc 1.40.0 (73528e339 2019-12-16)`
148+
// run: /home/runner/work/coreutils/coreutils/target/debug/coreutils id -p
149+
// thread 'test_id::test_id_pretty_print' panicked at 'Command was expected to succeed.
150+
// stdout =
151+
// stderr = ', tests/common/util.rs:157:13
152+
println!("test skipped:");
130153
return;
131154
}
155+
132156
result.success().stdout_contains(username);
133157
}
134158

135159
#[test]
136160
fn test_id_password_style() {
137161
let username = return_whoami_username();
138-
if username == "" {
139-
// Sometimes, the CI is failing here
162+
if username.is_empty() {
140163
return;
141164
}
142165

143166
let result = new_ucmd!().arg("-P").succeeds();
167+
144168
assert!(result.stdout_str().starts_with(&username));
145169
}

tests/by-util/test_whoami.rs

Lines changed: 40 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,63 @@
11
use crate::common::util::*;
2-
use std::env;
2+
3+
// Apparently some CI environments have configuration issues, e.g. with 'whoami' and 'id'.
4+
// If we are running inside the CI and "needle" is in "stderr" skipping this test is
5+
// considered okay. If we are not inside the CI this calls assert!(result.success).
6+
//
7+
// From the Logs: "Build (ubuntu-18.04, x86_64-unknown-linux-gnu, feat_os_unix, use-cross)"
8+
// stderr: "whoami: error: failed to get username"
9+
// Maybe: "adduser --uid 1001 username" can put things right?
10+
fn skipping_test_is_okay(result: &CmdResult, needle: &str) -> bool {
11+
if !result.succeeded() {
12+
println!("result.stdout = {}", result.stdout_str());
13+
println!("result.stderr = {}", result.stderr_str());
14+
if is_ci() && result.stderr_str().contains(needle) {
15+
println!("test skipped:");
16+
return true;
17+
} else {
18+
result.success();
19+
}
20+
}
21+
false
22+
}
323

424
#[test]
525
fn test_normal() {
626
let (_, mut ucmd) = at_and_ucmd!();
727

828
let result = ucmd.run();
9-
println!("result.stdout = {}", result.stdout);
10-
println!("result.stderr = {}", result.stderr);
11-
println!("env::var(CI).is_ok() = {}", env::var("CI").is_ok());
1229

13-
for (key, value) in env::vars() {
14-
println!("{}: {}", key, value);
15-
}
16-
if is_ci() && result.stderr.contains("failed to get username") {
17-
// In the CI, some server are failing to return whoami.
18-
// As seems to be a configuration issue, ignoring it
30+
// use std::env;
31+
// println!("env::var(CI).is_ok() = {}", env::var("CI").is_ok());
32+
// for (key, value) in env::vars() {
33+
// println!("{}: {}", key, value);
34+
// }
35+
36+
if skipping_test_is_okay(&result, "failed to get username") {
1937
return;
2038
}
2139

22-
assert!(result.success);
23-
assert!(!result.stdout.trim().is_empty());
40+
result.no_stderr();
41+
assert!(!result.stdout_str().trim().is_empty());
2442
}
2543

2644
#[test]
2745
#[cfg(not(windows))]
2846
fn test_normal_compare_id() {
29-
let (_, mut ucmd) = at_and_ucmd!();
30-
31-
let result = ucmd.run();
47+
let scene = TestScenario::new(util_name!());
3248

33-
println!("result.stdout = {}", result.stdout);
34-
println!("result.stderr = {}", result.stderr);
35-
if is_ci() && result.stderr.contains("failed to get username") {
36-
// In the CI, some server are failing to return whoami.
37-
// As seems to be a configuration issue, ignoring it
49+
let result_ucmd = scene.ucmd().run();
50+
if skipping_test_is_okay(&result_ucmd, "failed to get username") {
3851
return;
3952
}
40-
assert!(result.success);
41-
let ts = TestScenario::new("id");
42-
let id = ts.cmd("id").arg("-un").run();
4353

44-
if is_ci() && id.stderr.contains("cannot find name for user ID") {
45-
// In the CI, some server are failing to return whoami.
46-
// As seems to be a configuration issue, ignoring it
54+
let result_cmd = scene.cmd("id").arg("-un").run();
55+
if skipping_test_is_okay(&result_cmd, "cannot find name for user ID") {
4756
return;
4857
}
49-
assert_eq!(result.stdout.trim(), id.stdout.trim());
58+
59+
assert_eq!(
60+
result_ucmd.stdout_str().trim(),
61+
result_cmd.stdout_str().trim()
62+
);
5063
}

0 commit comments

Comments
 (0)