From e3be9e5300f96f35fcdf8b28984cba6b06327b15 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 build dependency type Signed-off-by: Matthias Beyer --- src/package/dependency/build.rs | 111 ++++++++++++++++++++++++++++++++++-- src/package/dependency/condition.rs | 20 ++++++- 2 files changed, 124 insertions(+), 7 deletions(-) (limited to 'src/package') diff --git a/src/package/dependency/build.rs b/src/package/dependency/build.rs index 42c0763..4d51335 100644 --- a/src/package/dependency/build.rs +++ b/src/package/dependency/build.rs @@ -23,14 +23,17 @@ use crate::package::dependency::condition::Condition; #[serde(untagged)] pub enum BuildDependency { Simple(String), - Conditional(String, Condition), + Conditional { + name: String, + condition: Condition, + }, } impl AsRef for BuildDependency { fn as_ref(&self) -> &str { match self { BuildDependency::Simple(name) => name, - BuildDependency::Conditional(name, _) => name, + BuildDependency::Conditional { name, .. } => name, } } } @@ -39,7 +42,7 @@ impl StringEqual for BuildDependency { fn str_equal(&self, s: &str) -> bool { match self { BuildDependency::Simple(name) => name == s, - BuildDependency::Conditional(name, _) => name == s, + BuildDependency::Conditional { name, .. } => name == s, } } } @@ -53,8 +56,9 @@ impl ParseDependency for BuildDependency { #[cfg(test)] mod tests { use super::*; + use crate::package::dependency::condition::OneOrMore; - #[derive(serde::Deserialize)] + #[derive(serde::Serialize, serde::Deserialize)] #[allow(unused)] pub struct TestSetting { setting: BuildDependency, @@ -68,5 +72,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 { + BuildDependency::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 { + BuildDependency::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") { + BuildDependency::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") { + BuildDependency::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") { + BuildDependency::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), + } + } } diff --git a/src/package/dependency/condition.rs b/src/package/dependency/condition.rs index 2f46dfe..a3eeaaf 100644 --- a/src/package/dependency/condition.rs +++ b/src/package/dependency/condition.rs @@ -29,15 +29,15 @@ use crate::util::EnvironmentVariableName; pub struct Condition { #[serde(rename = "has_env", skip_serializing_if = "Option::is_none")] #[getset(get = "pub")] - has_env: Option>, + pub(super) has_env: Option>, #[serde(rename = "env_eq", skip_serializing_if = "Option::is_none")] #[getset(get = "pub")] - env_eq: Option>, + pub(super) env_eq: Option>, #[serde(rename = "in_image", skip_serializing_if = "Option::is_none")] #[getset(get = "pub")] - in_image: Option>, + pub(super) in_image: Option>, } /// Manual implementation of PartialOrd for Condition @@ -120,6 +120,20 @@ impl Into> for OneOrMore { } } +#[cfg(test)] +impl From> for OneOrMore { + fn from(v: Vec) -> Self { + OneOrMore::More(v) + } +} + +#[cfg(test)] +impl From for OneOrMore { + fn from(s: String) -> Self { + OneOrMore::One(s) + } +} + #[cfg(test)] mod tests { use super::*; -- cgit v1.2.3