From 4ac85d7dc98b08de411103a343e8bc892bf152ca Mon Sep 17 00:00:00 2001 From: "andy.boot" Date: Fri, 27 Apr 2018 11:07:10 +0100 Subject: [PATCH] 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 --- src/tests.rs | 45 ++++++++++++--------------------------------- src/utils/mod.rs | 11 ++++++++++- 2 files changed, 22 insertions(+), 34 deletions(-) diff --git a/src/tests.rs b/src/tests.rs index 792d529..fcc59d7 100644 --- a/src/tests.rs +++ b/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 diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 340f1d1..7f32a0a 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -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) -> 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 }