Merge pull request #772 from matthiasbeyer/libimagstore/store-tests-erroring-hooks
libimagstore/store-tests: erroring hooks
This commit is contained in:
commit
dd621a08a6
1 changed files with 46 additions and 20 deletions
|
@ -2418,36 +2418,36 @@ mod store_tests {
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod store_hook_tests {
|
mod store_hook_tests {
|
||||||
|
|
||||||
mod succeeding_hook {
|
mod test_hook {
|
||||||
use hook::Hook;
|
use hook::Hook;
|
||||||
use hook::accessor::HookDataAccessor;
|
use hook::accessor::HookDataAccessor;
|
||||||
use hook::accessor::HookDataAccessorProvider;
|
use hook::accessor::HookDataAccessorProvider;
|
||||||
use hook::position::HookPosition;
|
use hook::position::HookPosition;
|
||||||
|
|
||||||
use self::accessor::SucceedingHookAccessor as DHA;
|
use self::accessor::TestHookAccessor as DHA;
|
||||||
|
|
||||||
use toml::Value;
|
use toml::Value;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct SucceedingHook {
|
pub struct TestHook {
|
||||||
position: HookPosition,
|
position: HookPosition,
|
||||||
accessor: DHA,
|
accessor: DHA,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SucceedingHook {
|
impl TestHook {
|
||||||
|
|
||||||
pub fn new(pos: HookPosition) -> SucceedingHook {
|
pub fn new(pos: HookPosition, succeed: bool, error_aborting: bool) -> TestHook {
|
||||||
SucceedingHook { position: pos.clone(), accessor: DHA::new(pos) }
|
TestHook { position: pos.clone(), accessor: DHA::new(pos, succeed, error_aborting) }
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Hook for SucceedingHook {
|
impl Hook for TestHook {
|
||||||
fn name(&self) -> &'static str { "testhook_succeeding" }
|
fn name(&self) -> &'static str { "testhook_succeeding" }
|
||||||
fn set_config(&mut self, _: &Value) { }
|
fn set_config(&mut self, _: &Value) { }
|
||||||
}
|
}
|
||||||
|
|
||||||
impl HookDataAccessorProvider for SucceedingHook {
|
impl HookDataAccessorProvider for TestHook {
|
||||||
|
|
||||||
fn accessor(&self) -> HookDataAccessor {
|
fn accessor(&self) -> HookDataAccessor {
|
||||||
use hook::position::HookPosition as HP;
|
use hook::position::HookPosition as HP;
|
||||||
|
@ -2476,38 +2476,64 @@ mod store_hook_tests {
|
||||||
use hook::position::HookPosition;
|
use hook::position::HookPosition;
|
||||||
use store::FileLockEntry;
|
use store::FileLockEntry;
|
||||||
use storeid::StoreId;
|
use storeid::StoreId;
|
||||||
|
use hook::error::HookErrorKind as HEK;
|
||||||
|
use hook::error::CustomData;
|
||||||
|
use libimagerror::into::IntoError;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct SucceedingHookAccessor(HookPosition);
|
pub struct TestHookAccessor {
|
||||||
|
pos: HookPosition,
|
||||||
|
succeed: bool,
|
||||||
|
error_aborting: bool
|
||||||
|
}
|
||||||
|
|
||||||
impl SucceedingHookAccessor {
|
impl TestHookAccessor {
|
||||||
|
|
||||||
pub fn new(position: HookPosition) -> SucceedingHookAccessor {
|
pub fn new(position: HookPosition, succeed: bool, error_aborting: bool)
|
||||||
SucceedingHookAccessor(position)
|
-> TestHookAccessor
|
||||||
|
{
|
||||||
|
TestHookAccessor {
|
||||||
|
pos: position,
|
||||||
|
succeed: succeed,
|
||||||
|
error_aborting: error_aborting,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl StoreIdAccessor for SucceedingHookAccessor {
|
fn get_result(succeed: bool, abort: bool) -> HookResult<()> {
|
||||||
|
if succeed {
|
||||||
|
Ok(())
|
||||||
|
} else {
|
||||||
|
if abort {
|
||||||
|
Err(HEK::HookExecutionError.into_error())
|
||||||
|
} else {
|
||||||
|
let custom = CustomData::default().aborting(false);
|
||||||
|
Err(HEK::HookExecutionError.into_error().with_custom_data(custom))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl StoreIdAccessor for TestHookAccessor {
|
||||||
|
|
||||||
fn access(&self, id: &StoreId) -> HookResult<()> {
|
fn access(&self, id: &StoreId) -> HookResult<()> {
|
||||||
Ok(())
|
get_result(self.succeed, self.error_aborting)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl MutableHookDataAccessor for SucceedingHookAccessor {
|
impl MutableHookDataAccessor for TestHookAccessor {
|
||||||
|
|
||||||
fn access_mut(&self, fle: &mut FileLockEntry) -> HookResult<()> {
|
fn access_mut(&self, fle: &mut FileLockEntry) -> HookResult<()> {
|
||||||
Ok(())
|
get_result(self.succeed, self.error_aborting)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl NonMutableHookDataAccessor for SucceedingHookAccessor {
|
impl NonMutableHookDataAccessor for TestHookAccessor {
|
||||||
|
|
||||||
fn access(&self, fle: &FileLockEntry) -> HookResult<()> {
|
fn access(&self, fle: &FileLockEntry) -> HookResult<()> {
|
||||||
Ok(())
|
get_result(self.succeed, self.error_aborting)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -2522,7 +2548,7 @@ mod store_hook_tests {
|
||||||
use storeid::StoreId;
|
use storeid::StoreId;
|
||||||
use store::Store;
|
use store::Store;
|
||||||
|
|
||||||
use self::succeeding_hook::SucceedingHook;
|
use self::test_hook::TestHook;
|
||||||
|
|
||||||
fn get_store_with_config() -> Store {
|
fn get_store_with_config() -> Store {
|
||||||
use toml::Parser;
|
use toml::Parser;
|
||||||
|
@ -2560,7 +2586,7 @@ aspect = "test"
|
||||||
fn test_pre_create() {
|
fn test_pre_create() {
|
||||||
let mut store = get_store_with_config();
|
let mut store = get_store_with_config();
|
||||||
let pos = HP::PreCreate;
|
let pos = HP::PreCreate;
|
||||||
let hook = SucceedingHook::new(pos.clone());
|
let hook = TestHook::new(pos.clone(), true, false);
|
||||||
|
|
||||||
assert!(store.register_hook(pos, "test", Box::new(hook)).map_err(|e| println!("{:?}", e)).is_ok());
|
assert!(store.register_hook(pos, "test", Box::new(hook)).map_err(|e| println!("{:?}", e)).is_ok());
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue