Skip to content

Commit ceac37a

Browse files
author
Andy Chu
committed
[test/spec refactor] Extract new file spec/builtin-set
Compare against zsh too Related to #2364 - we have a test case with set -
1 parent 99d2ffb commit ceac37a

File tree

3 files changed

+172
-158
lines changed

3 files changed

+172
-158
lines changed

spec/builtin-set.test.sh

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
## compare_shells: bash dash mksh zsh
2+
3+
#### can continue after unknown option
4+
#
5+
# TODO: this is the posix special builtin logic?
6+
# dash and mksh make this a fatal error no matter what.
7+
8+
set -o errexit
9+
set -o STRICT || true # unknown option
10+
echo hello
11+
## stdout: hello
12+
## status: 0
13+
## BUG dash/mksh/zsh stdout-json: ""
14+
## BUG dash status: 2
15+
## BUG mksh/zsh status: 1
16+
17+
#### set with both options and argv
18+
set -o errexit a b c
19+
echo "$@"
20+
false
21+
echo done
22+
## stdout: a b c
23+
## status: 1
24+
25+
26+
#### nounset with "$@"
27+
set a b c
28+
set -u # shouldn't touch argv
29+
echo "$@"
30+
## stdout: a b c
31+
32+
#### set -u -- clears argv
33+
set a b c
34+
set -u -- # shouldn't touch argv
35+
echo "$@"
36+
## stdout:
37+
38+
#### set -u -- x y z
39+
set a b c
40+
set -u -- x y z
41+
echo "$@"
42+
## stdout: x y z
43+
44+
#### reset option with long flag
45+
set -o errexit
46+
set +o errexit
47+
echo "[$unset]"
48+
## stdout: []
49+
## status: 0
50+
51+
#### reset option with short flag
52+
set -u
53+
set +u
54+
echo "[$unset]"
55+
## stdout: []
56+
## status: 0
57+
58+
#### set -eu (flag parsing)
59+
set -eu
60+
echo "[$unset]"
61+
echo status=$?
62+
## stdout-json: ""
63+
## status: 1
64+
## OK dash status: 2
65+
66+
#### set - -
67+
set a b
68+
echo "$@"
69+
set - a b
70+
echo "$@"
71+
set -- a b
72+
echo "$@"
73+
set - -
74+
echo "$@"
75+
set - +
76+
echo "$@"
77+
set + -
78+
echo "$@"
79+
set -- --
80+
echo "$@"
81+
82+
# note: zsh is different, and yash is totally different
83+
## STDOUT:
84+
a b
85+
a b
86+
a b
87+
-
88+
+
89+
+
90+
--
91+
## END
92+
## OK osh/yash STDOUT:
93+
a b
94+
- a b
95+
a b
96+
- -
97+
- +
98+
+ -
99+
--
100+
## END
101+
## BUG mksh STDOUT:
102+
a b
103+
a b
104+
a b
105+
-
106+
+
107+
-
108+
--
109+
## END
110+
## BUG zsh STDOUT:
111+
a b
112+
a b
113+
a b
114+
115+
+
116+
117+
--
118+
## END
119+
120+
#### set -o lists options
121+
# NOTE: osh doesn't use the same format yet.
122+
set -o | grep -o noexec
123+
## STDOUT:
124+
noexec
125+
## END
126+
127+
#### 'set' and 'eval' round trip
128+
129+
# NOTE: not testing arrays and associative arrays!
130+
_space='[ ]'
131+
_whitespace=$'[\t\r\n]'
132+
_sq="'single quotes'"
133+
_backslash_dq="\\ \""
134+
_unicode=$'[\u03bc]'
135+
136+
# Save the variables
137+
varfile=$TMP/vars-$(basename $SH).txt
138+
139+
set | grep '^_' > "$varfile"
140+
141+
# Unset variables
142+
unset _space _whitespace _sq _backslash_dq _unicode
143+
echo [ $_space $_whitespace $_sq $_backslash_dq $_unicode ]
144+
145+
# Restore them
146+
147+
. $varfile
148+
echo "Code saved to $varfile" 1>&2 # for debugging
149+
150+
test "$_space" = '[ ]' && echo OK
151+
test "$_whitespace" = $'[\t\r\n]' && echo OK
152+
test "$_sq" = "'single quotes'" && echo OK
153+
test "$_backslash_dq" = "\\ \"" && echo OK
154+
test "$_unicode" = $'[\u03bc]' && echo OK
155+
156+
## STDOUT:
157+
[ ]
158+
OK
159+
OK
160+
OK
161+
OK
162+
OK
163+
## END
164+
165+
## BUG zsh status: 1
166+
## BUG zsh STDOUT:
167+
[ ]
168+
## END

spec/sh-options.test.sh

Lines changed: 0 additions & 158 deletions
Original file line numberDiff line numberDiff line change
@@ -66,25 +66,6 @@ foo bar
6666
## N-I dash status: 2
6767
## N-I mksh status: 1
6868

