From 0209c90b4ea97a0feb1a85da65097dd57d0c36bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dorian=20P=C3=A9ron?= Date: Tue, 14 May 2024 00:43:30 +0200 Subject: [PATCH 1/3] tests(ls): Test default quoting style on TTY output --- tests/by-util/test_ls.rs | 42 ++++++++++++++++++++++++++++++++-------- 1 file changed, 34 insertions(+), 8 deletions(-) diff --git a/tests/by-util/test_ls.rs b/tests/by-util/test_ls.rs index 8ecabf86d..b0dfb63ba 100644 --- a/tests/by-util/test_ls.rs +++ b/tests/by-util/test_ls.rs @@ -2686,16 +2686,24 @@ fn test_ls_quoting_style() { { at.touch("one\ntwo"); at.touch("one\\two"); - // Default is literal, when stdout is not a TTY. - // Otherwise, it is shell-escape + + // Default is literal, when stdout is not a TTY... scene .ucmd() .arg("--hide-control-chars") .arg("one\ntwo") .succeeds() .stdout_only("one?two\n"); - // TODO: TTY-expected output, find a way to check this as well - // .stdout_only("'one'$'\\n''two'\n"); + + // ... Otherwise, it is shell-escape + #[cfg(unix)] + scene + .ucmd() + .arg("--hide-control-chars") + .arg("one\ntwo") + .terminal_simulation(true) + .succeeds() + .stdout_only("'one'$'\\n''two'\r\n"); for (arg, correct) in [ ("--quoting-style=literal", "one?two"), @@ -2786,13 +2794,21 @@ fn test_ls_quoting_style() { } } + // No-TTY scene .ucmd() .arg("one two") .succeeds() .stdout_only("one two\n"); - // TODO: TTY-expected output - // .stdout_only("'one two'\n"); + + // TTY + #[cfg(unix)] + scene + .ucmd() + .arg("one two") + .terminal_simulation(true) + .succeeds() + .stdout_only("'one two'\r\n"); for (arg, correct) in [ ("--quoting-style=literal", "one two"), @@ -2914,14 +2930,24 @@ fn test_ls_quoting_and_color() { let at = &scene.fixtures; at.touch("one two"); + + // No-TTY scene .ucmd() .arg("--color") .arg("one two") .succeeds() .stdout_only("one two\n"); - // TODO: TTY-expected output - // .stdout_only("'one two'\n"); + + // TTY + #[cfg(unix)] + scene + .ucmd() + .arg("--color") + .arg("one two") + .terminal_simulation(true) + .succeeds() + .stdout_only("'one two'\r\n"); } #[test] From 147c0b6962f1a3464e2386f2dea1a875e54cdb03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dorian=20P=C3=A9ron?= Date: Tue, 14 May 2024 00:47:27 +0200 Subject: [PATCH 2/3] tests(ls): Test space alignment when quoting is involved on grid output --- tests/by-util/test_ls.rs | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/tests/by-util/test_ls.rs b/tests/by-util/test_ls.rs index b0dfb63ba..596f1954d 100644 --- a/tests/by-util/test_ls.rs +++ b/tests/by-util/test_ls.rs @@ -2950,6 +2950,41 @@ fn test_ls_quoting_and_color() { .stdout_only("'one two'\r\n"); } +#[test] +fn test_ls_align_unquoted() { + let scene = TestScenario::new(util_name!()); + let at = &scene.fixtures; + + at.touch("elf two"); + at.touch("foobar"); + at.touch("CAPS"); + at.touch("'quoted'"); + + // In TTY + scene + .ucmd() + .arg("--color") + .terminal_simulation(true) + .succeeds() + .stdout_only("\"'quoted'\" CAPS 'elf two' foobar\r\n"); + // ^ ^ ^ + // space no-space space + + // The same should happen with format columns/across + // and shell quoting style, except for the `\r` at the end. + for format in &["--format=column", "--format=across"] { + scene + .ucmd() + .arg("--color") + .arg(format) + .arg("--quoting-style=shell") + .succeeds() + .stdout_only("\"'quoted'\" CAPS 'elf two' foobar\n"); + // ^ ^ ^ + // space no-space space + } +} + #[test] fn test_ls_ignore_hide() { let scene = TestScenario::new(util_name!()); From 105024e708830c9121e9cdaac5fd47e2203f4fed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dorian=20P=C3=A9ron?= Date: Tue, 14 May 2024 00:49:12 +0200 Subject: [PATCH 3/3] ls: Fix quoting alignment --- src/uu/ls/src/ls.rs | 18 +++++++++++++++--- tests/by-util/test_ls.rs | 1 + 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/uu/ls/src/ls.rs b/src/uu/ls/src/ls.rs index 49768e053..d9bc00de3 100644 --- a/src/uu/ls/src/ls.rs +++ b/src/uu/ls/src/ls.rs @@ -2616,12 +2616,24 @@ fn display_grid( } } else { let names = if quoted { + // In case some names are quoted, GNU adds a space before each + // entry that does not start with a quote to make it prettier + // on multiline. + // + // Example: + // ``` + // $ ls + // 'a\nb' bar + // foo baz + // ^ ^ + // These spaces is added + // ``` names .map(|n| { - if n.starts_with('\'') { - format!(" {n}") - } else { + if n.starts_with('\'') || n.starts_with('"') { n + } else { + format!(" {n}") } }) .collect() diff --git a/tests/by-util/test_ls.rs b/tests/by-util/test_ls.rs index 596f1954d..03c8327dd 100644 --- a/tests/by-util/test_ls.rs +++ b/tests/by-util/test_ls.rs @@ -2961,6 +2961,7 @@ fn test_ls_align_unquoted() { at.touch("'quoted'"); // In TTY + #[cfg(unix)] scene .ucmd() .arg("--color")