Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
w: Remove unit tests and add integration tests
Browse files Browse the repository at this point in the history
It makes more since to make fetch_cmdline and format_time public
functions until they are moved into a library or seperate file and
just use integration tests from the start.
fortifiedhill committed Mar 27, 2024
1 parent bf4c480 commit b7cc844
Showing 2 changed files with 19 additions and 26 deletions.
28 changes: 2 additions & 26 deletions src/uu/w/src/w.rs
Original file line number Diff line number Diff line change
@@ -23,12 +23,12 @@ struct UserInfo {
command: String,
}

fn format_time(time: time::OffsetDateTime) -> Result<String, time::error::Format> {
pub fn format_time(time: time::OffsetDateTime) -> Result<String, time::error::Format> {
let time_format = time::format_description::parse("[hour]:[minute]").unwrap();
time.format(&time_format)
}

fn fetch_cmdline(pid: i32) -> Result<String, std::io::Error> {
pub 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)
}
@@ -142,27 +142,3 @@ pub fn uu_app() -> Command {
.action(ArgAction::SetTrue),
)
}

#[cfg(test)]
mod tests {
use time::OffsetDateTime;
use crate::format_time;
use crate::fetch_cmdline;
use std::{fs::read_to_string, path::Path, process};


#[test]
fn test_format_time() {
let unix_epoc = OffsetDateTime::UNIX_EPOCH;
assert_eq!(format_time(unix_epoc).unwrap(), "00:00");
}

#[test]
// 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.
let pid = process::id() as i32;
let path = Path::new("/proc").join(pid.to_string()).join("cmdline");
assert_eq!(read_to_string(path).unwrap(), fetch_cmdline(pid).unwrap())
}
}
17 changes: 17 additions & 0 deletions tests/by-util/test_w.rs
Original file line number Diff line number Diff line change
@@ -4,6 +4,8 @@
// file that was distributed with this source code.
// spell-checker:ignore (words) symdir somefakedir

use std::{fs, path::Path, process};

use crate::common::util::TestScenario;

#[test]
@@ -19,3 +21,18 @@ fn test_no_header() {

assert!(!result.contains("USER\tTTY\t\tLOGIN@\t\tIDLE\tJCPU\tPCPU\tWHAT"));
}

#[test]
fn test_format_time() {
let unix_epoc = time::OffsetDateTime::UNIX_EPOCH;
assert_eq!(w::format_time(unix_epoc).unwrap(), "00:00");
}

#[test]
// 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.
let pid = process::id() as i32;
let path = Path::new("/proc").join(pid.to_string()).join("cmdline");
assert_eq!(fs::read_to_string(path).unwrap(), w::fetch_cmdline(pid).unwrap())
}

0 comments on commit b7cc844

Please sign in to comment.