Skip to content

Commit

Permalink
Update tests for POSIX.1-2024
Browse files Browse the repository at this point in the history
This commit adds test cases for the requirements that were added in
POSIX.1-2024 and are already implemented in yash.

- Some test cases in bg-p.tst and fg-p.tst no longer examines the
  standard error because POSIX allows the shell to report suspension of
  background jobs, which should not fail the test.
- Some comments in the test scripts are updated to reflect the changes
  to POSIX.
  • Loading branch information
magicant committed Nov 4, 2024
1 parent 6e8e9f0 commit 00c7179
Show file tree
Hide file tree
Showing 14 changed files with 132 additions and 73 deletions.
30 changes: 23 additions & 7 deletions tests/POSIX
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,28 @@ Volume 3: Shell & Utilities
* error-p.tst

2.9.1 Simple Commands

2.9.1.1 Order of Processing
* simple-p.tst

2.9.1.2 Variable Assignments
* builtins-p.tst
* simple-p.tst

2.9.1.3 Commands with no Command Name
* redir-p.tst
* simple-p.tst

Command Search and Execution
2.9.1.4 Command Search and Execution
* builtins-p.tst
* builtins-y.tst
* simple-p.tst

2.9.1.5 Standard File Descriptors

2.9.1.6 Non-built-in Utility Execution
* simple-p.tst

2.9.2 Pipelines
* pipeline-p.tst
* pipeline-y.tst
Expand Down Expand Up @@ -126,21 +139,24 @@ Command Search and Execution
* trap-p.tst
* wait-p.tst

2.12 Shell Execution Environment
2.12 Job Control
* job-p.tst

2.13 Shell Execution Environment
* trap-p.tst

2.13 Pattern Matching Notation
2.14 Pattern Matching Notation

2.13.1 Patterns Matching a Single Character
2.14.1 Patterns Matching a Single Character
* fnmatch-p.tst

2.13.2 Patterns Matching Multiple Characters
2.14.2 Patterns Matching Multiple Characters
* fnmatch-p.tst

2.13.3 Patterns Used for Filename Expansion
2.14.3 Patterns Used for Filename Expansion
* path-p.tst

2.14 Special Built-In Utilities
2.15 Special Built-In Utilities
* builtins-p.tst

break
Expand Down
14 changes: 7 additions & 7 deletions tests/bg-p.tst
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ test_O -d -e n 'bg cannot be used when job control is disabled' +m
bg
__IN__

test_oE 'default operand chooses most recently suspended job' -m
test_o 'default operand chooses most recently suspended job' -m
:&
sh -c 'kill -s STOP $$; echo 1'
bg >/dev/null
Expand All @@ -30,21 +30,21 @@ bg >/dev/null
kill %
__IN__

test_OE -e 17 'resumed job is awaitable' -m
test_O -e 17 'resumed job is awaitable' -m
sh -c 'kill -s STOP $$; exit 17'
bg >/dev/null
wait %
__IN__

test_oE 'resumed job is in background' -m
test_o 'resumed job is in background' -m
sh -c 'kill -s STOP $$; ../checkfg || echo bg'
bg >/dev/null
wait %
__IN__
bg
__OUT__

test_oE 'specifying job ID' -m
test_o 'specifying job ID' -m
./job1
./job2
echo -
Expand All @@ -57,7 +57,7 @@ __IN__

__OUT__

test_oE 'specifying more than one job ID' -m
test_o 'specifying more than one job ID' -m
./job1
./job2
echo -
Expand All @@ -69,13 +69,13 @@ __IN__

__OUT__

test_OE -e 0 'bg prints resumed job' -m
test_O -e 0 'bg prints resumed job' -m
sleep 1&
bg >bg.out
grep -q '^\[[[:digit:]][[:digit:]]*][[:blank:]]*sleep 1' bg.out
__IN__

test_OE -e 0 'exit status of bg' -m
test_O -e 0 'exit status of bg' -m
sh -c 'kill -s STOP $$; exit 17'
bg >/dev/null
__IN__
Expand Down
5 changes: 1 addition & 4 deletions tests/builtins-p.tst
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,6 @@ test_nonspecial_builtin_function_override "$LINENO" getopts
test_nonspecial_builtin_function_override "$LINENO" hash
test_nonspecial_builtin_function_override "$LINENO" jobs
test_nonspecial_builtin_function_override "$LINENO" kill
test_nonspecial_builtin_function_override "$LINENO" newgrp
test_nonspecial_builtin_function_override "$LINENO" pwd
test_nonspecial_builtin_function_override "$LINENO" read
test_nonspecial_builtin_function_override "$LINENO" true
Expand All @@ -237,6 +236,7 @@ test_nonspecial_builtin_function_override "$LINENO" unalias
test_nonspecial_builtin_function_override "$LINENO" wait

