1#! /bin/sh
2#
3# @(#)test.sh	1.7 17/09/22 Copyright 2016 J. Schilling
4#
5
6# Read test core functions
7. ../../common/test-common
8
9#
10# Basic tests to check the test builtin
11#
12
13#
14# POSIX zero args
15# test without arguments fails
16#
17docommand test00 "$SHELL -c 'test || echo FAIL'" 0 "FAIL\n" ""
18
19#
20# POSIX one arg
21# test with a zero length argument fails
22#
23docommand test01 "$SHELL -c 'test \"\" || echo FAIL'" 0 "FAIL\n" ""
24#
25# test with a non zero length argument succeeds
26#
27docommand test02 "$SHELL -c 'test \"a\" && echo OK'" 0 "OK\n" ""
28
29#
30# POSIX two args -> ! + one arg
31# Repeat the same with negation
32#
33docommand test03 "$SHELL -c 'test ! \"\" && echo OK'" 0 "OK\n" ""
34docommand test04 "$SHELL -c 'test ! \"a\" || echo FAIL'" 0 "FAIL\n" ""
35
36#
37# POSIX introduced an incompatible interface change and requires to
38# select the behavior on the number of arguments only.
39# A UNIX test with "-r" as argument complains with "missing argument".
40# A POSIX test with the same argument reports non-zero string length.
41#
42docommand test05 "$SHELL -c 'test \"-r\" && echo OK'" 0 "OK\n" ""
43docommand test06 "$SHELL -c 'test ! \"-r\" || echo FAIL'" 0 "FAIL\n" ""
44
45touch a
46
47#
48# POSIX two args -> -r file
49# Now with a readable file argument, "-r" is UNIX compatible again.
50#
51docommand test07 "$SHELL -c 'test \"-r\" a && echo OK'" 0 "OK\n" ""
52
53if [ "$is_bosh" = true ]; then
54#
55# UNIX kompatibility for "-t" without parameter is important
56#
57docommand test08 "$SHELL -c 'test \"-t\" > /dev/tty && echo OK'" 0 "OK\n" ""
58docommand test09 "$SHELL -c 'set +o posix; test \"-t\" > /dev/null || echo FAIL'" 0 "FAIL\n" ""
59docommand test10 "$SHELL -c 'set -o posix; test \"-t\" > /dev/null && echo OK'" 0 "OK\n" ""
60fi
61
62#
63# Tests for the ksh enhancements -nt/-ot/-ef (newer/older than & equal file)
64#
65expect_fail_save=$expect_fail
66expect_fail=true
67docommand -silent -esilent test20 "$SHELL -c 'test a -nt b && echo nt OK || echo nt BAD'" 0 "nt OK\n" ""
68expect_fail=$expect_fail_save
69if [ "$failed" = true ]; then
70	echo
71	echo "Test $cmd_label is a ksh enhancement."
72	echo "Skipping test20..test21."
73	echo
74else
75docommand test20 "$SHELL -c 'test a -nt b && echo nt OK || echo nt BAD'" 0 "nt OK\n" ""
76docommand test21 "$SHELL -c 'test b -ot a && echo ot OK || echo ot BAD'" 0 "ot OK\n" ""
77docommand test22 "$SHELL -c 'test a -ef a && echo ot OK || echo ot BAD'" 0 "ot OK\n" ""
78fi
79
80
81#
82# More POSIX tests based on the number of arguments
83#
84#
85# POSIX three args
86#
87docommand test30 "$SHELL -c 'test 1 -lt 2 && echo OK'" 0 "OK\n" ""
88docommand test31 "$SHELL -c 'test 1 -a 2 && echo OK'" 0 "OK\n" ""
89docommand test32 "$SHELL -c 'test ! -a 2 && echo OK'" 0 "OK\n" ""
90docommand test33 "$SHELL -c 'test ! -a ! && echo OK'" 0 "OK\n" ""
91docommand test34 "$SHELL -c 'test ! != b && echo OK'" 0 "OK\n" ""
92docommand test35 "$SHELL -c 'test \"\" -a 2 || echo FAIL'" 0 "FAIL\n" ""
93docommand test36 "$SHELL -c 'test \"\" -o 2 && echo OK'" 0 "OK\n" ""
94docommand test37 "$SHELL -c 'test 2 -eq 2 && echo OK'" 0 "OK\n" ""
95docommand test38 "$SHELL -c 'test 0 -eq 2 || echo FAIL'" 0 "FAIL\n" ""
96#
97# non-valid binary operator
98#
99docommand test50 "$SHELL -c 'test bla -r a || echo FAIL'" 0 "FAIL\n" NONEMPTY
100
101
102#
103# POSIX three args -> ! + two args
104#
105docommand test80 "$SHELL -c 'test ! -r a || echo FAIL'" 0 "FAIL\n" ""
106docommand test81 "$SHELL -c 'test ! -r b && echo OK'" 0 "OK\n" ""
107#
108# POSIX three args -> ! + ! + one arg
109#
110docommand test82 "$SHELL -c 'test ! ! bla && echo OK'" 0 "OK\n" ""
111
112#
113# POSIX three args -> ( $2 ) () + one arg
114#
115docommand test83 "$SHELL -c 'test \( \"bla\" \) && echo OK'" 0 "OK\n" ""
116docommand test84 "$SHELL -c 'test \( \"-r\" \) && echo OK'" 0 "OK\n" ""
117
118#
119# POSIX four args -> ! + three args
120#
121docommand test85 "$SHELL -c 'test ! one \"-a\" two || echo FAIL'" 0 "FAIL\n" ""
122docommand test86 "$SHELL -c 'test ! ! \"-a\" two || echo FAIL'" 0 "FAIL\n" ""
123
124#
125# POSIX four args -> ( $2 $3 ) + () two args
126#
127docommand test125 "$SHELL -c 'test \( \"-r\" a \) && echo OK'" 0 "OK\n" ""
128docommand test126 "$SHELL -c 'test \( ! \"-r\" \) || echo FAIL'" 0 "FAIL\n" ""
129docommand test127 "$SHELL -c 'test \( ! \"\" \) && echo OK'" 0 "OK\n" ""
130
131
132docommand test140 "$SHELL -c 'test  \"-r\" \) || echo FAIL'" 0 "FAIL\n" ""
133docommand test141 "$SHELL -c 'test ! \"-r\" \) && echo OK'" 0 "OK\n" ""
134#
135# Fail because ")" is missing
136#
137docommand test142 "$SHELL -c 'test \( \"-r\" || echo FAIL'" 0 "FAIL\n" IGNORE
138#
139# Fail because ")" is missing
140#
141docommand test143 "$SHELL -c 'test \( \"-r\" a || echo FAIL'" 0 "FAIL\n" IGNORE
142
143#
144# This case is unspecified by POSIX, so there may be an error message
145#
146docommand test150 "$SHELL -c 'test \( \) || echo FAIL'" 0 "FAIL\n" IGNORE
147#
148# The 3 and 4 argument POSIX case with ( )
149#
150docommand test151 "$SHELL -c 'test \( a \) && echo OK'" 0 "OK\n" IGNORE
151docommand test152 "$SHELL -c 'test ! \( a \) || echo FAIL'" 0 "FAIL\n" IGNORE
152#
153# This is the "most complex" case defined by POSIX
154#
155docommand test153 "$SHELL -c 'test ! 2 -lt 1 && echo OK'" 0 "OK\n" ""
156
157#
158# Tests to check test builtin syntax error behavior
159#
160# POSIX hat test ge�ndert: mit einem Argument ist immer strlen(a) > 0 gemeint
161#
162docommand test200 "$SHELL -c 'test -r; echo \$?'" 0 "0\n" ""
163
164docommand test250 "$SHELL -c 'test 123 -eq; echo \$?'" 0 "2\n" NONEMPTY
165docommand test251 "$SHELL -c '[ 123 -eq ]; echo \$?'" 0 "2\n" NONEMPTY
166docommand test252 "$SHELL -c 'test 123 -gt 1XX; echo \$?'" 0 "2\n" NONEMPTY
167
168remove a
169success
170
171exit
172
173From mksh:
174        test 2005/10/08 \< 2005/08/21 && echo ja || echo nein
175        test 2005/08/21 \< 2005/10/08 && echo ja || echo nein
176        test 2005/10/08 \> 2005/08/21 && echo ja || echo nein
177        test 2005/08/21 \> 2005/10/08 && echo ja || echo nein
178expected-stdout:
179        nein
180        ja
181        ja
182        nein
183