Use a uniform output format for searching ../
Before adoption: this commit came in with the history the project adopted, so no landing records it.
closes #107, fixes #82 by the way
authored by J.W · committed by David Peter · parent fe5d8aa · Browse files · 5 files·+120 added−54 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+120 added−54 removed
- tests:+85 added−18 removed
- other:+35 added−36 removed
Provenance
- Commit
e649c8f- Tree
8789d04- Author
J.W <jakwings@gmail.com>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
fe5d8aa
5 files changed+120 added−54 removed
modified:src/fshelper/mod.rs+1 added-1 removed
8 unmodified lines9 if path.is_absolute() != base.is_absolute() {10 if path.is_absolute() {11 Some(PathBuf::from(path))12 } else {13- None13+ Some(PathBuf::from(base.join(path)))14 }15 } else {16 let mut ita = path.components();17 let mut itb = base.components();38 unmodified linesmodified:src/main.rs+27 added-28 removed
14 unmodified lines15 mod walk;16 17 use std::env;18 use std::error::Error;19-use std::path::Path;19+use std::path::{Path, PathBuf};20 use std::sync::Arc;21 use std::time;22 23 use atty::Stream;10 unmodified lines34 let empty_pattern = String::new();35 let pattern = matches.value_of("pattern").unwrap_or(&empty_pattern);36 37 // Get the current working directory38- let current_dir_buf = match env::current_dir() {39- Ok(cd) => cd,40- Err(_) => error("Error: could not get current directory."),41- };42- let current_dir = current_dir_buf.as_path();38+ let current_dir = Path::new(".");39+ if !current_dir.is_dir() {40+ error("Error: could not get current directory.");41+ }42 43 // Get the root directory for the search45- let mut root_dir_is_absolute = false;46- let root_dir_buf = if let Some(rd) = matches.value_of("path") {47- let path = Path::new(rd);48-49- root_dir_is_absolute = path.is_absolute();50-51- fshelper::absolute_path(path).unwrap_or_else(|_| {52- error(&format!("Error: could not find directory '{}'.", rd))53- })54- } else {55- current_dir_buf.clone()44+ let mut root_dir_buf = match matches.value_of("path") {45+ Some(path) => PathBuf::from(path),46+ None => current_dir.to_path_buf(),47 };57-48 if !root_dir_buf.is_dir() {49 error(&format!(50 "Error: '{}' is not a directory.",51 root_dir_buf.to_string_lossy()52 ));53 }54 55+ let path_display = if matches.is_present("absolute-path") || root_dir_buf.is_absolute() {56+ PathDisplay::Absolute57+ } else {58+ PathDisplay::Relative59+ };60+61+ if path_display == PathDisplay::Absolute && root_dir_buf.is_relative() {62+ root_dir_buf = fshelper::absolute_path(root_dir_buf.as_path()).unwrap();63+ }64 let root_dir = root_dir_buf.as_path();65 66 // The search will be case-sensitive if the command line flag is set or67 // if the pattern has an uppercase character (smart case).39 unmodified lines107 max_buffer_time: matches108 .value_of("max-buffer-time")109 .and_then(|n| u64::from_str_radix(n, 10).ok())110 .map(time::Duration::from_millis),112- path_display: if matches.is_present("absolute-path") || root_dir_is_absolute {113- PathDisplay::Absolute114- } else {115- PathDisplay::Relative116- },111+ path_display: path_display,112 ls_colors: ls_colors,113 file_type: match matches.value_of("file-type") {114 Some("f") | Some("file") => FileType::RegularFile,115 Some("d") |5 unmodified lines121 e.trim_left_matches('.').to_lowercase()122 }),123 };124 125+ // If base_dir is ROOT_DIR, then root_dir must be absolute.126+ // Otherwise root_dir/entry cannot be turned into an existing relative path from base_dir.127+ //130- let root = Path::new(ROOT_DIR);131- let base = match config.path_display {132- PathDisplay::Relative => current_dir,133- PathDisplay::Absolute => root,128+ // We utilize ROOT_DIR to avoid resolving parent directories the root_dir.129+ let base_dir_buf = match config.path_display {130+ PathDisplay::Relative => current_dir.to_path_buf(),131+ PathDisplay::Absolute => PathBuf::from(ROOT_DIR),132 };133+ let base_dir = base_dir_buf.as_path();134 135 match RegexBuilder::new(pattern)136 .case_insensitive(!config.case_sensitive)137 .dot_matches_new_line(true)138 .build() {140- Ok(re) => walk::scan(root_dir, Arc::new(re), base, Arc::new(config)),139+ Ok(re) => walk::scan(root_dir, Arc::new(re), base_dir, Arc::new(config)),140 Err(err) => error(err.description()),141 }142 }modified:src/output.rs+5 added-1 removed
9 unmodified lines10 11 use ansi_term;12 13 pub fn print_entry(base: &Path, entry: &PathBuf, config: &FdOptions) {14- let path_full = base.join(entry);14+ let path_full = if !entry.as_os_str().is_empty() {15+ base.join(entry)16+ } else {17+ base.to_path_buf()18+ };19 20 let path_to_print = if config.path_display == PathDisplay::Absolute {21 &path_full22 } else {106 unmodified linesmodified:src/walk.rs+2 added-6 removed
1 use internal::{error, FdOptions};2 use fshelper;3 use output;4 5-use std::path::{Path, PathBuf};5+use std::path::Path;6 use std::sync::Arc;7 use std::sync::mpsc::channel;8 use std::thread;9 use std::time;142 unmodified lines152 };153 154 if let Some(search_str) = search_str_o {155 pattern.find(&*search_str).map(|_| {156- let mut path_rel_buf = match fshelper::path_relative_from(entry_path, &*base) {156+ let path_rel_buf = match fshelper::path_relative_from(entry_path, &*base) {157 Some(p) => p,158 None => error("Error: could not get relative path for directory entry."),159 };160 161- if path_rel_buf == PathBuf::new() {162- path_rel_buf.push(".");163- }164-161 // TODO: take care of the unwrap call162 tx_thread.send(path_rel_buf.to_owned()).unwrap()163 });164 }12 unmodified linesmodified:tests/tests.rs+85 added-18 removed
2 unmodified lines3 mod testenv;4 5 use testenv::TestEnv;6 7+fn get_absolute_root_path(env: &TestEnv) -> String {8+ let path = env.root()9+ .canonicalize()10+ .expect("absolute path")11+ .to_str()12+ .expect("string")13+ .to_string();14+15+ #[cfg(windows)]16+ let path = path.trim_left_matches(r"\\?\").to_string();17+18+ path19+}20+21 /// Simple tests22 #[test]23 fn test_simple() {24 let te = TestEnv::new();50 unmodified lines75 te.assert_output_subdirectory(76 "one/two",77 &["foo", "../../"],78 "../../a.foo65- ../b.foo66- c.foo67- C.Foo268- three/d.foo69- three/directory_foo",79+ ../../one/b.foo80+ ../../one/two/c.foo81+ ../../one/two/C.Foo282+ ../../one/two/three/d.foo83+ ../../one/two/three/directory_foo",84 );85 86 te.assert_output_subdirectory(87 "one/two/three",88 &["", ".."],75- ".76- ../c.foo89+ "../c.foo90 ../C.Foo278- d.foo79- directory_foo",91+ ../three92+ ../three/d.foo93+ ../three/directory_foo",94 );95 }96 97 /// Regex searches229 unmodified lines327 #[test]328 fn test_absolute_path() {329 let te = TestEnv::new();330 317- let abs_path = te.root()318- .canonicalize()319- .expect("absolute path")320- .to_str()321- .expect("string")322- .to_string();331+ let abs_path = get_absolute_root_path(&te);332 324- #[cfg(windows)]325- let abs_path = abs_path.trim_left_matches(r"\\?\");326-333 te.assert_output(334 &["--absolute-path"],335 &format!(336 "{abs_path}/a.foo84 unmodified lines421 );422 423 te.assert_output(&["--extension", "foo2"], "one/two/C.Foo2");424 }425+426+/// Symlinks misc427+#[test]428+fn test_symlink() {429+ let te = TestEnv::new();430+431+ let abs_path = get_absolute_root_path(&te);432+433+ // From: http://pubs.opengroup.org/onlinepubs/9699919799/functions/getcwd.html434+ // The getcwd() function shall place an absolute pathname of the current working directory in435+ // the array pointed to by buf, and return buf. The pathname shall contain no components that436+ // are dot or dot-dot, or are symbolic links.437+ //438+ // Symlinks on Unix are aliases to real paths, only has one redirection.439+ //440+ // Symlinks on Windows can refer to symlinks, and are resolved after logical step "..".441+442+ let parent_parent = if cfg!(windows) { ".." } else { "../.." };443+ te.assert_output_subdirectory(444+ "symlink",445+ &["", &parent_parent],446+ &format!(447+ "{dir}/a.foo448+ {dir}/one449+ {dir}/one/b.foo450+ {dir}/one/two451+ {dir}/one/two/c.foo452+ {dir}/one/two/C.Foo2453+ {dir}/one/two/three454+ {dir}/one/two/three/d.foo455+ {dir}/one/two/three/directory_foo456+ {dir}/symlink",457+ dir = &parent_parent458+ ),459+ );460+461+ te.assert_output_subdirectory(462+ "symlink",463+ &["--absolute-path"],464+ &format!(465+ "{abs_path}/one/two/c.foo466+ {abs_path}/one/two/C.Foo2467+ {abs_path}/one/two/three468+ {abs_path}/one/two/three/d.foo469+ {abs_path}/one/two/three/directory_foo",470+ abs_path = abs_path471+ ),472+ );473+474+ te.assert_output(475+ &["", &format!("{abs_path}/symlink", abs_path = abs_path)],476+ &format!(477+ "{abs_path}/symlink/c.foo478+ {abs_path}/symlink/C.Foo2479+ {abs_path}/symlink/three480+ {abs_path}/symlink/three/d.foo481+ {abs_path}/symlink/three/directory_foo",482+ abs_path = abs_path483+ ),484+ );485+}