From 2c2f5f14a40d762374e02f3f96985ee872c3d912 Mon Sep 17 00:00:00 2001 From: Daniel Hofstetter Date: Tue, 24 Dec 2024 16:03:00 +0100 Subject: [PATCH 1/2] echo: use succeeds() to simplify some tests --- tests/by-util/test_echo.rs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/tests/by-util/test_echo.rs b/tests/by-util/test_echo.rs index 136500b48..8cb60877c 100644 --- a/tests/by-util/test_echo.rs +++ b/tests/by-util/test_echo.rs @@ -219,8 +219,7 @@ fn test_hyphen_values_at_start() { .arg("-test") .arg("araba") .arg("-merci") - .run() - .success() + .succeeds() .stdout_does_not_contain("-E") .stdout_is("-test araba -merci\n"); } @@ -231,8 +230,7 @@ fn test_hyphen_values_between() { .arg("test") .arg("-E") .arg("araba") - .run() - .success() + .succeeds() .stdout_is("test -E araba\n"); new_ucmd!() @@ -240,8 +238,7 @@ fn test_hyphen_values_between() { .arg("dum dum dum") .arg("-e") .arg("dum") - .run() - .success() + .succeeds() .stdout_is("dumdum dum dum dum -e dum\n"); } From 90465357e2d313cf51ad5f470cc6bfd5c339c39c Mon Sep 17 00:00:00 2001 From: Daniel Hofstetter Date: Tue, 24 Dec 2024 17:01:45 +0100 Subject: [PATCH 2/2] echo: handle double hyphens --- src/uu/echo/src/echo.rs | 19 ++++++++++++++++++- tests/by-util/test_echo.rs | 10 ++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/uu/echo/src/echo.rs b/src/uu/echo/src/echo.rs index 2d2884b1d..097e4f2e9 100644 --- a/src/uu/echo/src/echo.rs +++ b/src/uu/echo/src/echo.rs @@ -255,9 +255,26 @@ fn print_escaped(input: &[u8], output: &mut StdoutLock) -> io::Result impl uucore::Args { + let mut result = Vec::new(); + let mut is_first_double_hyphen = true; + + for arg in args { + if arg == "--" && is_first_double_hyphen { + result.push(OsString::from("--")); + is_first_double_hyphen = false; + } + result.push(arg); + } + + result.into_iter() +} + #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { - let matches = uu_app().get_matches_from(args); + let matches = uu_app().get_matches_from(handle_double_hyphens(args)); // TODO // "If the POSIXLY_CORRECT environment variable is set, then when echo’s first argument is not -n it outputs option-like arguments instead of treating them as options." diff --git a/tests/by-util/test_echo.rs b/tests/by-util/test_echo.rs index 8cb60877c..dd6b412a4 100644 --- a/tests/by-util/test_echo.rs +++ b/tests/by-util/test_echo.rs @@ -242,6 +242,16 @@ fn test_hyphen_values_between() { .stdout_is("dumdum dum dum dum -e dum\n"); } +#[test] +fn test_double_hyphens() { + new_ucmd!().arg("--").succeeds().stdout_only("--\n"); + new_ucmd!() + .arg("--") + .arg("--") + .succeeds() + .stdout_only("-- --\n"); +} + #[test] fn wrapping_octal() { // Some odd behavior of GNU. Values of \0400 and greater do not fit in the