OILS / spec / builtin-process.test.sh View on Github | oils.pub

612 lines, 272 significant
1## oils_failures_allowed: 2
2## compare_shells: dash bash mksh zsh
3
4#### exec builtin
5exec echo hi
6## stdout: hi
7
8#### exec builtin with redirects
9exec 1>&2
10echo 'to stderr'
11## stdout-json: ""
12## stderr: to stderr
13
14#### exec builtin with here doc
15# This has in a separate file because both code and data can be read from
16# stdin.
17$SH $REPO_ROOT/spec/bin/builtins-exec-here-doc-helper.sh
18## STDOUT:
19x=one
20y=two
21DONE
22## END
23
24#### exec builtin accepts --
25exec -- echo hi
26## STDOUT:
27hi
28## END
29## BUG dash status: 127
30## BUG dash stdout-json: ""
31
32#### exec -- 2>&1
33exec -- 3>&1
34echo stdout 1>&3
35## STDOUT:
36stdout
37## END
38## BUG dash status: 127
39## BUG dash stdout-json: ""
40## BUG mksh status: -11
41## BUG mksh stdout-json: ""
42
43#### Exit out of function
44f() { exit 3; }
45f
46exit 4
47## status: 3
48
49#### Exit builtin with invalid arg
50exit invalid
51# Rationale: runtime errors are 1
52## status: 1
53## OK dash/bash status: 2
54## BUG zsh status: 0
55
56#### Exit builtin with too many args
57# This is a parse error in OSH.
58exit 7 8 9
59echo status=$?
60## status: 2
61## stdout-json: ""
62## BUG bash/zsh status: 0
63## BUG bash/zsh stdout: status=1
64## BUG dash status: 7
65## BUG dash stdout-json: ""
66## OK mksh status: 1
67## OK mksh stdout-json: ""
68
69#### time with brace group argument
70
71err=time-$(basename $SH).txt
72{
73 time {
74 sleep 0.01
75 sleep 0.02
76 }
77} 2> $err
78
79grep --only-matching user $err
80echo result=$?
81
82# Regression: check fractional seconds
83gawk '
84BEGIN { ok = 0 }
85match( $0, /\.([0-9]+)/, m) {
86 if (m[1] > 0) { # check fractional seconds
87 ok = 1
88 }
89}
90END { if (ok) { print "non-zero" } }
91' $err
92
93## status: 0
94## STDOUT:
95user
96result=0
97non-zero
98## END
99
100# time doesn't accept a block?
101## BUG zsh STDOUT:
102result=1
103## END
104
105# dash doesn't have time keyword
106## N-I dash status: 2
107## N-I dash stdout-json: ""
108
109
110#### get umask
111umask | grep '[0-9]\+' # check for digits
112## status: 0
113
114#### set umask in octal
115rm -f $TMP/umask-one $TMP/umask-two
116umask 0002
117echo one > $TMP/umask-one
118umask 0022
119echo two > $TMP/umask-two
120stat -c '%a' $TMP/umask-one $TMP/umask-two
121## status: 0
122## STDOUT:
123664
124644
125## END
126## stderr-json: ""
127
128#### set umask symbolically
129umask 0002 # begin in a known state for the test
130rm -f $TMP/umask-one $TMP/umask-two
131echo one > $TMP/umask-one
132umask g-w,o-w
133echo two > $TMP/umask-two
134stat -c '%a' $TMP/umask-one $TMP/umask-two
135## status: 0
136## STDOUT:
137664
138644
139## END
140## stderr-json: ""
141
142#### ulimit with no flags is like -f
143
144ulimit > no-flags.txt
145echo status=$?
146
147ulimit -f > f.txt
148echo status=$?
149
150diff -u no-flags.txt f.txt
151echo diff=$?
152
153# Print everything
154# ulimit -a
155
156## STDOUT:
157status=0
158status=0
159diff=0
160## END
161
162
163#### ulimit too many args
164
165ulimit 1 2
166if test $? -ne 0; then
167 echo pass
168else
169 echo fail
170fi
171
172#ulimit -f
173
174## STDOUT:
175pass
176## END
177
178## BUG bash/zsh STDOUT:
179fail
180## END
181
182
183#### ulimit negative flag
184
185ulimit -f
186
187# interpreted as a flag
188ulimit -f -42
189if test $? -ne 0; then
190 echo pass
191else
192 echo fail
193fi
194
195## STDOUT:
196unlimited
197pass
198## END
199
200#### ulimit negative arg
201
202ulimit -f
203
204# an arg
205ulimit -f -- -42
206if test $? -ne 0; then
207 echo pass
208else
209 echo fail
210fi
211
212## STDOUT:
213unlimited
214pass
215## END
216
217## BUG mksh STDOUT:
218unlimited
219fail
220## END
221
222
223#### ulimit -a doesn't take arg
224case $SH in bash) exit ;; esac
225
226ulimit -a 42
227if test $? -ne 0; then
228 echo 'failure that was expected'
229fi
230
231## STDOUT:
232failure that was expected
233## END
234## BUG bash STDOUT:
235## END
236
237
238#### ulimit doesn't accept multiple flags - reduce confusion between shells
239
240# - bash, zsh, busybox ash accept multiple "commands", which requires custom
241# flag parsing, like
242
243# ulimit -f 999 -n
244# ulimit -f 999 -n 888
245#
246# - dash and mksh accept a single ARG
247#
248# we want to make it clear we're like the latter
249
250# can't print all and -f
251ulimit -f -a >/dev/null
252echo status=$?
253
254ulimit -f -n >/dev/null
255echo status=$?
256
257ulimit -f -n 999 >/dev/null
258echo status=$?
259
260## STDOUT:
261status=2
262status=2
263status=2
264## END
265
266## BUG dash/bash/mksh STDOUT:
267status=0
268status=0
269status=0
270## END
271
272# zsh is better - it checks that -a and -f are exclusive
273
274## BUG zsh STDOUT:
275status=1
276status=0
277status=0
278## END
279
280
281#### YSH readability: ulimit --all the same as ulimit -a
282
283case $SH in bash|dash|mksh|zsh) exit ;; esac
284
285ulimit -a > short.txt
286ulimit --all > long.txt
287
288wc -l short.txt long.txt
289
290diff -u short.txt long.txt
291echo status=$?
292
293## STDOUT:
294 8 short.txt
295 8 long.txt
296 16 total
297status=0
298## END
299
300## N-I bash/dash/mksh/zsh STDOUT:
301## END
302
303#### ulimit accepts 'unlimited'
304
305for arg in zz unlimited; do
306 echo " arg $arg"
307 ulimit -f
308 echo status=$?
309 ulimit -f $arg
310 if test $? -ne 0; then
311 echo 'FAILED'
312 fi
313 echo
314done
315## STDOUT:
316 arg zz
317unlimited
318status=0
319FAILED
320
321 arg unlimited
322unlimited
323status=0
324
325## END
326
327
328#### ulimit of 2**32, 2**31 (int overflow)
329
330echo -n 'one '; ulimit -f
331
332
333ulimit -f $(( 1 << 32 ))
334
335echo -n 'two '; ulimit -f
336
337
338# mksh fails because it overflows signed int, turning into negative number
339ulimit -f $(( 1 << 31 ))
340
341echo -n 'three '; ulimit -f
342
343## STDOUT:
344one unlimited
345two 4294967296
346three 2147483648
347## END
348## BUG mksh STDOUT:
349one unlimited
350two 1
351three 1
352## END
353
354
355#### ulimit that is 64 bits
356
357# no 64-bit integers
358case $SH in mksh) exit ;; esac
359
360echo -n 'before '; ulimit -f
361
362# 1 << 63 overflows signed int
363
364# 512 is 1 << 9, so make it 62-9 = 53 bits
365
366lim=$(( 1 << 53 ))
367#echo $lim
368
369# bash says this is out of range
370ulimit -f $lim
371
372echo -n 'after '; ulimit -f
373
374## STDOUT:
375before unlimited
376after 9007199254740992
377## END
378
379## BUG mksh STDOUT:
380## END
381
382
383#### arg that would overflow 64 bits is detected
384
385# no 64-bit integers
386case $SH in mksh) exit ;; esac
387
388echo -n 'before '; ulimit -f
389
390# 1 << 63 overflows signed int
391
392lim=$(( (1 << 62) + 1 ))
393#echo lim=$lim
394
395# bash detects that this is out of range
396# so does osh-cpp, but not osh-cpython
397
398ulimit -f $lim
399echo -n 'after '; ulimit -f
400
401## STDOUT:
402before unlimited
403after unlimited
404## END
405
406## BUG dash/zsh STDOUT:
407before unlimited
408after 1
409## END
410
411## BUG mksh STDOUT:
412## END
413
414
415#### ulimit -f 1 prevents files larger 512 bytes
416
417# dash and zsh give too much spew
418# mksh gives 512 byte files?
419
420#case $SH in dash|zsh|mksh) exit ;; esac
421
422rm -f err.txt
423touch err.txt
424
425bytes() {
426 local n=$1
427 local st=0
428 for i in $(seq $n); do
429 echo -n x
430 st=$?
431 if test $st -ne 0; then
432 echo "ERROR: echo failed with status $st" >> err.txt
433 fi
434 done
435}
436
437
438ulimit -f 1
439
440bytes 512 > ok.txt
441echo 512 status=$?
442
443bytes 513 > too-big.txt
444echo 513 status=$?
445echo
446
447wc --bytes ok.txt too-big.txt
448echo
449
450cat err.txt
451
452## STDOUT:
453512 status=0
454513 status=0
455
456 512 ok.txt
457 512 too-big.txt
4581024 total
459
460ERROR: echo failed with status 1
461## END
462
463## BUG bash STDOUT:
464512 status=0
465513 status=0
466
467 512 ok.txt
468 513 too-big.txt
4691025 total
470
471## END
472
473#### write big file with ulimit
474
475# I think this will test write() errors, rather than the final flush() error
476# (which is currently skipped by C++
477
478{ echo 'ulimit -f 1'
479 # More than 8 KiB may cause a flush()
480 python2 -c 'print("echo " + "X"*9000 + " >out.txt")'
481 echo 'echo inner=$?'
482} > big.sh
483
484$SH big.sh
485echo outer=$?
486
487## STDOUT:
488inner=1
489outer=0
490## END
491
492
493#### ulimit -S for soft limit (default), -H for hard limit
494case $SH in dash|zsh) exit ;; esac
495
496# Note: ulimit -n -S 1111 is OK in osh/dash/mksh, but not bash/zsh
497# Mus be ulimit -S -n 1111
498
499show_state() {
500 local msg=$1
501 echo "$msg"
502 echo -n ' '; ulimit -S -t
503 echo -n ' '; ulimit -H -t
504 echo
505}
506
507show_state 'init'
508
509ulimit -S -t 123456
510show_state '-S'
511
512ulimit -H -t 123457
513show_state '-H'
514
515ulimit -t 123455
516show_state 'no flag'
517
518echo 'GET'
519
520ulimit -S -t 123454
521echo -n ' '; ulimit -t
522echo -n ' '; ulimit -S -t
523echo -n ' '; ulimit -H -t
524
525## STDOUT:
526init
527 unlimited
528 unlimited
529
530-S
531 123456
532 unlimited
533
534-H
535 123456
536 123457
537
538no flag
539 123455
540 123455
541
542GET
543 123454
544 123454
545 123455
546## END
547
548## BUG dash/zsh STDOUT:
549## END
550
551#### Changing resource limit is denied
552
553# Not sure why these don't work
554case $SH in dash|mksh) exit ;; esac
555
556
557flag=-t
558
559ulimit -S -H $flag 100
560echo both=$?
561
562ulimit -S $flag 90
563echo soft=$?
564
565ulimit -S $flag 95
566echo soft=$?
567
568ulimit -S $flag 105
569if test $? -ne 0; then
570 echo soft OK
571else
572 echo soft fail
573fi
574
575ulimit -H $flag 200
576if test $? -ne 0; then
577 echo hard OK
578else
579 echo hard fail
580fi
581
582## STDOUT:
583both=0
584soft=0
585soft=0
586soft OK
587hard OK
588## END
589
590## BUG dash/mksh STDOUT:
591## END
592
593#### ulimit -n limits file descriptors
594
595# OSH bug
596# https://oilshell.zulipchat.com/#narrow/channel/502349-osh/topic/alpine.20build.20failures.20-.20make.20-.20ulimit.20-n.2064/with/519691301
597
598$SH -c 'ulimit -n 64; echo hi >out'
599echo status=$?
600
601$SH -c 'ulimit -n 0; echo hi >out'
602echo status=$?
603
604## STDOUT:
605status=0
606status=1
607## END
608
609## OK dash STDOUT:
610status=0
611status=2
612## END