use blkpath::{resolve_device, resolve_device_from_file, ResolveDevice};
use std::fs::File;
use std::io;
use std::path::{Path, PathBuf};
#[test]
fn test_root_device_resolution() {
let path = Path::new("/");
let result = path.resolve_device();
match result {
Ok(device) => {
assert!(
device.to_string_lossy().starts_with("/dev"),
"Device path should start with /dev, got: {}",
device.display()
);
}
Err(ref e) if e.kind() == io::ErrorKind::NotFound => {
println!("Device not found for root");
}
Err(e) => {
panic!("Unexpected error resolving root device: {}", e);
}
}
}
#[test]
fn test_convenience_function_matches_trait() {
let path = Path::new("/");
let trait_result = path.resolve_device();
let fn_result = resolve_device(path);
match (trait_result, fn_result) {
(Ok(a), Ok(b)) => assert_eq!(a, b),
(Err(_), Err(_)) => { }
_ => panic!("Results should match"),
}
}
#[test]
fn test_pathbuf_matches_path() {
let path = Path::new("/");
let pathbuf = PathBuf::from("/");
let path_result = path.resolve_device();
let pathbuf_result = pathbuf.resolve_device();
match (path_result, pathbuf_result) {
(Ok(a), Ok(b)) => assert_eq!(a, b),
(Err(_), Err(_)) => { }
_ => panic!("Results should match"),
}
}
#[test]
fn test_file_resolution() {
let file = File::open("/").unwrap();
let result = file.resolve_device();
match result {
Ok(device) => {
assert!(
device.to_string_lossy().starts_with("/dev"),
"Device path should start with /dev, got: {}",
device.display()
);
}
Err(ref e) if e.kind() == io::ErrorKind::NotFound => {
println!("Device not found for file");
}
Err(e) => {
panic!("Unexpected error resolving file device: {}", e);
}
}
}
#[test]
fn test_file_reference_resolution() {
let file = File::open("/").unwrap();
let result = resolve_device_from_file(&file);
match result {
Ok(device) => {
assert!(
device.to_string_lossy().starts_with("/dev"),
"Device path should start with /dev, got: {}",
device.display()
);
}
Err(ref e) if e.kind() == io::ErrorKind::NotFound => {
println!("Device not found for file ref");
}
Err(e) => {
panic!("Unexpected error resolving file device: {}", e);
}
}
}
#[test]
fn test_nonexistent_path_returns_error() {
let path = Path::new("/nonexistent/path/that/does/not/exist");
let result = path.resolve_device();
assert!(result.is_err());
match result {
Err(ref e) if e.kind() == io::ErrorKind::NotFound => { }
Err(e) => panic!("Unexpected error type: {:?}", e),
Ok(_) => panic!("Should have failed"),
}
}
#[test]
fn test_consistent_results() {
let path = Path::new("/");
let result1 = path.resolve_device();
let result2 = path.resolve_device();
match (result1, result2) {
(Ok(a), Ok(b)) => assert_eq!(a, b, "Same path should return same device"),
(Err(_), Err(_)) => { }
_ => panic!("Inconsistent results"),
}
}
#[test]
fn test_proc_filesystem() {
if Path::new("/proc").exists() {
let result = Path::new("/proc").resolve_device();
match result {
Ok(_) => { }
Err(ref e) if e.kind() == io::ErrorKind::NotFound => { }
Err(e) => panic!("Unexpected error for /proc: {}", e),
}
}
}
#[test]
fn test_tmp_directory() {
if Path::new("/tmp").exists() {
let result = Path::new("/tmp").resolve_device();
match result {
Ok(device) => {
assert!(
device.to_string_lossy().starts_with("/dev"),
"Device path should start with /dev, got: {}",
device.display()
);
}
Err(ref e) if e.kind() == io::ErrorKind::NotFound => {
}
Err(e) => panic!("Unexpected error for /tmp: {}", e),
}
}
}