26754: tweak zgetdir() and test for realpath()
[zsh.git] / Test / C02cond.ztst
blobde82dcbe2b358e5ceaa072b223ff84e4f32e6765
1 # Tests corresponding to the texinfo node `Conditional Expressions'
3 %prep
5   umask 077
7   mkdir cond.tmp
9   cd cond.tmp
11   touch unmodified
13   touch zerolength
14   chgrp $EGID zerolength
16   print 'Garbuglio' >nonzerolength
18   mkdir modish
19   chgrp $EGID modish
21   chmod 7710 modish  # g+xs,u+s,+t
22   chmod g+s modish   # two lines combined work around chmod bugs
24   touch unmodish
25   chmod 000 unmodish
27   print 'MZ' > cmd.exe
28   chmod +x cmd.exe
29 %test
31   [[ -a zerolength && ! -a nonexistent ]]
32 0:-a cond
34   # Find a block special file system.  This is a little tricky.
35   block=$(find /dev(|ices)/ -type b -print)
36   if [[ -n $block ]]; then
37     [[ -b $block[(f)1] && ! -b zerolength ]]
38   else
39     print -u$ZTST_fd 'Warning: Not testing [[ -b blockdevice ]] (no devices found)'
40     [[ ! -b zerolength ]]
41   fi
42 0D:-b cond
44   # Use hardcoded /dev/tty because globbing inside /dev fails on Cygwin
45   char=/dev/tty
46   [[ -c $char && ! -c $zerolength ]]
47 0:-c cond
49   [[ -d . && ! -d zerolength ]]
50 0:-d cond
52   [[ -e zerolength && ! -e nonexistent ]]
53 0:-e cond
55   if [[ -n $block ]]; then
56     [[ -f zerolength && ! -f cond && ! -f $char && ! -f $block[(f)1] && ! -f . ]]
57   else
58     print -u$ZTST_fd 'Warning: Not testing [[ -f blockdevice ]] (no devices found)'
59     [[ -f zerolength && ! -f cond && ! -f $char && ! -f . ]]
60   fi
61 0:-f cond
63   [[ -g modish && ! -g zerolength ]]
64 0:-g cond
66   ln -s zerolength link
67   [[ -h link && ! -h zerolength ]]
68 0:-h cond
70   [[ -k modish && ! -k zerolength ]]
71 0:-k cond
73   foo=foo
74   bar=
75   [[ -n $foo && ! -n $bar && ! -n '' ]]
76 0:-n cond
78   [[ -o rcs && ! -o norcs && -o noerrexit && ! -o errexit ]]
79 0:-o cond
81   if ! grep '#define HAVE_FIFOS' $ZTST_testdir/../config.h; then
82     print -u$ZTST_fd 'Warning: Not testing [[ -p pipe ]] (FIFOs not supported)'
83     [[ ! -p zerolength ]]
84   else
85     if whence mkfifo && mkfifo pipe || mknod pipe p; then
86       [[ -p pipe && ! -p zerolength ]]
87     else
88       print -u$ZTST_fd 'Warning: Not testing [[ -p pipe ]] (cannot create FIFO)'
89       [[ ! -p zerolength ]]
90     fi
91   fi
92 0dD:-p cond
94   if (( EUID == 0 )); then
95     print -u$ZTST_fd 'Warning: Not testing [[ ! -r file ]] (root reads anything)'
96     [[ -r zerolength && -r unmodish ]]
97   elif [[ $OSTYPE = cygwin ]]; then
98     print -u$ZTST_fd 'Warning: Not testing [[ ! -r file ]]
99    (all files created by user may be readable)'
100    [[ -r zerolength ]]
101   else
102     [[ -r zerolength && ! -r unmodish ]]
103   fi
104 0:-r cond
106   [[ -s nonzerolength && ! -s zerolength ]]
107 0:-s cond
109 # no simple way of guaranteeing test for -t
111   [[ -u modish && ! -u zerolength ]]
112 0:-u cond
114   [[ -x cmd.exe && ! -x zerolength ]]
115 0:-x cond
117   [[ -z $bar && -z '' && ! -z $foo ]]
118 0:-z cond
120   [[ -L link && ! -L zerolength ]]
121 0:-L cond
123 # hard to guarantee a file not owned by current uid
124   [[ -O zerolength ]]
125 0:-O cond
127   [[ -G zerolength ]]
128 0:-G cond
130 # can't be bothered with -S
132   print -u $ZTST_fd 'This test takes two seconds...'
133   sleep 2
134   cat unmodified
135   touch newnewnew
136   if [[ $OSTYPE == "cygwin" ]]; then
137     print -u$ZTST_fd "Warning: not testing [[ -N file ]] (not supported on Cygwin)"
138     true
139   elif [[ "$(find . -prune -fstype nfs 2>/dev/null)" == "." ]]; then
140     print -u$ZTST_fd "Warning: not testing [[ -N file ]] (not supported with NFS)"
141     true
142   else
143     [[ -N newnewnew && ! -N unmodified ]]
144   fi
145 0:-N cond
146 F:This test can fail on NFS-mounted filesystems as the access and
147 F:modification times are not updated separately.  The test will fail
148 F:on HFS+ (Apple Mac OS X default) filesystems because access times
149 F:are not recorded.  Also, Linux ext3 filesystems may be mounted
150 F:with the noatime option which does not update access times.
151 F:Failures in these cases do not indicate a problem in the shell.
153   [[ newnewnew -nt zerolength && ! (unmodified -nt zerolength) ]]
154 0:-nt cond
156   [[ zerolength -ot newnewnew && ! (zerolength -ot unmodified) ]]
157 0:-ot cond
159   [[ link -ef zerolength && ! (link -ef nonzerolength) ]]
160 0:-ef cond
162   [[ foo = foo && foo != bar && foo == foo && foo != '' ]]
163 0:=, == and != conds
165   [[ bar < foo && foo > bar ]]
166 0:< and > conds
168   [[ $(( 3 + 4 )) -eq 0x07 && $(( 5 * 2 )) -ne 0x10 ]]
169 0:-eq and -ne conds
171   [[ 3 -lt 04 && 05 -gt 2 ]]
172 0:-lt and -gt conds
174   [[ 3 -le 3 && ! (4 -le 3) ]]
175 0:-le cond
177   [[ 3 -ge 3 && ! (3 -ge 4) ]]
178 0:-ge cond
180   [[ 1 -lt 2 || 2 -lt 2 && 3 -gt 4 ]]
181 0:|| and && in conds
183   if ! grep '#define PATH_DEV_FD' $ZTST_testdir/../config.h; then
184     print -u$ZTST_fd "Warning: not testing [[ -e /dev/fd/0 ]] (/dev/fd not supported)"
185     true
186   else
187     [[ -e /dev/fd/0 ]]
188   fi
189 0dD:/dev/fd support in conds handled by access
191   if ! grep '#define PATH_DEV_FD' $ZTST_testdir/../config.h; then
192     print -u$ZTST_fd "Warning: not testing [[ -O /dev/fd/0 ]] (/dev/fd not supported)"
193     true
194   else
195     [[ -O /dev/fd/0 ]]
196   fi
197 0dD:/dev/fd support in conds handled by stat
199   [[ ( -z foo && -z foo ) || -z foo ]]
200 1:complex conds with skipping
202   [ '' != bar -a '' = '' ]
203 0:strings with `[' builtin
205   [ `echo 0` -lt `echo 1` ]
206 0:substitution in `[' builtin
208   [ -n foo scrimble ]
209 1:argument checking for [ builtin
210 ?(eval):[:1: too many arguments
212   test -n foo scramble
213 1:argument checking for test builtin
214 ?(eval):test:1: too many arguments
216   [ -n foo scrimble scromble ]
217 1:argument checking for [ builtin
218 ?(eval):[:1: too many arguments
220   test -n foo scramble scrumble
221 1:argument checking for test builtin
222 ?(eval):test:1: too many arguments
224   [ -n foo -a -n bar scrimble ]
225 1:argument checking for [ builtin
226 ?(eval):[:1: too many arguments
228   test -n foo -a -z "" scramble
229 1:argument checking for test builtin
230 ?(eval):test:1: too many arguments
232   fn() {
233     # careful: first file must exist to trigger bug
234     [[ -e unmodified ]] || print Where\'s my file\?
235     [[ unmodified -nt NonExistentFile ]]
236     print status = $?
237   }
238   fn
239 0:-nt shouldn't abort on non-existent files
240 >status = 1
242 # core dumps on failure
243   if zmodload -i zsh/regex 2>/dev/null; then
244      echo >regex_test.sh 'if [[ $# = 1 ]]; then
245         if [[ $1 =~ /?[^/]+:[0-9]+:$ ]]; then
246           :
247         fi
248       fi
249       exit 0'
250       $ZTST_testdir/../Src/zsh -f ./regex_test.sh
251   fi
252 0:regex tests shouldn't crash
254 %clean
255   # This works around a bug in rm -f in some versions of Cygwin
256   chmod 644 unmodish