From 100a48fda90baa9d77b792e1ca8e295db4576b8b Mon Sep 17 00:00:00 2001 From: Ben Wiederhake Date: Fri, 1 Mar 2024 12:12:29 +0100 Subject: [PATCH 1/4] cat: permit repeating command-line flags --- src/uu/cat/src/cat.rs | 1 + tests/by-util/test_cat.rs | 69 ++++++++++++++++++++++++++++++++++++--- 2 files changed, 66 insertions(+), 4 deletions(-) diff --git a/src/uu/cat/src/cat.rs b/src/uu/cat/src/cat.rs index af55442ca..7b8b69119 100644 --- a/src/uu/cat/src/cat.rs +++ b/src/uu/cat/src/cat.rs @@ -231,6 +231,7 @@ pub fn uu_app() -> Command { .override_usage(format_usage(USAGE)) .about(ABOUT) .infer_long_args(true) + .args_override_self(true) .arg( Arg::new(options::FILE) .hide(true) diff --git a/tests/by-util/test_cat.rs b/tests/by-util/test_cat.rs index ab8e71899..4956114bd 100644 --- a/tests/by-util/test_cat.rs +++ b/tests/by-util/test_cat.rs @@ -255,6 +255,28 @@ fn test_output_multi_files_print_all_chars() { // spell-checker:enable } +#[test] +fn test_output_multi_files_print_all_chars_repeated() { + // spell-checker:disable + new_ucmd!() + .args(&["alpha.txt", "256.txt", "-A", "-n", "-A", "-n"]) + .succeeds() + .stdout_only( + " 1\tabcde$\n 2\tfghij$\n 3\tklmno$\n 4\tpqrst$\n \ + 5\tuvwxyz$\n 6\t^@^A^B^C^D^E^F^G^H^I$\n \ + 7\t^K^L^M^N^O^P^Q^R^S^T^U^V^W^X^Y^Z^[^\\^]^^^_ \ + !\"#$%&\'()*+,-./0123456789:;\ + <=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~^?M-^@M-^AM-^\ + BM-^CM-^DM-^EM-^FM-^GM-^HM-^IM-^JM-^KM-^LM-^MM-^NM-^OM-^PM-^QM-^RM-^SM-^TM-^UM-^V\ + M-^WM-^XM-^YM-^ZM-^[M-^\\M-^]M-^^M-^_M- \ + M-!M-\"M-#M-$M-%M-&M-\'M-(M-)M-*M-+M-,M--M-.M-/M-0M-1M-2M-3M-4M-5M-6M-7M-8M-9M-:\ + M-;M-M-?M-@M-AM-BM-CM-DM-EM-FM-GM-HM-IM-JM-KM-LM-MM-NM-OM-PM-QM-RM-SM-TM-U\ + M-VM-WM-XM-YM-ZM-[M-\\M-]M-^M-_M-`M-aM-bM-cM-dM-eM-fM-gM-hM-iM-jM-kM-lM-mM-nM-oM-\ + pM-qM-rM-sM-tM-uM-vM-wM-xM-yM-zM-{M-|M-}M-~M-^?", + ); + // spell-checker:enable +} + #[test] fn test_numbered_lines_no_trailing_newline() { // spell-checker:disable @@ -270,7 +292,7 @@ fn test_numbered_lines_no_trailing_newline() { #[test] fn test_stdin_show_nonprinting() { - for same_param in ["-v", "--show-nonprinting", "--show-non"] { + for same_param in ["-v", "-vv", "--show-nonprinting", "--show-non"] { new_ucmd!() .args(&[same_param]) .pipe_in("\t\0\n") @@ -281,7 +303,7 @@ fn test_stdin_show_nonprinting() { #[test] fn test_stdin_show_tabs() { - for same_param in ["-T", "--show-tabs", "--show-ta"] { + for same_param in ["-T", "-TT", "--show-tabs", "--show-ta"] { new_ucmd!() .args(&[same_param]) .pipe_in("\t\0\n") @@ -292,7 +314,7 @@ fn test_stdin_show_tabs() { #[test] fn test_stdin_show_ends() { - for same_param in ["-E", "--show-ends", "--show-e"] { + for same_param in ["-E", "-EE", "--show-ends", "--show-e"] { new_ucmd!() .args(&[same_param, "-"]) .pipe_in("\t\0\n\t") @@ -312,6 +334,17 @@ fn squeeze_all_files() { .stdout_only("a\n\nb"); } +#[test] +fn squeeze_all_files_repeated() { + // empty lines at the end of a file are "squeezed" together with empty lines at the beginning + let (at, mut ucmd) = at_and_ucmd!(); + at.write("input1", "a\n\n"); + at.write("input2", "\n\nb"); + ucmd.args(&["-s", "input1", "input2", "-s"]) + .succeeds() + .stdout_only("a\n\nb"); +} + #[test] fn test_show_ends_crlf() { new_ucmd!() @@ -341,6 +374,15 @@ fn test_stdin_nonprinting_and_endofline() { .stdout_only("\t^@$\n"); } +#[test] +fn test_stdin_nonprinting_and_endofline_repeated() { + new_ucmd!() + .args(&["-ee", "-e"]) + .pipe_in("\t\0\n") + .succeeds() + .stdout_only("\t^@$\n"); +} + #[test] fn test_stdin_nonprinting_and_tabs() { new_ucmd!() @@ -350,6 +392,15 @@ fn test_stdin_nonprinting_and_tabs() { .stdout_only("^I^@\n"); } +#[test] +fn test_stdin_nonprinting_and_tabs_repeated() { + new_ucmd!() + .args(&["-tt", "-t"]) + .pipe_in("\t\0\n") + .succeeds() + .stdout_only("^I^@\n"); +} + #[test] fn test_stdin_squeeze_blank() { for same_param in ["-s", "--squeeze-blank", "--squeeze"] { @@ -364,7 +415,7 @@ fn test_stdin_squeeze_blank() { #[test] fn test_stdin_number_non_blank() { // spell-checker:disable-next-line - for same_param in ["-b", "--number-nonblank", "--number-non"] { + for same_param in ["-b", "-bb", "--number-nonblank", "--number-non"] { new_ucmd!() .arg(same_param) .arg("-") @@ -386,6 +437,16 @@ fn test_non_blank_overrides_number() { } } +#[test] +#[ignore = "known issue"] +fn test_non_blank_overrides_number_even_when_present() { + new_ucmd!() + .args(&["-n", "-b", "-n"]) + .pipe_in("\na\nb\n\n\nc") + .succeeds() + .stdout_only("\n 1\ta\n 2\tb\n\n\n 3\tc"); +} + #[test] fn test_squeeze_blank_before_numbering() { for same_param in ["-s", "--squeeze-blank"] { From 472b56f0ff62d41b0bb55976d3476ca1ae84f1cf Mon Sep 17 00:00:00 2001 From: Ben Wiederhake Date: Fri, 1 Mar 2024 12:18:26 +0100 Subject: [PATCH 2/4] cat: fix -b and -n anti-symmetry --- src/uu/cat/src/cat.rs | 3 ++- tests/by-util/test_cat.rs | 1 - 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/uu/cat/src/cat.rs b/src/uu/cat/src/cat.rs index 7b8b69119..8823f27b8 100644 --- a/src/uu/cat/src/cat.rs +++ b/src/uu/cat/src/cat.rs @@ -250,7 +250,8 @@ pub fn uu_app() -> Command { .short('b') .long(options::NUMBER_NONBLANK) .help("number nonempty output lines, overrides -n") - .overrides_with(options::NUMBER) + // Note: This MUST NOT .overrides_with(options::NUMBER)! + // In clap, overriding is symmetric, so "-b -n" counts as "-n", which is not what we want. .action(ArgAction::SetTrue), ) .arg( diff --git a/tests/by-util/test_cat.rs b/tests/by-util/test_cat.rs index 4956114bd..1fd829ed9 100644 --- a/tests/by-util/test_cat.rs +++ b/tests/by-util/test_cat.rs @@ -438,7 +438,6 @@ fn test_non_blank_overrides_number() { } #[test] -#[ignore = "known issue"] fn test_non_blank_overrides_number_even_when_present() { new_ucmd!() .args(&["-n", "-b", "-n"]) From cd70d7dec1a0599c8dffe6fb3edac44b50ba7587 Mon Sep 17 00:00:00 2001 From: Ben Wiederhake Date: Fri, 1 Mar 2024 12:22:33 +0100 Subject: [PATCH 3/4] cat: ignore -u flag, just like GNU does --- src/uu/cat/src/cat.rs | 7 +++++++ tests/by-util/test_cat.rs | 11 +++++++++++ 2 files changed, 18 insertions(+) diff --git a/src/uu/cat/src/cat.rs b/src/uu/cat/src/cat.rs index 8823f27b8..b239dc87a 100644 --- a/src/uu/cat/src/cat.rs +++ b/src/uu/cat/src/cat.rs @@ -170,6 +170,7 @@ mod options { pub static SHOW_NONPRINTING_TABS: &str = "t"; pub static SHOW_TABS: &str = "show-tabs"; pub static SHOW_NONPRINTING: &str = "show-nonprinting"; + pub static IGNORED_U: &str = "ignored-u"; } #[uucore::main] @@ -301,6 +302,12 @@ pub fn uu_app() -> Command { .help("use ^ and M- notation, except for LF (\\n) and TAB (\\t)") .action(ArgAction::SetTrue), ) + .arg( + Arg::new(options::IGNORED_U) + .short('u') + .help("(ignored)") + .action(ArgAction::SetTrue), + ) } fn cat_handle( diff --git a/tests/by-util/test_cat.rs b/tests/by-util/test_cat.rs index 1fd829ed9..89b945b80 100644 --- a/tests/by-util/test_cat.rs +++ b/tests/by-util/test_cat.rs @@ -612,3 +612,14 @@ fn test_error_loop() { .fails() .stderr_is("cat: 1: Too many levels of symbolic links\n"); } + +#[test] +fn test_u_ignored() { + for same_param in ["-u", "-uu"] { + new_ucmd!() + .arg(same_param) + .pipe_in("hello") + .succeeds() + .stdout_only("hello"); + } +} From 679b9e2c0adb1fa2573ec88e12fa2595c2cc3bfe Mon Sep 17 00:00:00 2001 From: Daniel Hofstetter Date: Mon, 4 Mar 2024 09:41:09 +0100 Subject: [PATCH 4/4] cat: prefix two test fns with "test_" --- tests/by-util/test_cat.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/by-util/test_cat.rs b/tests/by-util/test_cat.rs index 89b945b80..b7b19fd32 100644 --- a/tests/by-util/test_cat.rs +++ b/tests/by-util/test_cat.rs @@ -324,7 +324,7 @@ fn test_stdin_show_ends() { } #[test] -fn squeeze_all_files() { +fn test_squeeze_all_files() { // empty lines at the end of a file are "squeezed" together with empty lines at the beginning let (at, mut ucmd) = at_and_ucmd!(); at.write("input1", "a\n\n"); @@ -335,7 +335,7 @@ fn squeeze_all_files() { } #[test] -fn squeeze_all_files_repeated() { +fn test_squeeze_all_files_repeated() { // empty lines at the end of a file are "squeezed" together with empty lines at the beginning let (at, mut ucmd) = at_and_ucmd!(); at.write("input1", "a\n\n");