Skip to content

Commit

Permalink
test: use assert_var_not_found() instead of !manager.exists(), to…
Browse files Browse the repository at this point in the history
… also verify error contents
  • Loading branch information
iTrooz committed Oct 19, 2023
1 parent 61a697d commit 3e9d5c1
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 19 deletions.
3 changes: 2 additions & 1 deletion efiboot/src/cli/boot/tests/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use efivar::{
boot::{BootEntry, BootEntryAttributes, FilePath, FilePathList},
efi::Variable,
store::MemoryStore,
test_utils::assert_var_not_found,
utils, VarReader,
};

Expand Down Expand Up @@ -100,7 +101,7 @@ fn add_set_id() {
);

// verify Boot0000 did not get inserted
assert!(!manager.exists(&Variable::new("Boot0000")).unwrap());
assert_var_not_found(manager, &Variable::new("Boot0000"));

// verify inserted entry is right
let (data, _) = manager.read(&Variable::new("Boot1000")).unwrap();
Expand Down
4 changes: 2 additions & 2 deletions efiboot/src/cli/boot/tests/delete.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use clap::Parser;
use efivar::{efi::Variable, store::MemoryStore, VarReader};
use efivar::{efi::Variable, store::MemoryStore, test_utils::assert_var_not_found};

use crate::{
cli::{boot::tests::standard_setup, Command},
Expand All @@ -20,5 +20,5 @@ fn delete() {
)
);

assert!(!manager.exists(&Variable::new("Boot0001")).unwrap());
assert_var_not_found(manager, &Variable::new("Boot0001"));
}
7 changes: 4 additions & 3 deletions efiboot/src/cli/boot/tests/next.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use clap::Parser;
use efivar::{
efi::{Variable, VariableFlags},
store::MemoryStore,
test_utils::assert_var_not_found,
utils, VarReader, VarWriter,
};

Expand Down Expand Up @@ -40,7 +41,7 @@ fn set_inexistent_next() {
)
);

assert!(!manager.exists(&Variable::new("BootNext")).unwrap());
assert_var_not_found(manager, &Variable::new("BootNext"));
}

#[test]
Expand All @@ -63,7 +64,7 @@ fn unset_next() {
)
);

assert!(!manager.exists(&Variable::new("BootNext")).unwrap());
assert_var_not_found(manager, &Variable::new("BootNext"));
}

#[test]
Expand All @@ -78,7 +79,7 @@ fn unset_inexistent_next() {
)
);

assert!(!manager.exists(&Variable::new("BootNext")).unwrap());
assert_var_not_found(manager, &Variable::new("BootNext"));
}

#[test]
Expand Down
18 changes: 5 additions & 13 deletions efiboot/src/cli/tests.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use core::panic;
use std::{fs::File, io::Write};

use efivar::{
efi::{Variable, VariableFlags},
store::MemoryStore,
Error, VarReader, VarWriter,
test_utils::assert_var_not_found,
VarReader, VarWriter,
};

use crate::{cli::Command, exit_code::ExitCode};
Expand Down Expand Up @@ -103,7 +103,7 @@ fn import_non_existent() {
)
);

assert!(!manager.exists(&Variable::new("MyVariable")).unwrap());
assert_var_not_found(&mut manager, &Variable::new("MyVariable"));
}

#[test]
Expand Down Expand Up @@ -165,11 +165,7 @@ fn export_no_var() {
)
);

if let Error::VarNotFound { var } = manager.read(&Variable::new("MyVariable")).unwrap_err() {
assert_eq!(var, Variable::new("MyVariable"));
} else {
panic!("Reading a non-existent variable should raise VarNotFound");
}
assert_var_not_found(&mut manager, &Variable::new("MyVariable"));
}

#[test]
Expand All @@ -194,11 +190,7 @@ fn delete() {
)
);

if let Error::VarNotFound { var } = manager.read(&Variable::new("MyVariable")).unwrap_err() {
assert_eq!(var, Variable::new("MyVariable"));
} else {
panic!("Reading a non-existent variable should raise VarNotFound");
}
assert_var_not_found(&mut manager, &Variable::new("MyVariable"));
}

#[test]
Expand Down
1 change: 1 addition & 0 deletions efivar/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ mod enumerator;
mod error;
mod reader;
mod sys;
pub mod test_utils;
pub mod utils;
mod writer;

Expand Down
10 changes: 10 additions & 0 deletions efivar/src/test_utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use crate::{efi::Variable, Error, VarReader};

/// asserts that the variable doesn't exist. Also validates the error
pub fn assert_var_not_found(manager: &mut dyn VarReader, var: &Variable) {
if let Error::VarNotFound { var: error_var } = manager.read(var).unwrap_err() {
assert_eq!(&error_var, var);
} else {
panic!("Reading a non-existent variable should raise VarNotFound");
}
}

0 comments on commit 3e9d5c1

Please sign in to comment.