shell: tweak getopts tests, no code changes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2017-08-13 02:59:00 +02:00
parent 419db0391e
commit 007ce9f807
6 changed files with 34 additions and 4 deletions

View File

@ -1,6 +1,6 @@
# This test can fail with libc with buggy getopt() implementation. # This test can fail with libc with buggy getopt() implementation.
# If getopt() wants to parse multi-option args (-abc), # If getopt() wants to parse multi-option args (-abc),
# it needs to remember a position withit current arg. # it needs to remember a position within current arg.
# #
# If this position is kept as a POINTER, not an offset, # If this position is kept as a POINTER, not an offset,
# and if argv[] ADDRESSES (not contents!) change, it blows up. # and if argv[] ADDRESSES (not contents!) change, it blows up.
@ -24,7 +24,7 @@ VAR111=NEWVAL; getopts "ac" var -a -b -c -d e; echo "3 rc:$? var:'$var' OPTIND:$
VAR222=NEWVAL; getopts "ac" var -a -b -c -d e; echo "4 rc:$? var:'$var' OPTIND:$OPTIND OPTARG:'$OPTARG'" VAR222=NEWVAL; getopts "ac" var -a -b -c -d e; echo "4 rc:$? var:'$var' OPTIND:$OPTIND OPTARG:'$OPTARG'"
VAR333=NEWVAL; getopts "ac" var -a -b -c -d e; echo "5 rc:$? var:'$var' OPTIND:$OPTIND OPTARG:'$OPTARG'" VAR333=NEWVAL; getopts "ac" var -a -b -c -d e; echo "5 rc:$? var:'$var' OPTIND:$OPTIND OPTARG:'$OPTARG'"
# Sligntly different attempts to force reallocations # Slightly different attempts to force reallocations
echo echo
echo "*** optstring:'ac' args:-a -b -c -d e" echo "*** optstring:'ac' args:-a -b -c -d e"

View File

@ -1,3 +1,5 @@
(
set -- -q -w e -r -t -y set -- -q -w e -r -t -y
echo "*** no OPTIND, optstring:'w:et' args:$*" echo "*** no OPTIND, optstring:'w:et' args:$*"
var=QWERTY var=QWERTY
@ -16,3 +18,7 @@ while getopts "w:et" var; do
OPTARG=ASDFGH OPTARG=ASDFGH
done done
echo "exited: var:'$var' OPTIND:$OPTIND OPTARG:'$OPTARG'" echo "exited: var:'$var' OPTIND:$OPTIND OPTARG:'$OPTARG'"
) 2>&1 \
| sed -e 's/ unrecognized option: / invalid option -- /' \
-e 's/ illegal option -- / invalid option -- /' \

View File

@ -1,3 +1,5 @@
(
set -- -q -w -e r -t -y set -- -q -w -e r -t -y
echo "*** no OPTIND, optstring:'we' args:$*" echo "*** no OPTIND, optstring:'we' args:$*"
var=QWERTY var=QWERTY
@ -5,3 +7,7 @@ while getopts "we" var; do
echo "var:'$var' OPTIND:$OPTIND" echo "var:'$var' OPTIND:$OPTIND"
done done
echo "exited: var:'$var' OPTIND:$OPTIND" echo "exited: var:'$var' OPTIND:$OPTIND"
) 2>&1 \
| sed -e 's/ unrecognized option: / invalid option -- /' \
-e 's/ illegal option -- / invalid option -- /' \

View File

@ -11,6 +11,8 @@
# and OPTARG shall be set to the option character found. # and OPTARG shall be set to the option character found.
# """ # """
(
echo "*** optstring:':ac' args:-a -b -c" echo "*** optstring:':ac' args:-a -b -c"
getopts ":ac" var -a -b -c; echo "1 rc:$? var:'$var' OPTIND:$OPTIND OPTARG:'$OPTARG'" getopts ":ac" var -a -b -c; echo "1 rc:$? var:'$var' OPTIND:$OPTIND OPTARG:'$OPTARG'"
getopts ":ac" var -a -b -c; echo "2 rc:$? var:'$var' OPTIND:$OPTIND OPTARG:'$OPTARG'" getopts ":ac" var -a -b -c; echo "2 rc:$? var:'$var' OPTIND:$OPTIND OPTARG:'$OPTARG'"
@ -21,3 +23,7 @@ getopts ":ac" var -a -b -c; echo "4 rc:$? var:'$var' OPTIND:$OPTIND OPTARG:'$OPT
# Nevertheless, let's verify that calling it yet another time doesn't do # Nevertheless, let's verify that calling it yet another time doesn't do
# anything weird: # anything weird:
getopts ":ac" var -a -b -c; echo "5 rc:$? var:'$var' OPTIND:$OPTIND OPTARG:'$OPTARG'" getopts ":ac" var -a -b -c; echo "5 rc:$? var:'$var' OPTIND:$OPTIND OPTARG:'$OPTARG'"
) 2>&1 \
| sed -e 's/ unrecognized option: / invalid option -- /' \
-e 's/ illegal option -- / invalid option -- /' \

