Debian Codegraph @

tests/by-util/test_who.rs

raw
1 // This file is part of the uutils coreutils package.
2 //
3 // For the full copyright and license information, please view the LICENSE
4 // file that was distributed with this source code.
5
6 // spell-checker:ignore (flags) runlevel mesg
7
8 use uutests::new_ucmd;
9 use uutests::unwrap_or_return;
10 use uutests::util::{TestScenario, expected_result, gnu_cmd_result};
11 use uutests::util_name;
12 #[test]
13 fn test_invalid_arg() {
14 new_ucmd!().arg("--definitely-invalid").fails_with_code(1);
15 }
16
17 #[cfg(unix)]
18 #[test]
19 #[ignore = "issue #3219"]
20 fn test_count() {
21 let ts = TestScenario::new(util_name!());
22 for opt in ["-q", "--count", "--c"] {
23 let expected_stdout = unwrap_or_return!(expected_result(&ts, &[opt])).stdout_move_str();
24 ts.ucmd().arg(opt).succeeds().stdout_is(expected_stdout);
25 }
26 }
27
28 #[cfg(unix)]
29 #[test]
30 #[cfg(not(target_os = "openbsd"))]
31 #[cfg_attr(
32 all(target_arch = "aarch64", target_os = "linux"),
33 ignore = "Issue #7174 - Test not supported on ARM64 Linux"
34 )]
35 fn test_boot() {
36 let ts = TestScenario::new(util_name!());
37 for opt in ["-b", "--boot", "--b"] {
38 let expected_stdout = unwrap_or_return!(expected_result(&ts, &[opt])).stdout_move_str();
39 ts.ucmd().arg(opt).succeeds().stdout_is(expected_stdout);
40 }
41 }
42
43 #[cfg(unix)]
44 #[test]
45 #[ignore = "issue #3219"]
46 fn test_heading() {
47 let ts = TestScenario::new(util_name!());
48 for opt in ["-H", "--heading", "--head"] {
49 // allow whitespace variation
50 // * minor whitespace differences occur between platform built-in outputs;
51 // specifically number of TABs between "TIME" and "COMMENT" may be variant
52 let actual = ts.ucmd().arg(opt).succeeds().stdout_move_str();
53 let expect = unwrap_or_return!(expected_result(&ts, &[opt])).stdout_move_str();
54 println!("actual: {actual:?}");
55 println!("expect: {expect:?}");
56 let v_actual: Vec<&str> = actual.split_whitespace().collect();
57 let v_expect: Vec<&str> = expect.split_whitespace().collect();
58 assert_eq!(v_actual, v_expect);
59 }
60 }
61
62 #[cfg(unix)]
63 #[test]
64 #[ignore = "issue #3219"]
65 fn test_short() {
66 let ts = TestScenario::new(util_name!());
67 for opt in ["-s", "--short", "--s"] {
68 let expected_stdout = unwrap_or_return!(expected_result(&ts, &[opt])).stdout_move_str();
69 ts.ucmd().arg(opt).succeeds().stdout_is(expected_stdout);
70 }
71 }
72
73 #[cfg(unix)]
74 #[test]
75 #[cfg(not(target_os = "openbsd"))]
76 fn test_login() {
77 let ts = TestScenario::new(util_name!());
78 for opt in ["-l", "--login", "--log"] {
79 let expected_stdout = unwrap_or_return!(expected_result(&ts, &[opt])).stdout_move_str();
80 ts.ucmd().arg(opt).succeeds().stdout_is(expected_stdout);
81 }
82 }
83
84 #[cfg(unix)]
85 #[test]
86 #[cfg(not(target_os = "openbsd"))]
87 fn test_m() {
88 let ts = TestScenario::new(util_name!());
89 let expected_stdout = unwrap_or_return!(expected_result(&ts, &["-m"])).stdout_move_str();
90 ts.ucmd().arg("-m").succeeds().stdout_is(expected_stdout);
91 }
92
93 #[cfg(unix)]
94 #[test]
95 #[cfg(not(target_os = "openbsd"))]
96 fn test_process() {
97 let ts = TestScenario::new(util_name!());
98 for opt in ["-p", "--process", "--p"] {
99 let expected_stdout = unwrap_or_return!(expected_result(&ts, &[opt])).stdout_move_str();
100 ts.ucmd().arg(opt).succeeds().stdout_is(expected_stdout);
101 }
102 }
103
104 #[cfg(unix)]
105 #[test]
106 #[cfg(not(target_os = "openbsd"))]
107 fn test_runlevel() {
108 let ts = TestScenario::new(util_name!());
109 for opt in ["-r", "--runlevel", "--r"] {
110 let expected_stdout = unwrap_or_return!(expected_result(&ts, &[opt])).stdout_move_str();
111 ts.ucmd().arg(opt).succeeds().stdout_is(expected_stdout);
112
113 #[cfg(not(target_os = "linux"))]
114 ts.ucmd().arg(opt).succeeds().no_output();
115 }
116 }
117
118 #[cfg(unix)]
119 #[test]
120 #[cfg(not(target_os = "openbsd"))]
121 fn test_time() {
122 let ts = TestScenario::new(util_name!());
123 for opt in ["-t", "--time", "--t"] {
124 let expected_stdout = unwrap_or_return!(expected_result(&ts, &[opt])).stdout_move_str();
125 ts.ucmd().arg(opt).succeeds().stdout_is(expected_stdout);
126 }
127 }
128
129 #[cfg(unix)]
130 #[test]
131 #[ignore = "issue #3219"]
132 fn test_mesg() {
133 // -T, -w, --mesg
134 // add user's message status as +, - or ?
135 // --message
136 // same as -T
137 // --writable
138 // same as -T
139 let ts = TestScenario::new(util_name!());
140 for opt in [
141 "-T",
142 "-w",
143 "--mesg",
144 "--m",
145 "--message",
146 "--writable",
147 "--w",
148 ] {
149 let expected_stdout = unwrap_or_return!(expected_result(&ts, &[opt])).stdout_move_str();
150 ts.ucmd().arg(opt).succeeds().stdout_is(expected_stdout);
151 }
152 }
153
154 #[cfg(unix)]
155 #[test]
156 #[cfg(not(target_os = "openbsd"))]
157 fn test_arg1_arg2() {
158 let args = ["am", "i"];
159 let ts = TestScenario::new(util_name!());
160 let expected_stdout = unwrap_or_return!(expected_result(&ts, &args)).stdout_move_str();
161 ts.ucmd().args(&args).succeeds().stdout_is(expected_stdout);
162 }
163
164 #[test]
165 fn test_too_many_args() {
166 const EXPECTED: &str =
167 "error: unexpected value 'u' for '[FILE]...' found; no more were expected";
168
169 let args = ["am", "i", "u"];
170 new_ucmd!().args(&args).fails().stderr_contains(EXPECTED);
171 }
172
173 #[cfg(unix)]
174 #[test]
175 #[ignore = "issue #3219"]
176 fn test_users() {
177 let ts = TestScenario::new(util_name!());
178 for opt in ["-u", "--users", "--us"] {
179 let actual = ts.ucmd().arg(opt).succeeds().stdout_move_str();
180 let expect = unwrap_or_return!(expected_result(&ts, &[opt])).stdout_move_str();
181 println!("actual: {actual:?}");
182 println!("expect: {expect:?}");
183
184 let mut v_actual: Vec<&str> = actual.split_whitespace().collect();
185 let mut v_expect: Vec<&str> = expect.split_whitespace().collect();
186
187 // TODO: `--users` sometimes differs from GNU's output on macOS (race condition?)
188 // actual: "runner console Jun 23 06:37 00:34 196\n"
189 // expect: "runner console Jun 23 06:37 old 196\n"
190 if cfg!(target_vendor = "apple") {
191 v_actual.remove(5);
192 v_expect.remove(5);
193 }
194
195 assert_eq!(v_actual, v_expect);
196 }
197 }
198
199 #[cfg(unix)]
200 #[test]
201 #[ignore = "issue #3219"]
202 fn test_lookup() {
203 let opt = "--lookup";
204 let ts = TestScenario::new(util_name!());
205 let expected_stdout = unwrap_or_return!(expected_result(&ts, &[opt])).stdout_move_str();
206 ts.ucmd().arg(opt).succeeds().stdout_is(expected_stdout);
207 }
208
209 #[cfg(unix)]
210 #[test]
211 #[cfg(not(target_os = "openbsd"))]
212 fn test_dead() {
213 let ts = TestScenario::new(util_name!());
214 for opt in ["-d", "--dead", "--de"] {
215 let expected_stdout = unwrap_or_return!(expected_result(&ts, &[opt])).stdout_move_str();
216 ts.ucmd().arg(opt).succeeds().stdout_is(expected_stdout);
217 }
218 }
219
220 #[cfg(unix)]
221 #[test]
222 #[ignore = "issue #3219"]
223 fn test_all_separately() {
224 if cfg!(target_vendor = "apple") {
225 // TODO: fix `-u`, see: test_users
226 return;
227 }
228
229 // -a, --all same as -b -d --login -p -r -t -T -u
230 let args = ["-b", "-d", "--login", "-p", "-r", "-t", "-T", "-u"];
231 let ts = TestScenario::new(util_name!());
232 let expected_stdout = unwrap_or_return!(expected_result(&ts, &args)).stdout_move_str();
233 ts.ucmd().args(&args).succeeds().stdout_is(expected_stdout);
234 let expected_stdout = unwrap_or_return!(expected_result(&ts, &["--all"])).stdout_move_str();
235 ts.ucmd().arg("--all").succeeds().stdout_is(expected_stdout);
236 }
237
238 #[cfg(unix)]
239 #[test]
240 #[ignore = "issue #3219"]
241 fn test_all() {
242 if cfg!(target_vendor = "apple") {
243 // TODO: fix `-u`, see: test_users
244 return;
245 }
246
247 let ts = TestScenario::new(util_name!());
248 for opt in ["-a", "--all", "--a"] {
249 let expected_stdout = unwrap_or_return!(expected_result(&ts, &[opt])).stdout_move_str();
250 ts.ucmd().arg(opt).succeeds().stdout_is(expected_stdout);
251 }
252 }
253
254 #[cfg(unix)]
255 #[test]
256 #[ignore = "issue #3219"]
257 fn test_locale() {
258 let ts = TestScenario::new(util_name!());
259
260 let expected_stdout =
261 unwrap_or_return!(gnu_cmd_result(&ts, &[], &[("LC_ALL", "C")])).stdout_move_str();
262 ts.ucmd()
263 .env("LC_ALL", "C")
264 .succeeds()
265 .stdout_is(&expected_stdout);
266
267 let expected_stdout =
268 unwrap_or_return!(gnu_cmd_result(&ts, &[], &[("LC_ALL", "en_US.UTF-8")])).stdout_move_str();
269 ts.ucmd()
270 .env("LC_ALL", "C")
271 .succeeds()
272 .stdout_str_check(|s| s != expected_stdout);
273 ts.ucmd()
274 .env("LC_ALL", "en_US.UTF-8")
275 .succeeds()
276 .stdout_is(&expected_stdout);
277 }
278
279 #[cfg(target_os = "linux")]
280 #[test]
281 fn test_piped_to_dev_full() {
282 let ts = TestScenario::new(util_name!());
283
284 let dev_full = std::fs::OpenOptions::new()
285 .write(true)
286 .open("/dev/full")
287 .unwrap();
288
289 ts.ucmd()
290 .arg("--heading")
291 .set_stdout(dev_full)
292 .fails()
293 .stderr_is("who: No space left on device\n");
294 }
295
296 // `-q` took a separate branch that printed with `println!`, which aborts the
297 // process on a write error instead of reporting it (#13388).
298 #[cfg(target_os = "linux")]
299 #[test]
300 fn test_short_list_piped_to_dev_full() {
301 let ts = TestScenario::new(util_name!());
302
303 let dev_full = std::fs::OpenOptions::new()
304 .write(true)
305 .open("/dev/full")
306 .unwrap();
307
308 ts.ucmd()
309 .arg("-q")
310 .set_stdout(dev_full)
311 .fails()
312 .stderr_is("who: No space left on device\n");
313 }