1# Tests corresponding to the texinfo node `Conditional Expressions'
2
3%prep
4
5  umask 077
6
7  mkdir cond.tmp
8
9  cd cond.tmp
10
11  touch	unmodified
12
13  touch zerolength
14  chgrp $EGID zerolength
15
16  print 'Garbuglio' >nonzerolength
17
18  mkdir modish
19  chgrp $EGID modish
20
21  chmod 7710 modish  # g+xs,u+s,+t
22  chmod g+s modish   # two lines combined work around chmod bugs
23
24  touch unmodish
25  chmod 000 unmodish
26
27  print 'MZ' > cmd.exe
28  chmod +x cmd.exe
29%test
30
31  [[ -a zerolength && ! -a nonexistent ]]
320:-a cond
33
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
420D:-b cond
43
44  # Use hardcoded /dev/tty because globbing inside /dev fails on Cygwin
45  char=/dev/tty
46  [[ -c $char && ! -c $zerolength ]]
470:-c cond
48
49  [[ -d . && ! -d zerolength ]]
500:-d cond
51
52  [[ -e zerolength && ! -e nonexistent ]]
530:-e cond
54
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
610:-f cond
62
63  [[ -g modish && ! -g zerolength ]]
640:-g cond
65
66  ln -s zerolength link
67  [[ -h link && ! -h zerolength ]]
680:-h cond
69
70  [[ -k modish && ! -k zerolength ]]
710:-k cond
72
73  foo=foo
74  bar=
75  [[ -n $foo && ! -n $bar && ! -n '' ]]
760:-n cond
77
78  [[ -o rcs && ! -o norcs && -o noerrexit && ! -o errexit ]]
790:-o cond
80
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
920dD:-p cond
93
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
1040:-r cond
105
106  [[ -s nonzerolength && ! -s zerolength ]]
1070:-s cond
108
109# no simple way of guaranteeing test for -t
110
111  [[ -u modish && ! -u zerolength ]]
1120:-u cond
113
114  [[ -x cmd.exe && ! -x zerolength ]]
1150:-x cond
116
117  [[ -z $bar && -z '' && ! -z $foo ]]
1180:-z cond
119
120  [[ -L link && ! -L zerolength ]]
1210:-L cond
122
123# hard to guarantee a file not owned by current uid
124  [[ -O zerolength ]]
1250:-O cond
126
127  [[ -G zerolength ]]
1280:-G cond
129
130# can't be bothered with -S
131
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  elif test -f /etc/mtab && { grep $(df . 2>/dev/null| tail -n1 | awk '{print $1}') /etc/mtab | grep -q noatime; }; then
143    print -u$ZTST_fd "Warning: not testing [[ -N file ]] (not supported with noatime file system)"
144    true
145  else
146    [[ -N newnewnew && ! -N unmodified ]]
147  fi
1480:-N cond
149F:This test can fail on NFS-mounted filesystems as the access and
150F:modification times are not updated separately.  The test will fail
151F:on HFS+ (Apple Mac OS X default) filesystems because access times
152F:are not recorded.  Also, Linux ext3 filesystems may be mounted
153F:with the noatime option which does not update access times.
154F:Failures in these cases do not indicate a problem in the shell.
155
156  [[ newnewnew -nt zerolength && ! (unmodified -nt zerolength) ]]
1570:-nt cond
158
159  [[ zerolength -ot newnewnew && ! (zerolength -ot unmodified) ]]
1600:-ot cond
161
162  [[ link -ef zerolength && ! (link -ef nonzerolength) ]]
1630:-ef cond
164
165  [[ foo = foo && foo != bar && foo == foo && foo != '' ]]
1660:=, == and != conds
167
168  [[ bar < foo && foo > bar ]]
1690:< and > conds
170
171  [[ $(( 3 + 4 )) -eq 0x07 && $(( 5 * 2 )) -ne 0x10 ]]
1720:-eq and -ne conds
173
174  [[ 3 -lt 04 && 05 -gt 2 ]]
1750:-lt and -gt conds
176
177  [[ 3 -le 3 && ! (4 -le 3) ]]
1780:-le cond
179
180  [[ 3 -ge 3 && ! (3 -ge 4) ]]
1810:-ge cond
182
183  [[ 1 -lt 2 || 2 -lt 2 && 3 -gt 4 ]]
1840:|| and && in conds
185
186  if ! grep '#define PATH_DEV_FD' $ZTST_testdir/../config.h; then
187    print -u$ZTST_fd "Warning: not testing [[ -e /dev/fd/0 ]] (/dev/fd not supported)"
188    true
189  else
190    [[ -e /dev/fd/0 ]]
191  fi
1920dD:/dev/fd support in conds handled by access
193
194  if ! grep '#define PATH_DEV_FD' $ZTST_testdir/../config.h; then
195    print -u$ZTST_fd "Warning: not testing [[ -O /dev/fd/0 ]] (/dev/fd not supported)"
196    true
197  else
198    [[ -O /dev/fd/0 ]]
199  fi
2000dD:/dev/fd support in conds handled by stat
201
202  [[ ( -z foo && -z foo ) || -z foo ]]
2031:complex conds with skipping
204
205  [ '' != bar -a '' = '' ]
2060:strings with `[' builtin
207
208  [ `echo 0` -lt `echo 1` ]
2090:substitution in `[' builtin
210
211  [ -n foo scrimble ]
2121:argument checking for [ builtin
213?(eval):[:1: too many arguments
214
215  test -n foo scramble
2161:argument checking for test builtin
217?(eval):test:1: too many arguments
218
219  [ -n foo scrimble scromble ]
2201:argument checking for [ builtin
221?(eval):[:1: too many arguments
222
223  test -n foo scramble scrumble
2241:argument checking for test builtin
225?(eval):test:1: too many arguments
226
227  [ -n foo -a -n bar scrimble ]
2281:argument checking for [ builtin
229?(eval):[:1: too many arguments
230
231  test -n foo -a -z "" scramble
2321:argument checking for test builtin
233?(eval):test:1: too many arguments
234
235  fn() {
236    # careful: first file must exist to trigger bug
237    [[ -e unmodified ]] || print Where\'s my file\?
238    [[ unmodified -nt NonExistentFile ]]
239    print status = $?
240  }
241  fn
2420:-nt shouldn't abort on non-existent files
243>status = 1
244
245# core dumps on failure
246  if zmodload -i zsh/regex 2>/dev/null; then
247     echo >regex_test.sh 'if [[ $# = 1 ]]; then
248	if [[ $1 =~ /?[^/]+:[0-9]+:$ ]]; then
249	  :
250	fi
251      fi
252      exit 0'
253      $ZTST_testdir/../Src/zsh -f ./regex_test.sh
254  fi
2550:regex tests shouldn't crash
256
257  (if zmodload -i zsh/regex 2>/dev/null; then
258    string="this has stuff in it"
259    bad_regex=0
260    if [[ $string =~ "h([a-z]*) s([a-z]*) " ]]; then
261      if [[ "$MATCH $MBEGIN $MEND" != "has stuff  6 15" ]]; then
262	print -r "regex variables MATCH MBEGIN MEND:
263  '$MATCH $MBEGIN $MEND'
264  should be:
265  'has stuff  6 15'"
266        bad_regex=1
267      else
268	results=("as 7 8" "tuff 11 14")
269	for i in 1 2; do
270	  if [[ "$match[$i] $mbegin[$i] $mend[$i]" != $results[i] ]]; then
271	    print -r "regex variables match[$i] mbegin[$i] mend[$i]:
272  '$match[$i] $mbegin[$i] $mend[$i]'
273  should be
274  '$results[$i]'"
275            bad_regex=1
276	    break
277	  fi
278	done
279      fi
280      (( bad_regex )) || print OK
281    else
282      print -r "regex failed to match '$string'"
283    fi
284  else
285    # if it didn't load, tough, but not a test error
286    print OK
287  fi)
2880:MATCH, MBEGIN, MEND, match, mbegin, mend
289>OK
290
291  (if zmodload -i zsh/regex 2>/dev/null; then
292    if [[ a =~ a && b == b ]]; then
293      print OK
294    else
295      print "regex =~ inverted following test"
296    fi
297  else
298    # not a test error
299    print OK
300  fi)
3010:regex infix operator should not invert following conditions
302>OK
303
304  [[ -fail badly ]]
3052:Error message for unknown prefix condition
306?(eval):1: unknown condition: -fail
307
308  [[ really -fail badly ]]
3092:Error message for unknown infix condition
310?(eval):1: unknown condition: -fail
311
312  crashme() {
313    if [[ $1 =~ ^http:* ]]
314    then
315      url=${1#*=}
316    fi
317  }
318  which crashme
3190:Regression test for examining code with regular expression match
320>crashme () {
321>	if [[ $1 =~ ^http:* ]]
322>	then
323>		url=${1#*=} 
324>	fi
325>}
326
327  weirdies=(
328    '! -a !'
329    '! -o !'
330    '! -a'
331    '! -o'
332    '! -a ! -a !'
333    '! = !'
334    '! !'
335    '= -a o'
336    '! = -a o')
337  for w in $weirdies; do
338     eval test $w
339     print $?
340  done
3410:test compatability weirdness: treat ! as a string sometimes
342>0
343>0
344>1
345>0
346>0
347>0
348>1
349>0
350>1
351
352%clean
353  # This works around a bug in rm -f in some versions of Cygwin
354  chmod 644 unmodish
355