From 311bc4538857dfbd72207ec273a1bacb22504116 Mon Sep 17 00:00:00 2001 From: "andy.boot" Date: Mon, 23 Dec 2019 15:50:01 +0000 Subject: [PATCH] Verify that '/' is parent of everything Bug could occur when run with '/' as it wasn't considered the parent of '/usr' due to having the same number of '/'s in the name --- src/utils/mod.rs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/utils/mod.rs b/src/utils/mod.rs index cfe84ce..a404c25 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -37,7 +37,7 @@ impl PartialEq for Node { } pub fn is_a_parent_of(parent: &str, child: &str) -> bool { - child.starts_with(parent) && child.chars().nth(parent.chars().count()) == Some('/') + (child.starts_with(parent) && child.chars().nth(parent.chars().count()) == Some('/')) || parent == "/" } pub fn simplify_dir_names(filenames: Vec<&str>) -> HashSet { @@ -232,4 +232,18 @@ mod tests { correct.insert("src_v2".to_string()); assert_eq!(simplify_dir_names(vec!["src/", "src_v2"]), correct); } + + #[test] + fn test_is_a_parent_of() { + assert!(is_a_parent_of("/usr", "/usr/andy")); + assert!(is_a_parent_of("/usr", "/usr/andy/i/am/descendant")); + assert!(!is_a_parent_of("/usr/andy", "/usr")); + assert!(!is_a_parent_of("/usr/andy", "/usr/sibling")); + } + + #[test] + fn test_is_a_parent_of_root() { + assert!(is_a_parent_of("/", "/usr/andy")); + assert!(is_a_parent_of("/", "/usr")); + } }