summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias Beyer <matthias.beyer@atos.net>2021-06-25 17:53:05 +0200
committerMatthias Beyer <matthias.beyer@atos.net>2021-09-16 12:32:16 +0200
commit7902138f93e397c8a41f0699e970924916d613b0 (patch)
treef308dbb07e50e5b1c2a80d83b58a10c3da5070bb
parente3be9e5300f96f35fcdf8b28984cba6b06327b15 (diff)
Add conditional dependency support for runtime dependency type
Signed-off-by: Matthias Beyer <matthias.beyer@atos.net>
-rw-r--r--src/package/dependency/runtime.rs109
1 files changed, 106 insertions, 3 deletions
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<str> 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::<String>::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::<String>::One(String::from("bar"))));
+ },
+ other => panic!("Unexpected deserialization to other variant: {:?}", other),
+ }
+ }
+
+
+ #[derive(serde::Serialize, serde::Deserialize)]
+ #[allow(unused)]
+ pub struct TestSettings {
+ settings: Vec<Dependency>,
+ }
+
+ #[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::<String>::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::<String>::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::<String>::One(String::from("bar"))));
+ },
+ other => panic!("Unexpected deserialization to other variant: {:?}", other),
+ }
+ }
}