diff --git a/Cargo.toml b/Cargo.toml index 10597f6..c44644c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,7 +23,7 @@ path = "src/main.rs" [dependencies] ansi_term = "=0.11" clap = "=2.33" -jwalk = "0.4" +jwalk = "0.4.0" [dev-dependencies] assert_cli = "=0.5" diff --git a/src/main.rs b/src/main.rs index 90a6030..ce51d69 100644 --- a/src/main.rs +++ b/src/main.rs @@ -133,7 +133,6 @@ fn build_tree(biggest_ones: Vec<(String, u64)>, depth: Option) -> Node { children: Vec::default(), }; recursively_build_tree(&mut top_parent, n, depth); - top_parent.children.sort_unstable() } top_parent } diff --git a/src/utils/mod.rs b/src/utils/mod.rs index cfe84ce..0d337ea 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 { @@ -125,6 +125,11 @@ fn examine_dir( } // This path and all its parent paths have their counter incremented for path_name in e.path().ancestors() { + // This is required due to bug in Jwalk that adds '/' to all sub dir lists + // see: https://github.com/jessegrosjean/jwalk/issues/13 + if path_name.to_string_lossy() == "/" && top_dir != "/" { + continue + } let path_name = path_name.to_string_lossy(); let s = data.entry(path_name.to_string()).or_insert(0); *s += size; @@ -232,4 +237,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")); + } }