Skip to content

Commit

Permalink
Replace impl const fn new with OnceCell
Browse files Browse the repository at this point in the history
  • Loading branch information
van-sprundel committed Oct 14, 2024
1 parent 845991d commit 673acda
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
5 changes: 0 additions & 5 deletions src/system/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,6 @@ impl UserId {
pub fn get(&self) -> libc::uid_t {
self.0
}

#[cfg(test)]
pub const fn new_const(uid: libc::uid_t) -> Self {
UserId(uid)
}
}

impl ProcessId {
Expand Down
25 changes: 18 additions & 7 deletions src/system/timestamp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -567,10 +567,13 @@ impl SessionRecord {
#[cfg(test)]
mod tests {
use super::*;

use crate::system::tests::tempfile;
use std::cell::OnceCell;

const TEST_USER_ID: UserId = UserId::new_const(1000);
const TEST_USER_ID: OnceCell<UserId> = OnceCell::new();
fn test_user_id() -> UserId {
*TEST_USER_ID.get_or_init(|| UserId::new(1000))
}

#[test]
fn can_encode_and_decode() {
Expand Down Expand Up @@ -688,25 +691,33 @@ mod tests {
// valid header should remain valid
let c = tempfile_with_data(&[0xD0, 0x50, 0x01, 0x00]).unwrap();
let timeout = Duration::seconds(30);
assert!(SessionRecordFile::new(TEST_USER_ID, c.try_clone().unwrap(), timeout).is_ok());
assert!(
SessionRecordFile::new(test_user_id(), c.try_clone().unwrap(), timeout).is_ok()
);
let v = data_from_tempfile(c).unwrap();
assert_eq!(&v[..], &[0xD0, 0x50, 0x01, 0x00]);

// invalid headers should be corrected
let c = tempfile_with_data(&[0xAB, 0xBA]).unwrap();
assert!(SessionRecordFile::new(TEST_USER_ID, c.try_clone().unwrap(), timeout).is_ok());
assert!(
SessionRecordFile::new(test_user_id(), c.try_clone().unwrap(), timeout).is_ok()
);
let v = data_from_tempfile(c).unwrap();
assert_eq!(&v[..], &[0xD0, 0x50, 0x01, 0x00]);

// empty header should be filled in
let c = tempfile_with_data(&[]).unwrap();
assert!(SessionRecordFile::new(TEST_USER_ID, c.try_clone().unwrap(), timeout).is_ok());
assert!(
SessionRecordFile::new(test_user_id(), c.try_clone().unwrap(), timeout).is_ok()
);
let v = data_from_tempfile(c).unwrap();
assert_eq!(&v[..], &[0xD0, 0x50, 0x01, 0x00]);

// invalid version should reset file
let c = tempfile_with_data(&[0xD0, 0x50, 0xAB, 0xBA, 0x0, 0x0]).unwrap();
assert!(SessionRecordFile::new(TEST_USER_ID, c.try_clone().unwrap(), timeout).is_ok());
assert!(
SessionRecordFile::new(test_user_id(), c.try_clone().unwrap(), timeout).is_ok()
);
let v = data_from_tempfile(c).unwrap();
assert_eq!(&v[..], &[0xD0, 0x50, 0x01, 0x00]);
}
Expand All @@ -716,7 +727,7 @@ mod tests {
let timeout = Duration::seconds(30);
let c = tempfile_with_data(&[]).unwrap();
let mut srf =
SessionRecordFile::new(TEST_USER_ID, c.try_clone().unwrap(), timeout).unwrap();
SessionRecordFile::new(test_user_id(), c.try_clone().unwrap(), timeout).unwrap();
let tty_scope = RecordScope::Tty {
tty_device: DeviceId::new(0),
session_pid: ProcessId::new(0),
Expand Down

0 comments on commit 673acda

Please sign in to comment.