69-
#### can continue after unknown option
70-
# dash and mksh make this a fatal error no matter what.
71-
set -o errexit
72-
set -o STRICT || true # unknown option
73-
echo hello
74-
## stdout: hello
75-
## status: 0
76-
## BUG dash/mksh stdout-json: ""
77-
## BUG dash status: 2
78-
## BUG mksh status: 1
79-
80-
#### set with both options and argv
81-
set -o errexit a b c
82-
echo "$@"
83-
false
84-
echo done
85-
## stdout: a b c
86-
## status: 1
87-
8869
#### set -o vi/emacs
8970
set -o vi
9071
echo $?
@@ -173,46 +154,6 @@ echo end # never reached
173154
## status: 1
174155
## OK dash status: 2
175156

176-
#### nounset with "$@"
177-
set a b c
178-
set -u # shouldn't touch argv
179-
echo "$@"
180-
## stdout: a b c
181-
182-
#### set -u -- clears argv
183-
set a b c
184-
set -u -- # shouldn't touch argv
185-
echo "$@"
186-
## stdout:
187-
188-
#### set -u -- x y z
189-
set a b c
190-
set -u -- x y z
191-
echo "$@"
192-
## stdout: x y z
193-
194-
#### reset option with long flag
195-
set -o errexit
196-
set +o errexit
197-
echo "[$unset]"
198-
## stdout: []
199-
## status: 0
200-
201-
#### reset option with short flag
202-
set -u
203-
set +u
204-
echo "[$unset]"
205-
## stdout: []
206-
## status: 0
207-
208-
#### set -eu (flag parsing)
209-
set -eu
210-
echo "[$unset]"
211-
echo status=$?
212-
## stdout-json: ""
213-
## status: 1
214-
## OK dash status: 2
215-
216157
#### -n for no execution (useful with --ast-output)
217158
# NOTE: set +n doesn't work because nothing is executed!
218159
echo 1
@@ -379,67 +320,6 @@ f.o
379320
.oo
380321
## END
381322

382-
#### set - -
383-
set a b
384-
echo "$@"
385-
set - a b
386-
echo "$@"
387-
set -- a b
388-
echo "$@"
389-
set - -
390-
echo "$@"
391-
set - +
392-
echo "$@"
393-
set + -
394-
echo "$@"
395-
set -- --
396-
echo "$@"
397-
398-
# note: zsh is different, and yash is totally different
399-
## STDOUT:
400-
a b
401-
a b
402-
a b
403-
-
404-
+
405-
+
406-
--
407-
## END
408-
## OK osh/yash STDOUT:
409-
a b
410-
- a b
411-
a b
412-
- -
413-
- +
414-
+ -
415-
--
416-
## END
417-
## BUG mksh STDOUT:
418-
a b
419-
a b
420-
a b
421-
-
422-
+
423-
-
424-
--
425-
## END
426-
## BUG zsh STDOUT:
427-
a b
428-
a b
429-
a b
430-
431-
+
432-
433-
--
434-
## END
435-
436-
#### set -o lists options
437-
# NOTE: osh doesn't use the same format yet.
438-
set -o | grep -o noexec
439-
## STDOUT:
440-
noexec
441-
## END
442-
443323
#### set without args lists variables
444324
__GLOBAL=g
445325
f() {
@@ -473,44 +353,6 @@ __mylocal='L'
473353
__var_in_parent_scope='D'
474354
## END
475355

476-
#### 'set' and 'eval' round trip
477-
478-
# NOTE: not testing arrays and associative arrays!
479-
_space='[ ]'
480-
_whitespace=$'[\t\r\n]'
481-
_sq="'single quotes'"
482-
_backslash_dq="\\ \""
483-
_unicode=$'[\u03bc]'
484-
485-
# Save the variables
486-
varfile=$TMP/vars-$(basename $SH).txt
487-
488-
set | grep '^_' > "$varfile"
489-
490-
# Unset variables
491-
unset _space _whitespace _sq _backslash_dq _unicode
492-
echo [ $_space $_whitespace $_sq $_backslash_dq $_unicode ]
493-
494-
# Restore them
495-
496-
. $varfile
497-
echo "Code saved to $varfile" 1>&2 # for debugging
498-
499-
test "$_space" = '[ ]' && echo OK
500-
test "$_whitespace" = $'[\t\r\n]' && echo OK
501-
test "$_sq" = "'single quotes'" && echo OK
502-
test "$_backslash_dq" = "\\ \"" && echo OK
503-
test "$_unicode" = $'[\u03bc]' && echo OK
504-
505-
## STDOUT:
506-
[ ]
507-
OK
508-
OK
509-
OK
510-
OK
511-
OK
512-
## END
513-
514356
#### set without args and array variables
515357
declare -a __array
516358
__array=(1 2 '3 4')

test/spec.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,10 @@ builtin-read() {
186186
test/spec-py.sh run-file builtin-read "$@"
187187
}
188188

189+
builtin-set() {
190+
test/spec-py.sh run-file builtin-set "$@"
191+
}
192+
189193
builtin-special() {
190194
test/spec-py.sh run-file builtin-special "$@"
191195
}

0 commit comments

Comments
 (0)