Added initialization failure.

This commit is contained in:
Jeff Baskin 2023-03-05 11:15:32 -05:00
parent 8fada737ac
commit c477a92945
1 changed files with 32 additions and 4 deletions

View File

@ -184,15 +184,21 @@ impl Entry {
struct Cache;
impl Cache {
async fn new<P>(dir: P) -> Self
async fn new<P>(dir: P) -> Result<Self, DBError>
where
P: Into<PathBuf>,
{
let pathbuf = dir.into();
let entry = pathbuf.as_path().join(ENTRY);
let store = DataType::new("store").unwrap();
Entry::new(entry, store).await.unwrap();
Self {}
match Entry::new(entry, store).await {
Ok(_) => Ok(Self {}),
Err(err) => {
let mut error = DBError::new("initialization failure");
error.add_source(err);
Err(error)
}
}
}
}
@ -580,12 +586,13 @@ mod entry {
#[cfg(test)]
mod cache {
use super::*;
use std::error::Error;
use tempfile::tempdir;
#[async_std::test]
async fn create() {
let dir = tempdir().unwrap();
Cache::new(dir.path().to_str().unwrap()).await;
Cache::new(dir.path().to_str().unwrap()).await.unwrap();
let epoint = dir.path().join(ENTRY);
assert!(
epoint.is_file(),
@ -598,4 +605,25 @@ mod cache {
Vec::<String>::new()
);
}
#[async_std::test]
async fn entry_failure() {
let dir = tempdir().unwrap();
let path = dir.path().join("bad").join("path");
match Cache::new(path).await {
Ok(_) => assert!(false, "Should have produced an error."),
Err(err) => {
assert_eq!(err.to_string(), "initialization failure");
assert!(err.source().is_some(), "Error should have a source.");
assert!(
err.source()
.unwrap()
.to_string()
.contains("failed to write"),
"Source Error Message: {}",
err.source().unwrap().to_string()
);
}
}
}
}