From 991ea1d86cf76bc87865c2e0922aefc01ee9cc49 Mon Sep 17 00:00:00 2001 From: Till Wegmueller Date: Tue, 20 Apr 2021 19:57:47 -0300 Subject: [PATCH] Fix tests with pest parsing --- libips/src/actions/manifest.pest | 2 +- libips/src/actions/mod.rs | 104 ++++++++++++++++++++++--------- libips/src/lib.rs | 22 +++---- 3 files changed, 83 insertions(+), 45 deletions(-) diff --git a/libips/src/actions/manifest.pest b/libips/src/actions/manifest.pest index 0d1ea6c..7483955 100644 --- a/libips/src/actions/manifest.pest +++ b/libips/src/actions/manifest.pest @@ -61,7 +61,7 @@ transform_character = { transform_action = @{ transform_character*} transform = {"" ~ " "? ~ transform_action ~ ">" } -property_name = @{ ( ASCII_ALPHANUMERIC | "." | "_" )+ } +property_name = @{ ( ASCII_ALPHANUMERIC | "." | "_" | "-" )+ } property_value = @{ ( ASCII_ALPHANUMERIC | "/" | "," | "." | "_" | "-" | "%" | "*" | "@" | "(" | ")" | "$" | ":" | "+" )+ | quoted_string } payload = @{ payload_character* } property = { " "? ~ property_name ~ "=" ~ property_value } diff --git a/libips/src/actions/mod.rs b/libips/src/actions/mod.rs index 960442d..085ba2b 100644 --- a/libips/src/actions/mod.rs +++ b/libips/src/actions/mod.rs @@ -72,17 +72,26 @@ pub struct Dir { impl From for Dir { fn from(act: Action) -> Self { let mut dir = Dir::default(); - for prop in act.properties { + let mut props = act.properties; + if !act.payload_string.is_empty() { + let p_str = split_property(act.payload_string); + props.push(Property{ + key: p_str.0, + value: p_str.1.replace("\"", ""), + }) + } + for prop in props { + let cleaned_value = prop.value.replace("\"", ""); match prop.key.as_str() { - "path" => dir.path = prop.value, - "owner" => dir.owner = prop.value, - "group" => dir.group = prop.value, - "mode" => dir.mode = prop.value, - "revert-tag" => dir.revert_tag = prop.value, - "salvage-from" => dir.salvage_from = prop.value, + "path" => dir.path = cleaned_value, + "owner" => dir.owner = cleaned_value, + "group" => dir.group = cleaned_value, + "mode" => dir.mode = cleaned_value, + "revert-tag" => dir.revert_tag = cleaned_value, + "salvage-from" => dir.salvage_from = cleaned_value, _ => { if is_facet(prop.key.clone()) { - dir.add_facet(Facet::from_key_value(prop.key, prop.value)); + dir.add_facet(Facet::from_key_value(prop.key, cleaned_value)); } } } @@ -142,39 +151,44 @@ impl From for File { if act.payload_string.contains("/") { file.properties.push(Property{ key: "original-path".to_string(), - value: act.payload_string + value: act.payload_string.replace("\"", "") }); } else { p.primary_identifier = Digest::from_str(&act.payload_string).unwrap(); } } for prop in act.properties { + let cleaned_value = prop.value.replace("\"", ""); match prop.key.as_str() { - "path" => file.path = prop.value, - "owner" => file.owner = prop.value, - "group" => file.group = prop.value, - "mode" => file.mode = prop.value, - "revert-tag" => file.revert_tag = prop.value, - "original_name" => file.original_name = prop.value, - "sysattr" => file.sys_attr = prop.value, - "overlay" => file.overlay = match string_to_bool(&prop.value) { + "path" => file.path = cleaned_value, + "owner" => file.owner = cleaned_value, + "group" => file.group = cleaned_value, + "mode" => file.mode = cleaned_value, + "revert-tag" => file.revert_tag = cleaned_value, + "original_name" => file.original_name = cleaned_value, + "sysattr" => file.sys_attr = cleaned_value, + "overlay" => file.overlay = match string_to_bool(&cleaned_value) { Ok(b) => b, _ => false, }, - "preserve" => file.preserve = match string_to_bool(&prop.value) { + "preserve" => file.preserve = match string_to_bool(&cleaned_value) { Ok(b) => b, _ => false, }, - "chash" | "pkg.content-hash" => p.additional_identifiers.push(Digest::from_str(&prop.value).unwrap()), + "chash" | "pkg.content-hash" => p.additional_identifiers.push(Digest::from_str(&cleaned_value).unwrap()), _ => { if is_facet(prop.key.clone()) { - file.add_facet(Facet::from_key_value(prop.key, prop.value)); + file.add_facet(Facet::from_key_value(prop.key, cleaned_value)); } else { - file.properties.push(prop.clone()); + file.properties.push(Property{ + key: prop.key, + value: prop.value.replace("\"", "") + }); } } } } + file.payload = Some(p); file } } @@ -209,15 +223,24 @@ pub struct Dependency { impl From for Dependency { fn from(act: Action) -> Self { let mut dep = Dependency::default(); - for prop in act.properties { + let mut props = act.properties; + if !act.payload_string.is_empty() { + let p_str = split_property(act.payload_string); + props.push(Property{ + key: p_str.0, + value: p_str.1.replace("\"", ""), + }) + } + for prop in props { + let cleaned_value = prop.value.replace("\"", ""); match prop.key.as_str() { - "fmri" => dep.fmri = prop.value, - "type" => dep.dependency_type = prop.value, - "predicate" => dep.predicate = prop.value, - "root-image" => dep.root_image = prop.value, + "fmri" => dep.fmri = cleaned_value, + "type" => dep.dependency_type = cleaned_value, + "predicate" => dep.predicate = cleaned_value, + "root-image" => dep.root_image = cleaned_value, _ => { if is_facet(prop.key.clone()) { - dep.add_facet(Facet::from_key_value(prop.key, prop.value)); + dep.add_facet(Facet::from_key_value(prop.key, cleaned_value)); } else { dep.optional.push(prop.clone()); } @@ -263,14 +286,23 @@ pub struct Attr { impl From for Attr { fn from(act: Action) -> Self { let mut attr = Attr::default(); - for prop in act.properties { + let mut props = act.properties; + if !act.payload_string.is_empty() { + let p_str = split_property(act.payload_string); + props.push(Property{ + key: p_str.0, + value: p_str.1.replace("\"", ""), + }) + } + for prop in props { + let cleaned_value = prop.value.replace("\"", ""); match prop.key.as_str() { - "name" => attr.key = prop.value, - "value" => attr.values.push(prop.value), + "name" => attr.key = cleaned_value, + "value" => attr.values.push(cleaned_value), _ => { attr.properties.insert(prop.key.clone(), Property{ key: prop.key, - value: prop.value + value: cleaned_value }); } } @@ -476,6 +508,16 @@ fn get_facet_key(facet_string: String) -> String { } } +fn split_property(property_string: String) -> (String, String) { + match property_string.find("=") { + Some(_) => { + let v :Vec <_> = property_string.split("=").collect(); + (String::from(v[0]), String::from(v[1])) + }, + None => (property_string.clone(), String::new()) + } +} + pub fn parse_manifest_file(filename: String) -> Result { let mut m = Manifest::new(); let f = OsFile::open(filename)?; diff --git a/libips/src/lib.rs b/libips/src/lib.rs index cf22703..0272808 100644 --- a/libips/src/lib.rs +++ b/libips/src/lib.rs @@ -165,10 +165,10 @@ mod tests { #[test] fn parse_direcory_actions() { let manifest_string = String::from("dir group=bin mode=0755 owner=root path=etc/nginx - dir group=bin mode=0755 owner=root path=usr/share/nginx - dir group=bin mode=0755 owner=root path=usr/share/nginx/html - dir group=bin mode=0755 owner=webservd path=var/nginx/logs - dir group=bin mode=0755 owner=root path=\"var/nginx\""); +dir group=bin mode=0755 owner=root path=usr/share/nginx +dir group=bin mode=0755 owner=root path=usr/share/nginx/html +dir group=bin mode=0755 owner=webservd path=var/nginx/logs +dir group=bin mode=0755 owner=root path=\"var/nginx\""); let test_results = vec![ Dir{ @@ -204,14 +204,10 @@ mod tests { }, ]; - let mut manifest = Manifest::new(); - match parse_manifest_string(manifest_string) { - Ok(m) => manifest = m, - Err(e) => { - println!("{}", e); - assert!(false, "caught error"); - } - }; + + let res = Manifest::parse_string(manifest_string); + assert!(res.is_ok(), "error during Manifest parsing: {:?}", res); + let manifest = res.unwrap(); assert_eq!(manifest.directories.len(), test_results.len()); @@ -811,7 +807,7 @@ file 6d5f820bb1d67594c7b757c79ef6f9242df49e98 chash=3ab17dde089f1eac7abd37d8efd7 }, ]; - let res = parse_manifest_string(manifest_string); + let res = Manifest::parse_string(manifest_string); assert!(res.is_ok(), "error during Manifest parsing: {:?}", res); let manifest = res.unwrap();