diff --git a/libimagstore/Cargo.toml b/libimagstore/Cargo.toml index f2e12cc0..99ee6c1a 100644 --- a/libimagstore/Cargo.toml +++ b/libimagstore/Cargo.toml @@ -24,6 +24,7 @@ toml = "^0.4" version = "2.0.1" crossbeam = "0.2.*" walkdir = "1.0.*" +itertools = "0.6.*" [dependencies.libimagerror] path = "../libimagerror" diff --git a/libimagstore/src/configuration.rs b/libimagstore/src/configuration.rs index fd73674b..06e0d3c3 100644 --- a/libimagstore/src/configuration.rs +++ b/libimagstore/src/configuration.rs @@ -329,6 +329,7 @@ impl AspectConfig { } fn get_aspect_names_for_aspect_position(config_name: &'static str, value: &Option) -> Vec { + use itertools::Itertools; let mut v = vec![]; match *value { @@ -348,7 +349,350 @@ fn get_aspect_names_for_aspect_position(config_name: &'static str, value: &Optio None => warn!("No store configuration, cannot get '{}'", config_name), _ => warn!("Configuration is not a table"), } - v + + v.into_iter().unique().collect() } +#[cfg(test)] +mod tests { + use toml::de::from_str as toml_from_str; + use configuration::*; + + #[test] + fn test_implicit_store_create_allowed_no_toml() { + assert!(!config_implicit_store_create_allowed(None)); + } + + #[test] + fn test_implicit_store_create_allowed_toml_empty() { + let config = toml_from_str("").unwrap(); + assert!(!config_implicit_store_create_allowed(Some(config).as_ref())); + } + + #[test] + fn test_implicit_store_create_allowed_toml_false() { + let config = toml_from_str(r#" + implicit-create = false + "#).unwrap(); + + assert!(!config_implicit_store_create_allowed(Some(config).as_ref())); + } + + #[test] + fn test_implicit_store_create_allowed_toml_true() { + let config = toml_from_str(r#" + implicit-create = true + "#).unwrap(); + + assert!(config_implicit_store_create_allowed(Some(config).as_ref())); + } + + #[test] + fn test_get_store_unload_aspect_names_not_existent() { + let config = toml_from_str("").unwrap(); + let names = get_store_unload_aspect_names(&Some(config)); + assert!(names.is_empty()); + } + + #[test] + fn test_get_store_unload_aspect_names_empty() { + let config = toml_from_str(r#" + store-unload-hook-aspects = [ ] + "#).unwrap(); + let names = get_store_unload_aspect_names(&Some(config)); + assert!(names.is_empty()); + } + + #[test] + fn test_get_store_unload_aspect_names_one_elem() { + let config = toml_from_str(r#" + store-unload-hook-aspects = [ "example" ] + "#).unwrap(); + let names = get_store_unload_aspect_names(&Some(config)); + assert_eq!(1, names.len()); + assert_eq!("example", names.iter().next().unwrap()); + } + + #[test] + fn test_get_pre_create_aspect_names_not_existent() { + let config = toml_from_str("").unwrap(); + assert!(get_pre_create_aspect_names(&Some(config)).is_empty()); + } + + #[test] + fn test_get_pre_create_aspect_names_empty() { + let config = toml_from_str(r#" + pre-create-hook-aspects = [ ] + "#).unwrap(); + let names = get_pre_create_aspect_names(&Some(config)); + assert!(names.is_empty()); + } + + #[test] + fn test_get_pre_create_aspect_names_one_elem() { + let config = toml_from_str(r#" + pre-create-hook-aspects = [ "example" ] + "#).unwrap(); + let names = get_pre_create_aspect_names(&Some(config)); + assert_eq!(1, names.len()); + assert_eq!("example", names.iter().next().unwrap()); + } + + #[test] + fn test_get_post_create_aspect_names_not_existent() { + let config = toml_from_str("").unwrap(); + assert!(get_post_create_aspect_names(&Some(config)).is_empty()); + } + + #[test] + fn test_get_post_create_aspect_names_empty() { + let config = toml_from_str(r#" + post-create-hook-aspects = [ ] + "#).unwrap(); + let names = get_post_create_aspect_names(&Some(config)); + assert!(names.is_empty()); + } + + #[test] + fn test_get_post_create_aspect_names_one_elem() { + let config = toml_from_str(r#" + post-create-hook-aspects = [ "example" ] + "#).unwrap(); + let names = get_post_create_aspect_names(&Some(config)); + assert_eq!(1, names.len()); + assert_eq!("example", names.iter().next().unwrap()); + } + + #[test] + fn test_get_pre_retrieve_aspect_names_not_existent() { + let config = toml_from_str("").unwrap(); + assert!(get_pre_retrieve_aspect_names(&Some(config)).is_empty()); + } + + #[test] + fn test_get_pre_retrieve_aspect_names_empty() { + let config = toml_from_str(r#" + pre-retrieve-hook-aspects = [ ] + "#).unwrap(); + let names = get_pre_retrieve_aspect_names(&Some(config)); + assert!(names.is_empty()); + } + + #[test] + fn test_get_pre_retrieve_aspect_names_one_elem() { + let config = toml_from_str(r#" + pre-retrieve-hook-aspects = [ "example" ] + "#).unwrap(); + let names = get_pre_retrieve_aspect_names(&Some(config)); + assert_eq!(1, names.len()); + assert_eq!("example", names.iter().next().unwrap()); + } + + #[test] + fn test_get_post_retrieve_aspect_names_not_existent() { + let config = toml_from_str("").unwrap(); + assert!(get_post_retrieve_aspect_names(&Some(config)).is_empty()); + } + + #[test] + fn test_get_post_retrieve_aspect_names_empty() { + let config = toml_from_str(r#" + post-retrieve-hook-aspects = [ ] + "#).unwrap(); + let names = get_post_retrieve_aspect_names(&Some(config)); + assert!(names.is_empty()); + } + + #[test] + fn test_get_post_retrieve_aspect_names_one_elem() { + let config = toml_from_str(r#" + post-retrieve-hook-aspects = [ "example" ] + "#).unwrap(); + let names = get_post_retrieve_aspect_names(&Some(config)); + assert_eq!(1, names.len()); + assert_eq!("example", names.iter().next().unwrap()); + } + + #[test] + fn test_get_pre_update_aspect_names_not_existent() { + let config = toml_from_str("").unwrap(); + assert!(get_pre_update_aspect_names(&Some(config)).is_empty()); + } + + #[test] + fn test_get_pre_update_aspect_names_empty() { + let config = toml_from_str(r#" + pre-update-hook-aspects = [ ] + "#).unwrap(); + let names = get_pre_update_aspect_names(&Some(config)); + assert!(names.is_empty()); + } + + #[test] + fn test_get_pre_update_aspect_names_one_elem() { + let config = toml_from_str(r#" + pre-update-hook-aspects = [ "example" ] + "#).unwrap(); + let names = get_pre_update_aspect_names(&Some(config)); + assert_eq!(1, names.len()); + assert_eq!("example", names.iter().next().unwrap()); + } + + #[test] + fn test_get_post_update_aspect_names_not_existent() { + let config = toml_from_str("").unwrap(); + assert!(get_post_update_aspect_names(&Some(config)).is_empty()); + } + + #[test] + fn test_get_post_update_aspect_names_empty() { + let config = toml_from_str(r#" + post-update-hook-aspects = [ ] + "#).unwrap(); + let names = get_post_update_aspect_names(&Some(config)); + assert!(names.is_empty()); + } + + #[test] + fn test_get_post_update_aspect_names_one_elem() { + let config = toml_from_str(r#" + post-update-hook-aspects = [ "example" ] + "#).unwrap(); + let names = get_post_update_aspect_names(&Some(config)); + assert_eq!(1, names.len()); + assert_eq!("example", names.iter().next().unwrap()); + } + + #[test] + fn test_get_pre_delete_aspect_names_not_existent() { + let config = toml_from_str("").unwrap(); + assert!(get_pre_delete_aspect_names(&Some(config)).is_empty()); + } + + #[test] + fn test_get_pre_delete_aspect_names_empty() { + let config = toml_from_str(r#" + pre-delete-hook-aspects = [ ] + "#).unwrap(); + let names = get_pre_delete_aspect_names(&Some(config)); + assert!(names.is_empty()); + } + + #[test] + fn test_get_pre_delete_aspect_names_one_elem() { + let config = toml_from_str(r#" + pre-delete-hook-aspects = [ "example" ] + "#).unwrap(); + let names = get_pre_delete_aspect_names(&Some(config)); + assert_eq!(1, names.len()); + assert_eq!("example", names.iter().next().unwrap()); + } + + #[test] + fn test_get_post_delete_aspect_names_not_existent() { + let config = toml_from_str("").unwrap(); + assert!(get_post_delete_aspect_names(&Some(config)).is_empty()); + } + + #[test] + fn test_get_post_delete_aspect_names_empty() { + let config = toml_from_str(r#" + post-delete-hook-aspects = [ ] + "#).unwrap(); + let names = get_post_delete_aspect_names(&Some(config)); + assert!(names.is_empty()); + } + + #[test] + fn test_get_post_delete_aspect_names_one_elem() { + let config = toml_from_str(r#" + post-delete-hook-aspects = [ "example" ] + "#).unwrap(); + let names = get_post_delete_aspect_names(&Some(config)); + assert_eq!(1, names.len()); + assert_eq!("example", names.iter().next().unwrap()); + } + + #[test] + fn test_get_pre_move_aspect_names_not_existent() { + let config = toml_from_str("").unwrap(); + assert!(get_pre_move_aspect_names(&Some(config)).is_empty()); + } + + #[test] + fn test_get_pre_move_aspect_names_empty() { + let config = toml_from_str(r#" + pre-move-hook-aspects = [ ] + "#).unwrap(); + let names = get_pre_move_aspect_names(&Some(config)); + assert!(names.is_empty()); + } + + #[test] + fn test_get_pre_move_aspect_names_one_elem() { + let config = toml_from_str(r#" + pre-move-hook-aspects = [ "example" ] + "#).unwrap(); + let names = get_pre_move_aspect_names(&Some(config)); + assert_eq!(1, names.len()); + assert_eq!("example", names.iter().next().unwrap()); + } + + #[test] + fn test_get_post_move_aspect_names_not_existent() { + let config = toml_from_str("").unwrap(); + assert!(get_post_move_aspect_names(&Some(config)).is_empty()); + } + + #[test] + fn test_get_post_move_aspect_names_empty() { + let config = toml_from_str(r#" + post-move-hook-aspects = [ ] + "#).unwrap(); + let names = get_post_move_aspect_names(&Some(config)); + assert!(names.is_empty()); + } + + #[test] + fn test_get_post_move_aspect_names_one_elem() { + let config = toml_from_str(r#" + post-move-hook-aspects = [ "example" ] + "#).unwrap(); + let names = get_post_move_aspect_names(&Some(config)); + assert_eq!(1, names.len()); + assert_eq!("example", names.iter().next().unwrap()); + } + + #[test] + fn test_get_aspect_names_for_aspect_position_arbitrary_empty() { + let config = toml_from_str(r#" + test-key = [ ] + "#).unwrap(); + let names = get_aspect_names_for_aspect_position("test-key", &Some(config)); + assert!(names.is_empty()); + } + + #[test] + fn test_get_aspect_names_for_aspect_position_arbitrary_one() { + let config = toml_from_str(r#" + test-key = [ "test-value" ] + "#).unwrap(); + let names = get_aspect_names_for_aspect_position("test-key", &Some(config)); + assert_eq!(1, names.len()); + assert_eq!("test-value", names.iter().next().unwrap()); + } + + #[test] + fn test_get_aspect_names_for_aspect_position_arbitrary_duplicated() { + let config = toml_from_str(r#" + test-key = [ "test-value", "test-value" ] + "#).unwrap(); + let names = get_aspect_names_for_aspect_position("test-key", &Some(config)); + assert_eq!(1, names.len()); + let mut iter = names.iter(); + assert_eq!("test-value", iter.next().unwrap()); + assert!(iter.next().is_none()); + } + +} diff --git a/libimagstore/src/lib.rs b/libimagstore/src/lib.rs index 56c3a742..835704a3 100644 --- a/libimagstore/src/lib.rs +++ b/libimagstore/src/lib.rs @@ -42,6 +42,7 @@ extern crate toml; extern crate semver; extern crate crossbeam; extern crate walkdir; +extern crate itertools; #[macro_use] extern crate libimagerror; #[macro_use] extern crate libimagutil;