|
| 1 | +// |
| 2 | +// Copyright (c) 2024 Jeff Garzik |
| 3 | +// |
| 4 | +// This file is part of the posixutils-rs project covered under |
| 5 | +// the MIT License. For the full license text, please see the LICENSE |
| 6 | +// file in the root directory of this project. |
| 7 | +// SPDX-License-Identifier: MIT |
| 8 | +// |
| 9 | + |
| 10 | +use plib::testing::{run_test_with_checker, TestPlan}; |
| 11 | +use std::process::Output; |
| 12 | + |
| 13 | +fn run_who_test(args: Vec<&str>, expected_exit_code: i32, check_fn: fn(&TestPlan, &Output)) { |
| 14 | + let plan = TestPlan { |
| 15 | + cmd: "who".to_string(), |
| 16 | + args: args.iter().map(|&s| s.to_string()).collect(), |
| 17 | + stdin_data: String::new(), |
| 18 | + expected_out: String::new(), |
| 19 | + expected_err: String::new(), |
| 20 | + expected_exit_code, |
| 21 | + }; |
| 22 | + run_test_with_checker(plan, check_fn); |
| 23 | +} |
| 24 | + |
| 25 | +// Checker functions |
| 26 | +fn check_exit_success(_: &TestPlan, output: &Output) { |
| 27 | + assert!(output.status.success(), "Expected successful exit"); |
| 28 | +} |
| 29 | + |
| 30 | +fn check_has_column_headings(_: &TestPlan, output: &Output) { |
| 31 | + let stdout = String::from_utf8_lossy(&output.stdout); |
| 32 | + let lines: Vec<&str> = stdout.lines().collect(); |
| 33 | + |
| 34 | + // Should have at least a header line |
| 35 | + assert!(!lines.is_empty(), "Expected output with headings"); |
| 36 | + |
| 37 | + // First line should contain column headers |
| 38 | + let first_line = lines[0].to_uppercase(); |
| 39 | + assert!( |
| 40 | + first_line.contains("NAME") || first_line.contains("LINE") || first_line.contains("TIME"), |
| 41 | + "Expected header line to contain NAME, LINE, or TIME" |
| 42 | + ); |
| 43 | +} |
| 44 | + |
| 45 | +fn check_summary_format(_: &TestPlan, output: &Output) { |
| 46 | + let stdout = String::from_utf8_lossy(&output.stdout); |
| 47 | + let lines: Vec<&str> = stdout.lines().collect(); |
| 48 | + |
| 49 | + // Summary format should have at least the "# users=" line |
| 50 | + assert!( |
| 51 | + lines.iter().any(|line| line.contains("# users=")), |
| 52 | + "Expected '# users=' line in summary output" |
| 53 | + ); |
| 54 | +} |
| 55 | + |
| 56 | +fn check_output_has_terminal_state(_: &TestPlan, output: &Output) { |
| 57 | + let stdout = String::from_utf8_lossy(&output.stdout); |
| 58 | + |
| 59 | + // With -T, output should include terminal state characters (+, -, ?) |
| 60 | + // Skip if no output (no logged in users) |
| 61 | + if !stdout.trim().is_empty() { |
| 62 | + let lines: Vec<&str> = stdout.lines().filter(|l| !l.trim().is_empty()).collect(); |
| 63 | + if !lines.is_empty() { |
| 64 | + // At least one line should potentially have terminal state indicators |
| 65 | + // The format should be: NAME STATE LINE TIME |
| 66 | + // We can't guarantee specific output, but we can check structure |
| 67 | + for line in lines { |
| 68 | + // Each line should have multiple fields |
| 69 | + let fields: Vec<&str> = line.split_whitespace().collect(); |
| 70 | + assert!(fields.len() >= 2, "Expected multiple fields in output"); |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +#[test] |
| 77 | +fn who_no_args() { |
| 78 | + // Default behavior: show logged in users |
| 79 | + run_who_test(vec![], 0, check_exit_success); |
| 80 | +} |
| 81 | + |
| 82 | +#[test] |
| 83 | +fn who_short_format() { |
| 84 | + // -s is the default, explicit test |
| 85 | + run_who_test(vec!["-s"], 0, check_exit_success); |
| 86 | +} |
| 87 | + |
| 88 | +#[test] |
| 89 | +fn who_heading() { |
| 90 | + // -H should print column headings |
| 91 | + run_who_test(vec!["-H"], 0, check_has_column_headings); |
| 92 | +} |
| 93 | + |
| 94 | +#[test] |
| 95 | +fn who_summary() { |
| 96 | + // -q should show summary format |
| 97 | + run_who_test(vec!["-q"], 0, check_summary_format); |
| 98 | +} |
| 99 | + |
| 100 | +#[test] |
| 101 | +fn who_boot() { |
| 102 | + // -b should show boot time |
| 103 | + run_who_test(vec!["-b"], 0, check_exit_success); |
| 104 | +} |
| 105 | + |
| 106 | +#[test] |
| 107 | +fn who_dead() { |
| 108 | + // -d should show dead processes |
| 109 | + run_who_test(vec!["-d"], 0, check_exit_success); |
| 110 | +} |
| 111 | + |
| 112 | +#[test] |
| 113 | +fn who_login() { |
| 114 | + // -l should show login processes |
| 115 | + run_who_test(vec!["-l"], 0, check_exit_success); |
| 116 | +} |
| 117 | + |
| 118 | +#[test] |
| 119 | +fn who_process() { |
| 120 | + // -p should show active processes spawned by init |
| 121 | + run_who_test(vec!["-p"], 0, check_exit_success); |
| 122 | +} |
| 123 | + |
| 124 | +#[test] |
| 125 | +fn who_runlevel() { |
| 126 | + // -r should show current runlevel |
| 127 | + run_who_test(vec!["-r"], 0, check_exit_success); |
| 128 | +} |
| 129 | + |
| 130 | +#[test] |
| 131 | +fn who_time() { |
| 132 | + // -t should show last system clock change |
| 133 | + run_who_test(vec!["-t"], 0, check_exit_success); |
| 134 | +} |
| 135 | + |
| 136 | +#[test] |
| 137 | +fn who_terminals() { |
| 138 | + // -T should show terminal state |
| 139 | + run_who_test(vec!["-T"], 0, check_output_has_terminal_state); |
| 140 | +} |
| 141 | + |
| 142 | +#[test] |
| 143 | +fn who_users() { |
| 144 | + // -u should show idle time for users |
| 145 | + run_who_test(vec!["-u"], 0, check_exit_success); |
| 146 | +} |
| 147 | + |
| 148 | +#[test] |
| 149 | +fn who_all() { |
| 150 | + // -a should enable all options |
| 151 | + run_who_test(vec!["-a"], 0, check_exit_success); |
| 152 | +} |
| 153 | + |
| 154 | +#[test] |
| 155 | +fn who_combined_options() { |
| 156 | + // Test combining options |
| 157 | + run_who_test(vec!["-H", "-b"], 0, check_has_column_headings); |
| 158 | +} |
| 159 | + |
| 160 | +#[test] |
| 161 | +fn who_current_terminal() { |
| 162 | + // -m should show only current terminal |
| 163 | + run_who_test(vec!["-m"], 0, check_exit_success); |
| 164 | +} |
| 165 | + |
| 166 | +#[test] |
| 167 | +fn who_userproc() { |
| 168 | + // --userproc should show normal user processes |
| 169 | + run_who_test(vec!["--userproc"], 0, check_exit_success); |
| 170 | +} |
0 commit comments