From 886eed3ff4b3f1d8fb0d7278e63e0e4f3b8c49da Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Thu, 22 Sep 2016 08:46:59 +0200 Subject: [PATCH 1/2] Rename: SucceedingHook -> TestHook --- libimagstore/src/store.rs | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/libimagstore/src/store.rs b/libimagstore/src/store.rs index b6a9c8ad..7c8651f2 100644 --- a/libimagstore/src/store.rs +++ b/libimagstore/src/store.rs @@ -2372,36 +2372,36 @@ mod store_tests { #[cfg(test)] mod store_hook_tests { - mod succeeding_hook { + mod test_hook { use hook::Hook; use hook::accessor::HookDataAccessor; use hook::accessor::HookDataAccessorProvider; use hook::position::HookPosition; - use self::accessor::SucceedingHookAccessor as DHA; + use self::accessor::TestHookAccessor as DHA; use toml::Value; #[derive(Debug)] - pub struct SucceedingHook { + pub struct TestHook { position: HookPosition, accessor: DHA, } - impl SucceedingHook { + impl TestHook { - pub fn new(pos: HookPosition) -> SucceedingHook { - SucceedingHook { position: pos.clone(), accessor: DHA::new(pos) } + pub fn new(pos: HookPosition) -> TestHook { + TestHook { position: pos.clone(), accessor: DHA::new(pos) } } } - impl Hook for SucceedingHook { + impl Hook for TestHook { fn name(&self) -> &'static str { "testhook_succeeding" } fn set_config(&mut self, _: &Value) { } } - impl HookDataAccessorProvider for SucceedingHook { + impl HookDataAccessorProvider for TestHook { fn accessor(&self) -> HookDataAccessor { use hook::position::HookPosition as HP; @@ -2432,17 +2432,17 @@ mod store_hook_tests { use storeid::StoreId; #[derive(Debug)] - pub struct SucceedingHookAccessor(HookPosition); + pub struct TestHookAccessor(HookPosition); - impl SucceedingHookAccessor { + impl TestHookAccessor { - pub fn new(position: HookPosition) -> SucceedingHookAccessor { - SucceedingHookAccessor(position) + pub fn new(position: HookPosition) -> TestHookAccessor { + TestHookAccessor(position) } } - impl StoreIdAccessor for SucceedingHookAccessor { + impl StoreIdAccessor for TestHookAccessor { fn access(&self, id: &StoreId) -> HookResult<()> { Ok(()) @@ -2450,7 +2450,7 @@ mod store_hook_tests { } - impl MutableHookDataAccessor for SucceedingHookAccessor { + impl MutableHookDataAccessor for TestHookAccessor { fn access_mut(&self, fle: &mut FileLockEntry) -> HookResult<()> { Ok(()) @@ -2458,7 +2458,7 @@ mod store_hook_tests { } - impl NonMutableHookDataAccessor for SucceedingHookAccessor { + impl NonMutableHookDataAccessor for TestHookAccessor { fn access(&self, fle: &FileLockEntry) -> HookResult<()> { Ok(()) @@ -2476,7 +2476,7 @@ mod store_hook_tests { use storeid::StoreId; use store::Store; - use self::succeeding_hook::SucceedingHook; + use self::test_hook::TestHook; fn get_store_with_config() -> Store { use toml::Parser; @@ -2514,7 +2514,7 @@ aspect = "test" fn test_pre_create() { let mut store = get_store_with_config(); let pos = HP::PreCreate; - let hook = SucceedingHook::new(pos.clone()); + let hook = TestHook::new(pos.clone()); assert!(store.register_hook(pos, "test", Box::new(hook)).map_err(|e| println!("{:?}", e)).is_ok()); From 1526c0b2dcdaae2057a894800d317f093161f066 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Thu, 22 Sep 2016 08:52:58 +0200 Subject: [PATCH 2/2] Make hook configurable whether it succeeds or not --- libimagstore/src/store.rs | 44 +++++++++++++++++++++++++++++++-------- 1 file changed, 35 insertions(+), 9 deletions(-) diff --git a/libimagstore/src/store.rs b/libimagstore/src/store.rs index 7c8651f2..4ac926a2 100644 --- a/libimagstore/src/store.rs +++ b/libimagstore/src/store.rs @@ -2390,8 +2390,8 @@ mod store_hook_tests { impl TestHook { - pub fn new(pos: HookPosition) -> TestHook { - TestHook { position: pos.clone(), accessor: DHA::new(pos) } + pub fn new(pos: HookPosition, succeed: bool, error_aborting: bool) -> TestHook { + TestHook { position: pos.clone(), accessor: DHA::new(pos, succeed, error_aborting) } } } @@ -2430,22 +2430,48 @@ mod store_hook_tests { use hook::position::HookPosition; use store::FileLockEntry; use storeid::StoreId; + use hook::error::HookErrorKind as HEK; + use hook::error::CustomData; + use libimagerror::into::IntoError; #[derive(Debug)] - pub struct TestHookAccessor(HookPosition); + pub struct TestHookAccessor { + pos: HookPosition, + succeed: bool, + error_aborting: bool + } impl TestHookAccessor { - pub fn new(position: HookPosition) -> TestHookAccessor { - TestHookAccessor(position) + pub fn new(position: HookPosition, succeed: bool, error_aborting: bool) + -> TestHookAccessor + { + TestHookAccessor { + pos: position, + succeed: succeed, + error_aborting: error_aborting, + } } } + 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<()> { - Ok(()) + get_result(self.succeed, self.error_aborting) } } @@ -2453,7 +2479,7 @@ mod store_hook_tests { impl MutableHookDataAccessor for TestHookAccessor { fn access_mut(&self, fle: &mut FileLockEntry) -> HookResult<()> { - Ok(()) + get_result(self.succeed, self.error_aborting) } } @@ -2461,7 +2487,7 @@ mod store_hook_tests { impl NonMutableHookDataAccessor for TestHookAccessor { fn access(&self, fle: &FileLockEntry) -> HookResult<()> { - Ok(()) + get_result(self.succeed, self.error_aborting) } } @@ -2514,7 +2540,7 @@ aspect = "test" fn test_pre_create() { let mut store = get_store_with_config(); let pos = HP::PreCreate; - let hook = TestHook::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());