From 88051cba9926952afca6e0b86f19e893cb81e8e7 Mon Sep 17 00:00:00 2001 From: Martin Paulsen <43757366+georgepaulsen@users.noreply.github.com> Date: Thu, 1 Jan 2026 04:58:18 -0500 Subject: [PATCH] test: fixing unary operators that are getting parsed as argument instead of string literal (#9951) * Check for three-string comparison in test * Add regression tests for unary operator in three-arg form --- src/uu/test/src/parser.rs | 11 ++++++++++- tests/by-util/test_test.rs | 7 +++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/uu/test/src/parser.rs b/src/uu/test/src/parser.rs index 167bf7702..c1c06e4c5 100644 --- a/src/uu/test/src/parser.rs +++ b/src/uu/test/src/parser.rs @@ -188,7 +188,16 @@ impl Parser { match symbol { Symbol::LParen => self.lparen()?, Symbol::Bang => self.bang()?, - Symbol::UnaryOp(_) => self.uop(symbol), + Symbol::UnaryOp(_) => { + // Three-argument string comparison: `-f = a` means "-f" = "a", not file test + let is_string_cmp = matches!(self.peek(), Symbol::Op(Operator::String(_))) + && !matches!(Symbol::new(self.tokens.clone().nth(1)), Symbol::None); + if is_string_cmp { + self.literal(symbol.into_literal())?; + } else { + self.uop(symbol); + } + } Symbol::None => self.stack.push(symbol), literal => self.literal(literal)?, } diff --git a/tests/by-util/test_test.rs b/tests/by-util/test_test.rs index 4b5460cfd..21ea1893e 100644 --- a/tests/by-util/test_test.rs +++ b/tests/by-util/test_test.rs @@ -1027,3 +1027,10 @@ fn test_string_lt_gt_operator() { .fails_with_code(1) .no_output(); } + +#[test] +fn test_unary_op_as_literal_in_three_arg_form() { + // `-f = a` is string comparison "-f" = "a", not file test + new_ucmd!().args(&["-f", "=", "a"]).fails_with_code(1); + new_ucmd!().args(&["-f", "=", "a", "-o", "b"]).succeeds(); +}