Skip to content

Commit

Permalink
w: Fix tests
Browse files Browse the repository at this point in the history
Most functions and tests have been enabled only on Linux, as that's
the only platform that they were tested on.
  • Loading branch information
fortifiedhill committed Apr 6, 2024
1 parent 8b70959 commit e4a9c7c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
20 changes: 12 additions & 8 deletions src/uu/w/src/w.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ struct UserInfo {
command: String,
}

#[cfg(not(windows))]
#[cfg(target_os = "linux")]
fn fetch_terminal_jcpu() -> Result<HashMap<u64, f64>, std::io::Error> {
// Hashmap of terminal numbers and their respective CPU usages
let mut pid_hashmap = HashMap::new();
Expand Down Expand Up @@ -62,7 +62,7 @@ fn fetch_terminal_jcpu() -> Result<HashMap<u64, f64>, std::io::Error> {
Ok(pid_hashmap)
}

#[cfg(not(windows))]
#[cfg(target_os = "linux")]
fn fetch_terminal_number(pid: i32) -> Result<u64, std::io::Error> {
let stat_path = Path::new("/proc").join(pid.to_string()).join("stat");
// Seperate stat and get terminal number, which is at position 6
Expand All @@ -72,12 +72,12 @@ fn fetch_terminal_number(pid: i32) -> Result<u64, std::io::Error> {
Ok(terminal_number)
}

#[cfg(not(windows))]
#[cfg(target_os = "linux")]
fn get_clock_tick() -> i64 {
unsafe { sysconf(_SC_CLK_TCK) }
}

#[cfg(not(windows))]
#[cfg(target_os = "linux")]
fn fetch_pcpu_time(pid: i32) -> Result<f64, std::io::Error> {
let stat_path = Path::new("/proc").join(pid.to_string()).join("stat");
// Seperate stat file by whitespace and get utime and stime, which are at
Expand All @@ -91,7 +91,7 @@ fn fetch_pcpu_time(pid: i32) -> Result<f64, std::io::Error> {
Ok((utime + stime) / get_clock_tick() as f64)
}

#[cfg(not(windows))]
#[cfg(target_os = "linux")]
fn format_time(time: String) -> Result<String, chrono::format::ParseError> {
let mut t: String = time;
// Trim the seconds off of timezone offset, as chrono can't parse the time with it present
Expand All @@ -109,13 +109,13 @@ fn format_time(time: String) -> Result<String, chrono::format::ParseError> {
}
}

#[cfg(not(windows))]
#[cfg(target_os = "linux")]
fn fetch_cmdline(pid: i32) -> Result<String, std::io::Error> {
let cmdline_path = Path::new("/proc").join(pid.to_string()).join("cmdline");
fs::read_to_string(cmdline_path)
}

#[cfg(not(windows))]
#[cfg(target_os = "linux")]
fn fetch_user_info() -> Result<Vec<UserInfo>, std::io::Error> {
let terminal_jcpu_hm = fetch_terminal_jcpu()?;

Expand Down Expand Up @@ -147,7 +147,7 @@ fn fetch_user_info() -> Result<Vec<UserInfo>, std::io::Error> {
Ok(user_info_list)
}

#[cfg(windows)]
#[cfg(any(target_os = "macos", target_os = "windows"))]
fn fetch_user_info() -> Result<Vec<UserInfo>, std::io::Error> {
Ok(Vec::new())
}
Expand Down Expand Up @@ -252,6 +252,7 @@ mod tests {
use std::{fs, path::Path, process};

#[test]
#[cfg(target_os = "linux")]
fn test_format_time() {
let unix_epoc = chrono::Local::now()
.format("%Y-%m-%d %H:%M:%S%.6f %::z")
Expand All @@ -271,6 +272,7 @@ mod tests {
}

#[test]
#[cfg(target_os = "linux")]
// Get PID of current process and use that for cmdline testing
fn test_fetch_cmdline() {
// uucore's utmpx returns an i32, so we cast to that to mimic it.
Expand All @@ -283,6 +285,7 @@ mod tests {
}

#[test]
#[cfg(target_os = "linux")]
fn test_fetch_terminal_number() {
let pid = process::id() as i32;
let path = Path::new("/proc").join(pid.to_string()).join("stat");
Expand All @@ -293,6 +296,7 @@ mod tests {
}

#[test]
#[cfg(target_os = "linux")]
fn test_fetch_pcpu_time() {
let pid = process::id() as i32;
let path = Path::new("/proc").join(pid.to_string()).join("stat");
Expand Down
2 changes: 2 additions & 0 deletions tests/by-util/test_w.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ fn test_no_header() {
}

#[test]
// As of now, output is only implemented for Linux
#[cfg(target_os = "linux")]
fn test_output_format() {
// Use no header to simplify testing
let cmd = new_ucmd!().arg("--no-header").succeeds();
Expand Down

0 comments on commit e4a9c7c

Please sign in to comment.