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
This commit is contained in:
andy.boot
2019-12-23 15:50:01 +00:00
parent ef66fb3938
commit 311bc45388
+15 -1
View File
@@ -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<String> {
@@ -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"));
}
}