Skip to content

Commit

Permalink
Various cleanups, mostly in regression tests
Browse files Browse the repository at this point in the history
src/cmd/ksh93/data/variables.sh: shtab_variables[]:
- Remove unused "CSWIDTH" entry. All use of it (including the
  matching CSWIDTHNOD macro) was removed in version 2003-04-22.

src/cmd/ksh93/tests/variables.sh:
- For the tests on the shtab_variables[] variables, read the
  variable names straight from variables.c instead of synching
  the list in the test script, which would surely be forgotten.

src/cmd/ksh93/tests/*.sh:
- Fix a number of mistaken tries to count errors from a subshell.
- Fix miscellaneous minor breakage and typos.
  • Loading branch information
McDutchie committed Jan 21, 2022
1 parent f792c4b commit 3f8add4
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 87 deletions.
3 changes: 0 additions & 3 deletions src/cmd/ksh93/data/variables.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,6 @@ const struct shtable2 shtab_variables[] =
".sh.pid", NV_INTEGER|NV_NOFREE, (char*)0,
".sh.tilde", 0, (char*)0,
"SHLVL", NV_INTEGER|NV_NOFREE|NV_EXPORT, (char*)0,
#if SHOPT_MULTIBYTE
"CSWIDTH", 0, (char*)0,
#endif /* SHOPT_MULTIBYTE */
"", 0, (char*)0
};

Expand Down
5 changes: 3 additions & 2 deletions src/cmd/ksh93/tests/case.sh
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,9 @@ got=$(eval 'case x in (foo);; (if);; esac' 2>&1) || err_exit "'(' + 'if' as nth
# ======
# Verify an invalid character class name is handled without a SIGSEGV or similar failure
# https://github.com/att/ast/issues/1409
got="$($SHELL -c 'case x in [x[:bogus:]]) echo x ;; esac')"
[[ -z $got ]] || err_exit "invalid char class name (got $(printf %q "$got"))"
got=$(set +x; { "$SHELL" -c 'case x in [x[:bogus:]]) echo x ;; esac'; } 2>&1)
((!(e = $?))) && [[ -z $got ]] || err_exit 'use of invalid character class name' \
"(got status $e$( ((e>128)) && print -n /SIG && kill -l "$e"), $(printf %q "$got"))"

# ======
exit $((Errors<125?Errors:125))
4 changes: 2 additions & 2 deletions src/cmd/ksh93/tests/coprocess.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
. "${SHTESTS_COMMON:-${0%/*}/_common}"

if [[ -d /cygdrive ]]
then err_exit 'Cygwin detected - coprocess tests disabled - enable at the risk of wedging your system'
exit $((Errors))
then warning 'Cygwin detected - coprocess tests disabled - enable at the risk of wedging your system'
exit 0
fi

bintrue=$(whence -p true)
Expand Down
7 changes: 4 additions & 3 deletions src/cmd/ksh93/tests/grep.sh
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,12 @@ and some lines contain bar only.
However, many lines contain both foo and also bar.
A line containing foobar should also be counted.
There should be six lines with foo and bar.
There are only two line with out foo but with bar.
There are only two lines without foo but with bar.
!

if (( $(grep -c 'foo*bar' $tmp/grep ) != 6))
then err_exit
got=$(grep -c 'foo*bar' $tmp/grep 2>&1)
if [[ $got != 6 ]]
then err_exit "shell version of grep fails (expected 6, got $(printf %q "$got"))"
fi

exit $((Errors<125?Errors:125))
24 changes: 18 additions & 6 deletions src/cmd/ksh93/tests/subshell.sh
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,22 @@ z.bar[1]=(x=12 y=5)
eval val="$z"
(
z.foo[three]=good
[[ ${z.foo[three]} == good ]] || err_exit 'associative array assignment in subshell not working'
)
[[ ${z.foo[three]} == good ]]
) || err_exit 'associative array assignment in subshell not working'
[[ $z == "$val" ]] || err_exit 'compound variable changes after associative array assignment'
eval val="$z"
(
z.foo[two]=ok
[[ ${z.foo[two]} == ok ]] || err_exit 'associative array assignment to compound variable in subshell not working'
[[ ${z.foo[two]} == ok ]] || exit 101
z.bar[1]=yes
[[ ${z.bar[1]} == yes ]] || err_exit 'indexed array assignment to compound variable in subshell not working'
[[ ${z.bar[1]} == yes ]] || exit 102
)
case $? in
0) ;;
101) err_exit 'associative array assignment to compound variable in subshell not working' ;;
102) err_exit 'indexed array assignment to compound variable in subshell not working' ;;
*) err_exit 'assignment to compound variable in subshell fails' ;;
esac
[[ $z == "$val" ]] || err_exit 'compound variable changes after associative array assignment'

