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
This commit is contained in:
Martin Paulsen
2026-01-01 10:58:18 +01:00
committed by GitHub
parent 1e11921c29
commit 88051cba99
2 changed files with 17 additions and 1 deletions
+10 -1
View File
@@ -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)?,
}
+7
View File
@@ -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();
}