OILS / spec / ysh-bugs.test.sh View on Github | oilshell.org

237 lines, 97 significant
1## our_shell: ysh
2## oils_failures_allowed: 3
3
4#### fastlex: NUL byte not allowed inside char literal #' '
5
6echo $'var x = #\'\x00\'; echo x=$x' > tmp.oil
7$SH tmp.oil
8
9echo $'var x = #\' ' > incomplete.oil
10$SH incomplete.oil
11
12## status: 2
13## STDOUT:
14## END
15
16#### fastlex: NUL byte inside shebang line
17
18# Hm this test doesn't really tickle the bug
19
20echo $'#! /usr/bin/env \x00 sh \necho hi' > tmp.oil
21env OILS_HIJACK_SHEBANG=1 $SH tmp.oil
22
23## STDOUT:
24hi
25## END
26
27#### Tea keywords don't interfere with YSH expressions
28
29var d = {data: 'foo'}
30
31echo $[d.data]
32
33var e = {enum: 1, class: 2, import: 3, const: 4, var: 5, set: 6}
34echo $[len(e)]
35
36## STDOUT:
37foo
386
39## END
40
41#### Catch AttributeError
42
43var s = 'foo'
44echo s=$s
45var t = s.bad()
46echo 'should not get here'
47
48## status: 3
49## STDOUT:
50s=foo
51## END
52
53
54#### Command sub paren parsing bug (#1387)
55
56write $(if (true) { write true })
57
58const a = $(write $[len('foo')])
59echo $a
60
61const b = $(write $[5 ** 3])
62echo $b
63
64const c = $(
65 write $[6 + 7]
66)
67echo $c
68
69## STDOUT:
70true
713
72125
7313
74## END
75
76
77#### More Command sub paren parsing
78
79write $( var mylist = ['for']; for x in (mylist) { echo $x } )
80
81write $( echo while; while (false) { write hi } )
82
83write $( if (true) { write 'if' } )
84
85write $( if (false) { write 'if' } elif (true) { write 'elif' } )
86
87## STDOUT:
88for
89while
90if
91elif
92## END
93
94#### don't execute empty command
95
96shopt --set ysh:all
97
98set -x
99
100try {
101 type -a ''
102}
103echo "type -a returned $_status"
104
105$(true)
106echo nope
107
108## status: 127
109## STDOUT:
110type -a returned 1
111## END
112
113
114#### Do && || with YSH constructs make sense/
115
116# I guess there's nothing wrong with this?
117#
118# But I generally feel && || are only for
119#
120# test --file x && test --file y
121
122var x = []
123true && call x->append(42)
124false && call x->append(43)
125pp test_ (x)
126
127func amp() {
128 true && return (42)
129}
130
131func pipe() {
132 false || return (42)
133}
134
135pp test_ (amp())
136pp test_ (pipe())
137
138## STDOUT:
139## END
140
141
142#### shvar then replace - bug #1986 context manager crash
143source --builtin ysh/shvar.ysh
144
145shvar FOO=bar {
146 for x in (1 .. 500) {
147 var Q = "hello"
148 setvar Q = Q=>replace("hello","world")
149 }
150}
151echo $Q
152
153## STDOUT:
154world
155## END
156
157
158#### Parsing crash - bug #2003
159
160set +o errexit
161
162$SH -c 'proc y (;x) { return = x }'
163echo status=$?
164
165$SH -c 'func y (;x) { return = x }'
166echo status=$?
167
168## STDOUT:
169status=2
170status=2
171## END
172
173
174#### proc with IFS= read -r line - dynamic scope - issue #2012
175
176# this is an issue with lack of dynamic scope
177# not sure exactly how to handle it ...
178
179# shvar IFS= { read } is our replacement for dynamic scope
180
181proc p {
182 read -r line
183 write $line
184}
185
186proc p-ifs {
187 IFS= read -r line
188 write $line
189}
190
191#set -x
192
193echo zz | p
194
195echo yy | p-ifs
196
197## STDOUT:
198zz
199yy
200## END
201
202#### func call inside proc call - error message attribution
203
204try 2> foo {
205 $SH -c '
206func ident(x) {
207 return (x)
208}
209
210proc p (; x) {
211 echo $x
212}
213
214# BUG: it points to ( in ident(
215# should point to ( in eval (
216
217eval (ident([1,2,3]))
218'
219}
220
221cat foo
222
223## STDOUT:
224## END
225
226
227#### Crash in parsing case on EOF condition - issue #2037
228
229var WEIGHT = ${1:-}
230case (WEIGHT) {
231 "-" { echo "got nothing" }
232 (else) { echo $WEIGHT
233}
234
235## status: 2
236## STDOUT:
237## END