From 6b70dc232ed7a94f17f7c7b7be6b0de7b46efb47 Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Tue, 13 Aug 2024 11:05:35 +0200 Subject: [PATCH] test: -o arg should trigger an error (#6642) * test: -o arg should trigger an error Should fix tests/test/test-diag.pl * test: use var directly in string --------- Co-authored-by: Daniel Hofstetter --- src/uu/test/src/error.rs | 2 ++ src/uu/test/src/test.rs | 4 ++++ tests/by-util/test_test.rs | 14 +++++++++++++- 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/uu/test/src/error.rs b/src/uu/test/src/error.rs index 48238fa13..e392216e1 100644 --- a/src/uu/test/src/error.rs +++ b/src/uu/test/src/error.rs @@ -11,6 +11,7 @@ pub enum ParseError { MissingArgument(String), UnknownOperator(String), InvalidInteger(String), + UnaryOperatorExpected(String), } /// A Result type for parsing test expressions @@ -26,6 +27,7 @@ impl std::fmt::Display for ParseError { Self::ExtraArgument(s) => write!(f, "extra argument {s}"), Self::UnknownOperator(s) => write!(f, "unknown operator {s}"), Self::InvalidInteger(s) => write!(f, "invalid integer {s}"), + Self::UnaryOperatorExpected(op) => write!(f, "{op}: unary operator expected"), } } } diff --git a/src/uu/test/src/test.rs b/src/uu/test/src/test.rs index 9d3c2b290..41f56d793 100644 --- a/src/uu/test/src/test.rs +++ b/src/uu/test/src/test.rs @@ -162,6 +162,10 @@ fn eval(stack: &mut Vec) -> ParseResult { Some(Symbol::Literal(s)) => Ok(!s.is_empty()), Some(Symbol::None) | None => Ok(false), Some(Symbol::BoolOp(op)) => { + if (op == "-a" || op == "-o") && stack.len() < 2 { + return Err(ParseError::UnaryOperatorExpected(op.quote().to_string())); + } + let b = eval(stack)?; let a = eval(stack)?; diff --git a/tests/by-util/test_test.rs b/tests/by-util/test_test.rs index ebabbf9b1..22976dd9e 100644 --- a/tests/by-util/test_test.rs +++ b/tests/by-util/test_test.rs @@ -67,7 +67,7 @@ fn test_double_not_is_false() { #[test] fn test_and_not_is_false() { - new_ucmd!().args(&["-a", "!"]).run().code_is(1); + new_ucmd!().args(&["-a", "!"]).run().code_is(2); } #[test] @@ -86,6 +86,18 @@ fn test_simple_or() { new_ucmd!().args(&["foo", "-o", ""]).succeeds(); } +#[test] +fn test_errors_miss_and_or() { + new_ucmd!() + .args(&["-o", "arg"]) + .fails() + .stderr_contains("'-o': unary operator expected"); + new_ucmd!() + .args(&["-a", "arg"]) + .fails() + .stderr_contains("'-a': unary operator expected"); +} + #[test] fn test_negated_or() { new_ucmd!()