From 414c92eed79d5b8687db332e84c6d67f9b2b2593 Mon Sep 17 00:00:00 2001 From: Anup Mahindre Date: Fri, 21 May 2021 22:04:24 +0530 Subject: [PATCH 1/3] ls: Fix printing paths behavior For any commandline arguments, ls should print the argument as is (and not truncate to just the file name) For any other files it reaches (say through recursive exploration), ls should print just the filename (as path is printed once when we enter the directory) --- src/uu/ls/src/ls.rs | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/src/uu/ls/src/ls.rs b/src/uu/ls/src/ls.rs index c5389295b..d467d431a 100644 --- a/src/uu/ls/src/ls.rs +++ b/src/uu/ls/src/ls.rs @@ -1110,7 +1110,7 @@ struct PathData { md: OnceCell>, ft: OnceCell>, // Name of the file - will be empty for . or .. - file_name: String, + display_name: String, // PathBuf that all above data corresponds to p_buf: PathBuf, must_dereference: bool, @@ -1126,14 +1126,18 @@ impl PathData { ) -> Self { // We cannot use `Path::ends_with` or `Path::Components`, because they remove occurrences of '.' // For '..', the filename is None - let name = if let Some(name) = file_name { + let display_name = if let Some(name) = file_name { name } else { - p_buf - .file_name() - .unwrap_or_else(|| p_buf.iter().next_back().unwrap()) - .to_string_lossy() - .into_owned() + let display_osstr = if command_line { + p_buf.as_os_str() + } else { + p_buf + .file_name() + .unwrap_or_else(|| p_buf.iter().next_back().unwrap()) + }; + + display_osstr.to_string_lossy().into_owned() }; let must_dereference = match &config.dereference { Dereference::All => true, @@ -1159,7 +1163,7 @@ impl PathData { Self { md: OnceCell::new(), ft, - file_name: name, + display_name, p_buf, must_dereference, } @@ -1243,7 +1247,7 @@ fn sort_entries(entries: &mut Vec, config: &Config) { entries.sort_by_key(|k| Reverse(k.md().as_ref().map(|md| md.len()).unwrap_or(0))) } // The default sort in GNU ls is case insensitive - Sort::Name => entries.sort_by(|a, b| a.file_name.cmp(&b.file_name)), + Sort::Name => entries.sort_by(|a, b| a.display_name.cmp(&b.display_name)), Sort::Version => entries.sort_by(|a, b| version_cmp::version_cmp(&a.p_buf, &b.p_buf)), Sort::Extension => entries.sort_by(|a, b| { a.p_buf @@ -1719,7 +1723,7 @@ fn classify_file(path: &PathData) -> Option { } fn display_file_name(path: &PathData, config: &Config) -> Option { - let mut name = escape_name(&path.file_name, &config.quoting_style); + let mut name = escape_name(&path.display_name, &config.quoting_style); #[cfg(unix)] { From 31545258ac6b6ef11ef8c04f2564825e08f7ad50 Mon Sep 17 00:00:00 2001 From: Anup Mahindre Date: Fri, 21 May 2021 22:20:54 +0530 Subject: [PATCH 2/3] tests: Fix test_ls_path --- tests/by-util/test_ls.rs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/tests/by-util/test_ls.rs b/tests/by-util/test_ls.rs index 8d32172b0..fc4051039 100644 --- a/tests/by-util/test_ls.rs +++ b/tests/by-util/test_ls.rs @@ -1967,8 +1967,6 @@ fn test_ls_sort_extension() { ); } -// This tests for the open issue described in #2223 -#[cfg_attr(not(feature = "test_unimplemented"), ignore)] #[test] fn test_ls_path() { let scene = TestScenario::new(util_name!()); @@ -1987,13 +1985,17 @@ fn test_ls_path() { scene.ucmd().arg(path).run().stdout_is(expected_stdout); let expected_stdout = &format!("./{}\n", path); - scene.ucmd().arg(path).run().stdout_is(expected_stdout); + scene + .ucmd() + .arg(format!("./{}", path)) + .run() + .stdout_is(expected_stdout); - let abs_path = format!("{}/{}\n", at.as_string(), path); - println!(":{}", abs_path); - scene.ucmd().arg(&abs_path).run().stdout_is(&abs_path); + let abs_path = format!("{}/{}", at.as_string(), path); + let expected_stdout = &format!("{}\n", abs_path); + scene.ucmd().arg(&abs_path).run().stdout_is(expected_stdout); - let expected_stdout = &format!("{}\n{}\n", file1, path); + let expected_stdout = &format!("{}\n{}\n", path, file1); scene .ucmd() .arg(file1) From 9f88963764ad2e02836b6583ac556503b4f3c687 Mon Sep 17 00:00:00 2001 From: Anup Mahindre Date: Sat, 22 May 2021 14:51:50 +0530 Subject: [PATCH 3/3] tests: fix test_ls_path for windows --- tests/by-util/test_ls.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/tests/by-util/test_ls.rs b/tests/by-util/test_ls.rs index fc4051039..6d6c65194 100644 --- a/tests/by-util/test_ls.rs +++ b/tests/by-util/test_ls.rs @@ -1992,10 +1992,18 @@ fn test_ls_path() { .stdout_is(expected_stdout); let abs_path = format!("{}/{}", at.as_string(), path); - let expected_stdout = &format!("{}\n", abs_path); + let expected_stdout = if cfg!(windows) { + format!("\'{}\'\n", abs_path) + } else { + format!("{}\n", abs_path) + }; scene.ucmd().arg(&abs_path).run().stdout_is(expected_stdout); - let expected_stdout = &format!("{}\n{}\n", path, file1); + let expected_stdout = if cfg!(windows) { + format!("{} {}\n", path, file1) + } else { + format!("{}\n{}\n", path, file1) + }; scene .ucmd() .arg(file1)