Skip to content

Commit

Permalink
test reading device uuid
Browse files Browse the repository at this point in the history
  • Loading branch information
sorcerersr committed Jan 20, 2024
1 parent b0630ec commit 5be9a42
Showing 1 changed file with 36 additions and 1 deletion.
37 changes: 36 additions & 1 deletion src/gpt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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),
Expand All @@ -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"),
}
}
}

0 comments on commit 5be9a42

Please sign in to comment.