View File

@ -10,6 +10,8 @@
# We check that loop does not stop on unknown option (sets "?"), # We check that loop does not stop on unknown option (sets "?"),
# stops on _first_ non-option argument. # stops on _first_ non-option argument.
(
echo "*** no OPTIND, optstring:'ab' args:-a -b c" echo "*** no OPTIND, optstring:'ab' args:-a -b c"
var=QWERTY var=QWERTY
while getopts "ab" var -a -b c; do while getopts "ab" var -a -b c; do
@ -73,3 +75,7 @@ while getopts "ab" var -a -c -b d; do
echo "var:'$var' OPTIND:$OPTIND" echo "var:'$var' OPTIND:$OPTIND"
done done
echo "exited: rc:$? var:'$var' OPTIND:$OPTIND" echo "exited: rc:$? var:'$var' OPTIND:$OPTIND"
) 2>&1 \
| sed -e 's/ unrecognized option: / invalid option -- /' \
-e 's/ illegal option -- / invalid option -- /' \

View File

@ -1,10 +1,12 @@
# This test can fail with libc with buggy getopt() implementation. # This test can fail with libc with buggy getopt() implementation.
# If getopt() wants to parse multi-option args (-abc), # If getopt() wants to parse multi-option args (-abc),
# it needs to remember a position withit current arg. # it needs to remember a position within current arg.
# #
# If this position is kept as a POINTER, not an offset, # If this position is kept as a POINTER, not an offset,
# and if argv[] ADDRESSES (not contents!) change, it blows up. # and if argv[] ADDRESSES (not contents!) change, it blows up.
(
echo "*** optstring:'ac' args:-a -b -c -d e" echo "*** optstring:'ac' args:-a -b -c -d e"
getopts "ac" var -a -b -c -d e; echo "1 rc:$? var:'$var' OPTIND:$OPTIND OPTARG:'$OPTARG'" getopts "ac" var -a -b -c -d e; echo "1 rc:$? var:'$var' OPTIND:$OPTIND OPTARG:'$OPTARG'"
getopts "ac" var -a -b -c -d e; echo "2 rc:$? var:'$var' OPTIND:$OPTIND OPTARG:'$OPTARG'" getopts "ac" var -a -b -c -d e; echo "2 rc:$? var:'$var' OPTIND:$OPTIND OPTARG:'$OPTARG'"
@ -24,7 +26,7 @@ VAR111=NEWVAL; getopts "ac" var -a -b -c -d e; echo "3 rc:$? var:'$var' OPTIND:$
VAR222=NEWVAL; getopts "ac" var -a -b -c -d e; echo "4 rc:$? var:'$var' OPTIND:$OPTIND OPTARG:'$OPTARG'" VAR222=NEWVAL; getopts "ac" var -a -b -c -d e; echo "4 rc:$? var:'$var' OPTIND:$OPTIND OPTARG:'$OPTARG'"
VAR333=NEWVAL; getopts "ac" var -a -b -c -d e; echo "5 rc:$? var:'$var' OPTIND:$OPTIND OPTARG:'$OPTARG'" VAR333=NEWVAL; getopts "ac" var -a -b -c -d e; echo "5 rc:$? var:'$var' OPTIND:$OPTIND OPTARG:'$OPTARG'"
# Sligntly different attempts to force reallocations # Slightly different attempts to force reallocations
echo echo
echo "*** optstring:'ac' args:-a -b -c -d e" echo "*** optstring:'ac' args:-a -b -c -d e"
@ -36,3 +38,7 @@ export VAR222; getopts "ac" var -a -b -c -d e; echo "4 rc:$? var:'$var' OPTIND:$
export VAR333; getopts "ac" var -a -b -c -d e; echo "5 rc:$? var:'$var' OPTIND:$OPTIND OPTARG:'$OPTARG'" export VAR333; getopts "ac" var -a -b -c -d e; echo "5 rc:$? var:'$var' OPTIND:$OPTIND OPTARG:'$OPTARG'"
# All copies of code above should generate identical output # All copies of code above should generate identical output
) 2>&1 \
| sed -e 's/ unrecognized option: / invalid option -- /' \
-e 's/ illegal option -- / invalid option -- /' \