Robustness / testing work
This commit is contained in:
parent
f280f71095
commit
8732696dea
3 changed files with 61 additions and 27 deletions
|
@ -117,40 +117,41 @@ mod test {
|
|||
use crate::{
|
||||
get_index_info,
|
||||
parser::{index::IndexInfo, key::Key, xattr::parse_xattr},
|
||||
set_key,
|
||||
};
|
||||
|
||||
use super::idx;
|
||||
|
||||
#[test]
|
||||
fn test_idx() {
|
||||
let data_dir = TempDir::new("ghee-test-idx:data").unwrap().into_path();
|
||||
let dir1 = TempDir::new("ghee-test-idx:1").unwrap().into_path();
|
||||
|
||||
let index_dir = TempDir::new("ghee-test-idx:idx").unwrap().into_path();
|
||||
let dir2 = TempDir::new("ghee-test-idx:2").unwrap().into_path();
|
||||
|
||||
let key = Key::new(vec![parse_xattr(b"test").unwrap().1]);
|
||||
let key1 = Key::new(vec![parse_xattr(b"test1").unwrap().1]);
|
||||
|
||||
let key2 = Key::new(vec![parse_xattr(b"test2").unwrap().1]);
|
||||
|
||||
set_key(&dir1, &key1).unwrap();
|
||||
|
||||
//TODO Initialize the data dir with data
|
||||
|
||||
idx(&data_dir, Some(&index_dir), &key, &false);
|
||||
idx(&dir1, Some(&dir2), &key2, &false);
|
||||
|
||||
let data_idx_info: IndexInfo = get_index_info(&data_dir).unwrap();
|
||||
let info1: IndexInfo = get_index_info(&dir1).unwrap();
|
||||
|
||||
assert!(data_idx_info
|
||||
.related_indices
|
||||
.contains_key(&String::from("test")));
|
||||
assert!(info1.related_indices.contains_key(&String::from("test2")));
|
||||
assert_eq!(
|
||||
data_idx_info.related_indices.get(&String::from("test")),
|
||||
Some(&index_dir)
|
||||
info1.related_indices.get(&String::from("test2")),
|
||||
Some(&dir2)
|
||||
);
|
||||
|
||||
let index_idx_info = get_index_info(&index_dir).unwrap();
|
||||
let info2 = get_index_info(&dir2).unwrap();
|
||||
|
||||
assert!(index_idx_info
|
||||
.related_indices
|
||||
.contains_key(&String::from("test")));
|
||||
assert!(info2.related_indices.contains_key(&String::from("test1")));
|
||||
assert_eq!(
|
||||
index_idx_info.related_indices.get(&String::from("test")),
|
||||
Some(&data_dir)
|
||||
info2.related_indices.get(&String::from("test1")),
|
||||
Some(&dir1)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -20,7 +20,7 @@ pub fn init(dir: &PathBuf, key: &Key, verbose: &bool) -> Result<()> {
|
|||
|
||||
let prior_key = get_key(dir)?;
|
||||
|
||||
if prior_key.is_empty() {
|
||||
if prior_key.is_none() {
|
||||
set_key(dir, &key)?;
|
||||
if *verbose {
|
||||
print!("Initialized table {} with key:", dir.display());
|
||||
|
@ -56,7 +56,7 @@ mod test {
|
|||
)
|
||||
.unwrap();
|
||||
|
||||
let key = get_key(dir).unwrap();
|
||||
let key = get_key(dir).unwrap().unwrap();
|
||||
|
||||
assert_eq!(key.len(), 1);
|
||||
|
||||
|
|
51
src/lib.rs
51
src/lib.rs
|
@ -55,10 +55,10 @@ pub enum GetKeyErr {
|
|||
CantParseKey(String),
|
||||
}
|
||||
|
||||
/// Get the key of a table; empty if not set
|
||||
/// Get the key of a table, if any
|
||||
///
|
||||
/// See `XATTR_KEY`
|
||||
pub fn get_key<P: AsRef<Path>>(path: P) -> Result<Key> {
|
||||
pub fn get_key<P: AsRef<Path>>(path: P) -> Result<Option<Key>> {
|
||||
let path = path.as_ref();
|
||||
|
||||
let value = xattr::get(path, XATTR_KEY.to_osstring())
|
||||
|
@ -66,7 +66,7 @@ pub fn get_key<P: AsRef<Path>>(path: P) -> Result<Key> {
|
|||
.unwrap_or_default();
|
||||
|
||||
if value.is_empty() {
|
||||
return Ok(Key::new(Vec::new()));
|
||||
return Ok(None);
|
||||
}
|
||||
|
||||
eprintln!("value: {:?}", String::from_utf8(value.clone()));
|
||||
|
@ -74,7 +74,7 @@ pub fn get_key<P: AsRef<Path>>(path: P) -> Result<Key> {
|
|||
let key: Key =
|
||||
serde_json::from_slice(value.as_slice()).map_err(GetKeyErr::CantDeserializeJson)?;
|
||||
|
||||
Ok(key)
|
||||
Ok(Some(key))
|
||||
}
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
|
@ -123,12 +123,18 @@ pub fn set_index_info(dir: &PathBuf, idx: IndexInfo) -> Result<()> {
|
|||
)
|
||||
}
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
pub enum IndexListPushErr {
|
||||
#[error("No key found")]
|
||||
NoKeyFound,
|
||||
}
|
||||
|
||||
/// Adds `index_dir` as an alternate index of the data in `dir`
|
||||
pub fn index_list_push(dir: &PathBuf, index_dir: &PathBuf) -> Result<()> {
|
||||
let mut idx = get_index_info(dir)?;
|
||||
|
||||
// The key by which `index_dir` is indexed
|
||||
let key = get_key(index_dir)?;
|
||||
let key = get_key(index_dir)?.ok_or(IndexListPushErr::NoKeyFound)?;
|
||||
|
||||
let key_str = key.to_minimal_string();
|
||||
|
||||
|
@ -142,19 +148,46 @@ mod test {
|
|||
use tempdir::TempDir;
|
||||
|
||||
use crate::{
|
||||
get_key,
|
||||
get_index_info, get_key, index_list_push,
|
||||
parser::{
|
||||
key::Key,
|
||||
xattr::{Namespace, Xattr},
|
||||
xattr::{parse_xattr, Namespace, Xattr},
|
||||
},
|
||||
set_key,
|
||||
};
|
||||
|
||||
#[test]
|
||||
fn test_index_list_push() {
|
||||
let dir1 = TempDir::new("ghee-test-index-list-push:1")
|
||||
.unwrap()
|
||||
.into_path();
|
||||
|
||||
let key1 = Key::new(vec![parse_xattr(b"name").unwrap().1]);
|
||||
set_key(&dir1, &key1).unwrap();
|
||||
|
||||
let dir2 = TempDir::new("ghee-test-index-list-push:2")
|
||||
.unwrap()
|
||||
.into_path();
|
||||
|
||||
let key2 = Key::new(vec![parse_xattr(b"state").unwrap().1]);
|
||||
set_key(&dir2, &key2).unwrap();
|
||||
|
||||
let info1 = get_index_info(&dir1).unwrap();
|
||||
|
||||
assert!(info1.related_indices.is_empty());
|
||||
|
||||
index_list_push(&dir1, &dir2).unwrap();
|
||||
|
||||
let info2 = get_index_info(&dir1).unwrap();
|
||||
|
||||
assert_eq!(info2.related_indices.len(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_key_roundtrip() {
|
||||
let dir = TempDir::new("ghee-test-key-roundtrip").unwrap().into_path();
|
||||
|
||||
assert_eq!(get_key(&dir).unwrap(), Key::new(Vec::new()));
|
||||
assert_eq!(get_key(&dir).unwrap(), None);
|
||||
|
||||
let key_component = Xattr::new(Namespace::User, "pizza");
|
||||
|
||||
|
@ -162,6 +195,6 @@ mod test {
|
|||
|
||||
set_key(&dir, &key).unwrap();
|
||||
|
||||
assert_eq!(get_key(&dir).unwrap(), key);
|
||||
assert_eq!(get_key(&dir).unwrap().unwrap(), key);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue