From 7902138f93e397c8a41f0699e970924916d613b0 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Fri, 25 Jun 2021 17:53:05 +0200 Subject: Add conditional dependency support for runtime dependency type Signed-off-by: Matthias Beyer --- src/package/dependency/runtime.rs | 109 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 106 insertions(+), 3 deletions(-) (limited to 'src/package') diff --git a/src/package/dependency/runtime.rs b/src/package/dependency/runtime.rs index 26679e2..40ec93c 100644 --- a/src/package/dependency/runtime.rs +++ b/src/package/dependency/runtime.rs @@ -23,14 +23,17 @@ use crate::package::dependency::condition::Condition; #[serde(untagged)] pub enum Dependency { Simple(String), - Conditional(String, Condition), + Conditional { + name: String, + condition: Condition, + }, } impl AsRef for Dependency { fn as_ref(&self) -> &str { match self { Dependency::Simple(name) => name, - Dependency::Conditional(name, _) => name, + Dependency::Conditional { name, .. } => name, } } } @@ -39,7 +42,7 @@ impl StringEqual for Dependency { fn str_equal(&self, s: &str) -> bool { match self { Dependency::Simple(name) => name == s, - Dependency::Conditional(name, _) => name == s, + Dependency::Conditional { name, .. } => name == s, } } } @@ -59,6 +62,7 @@ impl ParseDependency for Dependency { #[cfg(test)] mod tests { use super::*; + use crate::package::dependency::condition::OneOrMore; #[derive(serde::Deserialize)] #[allow(unused)] @@ -75,5 +79,104 @@ mod tests { other => panic!("Unexpected deserialization to other variant: {:?}", other), } } + + #[test] + fn test_parse_conditional_dependency() { + let s: TestSetting = toml::from_str(r#"setting = { name = "foo", condition = { in_image = "bar"} }"#).expect("Parsing TestSetting failed"); + match s.setting { + Dependency::Conditional { name, condition } => { + assert_eq!(name, "foo", "Expected 'foo', got {}", name); + assert_eq!(*condition.has_env(), None); + assert_eq!(*condition.env_eq(), None); + assert_eq!(condition.in_image().as_ref(), Some(&OneOrMore::::One(String::from("bar")))); + }, + other => panic!("Unexpected deserialization to other variant: {:?}", other), + } + } + + #[test] + fn test_parse_conditional_dependency_pretty() { + let pretty = r#" + [setting] + name = "foo" + [setting.condition] + in_image = "bar" + "#; + + let s: TestSetting = toml::from_str(pretty).expect("Parsing TestSetting failed"); + + match s.setting { + Dependency::Conditional { name, condition } => { + assert_eq!(name, "foo", "Expected 'foo', got {}", name); + assert_eq!(*condition.has_env(), None); + assert_eq!(*condition.env_eq(), None); + assert_eq!(condition.in_image().as_ref(), Some(&OneOrMore::::One(String::from("bar")))); + }, + other => panic!("Unexpected deserialization to other variant: {:?}", other), + } + } + + + #[derive(serde::Serialize, serde::Deserialize)] + #[allow(unused)] + pub struct TestSettings { + settings: Vec, + } + + #[test] + fn test_parse_conditional_dependencies() { + let s: TestSettings = toml::from_str(r#"settings = [{ name = "foo", condition = { in_image = "bar"} }]"#).expect("Parsing TestSetting failed"); + match s.settings.get(0).expect("Has not one dependency") { + Dependency::Conditional { name, condition } => { + assert_eq!(name, "foo", "Expected 'foo', got {}", name); + assert_eq!(*condition.has_env(), None); + assert_eq!(*condition.env_eq(), None); + assert_eq!(condition.in_image().as_ref(), Some(&OneOrMore::::One(String::from("bar")))); + }, + other => panic!("Unexpected deserialization to other variant: {:?}", other), + } + } + + #[test] + fn test_parse_conditional_dependencies_pretty() { + let pretty = r#" + [[settings]] + name = "foo" + condition = { in_image = "bar" } + "#; + + let s: TestSettings = toml::from_str(pretty).expect("Parsing TestSetting failed"); + + match s.settings.get(0).expect("Has not one dependency") { + Dependency::Conditional { name, condition } => { + assert_eq!(name, "foo", "Expected 'foo', got {}", name); + assert_eq!(*condition.has_env(), None); + assert_eq!(*condition.env_eq(), None); + assert_eq!(condition.in_image().as_ref(), Some(&OneOrMore::::One(String::from("bar")))); + }, + other => panic!("Unexpected deserialization to other variant: {:?}", other), + } + } + + #[test] + fn test_parse_conditional_dependencies_pretty_2() { + let pretty = r#" + [[settings]] + name = "foo" + condition.in_image = "bar" + "#; + + let s: TestSettings = toml::from_str(pretty).expect("Parsing TestSetting failed"); + + match s.settings.get(0).expect("Has not one dependency") { + Dependency::Conditional { name, condition } => { + assert_eq!(name, "foo", "Expected 'foo', got {}", name); + assert_eq!(*condition.has_env(), None); + assert_eq!(*condition.env_eq(), None); + assert_eq!(condition.in_image().as_ref(), Some(&OneOrMore::::One(String::from("bar")))); + }, + other => panic!("Unexpected deserialization to other variant: {:?}", other), + } + } } -- cgit v1.2.3