diff --git a/src/gpt.rs b/src/gpt.rs index 50c8bc5..a1b5e5c 100644 --- a/src/gpt.rs +++ b/src/gpt.rs @@ -2,6 +2,13 @@ use crate::Device; #[cfg(feature = "gpt")] use gpt; + +#[cfg(not(test))] +const DEV_DIR:&str = "/dev/"; + +#[cfg(test)] +const DEV_DIR:&str = concat!(env!("CARGO_MANIFEST_DIR"), "/resources", "/test"); + #[derive(Debug)] pub enum GptUUID { IoError(std::io::Error), @@ -20,7 +27,7 @@ pub fn enrich_with_gpt_uuid(mut device: Device) -> Device { #[cfg(feature = "gpt")] pub fn enrich_with_gpt_uuid(mut device: Device) -> Device { - let diskpath = std::path::Path::new("/dev/").join(device.name.to_string()); + let diskpath = std::path::Path::new(DEV_DIR).join(device.name.to_string()); let cfg = gpt::GptConfig::new().writable(false); match cfg.open(diskpath) { Err(error) => device.uuid = GptUUID::IoError(error), @@ -33,4 +40,32 @@ pub fn enrich_with_gpt_uuid(mut device: Device) -> Device { }; device +} + +#[cfg(test)] +mod tests { + + use super::*; + + #[cfg(feature = "gpt")] + #[test] + fn test_enrich_with_gpt_uuid() { + use crate::Size; + + let mut device = Device { + name: "gptdisk.img".to_string(), + partitions: Vec::new(), + is_removable: false, + model: None, + serial: None, + size: Size::new(42), + uuid: GptUUID::NotAvailable, + }; + device = enrich_with_gpt_uuid(device); + + match device.uuid { + GptUUID::UUID(uuid) => assert_eq!("f0ce7b2c-74af-47e4-8141-b2fe24ac20cc", uuid), + _ => panic!("No UUID"), + } + } } \ No newline at end of file