x=(
Expand All @@ -84,10 +90,16 @@ x=(
eval val="$x"
(
unset x.foo
[[ ${x.foo.qqq} ]] && err_exit 'x.foo.qqq should be unset'
[[ ${x.foo.qqq} ]] && exit 101
x.foo=good
[[ ${x.foo} == good ]] || err_exit 'x.foo should be good'
[[ ${x.foo} == good ]] || exit 102
)
case $? in
0) ;;
101) err_exit 'x.foo.qqq should be unset' ;;
102) err_exit 'x.foo should be good' ;;
*) err_exit "x.foo fails" ;;
esac
[[ $x == "$val" ]] || err_exit 'compound variable changes after unset leaves'
unset l
(
Expand Down
94 changes: 23 additions & 71 deletions src/cmd/ksh93/tests/variables.sh
Original file line number Diff line number Diff line change
Expand Up @@ -619,8 +619,10 @@ chmod +x $tmp/script
[[ $(fun .sh.subshell) == 2 ]] || err_exit ".sh.subshell not working for functions in subshells"
(( .sh.subshell == 1 )) || err_exit ".sh.subshell not working in a subshell"
)
TIMEFORMAT='this is a test'
[[ $(set +x; { { time :;} 2>&1;}) == "$TIMEFORMAT" ]] || err_exit 'TIMEFORMAT not working'
(
TIMEFORMAT='this is a test'
[[ $(set +x; { { time :;} 2>&1;}) == "$TIMEFORMAT" ]]
) || err_exit 'TIMEFORMAT not working'
alias _test_alias=true
: ${.sh.version}
[[ $(alias _test_alias) == *.sh.* ]] && err_exit '.sh. prefixed to alias name'
Expand Down Expand Up @@ -965,75 +967,25 @@ actual=$(env SHLVL="2#11+x[\$(env echo Exploited vuln CVE-2019-14868 >&2)0]" "$S
# ======
# Check unset, attribute and cleanup/restore behavior of special variables.

# Keep the list in sync (minus ".sh") with shtab_variables[] in src/cmd/ksh93/data/variables.c
set -- \
"PATH" \
"PS1" \
"PS2" \
"IFS" \
"PWD" \
"HOME" \
"MAIL" \
"REPLY" \
"SHELL" \
"EDITOR" \
"MAILCHECK" \
"RANDOM" \
"ENV" \
"HISTFILE" \
"HISTSIZE" \
"HISTEDIT" \
"HISTCMD" \
"FCEDIT" \
"CDPATH" \
"MAILPATH" \
"PS3" \
"OLDPWD" \
"VISUAL" \
"COLUMNS" \
"LINES" \
"PPID" \
"_" \
"TMOUT" \
"SECONDS" \
"LINENO" \
"OPTARG" \
"OPTIND" \
"PS4" \
"FPATH" \
"LANG" \
"LC_ALL" \
"LC_COLLATE" \
"LC_CTYPE" \
"LC_MESSAGES" \
"LC_NUMERIC" \
"LC_TIME" \
"FIGNORE" \
"KSH_VERSION" \
"JOBMAX" \
".sh.edchar" \
".sh.edcol" \
".sh.edtext" \
".sh.edmode" \
".sh.name" \
".sh.subscript" \
".sh.value" \
".sh.version" \
".sh.dollar" \
".sh.match" \
".sh.command" \
".sh.file" \
".sh.fun" \
".sh.lineno" \
".sh.subshell" \
".sh.level" \
".sh.stats" \
".sh.math" \
".sh.pool" \
".sh.pid" \
".sh.tilde" \
"SHLVL" \
"CSWIDTH"
# ... to avoid forgetting to keep this script synched with shtab_variables[], read from the source
set -- $(
srcdir=${SHTESTS_COMMON%/tests/*}
redirect < $srcdir/data/variables.c || exit
# skip lines until finding shtab_variables struct
while read -r line || exit
do [[ $line == *" shtab_variables[] =" ]] && break
done
read -r line
[[ $line == '{' ]] || exit
# read variable names until '};'
IFS=\"
while read -r first varname junk
do [[ $first == '};' ]] && exit
[[ -z $junk ]] && continue
[[ -n $varname && $varname != '.sh' ]] && print -r -- "$varname"
done
)
(($# >= 66)) || err_exit "could not read shtab_variables[]; adjust test script ($# items read)"

# ... unset
$SHELL -c '
Expand Down

0 comments on commit 3f8add4

Please sign in to comment.