Skip to content

Commit

Permalink
Added files to fixtures, revamped some of the tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AnirbanHalder654322 committed May 19, 2024
1 parent 1cb6ef9 commit 4fc2074
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 30 deletions.
2 changes: 1 addition & 1 deletion src/uu/uptime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ path = "src/uptime.rs"
chrono = { workspace = true }
clap = { workspace = true }
uucore = { workspace = true, features = ["libc", "utmpx"] }
quick-error = { workspace = true}
quick-error = { workspace = true }

[[bin]]
name = "uptime"
Expand Down
5 changes: 4 additions & 1 deletion src/uu/uptime/src/platform/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,13 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
"{}",
UptimeError::ExtraOperandError(path.to_owned().into_string().unwrap())
);
set_exit_code(1);
return Ok(());
}
uptime_with_file(file_path)
}

#[cfg(unix)]
fn uptime_with_file(file_path: &OsString) -> UResult<()> {
// Uptime will print loadavg and time to stderr unless we encounter an extra operand.
let mut non_fatal_error = false;
Expand All @@ -101,7 +104,7 @@ fn uptime_with_file(file_path: &OsString) -> UResult<()> {
}

let (boot_time, user_count) = process_utmpx_from_file(file_path);
print_time();
println!("Time is {:?}", boot_time);
if let Some(time) = boot_time {
let upsecs = get_uptime_from_boot_time(time);
print_uptime(upsecs);
Expand Down
45 changes: 17 additions & 28 deletions tests/by-util/test_uptime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,10 @@ fn test_uptime() {
#[test]
#[cfg(not(target_os = "openbsd"))]
fn test_uptime_for_file_without_utmpx_records() {
let ts = TestScenario::new(util_name!());
let at = &ts.fixtures;
let (at, mut ucmd) = at_and_ucmd!();
at.write("file1", "hello");

ts.ucmd()
.arg(at.plus_as_string("file1"))
ucmd.arg(at.plus_as_string("file1"))
.fails()
.stderr_contains("uptime: couldn't get boot time")
.stdout_contains("up ???? days ??:??")
Expand Down Expand Up @@ -80,62 +78,53 @@ fn test_uptime_with_file_containing_valid_utmpx_record() {
let re = Regex::new(r"up {1,2}[(\d){1,} days]*\d{1,2}:\d\d").unwrap();
#[cfg(not(target_os = "macos"))]
ts.ucmd()
.arg("/var/run/utmp")
.succeeds()
.stdout_matches(&re)
.stdout_contains("load average");
#[cfg(target_os = "macos")]
ts.ucmd()
.arg("/var/run/utmpx")
.arg("validRecord.txt")
.succeeds()
.stdout_matches(&re)
.stdout_contains("load average");
}

/// Assuming /var/log/wtmp has multiple records, /var/log/wtmp doesn't seem to exist in macos
#[test]
#[cfg(not(any(target_os = "macos", target_os = "openbsd")))]
#[cfg(not(target_os = "openbsd"))]
fn test_uptime_with_file_containing_multiple_valid_utmpx_record() {
let ts = TestScenario::new(util_name!());
// Checking for up 00:00 [can be any time]
let re = Regex::new(r"up {1,2}[(\d){1,} days]*\d{1,2}:\d\d").unwrap();
// Can be multiple users, for double digit users, only matches the last digit.
let re_users = Regex::new(r"\d user[s]?").unwrap();
ts.ucmd()
.arg("/var/log/wtmp")
.arg("validMultipleRecords.txt")
.succeeds()
.stdout_matches(&re)
.stdout_matches(&re_users)
.stdout_contains("load average");
}
#[test]
#[cfg(not(target_os = "openbsd"))]
fn test_uptime_with_extra_argument() {
let ts = TestScenario::new(util_name!());

ts.ucmd()
.arg("a")
.arg("b")
.fails()
.stderr_contains("extra operand 'b'");
}
/// Here we test if partial records are parsed properly and this may return an uptime of hours or
/// days, assuming /var/log/wtmp contains multiple records
#[test]
#[cfg(not(any(target_os = "macos", target_os = "openbsd")))]
#[cfg(not(target_os = "openbsd"))]
fn test_uptime_with_file_containing_multiple_valid_utmpx_record_with_partial_records() {
use std::fs;
use std::fs::OpenOptions;

let ts = TestScenario::new(util_name!());
let at = &ts.fixtures;
at.copy("/var/log/wtmp", "log_copy");

let file = OpenOptions::new()
.write(true)
.open(at.plus("log_copy"))
.unwrap();
// Setting the len to half, erasing records in an manner where data is lost.
file.set_len(fs::metadata(at.plus("log_copy")).unwrap().len() / 2)
.unwrap();

let re_users = Regex::new(r"\d user[s]?").unwrap();
// Regex matches for "up 00::00" ,"up 12 days 00::00", the time can be any valid time and
// the days can be more than 1 digit or not there. This will match even if the amount of whitespace is
// wrong between the days and the time.
let re_uptime = Regex::new(r"up {1,2}[(\d){1,} days]*\d{1,2}:\d\d").unwrap();
ts.ucmd()
.arg(at.plus("log_copy"))
.arg("validMultipleRecords.txt")
.succeeds()
.stdout_contains("load average")
.stdout_matches(&re_users)
Expand Down
Binary file added tests/fixtures/uptime/validMultipleRecords.txt
Binary file not shown.
Binary file added tests/fixtures/uptime/validRecord.txt
Binary file not shown.

0 comments on commit 4fc2074

Please sign in to comment.