Skip to content
Myr

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

5 files+120 added−54 removed

modified:src/fshelper/mod.rs+1 added-1 removed
8 unmodified lines
9 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 lines
modified:src/main.rs+27 added-28 removed
14 unmodified lines
15mod walk;1617use std::env;18use std::error::Error;19use std::path::Path;19use std::path::{Path, PathBuf};20use std::sync::Arc;21use std::time;2223use atty::Stream;
10 unmodified lines
34 let empty_pattern = String::new();35 let pattern = matches.value_of("pattern").unwrap_or(&empty_pattern);3637 // 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 }4243 // 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);4849 root_dir_is_absolute = path.is_absolute();5051 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 };5748 if !root_dir_buf.is_dir() {49 error(&format!(50 "Error: '{}' is not a directory.",51 root_dir_buf.to_string_lossy()52 ));53 }5455 let path_display = if matches.is_present("absolute-path") || root_dir_buf.is_absolute() {56 PathDisplay::Absolute57 } else {58 PathDisplay::Relative59 };6061 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();6566 // 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 lines
107 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 lines
121 e.trim_left_matches('.').to_lowercase()122 }),123 };124125 // 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();134135 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 lines
1011use ansi_term;1213pub 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 };1920 let path_to_print = if config.path_display == PathDisplay::Absolute {21 &path_full22 } else {
106 unmodified lines
modified:src/walk.rs+2 added-6 removed
1use internal::{error, FdOptions};2use fshelper;3use output;45use std::path::{Path, PathBuf};5use std::path::Path;6use std::sync::Arc;7use std::sync::mpsc::channel;8use std::thread;9use std::time;
142 unmodified lines
152 };153154 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 };160161 if path_rel_buf == PathBuf::new() {162 path_rel_buf.push(".");163 }164161 // TODO: take care of the unwrap call162 tx_thread.send(path_rel_buf.to_owned()).unwrap()163 });164 }
12 unmodified lines
modified:tests/tests.rs+85 added-18 removed
2 unmodified lines
3mod testenv;45use testenv::TestEnv;67fn 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();1415 #[cfg(windows)]16 let path = path.trim_left_matches(r"\\?\").to_string();1718 path19}2021/// Simple tests22#[test]23fn test_simple() {24 let te = TestEnv::new();
50 unmodified lines
75 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 );8586 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}9697/// Regex searches
229 unmodified lines
327#[test]328fn test_absolute_path() {329 let te = TestEnv::new();330317 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);332324 #[cfg(windows)]325 let abs_path = abs_path.trim_left_matches(r"\\?\");326333 te.assert_output(334 &["--absolute-path"],335 &format!(336 "{abs_path}/a.foo
84 unmodified lines
421 );422423 te.assert_output(&["--extension", "foo2"], "one/two/C.Foo2");424}425426/// Symlinks misc427#[test]428fn test_symlink() {429 let te = TestEnv::new();430431 let abs_path = get_absolute_root_path(&te);432433 // 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 "..".441442 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 );460461 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 );473474 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}