From f1827f39463e027869754a9003503620e36851dc Mon Sep 17 00:00:00 2001 From: Jon Gjengset Date: Sat, 18 Dec 2021 16:23:21 -0800 Subject: [PATCH] Skip leading ./s Fixes #11 --- src/main.rs | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 0af0c32..ed3de52 100644 --- a/src/main.rs +++ b/src/main.rs @@ -70,13 +70,17 @@ fn reorder(input: I, context_path: &str) -> impl Iterator where I: IntoIterator>, { - let path: Vec<_> = Path::new(context_path).components().collect(); + let path: Vec<_> = Path::new(context_path) + .components() + .skip_while(|c| matches!(c, std::path::Component::CurDir)) + .collect(); let mut lines = BinaryHeap::new(); for (i, line) in input.into_iter().enumerate() { let mut missed = false; let mut path = path.iter(); let proximity = Path::new(OsStr::from_bytes(&line)) .components() + .skip_while(|c| matches!(c, std::path::Component::CurDir)) .map(|c| { // if we've already missed, each additional dir is one further away if missed { @@ -232,6 +236,27 @@ mod tests { ); } + #[test] + fn skip_leading_dot() { + assert_eq!( + reorder( + vec![ + bts!("./first.txt"), + bts!("././second.txt"), + bts!("third.txt"), + ], + "null.txt", + ) + .map(Into::into) + .collect::>>(), + vec![ + bts!("./first.txt"), + bts!("././second.txt"), + bts!("third.txt"), + ] + ); + } + #[test] fn check_same_proximity_sorted() { assert_eq!(