test_nonspecial_builtin_function_override "$LINENO" grep
test_nonspecial_builtin_function_override "$LINENO" newgrp
test_nonspecial_builtin_function_override "$LINENO" sed

(
Expand Down Expand Up @@ -353,9 +353,6 @@ test_OE -e 0 'intrinsic built-in kill can be invoked without $PATH'
kill -0 $$
__IN__

# Many shells including yash does not implement newgrp as a built-in.
#TODO: test_OE -e 0 'intrinsic built-in newgrp can be invoked without $PATH'

test_E -e 0 'intrinsic built-in pwd can be invoked without $PATH'
pwd
__IN__
Expand Down
1 change: 0 additions & 1 deletion tests/command-y.tst
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ trap: a special built-in
unset: a special built-in
__OUT__

# `newgrp' is not a mandatory built-in in yash.
test_oE -e 0 'describing mandatory built-ins (-V)'
command -V alias bg cd command false fg getopts hash jobs kill pwd read true \
type umask unalias wait
Expand Down
7 changes: 7 additions & 0 deletions tests/dot-p.tst
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ __IN__
exit 11
__ERR__

test_oE -e 3 'option-operand separator'
(exit 5)
. -- ./file1
__IN__
5
__OUT__

(
# Ensure $PWD is safe to assign to $PATH
case $PWD in (*[:%]*)
Expand Down
11 changes: 11 additions & 0 deletions tests/exec-p.tst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ __IN__
reached
__OUT__

test_oE 'exec without arguments but -- separator'
exec --
echo $?
__IN__
0
__OUT__

test_Oe 'exec with redirections'
exec >&2 2>/dev/null
echo reached
Expand Down Expand Up @@ -41,6 +48,10 @@ test_OE -e 0 'executing external command with option'
exec cat -u /dev/null
__IN__

test_OE -e 0 'executing external command with -- separator'
exec -- cat /dev/null
__IN__

test_OE -e 0 'process ID of executed process'
exec sh -c "[ \$\$ -eq $$ ]"
__IN__
Expand Down
6 changes: 3 additions & 3 deletions tests/exit-p.tst
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ trap '(exit 1); exit' EXIT
exit
__IN__

# POSIX says the exit status in this case should be that of "the command that
# executed immediately preceding the trap action." Many shells including yash
# interprets it as the exit status of "exit" rather than "trap."
# POSIX says the exit status in this case should be "the value (of the special
# parameter '?') it had immediately preceding the trap action." Many shells
# including yash interpret it as the exit status of "exit" rather than "trap."
test_OE -e 1 'default exit status in EXIT trap in exiting with 1'
trap exit EXIT
exit 1
Expand Down
13 changes: 9 additions & 4 deletions tests/fg-p.tst
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ test_O -d -e n 'fg cannot be used when job control is disabled' +m
fg
__IN__

test_oE 'default operand chooses most recently suspended job' -m
test_o 'default operand chooses most recently suspended job' -m
:&
sh -c 'kill -s STOP $$; echo 1'
fg >/dev/null
__IN__
1
__OUT__

test_oE 'resumed job is in foreground' -m
test_o 'resumed job is in foreground' -m
sh -c 'kill -s STOP $$; ../checkfg && echo fg'
fg >/dev/null
__IN__
Expand All @@ -45,7 +45,7 @@ fg >/dev/null
wait $!
__IN__

test_oE 'specifying job ID' -m
test_o 'specifying job ID' -m
./job1
./job2
fg %./job1 >/dev/null
Expand All @@ -57,7 +57,7 @@ a
b
__OUT__

test_oE 'fg prints resumed job' -m
test_o 'fg prints resumed job' -m
./job1
fg
__IN__
Expand All @@ -66,6 +66,11 @@ __IN__
2
__OUT__

test_x -e 42 'exit status of fg' -m
sh -c 'kill -s STOP $$; exit 42'
fg
__IN__

test_O -d -e n 'no existing job' -m
fg
__IN__
Expand Down
25 changes: 23 additions & 2 deletions tests/getopts-p.tst
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,34 @@ getopts ab:c o -a -b arg -c
getopts ab:c o -a -b arg -c
__IN__

test_x -e n 'exit status is non-zero after parsing all options' -e
test_x -e 1 'exit status is one after parsing all options' -e
getopts ab:c o -a -b arg -c
getopts ab:c o -a -b arg -c
getopts ab:c o -a -b arg -c
getopts ab:c o -a -b arg -c
__IN__

test_o 'OPTIND is set when option argument is parsed: empty'
getopts a:b o -a '' -b
echo "[$OPTIND]"
__IN__
[3]
__OUT__

test_o 'OPTIND is set when option argument is parsed: non-empty separate'
getopts a:b o -a '-x foo' -b
echo "[$OPTIND]"
__IN__
[3]
__OUT__

test_o 'OPTIND is set when option argument is parsed: non-empty adjoined'
getopts a:b o -a' foo' -b
echo "[$OPTIND]"
__IN__
[2]
__OUT__

test_o 'OPTARG is set when option argument is parsed: empty'
getopts a: o -a ''
echo "[$OPTARG]"
Expand Down Expand Up @@ -163,7 +184,7 @@ __IN__
4[?]
__OUT__

test_x -e n 'single hyphen is not option'
test_x -e 1 'single hyphen is not an option but an operand'
getopts '' x -
__IN__

Expand Down
9 changes: 9 additions & 0 deletions tests/read-p.tst
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,15 @@ __IN__
0 [A] [B] [] [D] [E] [- F\]
__OUT__

test_oE 'input ending without newline'
printf 'A' | {
read a
echo $? $a
}
__IN__
1 A
__OUT__

test_oE 'in subshell'
(echo A | read a)
echoraw $? "[${a-unset}]"
Expand Down
11 changes: 0 additions & 11 deletions tests/read-y.tst
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,6 @@ typeset b=''
typeset c=''
__OUT__

test_oE 'input ending without newline'
printf 'A' | {
read a
echo $?
typeset -p a
}
__IN__
1
typeset a=A
__OUT__

test_oE 'input ending with backslash - not raw mode'
printf '%s' 'A\' | {
read a
Expand Down
38 changes: 38 additions & 0 deletions tests/test-p.tst
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ chmod u+s setuserid

mkfifo fifo

ln file hardlink
ln -s file filelink
ln -s _no_such_file_ brokenlink
ln -s unreadable unreadablelink
Expand All @@ -34,6 +35,11 @@ ln -s setgroupid setgroupidlink
ln -s setuserid setuseridlink
ln -s fifo fifolink

touch -t 200001010000 older
touch -t 200101010000 newer
touch -a -t 200101010000 old; touch -m -t 200001010000 old
touch -a -t 200001010000 new; touch -m -t 200101010000 new

# $1 = $LINENO, $2 = expected exit status, $3... = expression
assert() (
setup <<\__END__
Expand Down Expand Up @@ -334,6 +340,38 @@ assert_false 0 -le -3
assert_false 90 -le -3
assert_true 0 -le 0

# The behavior of the < and > operators cannot be fully tested.
assert_false 11 '<' 100
assert_false 11 '<' 11
assert_true 100 '<' 11

assert_true 11 '>' 100
assert_false 11 '>' 11
assert_false 100 '>' 11

assert_true XXXXX -ot newer
assert_false XXXXX -ot XXXXX
assert_false newer -ot XXXXX
assert_true older -ot newer
assert_false newer -ot newer
assert_false newer -ot older

assert_false XXXXX -nt newer
assert_false XXXXX -nt XXXXX
assert_true newer -nt XXXXX
assert_false older -nt newer
assert_false older -nt older
assert_true newer -nt older

assert_false XXXXX -ef newer
assert_false XXXXX -ef XXXXX
assert_false newer -ef XXXXX
assert_false older -ef newer
assert_true older -ef older
assert_false newer -ef older
assert_true file -ef hardlink
assert_false file -ef newer

assert_false "" -a ""
assert_false "" -a 1
assert_false 1 -a ""
Expand Down
Loading

0 comments on commit 00c7179

Please sign in to comment.