1
0
Fork 0
mirror of git://git.code.sf.net/p/cdesktopenv/code synced 2025-03-09 15:50:02 +00:00

test foo =~ foo should fail with exit status 2 (#245)

When test is passed the '=~' operator, it will silently fail with
exit status 1:
    $ test foo =~ foo; echo $?
    1
This bug is caused by test_binop reaching the 'NOTREACHED' area of
code. The bugfix was adapted from ksh2020:
https://github.com/att/ast/issues/1152

src/cmd/ksh93/bltins/test.c: test_binop():
- Error out with a message suggesting usage of '[[ ... ]]' if '=~'
  is passed to the test builtin.
- Special-case TEST_END (']]') as that is not really an operator.

Co-authored-by: Martijn Dekker <martijn@inlv.org>
This commit is contained in:
Johnothan King 2021-03-27 14:51:16 -07:00 committed by GitHub
parent 767d23b3fe
commit fc2d5a6019
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 51 additions and 6 deletions

View file

@ -996,6 +996,32 @@ then got=$( { "$SHELL" -c '
"(got status $e$( ((e>128)) && print -n / && kill -l "$e"), $(printf %q "$got"))"
fi
# ==========
# Verify that the POSIX 'test' builtin complains loudly when the '=~' operator is used rather than
# failing silently. See https://github.com/att/ast/issues/1152.
actual=$($SHELL -c 'test foo =~ foo' 2>&1)
actual_status=$?
actual=${actual#*: }
expect='test: =~: operator not supported; use [[...]]'
expect_status=2
[[ "$actual" = "$expect" ]] || err_exit "test =~ failed (expected $expect, got $actual)"
[[ "$actual_status" = "$expect_status" ]] ||
err_exit "test =~ failed with the wrong exit status (expected $expect_status, got $actual_status)"
# Invalid operators 'test' and '[[...]]' both reject should also cause an error with exit status 2.
for operator in '===' ']]'
do
actual="$($SHELL -c "test foo $operator foo" 2>&1)"
actual_status=$?
actual=${actual#*: }
expect="test: $operator: unknown operator"
expect_status=2
[[ "$actual" = "$expect" ]] || err_exit "test $operator failed" \
"(expected $(printf %q "$expect"), got $(printf %q "$actual"))"
[[ "$actual_status" = "$expect_status" ]] ||
err_exit "'test foo $operator foo' failed with the wrong exit status (expected $expect_status, got $actual_status)"
done
# ======
# Regression test for https://github.com/att/ast/issues/1402
#