diff --git a/src/walk.rs b/src/walk.rs index 9a2d990..a5666db 100644 --- a/src/walk.rs +++ b/src/walk.rs @@ -2,7 +2,7 @@ use internal::{error, FdOptions}; use fshelper; use output; -use std::path::Path; +use std::path::{Path, PathBuf}; use std::sync::Arc; use std::sync::mpsc::channel; use std::thread; @@ -101,6 +101,7 @@ pub fn scan(root: &Path, pattern: Arc, base: &Path, config: Arc, base: &Path, config: Arc return ignore::WalkState::Continue, }; + let entry_path = entry.path(); + + if entry_path == root { + return ignore::WalkState::Continue; + } + // Filter out unwanted file types. match config.file_type { FileType::Any => (), @@ -130,29 +137,32 @@ pub fn scan(root: &Path, pattern: Arc, base: &Path, config: Arc p, - None => error("Error: could not get relative path for directory entry."), - }; - let path_rel = path_rel_buf.as_path(); - let search_str_o = if config.search_full_path { - Some(path_rel.to_string_lossy()) + Some(entry_path.to_string_lossy()) } else { - path_rel.file_name().map(|f| f.to_string_lossy()) + entry_path.file_name().map(|f| f.to_string_lossy()) }; if let Some(search_str) = search_str_o { - // TODO: take care of the unwrap call pattern.find(&*search_str).map(|_| { + let mut path_rel_buf = match fshelper::path_relative_from(entry_path, &*base) { + Some(p) => p, + None => error("Error: could not get relative path for directory entry."), + }; + + if path_rel_buf == PathBuf::new() { + path_rel_buf.push("."); + } + + // TODO: take care of the unwrap call tx_thread.send(path_rel_buf.to_owned()).unwrap() }); } diff --git a/tests/tests.rs b/tests/tests.rs index 6297cc0..49faaef 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -68,6 +68,16 @@ fn test_explicit_root_path() { three/d.foo three/directory_foo", ); + + te.assert_output_subdirectory( + "one/two/three", + &["", ".."], + ". + ../c.foo + ../C.Foo2 + d.foo + directory_foo", + ); } /// Regex searches @@ -150,8 +160,6 @@ fn test_full_path() { "one/two/three/d.foo one/two/three/directory_foo", ); - - te.assert_output(&["--full-path", "^a\\.foo"], "a.foo"); } /// Hidden files (--hidden) @@ -308,6 +316,23 @@ fn test_absolute_path() { #[cfg(windows)] let abs_path = abs_path.trim_left_matches(r"\\?\"); + te.assert_output( + &["--absolute-path"], + &format!( + "{abs_path}/a.foo + {abs_path}/one + {abs_path}/one/b.foo + {abs_path}/one/two + {abs_path}/one/two/c.foo + {abs_path}/one/two/C.Foo2 + {abs_path}/one/two/three + {abs_path}/one/two/three/d.foo + {abs_path}/one/two/three/directory_foo + {abs_path}/symlink", + abs_path = abs_path + ), + ); + te.assert_output( &["--absolute-path", "foo"], &format!(