11use 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+
325fn 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]
1637fn 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]
3953fn 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]
5880fn 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]
8094fn 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]
91107fn 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]
108124fn 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]
119137fn 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]
136160fn 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}
0 commit comments