From a5a7af57b50c2c9ddaa5b278417f82e61deddc99 Mon Sep 17 00:00:00 2001 From: Alexis Sellier Date: Mon, 29 Aug 2022 16:32:58 +0200 Subject: [PATCH] node: Add `Storage::open` Signed-off-by: Alexis Sellier --- node/src/storage/git.rs | 14 ++++++++++---- node/src/test/fixtures.rs | 2 +- node/src/test/tests.rs | 2 ++ 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/node/src/storage/git.rs b/node/src/storage/git.rs index 0ff92554..2d615b00 100644 --- a/node/src/storage/git.rs +++ b/node/src/storage/git.rs @@ -1,5 +1,5 @@ use std::path::{Path, PathBuf}; -use std::{fmt, fs}; +use std::{fmt, fs, io}; use git_ref_format::refspec; use git_url::Url; @@ -59,10 +59,16 @@ impl WriteStorage for Storage { } impl Storage { - pub fn new>(path: P) -> Self { + pub fn open>(path: P) -> Result { let path = path.as_ref().to_path_buf(); - Self { path } + match fs::create_dir_all(&path) { + Err(err) if err.kind() == io::ErrorKind::AlreadyExists => {} + Err(err) => return Err(err), + Ok(()) => {} + } + + Ok(Self { path }) } pub fn path(&self) -> &Path { @@ -231,7 +237,7 @@ mod tests { fn test_fetch() { let tmp = tempfile::tempdir().unwrap(); let alice = fixtures::storage(tmp.path().join("alice")); - let bob = Storage::new(tmp.path().join("bob")); + let bob = Storage::open(tmp.path().join("bob")).unwrap(); let inventory = alice.inventory().unwrap(); let proj = inventory.first().unwrap(); let remotes = alice.repository(proj).unwrap().remotes().unwrap(); diff --git a/node/src/test/fixtures.rs b/node/src/test/fixtures.rs index cb24bab0..69092e35 100644 --- a/node/src/test/fixtures.rs +++ b/node/src/test/fixtures.rs @@ -7,7 +7,7 @@ use crate::test::arbitrary; pub fn storage>(path: P) -> Storage { let path = path.as_ref(); - let storage = Storage::new(path); + let storage = Storage::open(path).unwrap(); let proj_ids = arbitrary::set::(3..5); let user_ids = arbitrary::set::(1..3); diff --git a/node/src/test/tests.rs b/node/src/test/tests.rs index 75a66ed6..f34a7d63 100644 --- a/node/src/test/tests.rs +++ b/node/src/test/tests.rs @@ -8,7 +8,9 @@ use nakamoto_net::Protocol as _; use crate::collections::{HashMap, HashSet}; use crate::protocol::*; +use crate::storage::git::Storage; use crate::storage::ReadStorage; +use crate::test::fixtures; #[allow(unused)] use crate::test::logger; use crate::test::peer::Peer;