Ensuring that tests won't step on real key files

This commit is contained in:
Dave Hrycyszyn
2024-06-06 14:48:14 +01:00
parent 16fe760e10
commit db0a6a0725
2 changed files with 12 additions and 9 deletions

View File

@@ -8,11 +8,10 @@ use pem::Pem;
use bft_json_crdt::keypair::{make_keypair, KeyPair}; use bft_json_crdt::keypair::{make_keypair, KeyPair};
const KEY_FILE: &str = ".side/node/keys.pem"; const KEY_FILE: &str = "side/node/keys.pem";
pub(crate) fn init() -> Result<(), std::io::Error> { pub(crate) fn init(home: String) -> Result<(), std::io::Error> {
println!("Initializing Side Node"); println!("Initializing Side Node at {home}");
let home = std::env::var("HOME").expect("couldn't read home directory env variable");
let key_path = key_path(home); let key_path = key_path(home);
ensure_directory_exists(&key_path)?; ensure_directory_exists(&key_path)?;
@@ -59,15 +58,18 @@ mod tests {
#[test] #[test]
fn creates_side_node_directory() { fn creates_side_node_directory() {
let node_dir = Path::new(KEY_FILE).parent().unwrap().to_str().unwrap(); let test_home = format!("/tmp/{KEY_FILE}");
let _ = init(); let node_dir = Path::new(&test_home).parent().unwrap().to_str().unwrap();
let _ = init(test_home.clone());
assert!(std::path::Path::new(node_dir).exists()); assert!(std::path::Path::new(node_dir).exists());
} }
#[test] #[test]
fn creates_key_file() { fn creates_key_file() {
let _ = init(); let test_home = format!("/tmp/{KEY_FILE}");
assert!(std::path::Path::new(KEY_FILE).exists()); let node_dir = Path::new(&test_home).parent().unwrap().to_str().unwrap();
let _ = init(test_home.clone());
assert!(std::path::Path::new(node_dir).exists());
} }
// #[test] // #[test]

View File

@@ -11,7 +11,8 @@ async fn main() {
match &args.command { match &args.command {
Some(Commands::Init {}) => { Some(Commands::Init {}) => {
let _ = init::init(); let home = std::env::var("HOME").expect("couldn't read home directory env variable");
let _ = init::init(home);
} }
Some(Commands::Run {}) => { Some(Commands::Run {}) => {
let (mut bft_crdt, keys) = list_transaction_crdt::new(); let (mut bft_crdt, keys) = list_transaction_crdt::new();