diff options
author | Pierre Marsais <pimzero@users.noreply.github.com> | 2022-07-31 18:02:50 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-07-31 19:02:50 +0200 |
commit | 4ad0d5c5a3f30ee0b4ce6d8a2c2fb90fc3cba0a2 (patch) | |
tree | a520f0dcf3c9ab81629d1568f1227b5db70c17da /tests/by-util/test_ls.rs | |
parent | 38679f1c1bc7606dc9a5803f4844148baaf6950a (diff) |
ls: Implement --zero flag. (#2929) (#3746)
* ls: Implement --zero flag. (#2929)
This flag can be used to provide a easy machine parseable output from
ls, as discussed in the GNU bug report
https://debbugs.gnu.org/cgi/bugreport.cgi?bug=49716.
There are some peculiarities with this flag:
- Current implementation of GNU ls of the `--zero` flag implies some
other flags. Those can be overridden by setting those flags after
`--zero` in the command line.
- This flag is not compatible with `--dired`. This patch is not 100%
compliant with GNU ls: GNU ls `--zero` will fail if `--dired` and
`-l` are set, while with this patch only `--dired` is needed for the
command to fail.
We also add `--dired` flag to the parser, with no additional behaviour
change.
Testing done:
```
$ bash util/build-gnu.sh
[...]
$ bash util/run-gnu-test.sh tests/ls/zero-option.sh
[...]
PASS: tests/ls/zero-option.sh
============================================================================
Testsuite summary for GNU coreutils 9.1.36-8ec11
============================================================================
# TOTAL: 1
# PASS: 1
# SKIP: 0
# XFAIL: 0
# FAIL: 0
# XPASS: 0
# ERROR: 0
============================================================================
```
* Use the US way to spell Behavior
* Fix formatting with cargo fmt -- tests/by-util/test_ls.rs
* Simplify --zero flag overriding logic by using index_of
Also, allow multiple --zero flags, as this is possible with GNU ls
command. Only the last one is taken into account.
Co-authored-by: Sylvestre Ledru <sledru@mozilla.com>
Diffstat (limited to 'tests/by-util/test_ls.rs')
-rw-r--r-- | tests/by-util/test_ls.rs | 120 |
1 files changed, 120 insertions, 0 deletions
diff --git a/tests/by-util/test_ls.rs b/tests/by-util/test_ls.rs index dc6ce3bd1..f1f139387 100644 --- a/tests/by-util/test_ls.rs +++ b/tests/by-util/test_ls.rs @@ -797,6 +797,126 @@ fn test_ls_commas() { } #[test] +fn test_ls_zero() { + let scene = TestScenario::new(util_name!()); + let at = &scene.fixtures; + at.mkdir("0-test-zero"); + at.touch(&at.plus_as_string("2-test-zero")); + at.touch(&at.plus_as_string("3-test-zero")); + + let ignored_opts = [ + "--quoting-style=c", + "--color=always", + "-m", + "--hide-control-chars", + ]; + + scene + .ucmd() + .arg("--zero") + .succeeds() + .stdout_only("0-test-zero\x002-test-zero\x003-test-zero\x00"); + + for opt in ignored_opts { + scene + .ucmd() + .args(&[opt, "--zero"]) + .succeeds() + .stdout_only("0-test-zero\x002-test-zero\x003-test-zero\x00"); + } + + scene + .ucmd() + .args(&["--zero", "--quoting-style=c"]) + .succeeds() + .stdout_only("\"0-test-zero\"\x00\"2-test-zero\"\x00\"3-test-zero\"\x00"); + + scene + .ucmd() + .args(&["--zero", "--color=always"]) + .succeeds() + .stdout_only("\x1b[1;34m0-test-zero\x1b[0m\x002-test-zero\x003-test-zero\x00"); + + scene + .ucmd() + .args(&["--zero", "-m"]) + .succeeds() + .stdout_only("0-test-zero, 2-test-zero, 3-test-zero\x00"); + + scene + .ucmd() + .args(&["--zero", "--hide-control-chars"]) + .succeeds() + .stdout_only("0-test-zero\x002-test-zero\x003-test-zero\x00"); + + scene + .ucmd() + .args(&["--zero", "--quoting-style=c", "--zero"]) + .succeeds() + .stdout_only("0-test-zero\x002-test-zero\x003-test-zero\x00"); + + #[cfg(unix)] + { + at.touch(&at.plus_as_string("1\ntest-zero")); + + let ignored_opts = [ + "--quoting-style=c", + "--color=always", + "-m", + "--hide-control-chars", + ]; + + scene + .ucmd() + .arg("--zero") + .succeeds() + .stdout_only("0-test-zero\x001\ntest-zero\x002-test-zero\x003-test-zero\x00"); + + for opt in ignored_opts { + scene + .ucmd() + .args(&[opt, "--zero"]) + .succeeds() + .stdout_only("0-test-zero\x001\ntest-zero\x002-test-zero\x003-test-zero\x00"); + } + + scene + .ucmd() + .args(&["--zero", "--quoting-style=c"]) + .succeeds() + .stdout_only( + "\"0-test-zero\"\x00\"1\\ntest-zero\"\x00\"2-test-zero\"\x00\"3-test-zero\"\x00", + ); + + scene + .ucmd() + .args(&["--zero", "--color=always"]) + .succeeds() + .stdout_only( + "\x1b[1;34m0-test-zero\x1b[0m\x001\ntest-zero\x002-test-zero\x003-test-zero\x00", + ); + + scene + .ucmd() + .args(&["--zero", "-m"]) + .succeeds() + .stdout_only("0-test-zero, 1\ntest-zero, 2-test-zero, 3-test-zero\x00"); + + scene + .ucmd() + .args(&["--zero", "--hide-control-chars"]) + .succeeds() + .stdout_only("0-test-zero\x001?test-zero\x002-test-zero\x003-test-zero\x00"); + } + + scene + .ucmd() + .args(&["-l", "--zero"]) + .succeeds() + .stdout_contains("total "); +} + +#[test] fn test_ls_commas_trailing() { let scene = TestScenario::new(util_name!()); let at = &scene.fixtures; |