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

293 lines, 138 significant
1## our_shell: ysh
2## oils_failures_allowed: 2
3
4#### fastlex: NUL byte not allowed inside char literal #' '
5
6echo $'var x = #\'\x00\'; echo x=$x' > tmp.oil
7$[ENV.SH] tmp.oil
8
9echo $'var x = #\' ' > incomplete.oil
10$[ENV.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 $[ENV.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
143
144shvar FOO=bar {
145 for x in (1 ..< 500) {
146 var Q = "hello"
147 setvar Q = Q=>replace("hello","world")
148 }
149}
150echo $Q
151
152## STDOUT:
153world
154## END
155
156
157#### Parsing crash - bug #2003
158
159set +o errexit
160
161$[ENV.SH] -c 'proc y (;x) { return = x }'
162echo status=$?
163
164$[ENV.SH] -c 'func y (;x) { return = x }'
165echo status=$?
166
167## STDOUT:
168status=2
169status=2
170## END
171
172
173#### proc with IFS= read -r line - dynamic scope - issue #2012
174
175# 2024-10 - FIXED by the new Env Obj! Because in YSH, 'line' is NOT created in
176# TEMP stack frame - we use the ENCLOSED frame, and it fixes it.
177
178proc p {
179 read -r line
180 write $line
181}
182
183proc p-ifs {
184 IFS= read -r line
185 write $line
186}
187
188#set -x
189
190echo zz | p
191
192echo yy | p-ifs
193
194## STDOUT:
195zz
196yy
197## END
198
199#### func call inside proc call - error message attribution
200
201try 2> foo {
202 $[ENV.SH] -c '
203func ident(x) {
204 return (x)
205}
206
207proc p (; x) {
208 echo $x
209}
210
211# BUG: it points to ( in ident(
212# should point to ( in eval (
213
214eval (ident([1,2,3]))
215'
216}
217
218cat foo
219
220## STDOUT:
221## END
222
223
224#### Crash in parsing case on EOF condition - issue #2037
225
226var WEIGHT = ${1:-}
227case (WEIGHT) {
228 "-" { echo "got nothing" }
229 (else) { echo $WEIGHT
230}
231
232## status: 2
233## STDOUT:
234## END
235
236#### Crash due to incorrect of context manager rooting - issue #1986
237
238proc p {
239 var s = "hi"
240 for q in (1..<50) {
241 shvar Q="whatever" {
242 setvar s = "." ++ s
243 }
244 }
245}
246
247for i in (1..<10) {
248 p
249}
250
251if false {
252 echo 'testing for longer'
253 for i in (1 ..< 1000) {
254 p
255 }
256}
257
258## STDOUT:
259## END
260
261
262#### crash due to arbitrary PNode limit - issue #2078
263
264#!/usr/bin/env ysh
265var DelegatedCompName = {
266 "llvm" : "x_project",
267 "rocprofiler_register" : "x_rocprofiler_register",
268 "roct_thunk_interface" : "x_roct",
269 "rocr_runtime" : "x_rocr",
270 "openmp" : "x_openmp",
271 "offload" : "x_offload",
272 "aomp_extras" : "x_extras",
273 "comgr" : "x_comgr",
274 "rocminfo" : "x_rocminfo",
275 "rocsmilib" : "x_rocm_smi_lib",
276 "amdsmi" : "x_amdsmi",
277 "flang_legacy" : "x_flang_legacy",
278 "pgmath" : "x_pgmath",
279 "flang" : "x_flang",
280 "flang_runtime" : "x_flang_runtime",
281 "hipcc" : "x_hipcc",
282 "hipamd" : "x_hipamd",
283 "rocm_dbgapi" : "x_rocdbgapi",
284 "rocgdb" : "x_rocgdb",
285 "roctracer" : "x_roctracer",
286 "rocprofiler" : "x_rocprofiler"
287}
288
289echo $[len(DelegatedCompName)]
290
291## STDOUT:
29221
293## END