Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: -o arg should trigger an error #6642

Merged
merged 2 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/uu/test/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
MissingArgument(String),
UnknownOperator(String),
InvalidInteger(String),
UnaryOperatorExpected(String),

Check warning on line 14 in src/uu/test/src/error.rs

View check run for this annotation

Codecov / codecov/patch

src/uu/test/src/error.rs#L14

Added line #L14 was not covered by tests
}

/// A Result type for parsing test expressions
Expand All @@ -26,6 +27,7 @@
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"),
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/uu/test/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ fn eval(stack: &mut Vec<Symbol>) -> ParseResult<bool> {
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)?;

Expand Down
14 changes: 13 additions & 1 deletion tests/by-util/test_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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!()
Expand Down
Loading