From b7cc844650e6a8ff1300839f63e6c2b8ea2eca1b Mon Sep 17 00:00:00 2001 From: fortifiedhill <24689525+fortifiedhill@users.noreply.github.com> Date: Wed, 27 Mar 2024 05:27:18 -0500 Subject: [PATCH] w: Remove unit tests and add integration tests 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. --- src/uu/w/src/w.rs | 28 ++-------------------------- tests/by-util/test_w.rs | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 26 deletions(-) diff --git a/src/uu/w/src/w.rs b/src/uu/w/src/w.rs index 9fc61aa1..d50e3a69 100644 --- a/src/uu/w/src/w.rs +++ b/src/uu/w/src/w.rs @@ -23,12 +23,12 @@ struct UserInfo { command: String, } -fn format_time(time: time::OffsetDateTime) -> Result { +pub fn format_time(time: time::OffsetDateTime) -> Result { let time_format = time::format_description::parse("[hour]:[minute]").unwrap(); time.format(&time_format) } -fn fetch_cmdline(pid: i32) -> Result { +pub fn fetch_cmdline(pid: i32) -> Result { 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()) - } -} diff --git a/tests/by-util/test_w.rs b/tests/by-util/test_w.rs index 9203273c..77cff7b2 100644 --- a/tests/by-util/test_w.rs +++ b/tests/by-util/test_w.rs @@ -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()) +}