test: add unit tests for PTP module

Co-authored-by: aider (openai/gpt-5) <aider@aider.chat>
This commit is contained in:
Chaos Rogers 2025-10-26 12:08:03 +00:00
parent 90cc95be21
commit 5ed8198a8e

View file

@ -280,3 +280,74 @@ pub use statime_linux_mod::{spawn_statime, StatimeDaemon, StatimeOptions};
#[cfg(not(target_os = "linux"))] #[cfg(not(target_os = "linux"))]
pub use statime_nonlinux_mod::{spawn_statime, StatimeDaemon, StatimeOptions}; pub use statime_nonlinux_mod::{spawn_statime, StatimeDaemon, StatimeOptions};
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn display_formats() {
let io_err = std::io::Error::new(std::io::ErrorKind::Other, "oops");
let ptp_err: PtpError = io_err.into();
let s = ptp_err.to_string();
assert!(s.starts_with("I/O error: "));
assert!(s.contains("oops"));
assert_eq!(PtpError::ChronoOutOfRange.to_string(), "Chrono out-of-range error");
assert_eq!(PtpError::Unsupported.to_string(), "PTP is unsupported on this platform");
}
#[test]
#[cfg(target_os = "linux")]
fn supported_on_linux() {
assert!(is_supported());
}
#[test]
#[cfg(not(target_os = "linux"))]
fn unsupported_on_non_linux() {
assert!(!is_supported());
}
#[test]
#[cfg(not(target_os = "linux"))]
fn non_linux_ptp_clock_ops_unsupported() {
let r = PtpClock::open("/dev/ptp0");
match r {
Err(PtpError::Unsupported) => {}
other => panic!("expected Unsupported on non-Linux, got {:?}", other),
}
let opts = StatimeOptions::new("lo").with_phc_index(0).with_args(["--debug"]);
let r = spawn_statime(opts);
match r {
Err(PtpError::Unsupported) => {}
other => panic!("expected Unsupported on non-Linux, got {:?}", other),
}
}
#[test]
#[cfg(target_os = "linux")]
fn linux_open_invalid_device_returns_io() {
let r = PtpClock::open("/dev/this_device_should_not_exist_ptp99999");
match r {
Err(PtpError::Io(_)) => {}
Ok(_) => panic!("expected Io error for invalid device, got Ok"),
Err(e) => panic!("expected Io error, got {:?}", e),
}
}
#[test]
#[cfg(target_os = "linux")]
fn linux_spawn_statime_ok_or_not_found() {
let opts = StatimeOptions::new("lo");
let res = spawn_statime(opts);
match res {
Ok(mut d) => {
let _ = d.stop();
}
Err(PtpError::Io(_)) => { /* not found or failed to spawn: acceptable */ }
Err(e) => panic!("unexpected error: {:?}", e),
}
}
}