Skip to content

Commit

Permalink
CI Debuging commit
Browse files Browse the repository at this point in the history
  • Loading branch information
AnirbanHalder654322 committed May 26, 2024
1 parent a83e0e3 commit 002edae
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 52 deletions.
10 changes: 2 additions & 8 deletions src/uu/uptime/src/platform/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ fn process_utmpx() -> (Option<time_t>, usize) {
BOOT_TIME => {
let dt = line.login_time();
if dt.unix_timestamp() > 0 {
println!("{:?}", line.into_inner().ut_tv);
boot_time = Some(dt.unix_timestamp() as time_t);

Check warning on line 195 in src/uu/uptime/src/platform/unix.rs

View check run for this annotation

Codecov / codecov/patch

src/uu/uptime/src/platform/unix.rs#L194-L195

Added lines #L194 - L195 were not covered by tests
}
}
Expand All @@ -212,14 +213,7 @@ fn process_utmpx_from_file(file: &OsString) -> (Option<time_t>, usize) {
USER_PROCESS => nusers += 1,

Check warning on line 213 in src/uu/uptime/src/platform/unix.rs

View check run for this annotation

Codecov / codecov/patch

src/uu/uptime/src/platform/unix.rs#L213

Added line #L213 was not covered by tests
BOOT_TIME => {
let dt = line.login_time();
// Macos "getutxent" initializes all fields of the struct to 0, if it can't find any
// utmpx record from the file.

#[cfg(target_os = "macos")]
if line.into_inner().ut_tv.tv_sec == 0 {
continue;
}

println!("ut_tv: {:?}", line.into_inner().ut_tv);

Check warning on line 216 in src/uu/uptime/src/platform/unix.rs

View check run for this annotation

Codecov / codecov/patch

src/uu/uptime/src/platform/unix.rs#L215-L216

Added lines #L215 - L216 were not covered by tests
if dt.unix_timestamp() > 0 {
boot_time = Some(dt.unix_timestamp() as time_t);
}
Expand Down
64 changes: 20 additions & 44 deletions tests/by-util/test_uptime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ fn test_uptime_with_non_existent_file() {
}

#[test]
#[cfg(not(any(target_os = "openbsd", target_os = "macos")))]
#[cfg(not(target_os = "openbsd"))]
fn test_uptime_with_file_containing_valid_boot_time_utmpx_record() {
let ts = TestScenario::new(util_name!());
let at = &ts.fixtures;
Expand All @@ -92,8 +92,11 @@ fn test_uptime_with_file_containing_valid_boot_time_utmpx_record() {
// wrong between the days and the time.

let re = Regex::new(r"up [(\d){1,} days]*\d{1,2}:\d\d").unwrap();

#[cfg(not(target_os = "macos"))]
utmp(&at.plus("test"));
#[cfg(target_os = "macos")]
utmp_mac(&at.plus("test"));

ts.ucmd()
.arg("test")
.succeeds()
Expand All @@ -110,6 +113,16 @@ fn test_uptime_with_file_containing_valid_boot_time_utmpx_record() {
arr
}

// Helper function to create byte sequences
fn slice_256(slice: &[u8]) -> [i8; 256] {
let mut arr: [i8; 256] = [0; 256];

for (i, val) in slice.iter().enumerate() {
arr[i] = *val as i8;
}
arr
}

// Creates a file utmp records of three different types including a valid BOOT_TIME entry
fn utmp(path: &PathBuf) {
// Definitions of our utmpx structs
Expand All @@ -131,12 +144,12 @@ fn test_uptime_with_file_containing_valid_boot_time_utmpx_record() {
pub struct Utmp {
pub ut_type: i32,
pub ut_pid: i32,
pub ut_line: [i8; UT_LINESIZE],
pub ut_line: [i8; 32],
pub ut_id: [i8; 4],

pub ut_user: [i8; UT_NAMESIZE],
pub ut_user: [i8; 32],
#[serde(with = "BigArray")]
pub ut_host: [i8; UT_HOSTSIZE],
pub ut_host: [i8; 256],
pub ut_exit: ExitStatus,
pub ut_session: i32,
pub ut_tv: TimeVal,
Expand Down Expand Up @@ -209,46 +222,9 @@ fn test_uptime_with_file_containing_valid_boot_time_utmpx_record() {
let mut f = File::create(path).unwrap();
f.write_all(&buf).unwrap();
}
}

#[cfg(target_os = "macos")]
fn test_uptime_with_file_containing_valid_boot_time_utmpx_record_macos() {
let ts = TestScenario::new(util_name!());
let at = &ts.fixtures;
// 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 = Regex::new(r"up [(\d){1,} days]*\d{1,2}:\d\d").unwrap();

utmp(&at.plus("test"));
ts.ucmd()
.arg("test")
.succeeds()
.stdout_matches(&re)
.stdout_contains("load average");

// Helper function to create byte sequences
fn slice_256(slice: &[u8]) -> [i8; 256] {
let mut arr: [i8; 256] = [0; 256];

for (i, val) in slice.iter().enumerate() {
arr[i] = *val as i8;
}
arr
}
// Helper function to create byte sequences
fn slice_32(slice: &[u8]) -> [i8; 32] {
let mut arr: [i8; 32] = [0; 32];

for (i, val) in slice.iter().enumerate() {
arr[i] = *val as i8;
}
arr
}

// Creates a file utmp records of three different types including a valid BOOT_TIME entry
fn utmp(path: &PathBuf) {
fn utmp_mac(path: &PathBuf) {
// Definitions of our utmpx structs
#[derive(Serialize)]
#[repr(C)]
Expand All @@ -260,7 +236,7 @@ fn test_uptime_with_file_containing_valid_boot_time_utmpx_record_macos() {
#[repr(C)]
pub struct Utmp {
#[serde(with = "BigArray")]
pub ut_user: [i8; UT_NAMESIZE],
pub ut_user: [i8; 256],
pub ut_id: [i8; 4],
pub ut_line: [i8; UT_LINESIZE],
pub ut_pid: i32,
Expand Down

0 comments on commit 002edae

Please sign in to comment.