Skip to content

Commit 88051cb

Browse files
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
1 parent 1e11921 commit 88051cb

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/uu/test/src/parser.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,16 @@ impl Parser {
188188
match symbol {
189189
Symbol::LParen => self.lparen()?,
190190
Symbol::Bang => self.bang()?,
191-
Symbol::UnaryOp(_) => self.uop(symbol),
191+
Symbol::UnaryOp(_) => {
192+
// Three-argument string comparison: `-f = a` means "-f" = "a", not file test
193+
let is_string_cmp = matches!(self.peek(), Symbol::Op(Operator::String(_)))
194+
&& !matches!(Symbol::new(self.tokens.clone().nth(1)), Symbol::None);
195+
if is_string_cmp {
196+
self.literal(symbol.into_literal())?;
197+
} else {
198+
self.uop(symbol);
199+
}
200+
}
192201
Symbol::None => self.stack.push(symbol),
193202
literal => self.literal(literal)?,
194203
}

tests/by-util/test_test.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,3 +1027,10 @@ fn test_string_lt_gt_operator() {
10271027
.fails_with_code(1)
10281028
.no_output();
10291029
}
1030+
1031+
#[test]
1032+
fn test_unary_op_as_literal_in_three_arg_form() {
1033+
// `-f = a` is string comparison "-f" = "a", not file test
1034+
new_ucmd!().args(&["-f", "=", "a"]).fails_with_code(1);
1035+
new_ucmd!().args(&["-f", "=", "a", "-o", "b"]).succeeds();
1036+
}

0 commit comments

Comments
 (0)