mirror of
https://github.com/bootandy/dust.git
synced 2026-02-05 03:16:41 -08:00
Simplify tests
dust sort is now more predictable as it orders first by size and second by name. Walkdir still gives different iteration orders on different systems so all output is not entirely predictable
This commit is contained in:
45
src/tests.rs
45
src/tests.rs
@@ -123,60 +123,39 @@ pub fn test_soft_sym_link() {
|
||||
.output();
|
||||
assert!(c.is_ok());
|
||||
|
||||
let (r, r2) = soft_sym_link_output(dir_s, file_path_s, link_name_s);
|
||||
let r = soft_sym_link_output(dir_s, file_path_s, link_name_s);
|
||||
|
||||
// We cannot guarantee which version will appear first.
|
||||
// TODO: Consider adding predictable itteration order (sort file entries by name?)
|
||||
let result = panic::catch_unwind(|| {
|
||||
assert_cli::Assert::main_binary()
|
||||
.with_args(&[dir_s])
|
||||
.stdout()
|
||||
.contains(r)
|
||||
.unwrap();
|
||||
});
|
||||
if result.is_err() {
|
||||
assert_cli::Assert::main_binary()
|
||||
.with_args(&[dir_s])
|
||||
.stdout()
|
||||
.contains(r2)
|
||||
.unwrap();
|
||||
}
|
||||
assert_cli::Assert::main_binary()
|
||||
.with_args(&[dir_s])
|
||||
.stdout()
|
||||
.contains(r)
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
#[cfg(target_os = "macos")]
|
||||
fn soft_sym_link_output(dir: &str, file_path: &str, link_name: &str) -> (String, String) {
|
||||
let r = format!(
|
||||
fn soft_sym_link_output(dir: &str, file_path: &str, link_name: &str) -> String {
|
||||
format!(
|
||||
"{}
|
||||
{}
|
||||
{}",
|
||||
format_string(dir, true, true, " 8.0K", ""),
|
||||
format_string(file_path, true, true, " 4.0K", "├──",),
|
||||
format_string(link_name, false, true, " 4.0K", "└──",),
|
||||
);
|
||||
let r2 = format!(
|
||||
"{}
|
||||
{}
|
||||
{}",
|
||||
format_string(dir, true, true, " 8.0K", ""),
|
||||
format_string(link_name, true, true, " 4.0K", "├──",),
|
||||
format_string(file_path, false, true, " 4.0K", "└──",),
|
||||
);
|
||||
(r, r2)
|
||||
)
|
||||
}
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
fn soft_sym_link_output(dir: &str, file_path: &str, link_name: &str) -> (String, String) {
|
||||
let r = format!(
|
||||
fn soft_sym_link_output(dir: &str, file_path: &str, link_name: &str) -> String {
|
||||
format!(
|
||||
"{}
|
||||
{}
|
||||
{}",
|
||||
format_string(dir, true, true, " 8.0K", ""),
|
||||
format_string(file_path, true, true, " 4.0K", "├──",),
|
||||
format_string(link_name, false, true, " 0B", "└──",),
|
||||
);
|
||||
// I have not yet seen the output appear the other way round on linux. If this happens
|
||||
// then add in an alterate ordering like in the mac version of this function.
|
||||
(r, "".to_string())
|
||||
)
|
||||
}
|
||||
|
||||
// Hard links are ignored as the inode is the same as the file
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
use std::collections::HashMap;
|
||||
use std::collections::HashSet;
|
||||
use std::cmp::Ordering;
|
||||
|
||||
use walkdir::WalkDir;
|
||||
|
||||
@@ -80,10 +81,18 @@ fn examine_dir(
|
||||
}
|
||||
}
|
||||
}
|
||||
pub fn compare_tuple(a :&(String, u64), b: &(String, u64)) -> Ordering {
|
||||
let result = b.1.cmp(&a.1);
|
||||
if result == Ordering::Equal {
|
||||
a.0.cmp(&b.0)
|
||||
} else {
|
||||
result
|
||||
}
|
||||
}
|
||||
|
||||
pub fn sort<'a>(data: HashMap<String, u64>) -> Vec<(String, u64)> {
|
||||
let mut new_l: Vec<(String, u64)> = data.iter().map(|(a, b)| (a.clone(), *b)).collect();
|
||||
new_l.sort_by(|a, b| b.1.cmp(&a.1));
|
||||
new_l.sort_by(|a, b| compare_tuple(&a, &b));
|
||||
new_l
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user