Normalize absolute paths, fixes #268 (#270)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* Normalize absolute paths, fixes #268 * Split unit tests into several tests
authored by David Peter · committed by GitHub · parent f9a3258 · Browse files · 2 files·+57 added−14 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+57 added−14 removed
- tests:+51 added−13 removed
- other:+6 added−1 removed
Provenance
- Commit
8c1b037- Tree
062f715- Author
David Peter <sharkdp@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
f9a3258
2 files changed+57 added−14 removed
modified:src/main.rs+6 added-1 removed
71 unmodified lines72 73 if matches.is_present("absolute-path") {74 dir_vec = dir_vec75 .iter()76- .map(|path_buffer| fshelper::absolute_path(path_buffer).unwrap())76+ .map(|path_buffer| {77+ path_buffer78+ .canonicalize()79+ .and_then(|pb| fshelper::absolute_path(pb.as_path()))80+ .unwrap()81+ })82 .collect();83 }84 85 // Detect if the user accidentally supplied a path instead of a search pattern125 unmodified linesmodified:tests/tests.rs+51 added-13 removed
530 unmodified lines531 ),532 );533 }534 535+/// Absolute paths should be normalized536+#[test]537+fn test_normalized_absolute_path() {538+ let (te, abs_path) = get_test_env_with_abs_path(DEFAULT_DIRS, DEFAULT_FILES);539+540+ te.assert_output_subdirectory(541+ "one",542+ &["--absolute-path", "foo", ".."],543+ &format!(544+ "{abs_path}/a.foo545+ {abs_path}/one/b.foo546+ {abs_path}/one/two/c.foo547+ {abs_path}/one/two/C.Foo2548+ {abs_path}/one/two/three/d.foo549+ {abs_path}/one/two/three/directory_foo",550+ abs_path = &abs_path551+ ),552+ );553+}554+555 /// File type filter (--type)556 #[test]557 fn test_type() {558 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);113 unmodified lines672 673 te4.assert_output(&["--hidden", "--extension", ".hidden"], "test.hidden");674 }675 656-/// Symlinks misc676+/// Symlink as search directory677 #[test]658-fn test_symlink() {659- let (te, abs_path) = get_test_env_with_abs_path(DEFAULT_DIRS, DEFAULT_FILES);678+fn test_symlink_as_root() {679+ let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);680 681 // From: http://pubs.opengroup.org/onlinepubs/9699919799/functions/getcwd.html682 // The getcwd() function shall place an absolute pathname of the current working directory in683 // the array pointed to by buf, and return buf. The pathname shall contain no components that25 unmodified lines709 {dir}/symlink",710 dir = &parent_parent711 ),712 );713+}714 715+#[test]716+fn test_symlink_and_absolute_path() {717+ let (te, abs_path) = get_test_env_with_abs_path(DEFAULT_DIRS, DEFAULT_FILES);718+719 te.assert_output_subdirectory(720 "symlink",721 &["--absolute-path"],722 &format!(698- "{abs_path}/{dir}/c.foo699- {abs_path}/{dir}/C.Foo2700- {abs_path}/{dir}/three701- {abs_path}/{dir}/three/d.foo702- {abs_path}/{dir}/three/directory_foo",703- dir = if cfg!(windows) { "symlink" } else { "one/two" },723+ "{abs_path}/one/two/c.foo724+ {abs_path}/one/two/C.Foo2725+ {abs_path}/one/two/three726+ {abs_path}/one/two/three/d.foo727+ {abs_path}/one/two/three/directory_foo",728 abs_path = &abs_path729 ),730 );731+}732 733+#[test]734+fn test_symlink_as_absolute_root() {735+ let (te, abs_path) = get_test_env_with_abs_path(DEFAULT_DIRS, DEFAULT_FILES);736+737 te.assert_output(738 &["", &format!("{abs_path}/symlink", abs_path = abs_path)],739 &format!(740 "{abs_path}/symlink/c.foo3 unmodified lines744 {abs_path}/symlink/three/directory_foo",745 abs_path = &abs_path746 ),747 );748+}749 750+#[test]751+fn test_symlink_and_full_path() {752+ let (te, abs_path) = get_test_env_with_abs_path(DEFAULT_DIRS, DEFAULT_FILES);753 let root = te.system_root();754 let prefix = escape(&root.to_string_lossy());755 756 te.assert_output_subdirectory(3 unmodified lines760 "--full-path",761 &format!("^{prefix}.*three", prefix = prefix),762 ],763 &format!(731- "{abs_path}/{dir}/three732- {abs_path}/{dir}/three/d.foo733- {abs_path}/{dir}/three/directory_foo",734- dir = if cfg!(windows) { "symlink" } else { "one/two" },764+ "{abs_path}/one/two/three765+ {abs_path}/one/two/three/d.foo766+ {abs_path}/one/two/three/directory_foo",767 abs_path = &abs_path768 ),769 );770+}771 772+#[test]773+fn test_symlink_and_full_path_abs_path() {774+ let (te, abs_path) = get_test_env_with_abs_path(DEFAULT_DIRS, DEFAULT_FILES);775+ let root = te.system_root();776+ let prefix = escape(&root.to_string_lossy());777 te.assert_output(778 &[779 "--full-path",780 &format!("^{prefix}.*symlink.*three", prefix = prefix),204 unmodified lines