OILS / spec / ysh-introspect.test.sh View on Github | oils.pub

358 lines, 213 significant
1## oils_failures_allowed: 2
2## our_shell: ysh
3
4#### getFrame()
5
6var fr = vm.getFrame(0)
7pp test_ (fr)
8var d = dict(fr)
9pp test_ (d.ARGV)
10echo
11
12proc p1 {
13 var p1_var = 'x'
14 p2
15}
16
17proc p2 {
18 echo 'p2 frame -1'
19 var fr = vm.getFrame(-1)
20 var d = dict(fr)
21
22 pp test_ (fr)
23 pp test_ (d)
24 pp test_ (keys(d))
25 echo
26
27 echo 'p2 frame -2'
28 setvar fr = vm.getFrame(-2)
29 setvar d = dict(fr)
30
31 pp test_ (fr)
32 pp test_ (keys(d))
33 echo
34}
35
36p1
37
38var fr = vm.getFrame(99) # fails
39
40## status: 3
41## STDOUT:
42<Frame>
43(List) []
44
45p2 frame -1
46<Frame>
47(Dict) {"ARGV":[],"fr":<Frame>}
48(List) ["ARGV","fr"]
49
50p2 frame -2
51<Frame>
52(List) ["ARGV","p1_var"]
53
54## END
55
56
57#### bindFrame()
58
59var frag = ^(echo $i)
60
61# TODO: should be fragment
62pp test_ (frag)
63
64var cmd = bindFrame(frag, vm.getFrame(0))
65
66pp test_ (cmd)
67
68## STDOUT:
69## END
70
71#### vm.getDebugStack()
72
73proc p {
74 echo $[len(vm.getDebugStack())]
75}
76
77proc p2 {
78 p
79}
80
81p
82p2
83
84## STDOUT:
851
862
87## END
88
89#### DebugFrame.toString() running file
90
91$[ENV.SH] $[ENV.REPO_ROOT]/spec/testdata/debug-frame-main.ysh |
92 sed -e "s;$[ENV.REPO_ROOT];MYROOT;g" -e 's;#;%;g'
93
94## STDOUT:
95 %1 MYROOT/spec/testdata/debug-frame-main.ysh:4
96 print-stack
97 ^~~~~~~~~~~
98
99 %1 MYROOT/spec/testdata/debug-frame-main.ysh:7
100 my-proc
101 ^~~~~~~
102 %2 MYROOT/spec/testdata/debug-frame-lib.ysh:15
103 print-stack
104 ^~~~~~~~~~~
105## END
106
107
108#### DebugFrame.toString() running stdin and -c
109
110# stdin
111echo 'source $[ENV.REPO_ROOT]/spec/testdata/debug-frame-lib.ysh; my-proc' |
112 $[ENV.SH] |
113 sed -e "s;$[ENV.REPO_ROOT];MYROOT;g" -e 's;#;%;g'
114echo
115
116# -c
117$[ENV.SH] -c 'source $[ENV.REPO_ROOT]/spec/testdata/debug-frame-lib.ysh; my-proc' |
118 sed -e "s;$[ENV.REPO_ROOT];MYROOT;g" -e 's;#;%;g'
119
120## STDOUT:
121 %1 [ stdin ]:1
122 source $[ENV.REPO_ROOT]/spec/testdata/debug-frame-lib.ysh; my-proc
123 ^~~~~~~
124 %2 MYROOT/spec/testdata/debug-frame-lib.ysh:15
125 print-stack
126 ^~~~~~~~~~~
127
128 %1 [ -c flag ]:1
129 source $[ENV.REPO_ROOT]/spec/testdata/debug-frame-lib.ysh; my-proc
130 ^~~~~~~
131 %2 MYROOT/spec/testdata/debug-frame-lib.ysh:15
132 print-stack
133 ^~~~~~~~~~~
134## END
135
136#### DebugFrame.toString() running eval
137
138# -c and eval
139$[ENV.SH] -c 'source $[ENV.REPO_ROOT]/spec/testdata/debug-frame-lib.ysh; eval "my-proc a b"' |
140 sed -e "s;$[ENV.REPO_ROOT];MYROOT;g" -e 's;#;%;g'
141echo
142
143# eval
144$[ENV.SH] -c 'source $[ENV.REPO_ROOT]/spec/testdata/debug-frame-eval.ysh' |
145 sed -e "s;$[ENV.REPO_ROOT];MYROOT;g" -e 's;#;%;g'
146
147## STDOUT:
148 %1 [ -c flag ]:1
149 source $[ENV.REPO_ROOT]/spec/testdata/debug-frame-lib.ysh; eval "my-proc a b"
150 ^
151 %2 [ eval arg at line 1 of [ -c flag ] ]:1
152 my-proc a b
153 ^~~~~~~
154 %3 MYROOT/spec/testdata/debug-frame-lib.ysh:15
155 print-stack
156 ^~~~~~~~~~~
157
158 %1 [ -c flag ]:1
159 source $[ENV.REPO_ROOT]/spec/testdata/debug-frame-eval.ysh
160 ^~~~~~
161 %2 MYROOT/spec/testdata/debug-frame-eval.ysh:7
162 p3
163 ^~
164 %3 MYROOT/spec/testdata/debug-frame-eval.ysh:4
165 eval 'my-proc x y'
166 ^
167 %4 [ eval arg at line 4 of MYROOT/spec/testdata/debug-frame-eval.ysh ]:1
168 my-proc x y
169 ^~~~~~~
170 %5 MYROOT/spec/testdata/debug-frame-lib.ysh:15
171 print-stack
172 ^~~~~~~~~~~
173## END
174
175#### DebugFrame.toString() running eval methods
176$[ENV.SH] -c '
177source $[ENV.REPO_ROOT]/spec/testdata/debug-frame-lib.ysh
178var b = ^(my-proc a b)
179proc p {
180 call io->eval(b)
181}
182p
183' | sed -e "s;$[ENV.REPO_ROOT];MYROOT;g" -e 's;#;%;g'
184
185## STDOUT:
186 %1 [ -c flag ]:7
187 p
188 ^
189 %2 [ -c flag ]:5
190 call io->eval(b)
191 ^
192 %3 [ -c flag ]:3
193 var b = ^(my-proc a b)
194 ^~~~~~~
195 %4 MYROOT/spec/testdata/debug-frame-lib.ysh:15
196 print-stack
197 ^~~~~~~~~~~
198## END
199
200#### DebugFrame.toString() running YSH functions
201
202# functions
203$[ENV.SH] -c 'source $[ENV.REPO_ROOT]/spec/testdata/debug-frame-lib.ysh; call-func' |
204 sed -e "s;$[ENV.REPO_ROOT];MYROOT;g" -e 's;#;%;g'
205
206## STDOUT:
207 %1 [ -c flag ]:1
208 source $[ENV.REPO_ROOT]/spec/testdata/debug-frame-lib.ysh; call-func
209 ^~~~~~~~~
210 %2 MYROOT/spec/testdata/debug-frame-lib.ysh:32
211 var x = myfunc(99)
212 ^
213 %3 MYROOT/spec/testdata/debug-frame-lib.ysh:28
214 return (identity(myfunc2(42, x+1)))
215 ^
216 %4 MYROOT/spec/testdata/debug-frame-lib.ysh:23
217 print-stack
218 ^~~~~~~~~~~
219142
220## END
221
222#### DebugFrame.toString() with 'use' builtin
223
224# Work around spec test builtin limitation: line starting with # is treated as
225# a comment
226
227#echo $[ENV.REPO_ROOT]
228
229$[ENV.SH] -c 'use $[ENV.REPO_ROOT]/spec/testdata/debug-frame-use.ysh' |
230 sed -e "s;$[ENV.REPO_ROOT];MYROOT;g" -e 's;#;%;g'
231
232#write -- $[ENV.REPO_ROOT] | sed "s;$[ENV.REPO_ROOT];REPO_ROOT;g"
233
234## STDOUT:
235 %1 [ -c flag ]:1
236 use $[ENV.REPO_ROOT]/spec/testdata/debug-frame-use.ysh
237 ^~~
238 %2 MYROOT/spec/testdata/debug-frame-use.ysh:5
239 debug-frame-lib my-proc
240 ^~~~~~~
241 %3 MYROOT/spec/testdata/debug-frame-lib.ysh:15
242 print-stack
243 ^~~~~~~~~~~
244## END
245
246#### FUNCNAME BASH_LINENO BASH_SOURCE not available with YSH functions
247
248func g(x) {
249 echo ${FUNCNAME[@]}
250 echo ${BASH_LINENO[@]}
251 echo ${BASH_SOURCE[@]}
252}
253
254func f(x) {
255 return (g(x))
256}
257
258# We can allow it in procs -- there's no cost to doing so?
259
260proc p {
261 call f(42)
262}
263
264p
265
266## STDOUT:
267p
26816
269[ stdin ]
270## END
271
272#### DebugFrame.toString() with trap ERR - external failure
273
274source $[ENV.REPO_ROOT]/spec/testdata/debug-frame-lib.ysh
275
276trap 'print-stack (prefix=false)' ERR
277set -o errtrace # enable always
278
279proc f {
280 g
281}
282
283proc g {
284 false
285}
286
287f
288
289## status: 1
290## STDOUT:
291[ stdin ]:14
292 f
293 ^
294[ stdin ]:7
295 g
296 ^
297[ stdin ]:11
298 false
299 ^~~~~
300## END
301
302#### trap ERR - proc subshell failure
303
304source $[ENV.REPO_ROOT]/spec/testdata/debug-frame-lib.ysh
305
306trap 'print-stack (prefix=false)' ERR
307set -o errtrace # enable always
308
309proc f {
310 g
311}
312
313proc g {
314 ( exit 42 )
315 #return 42
316}
317
318f
319
320## status: 42
321## STDOUT:
322[ stdin ]:14
323 f
324 ^
325[ stdin ]:11
326 return 42
327 ^~~~~~
328## END
329
330#### trap ERR - proc non-zero return status
331
332source $[ENV.REPO_ROOT]/spec/testdata/debug-frame-lib.ysh
333
334trap 'print-stack (prefix=false)' ERR
335set -o errtrace # enable always
336
337proc f {
338 g
339}
340
341proc g {
342 return 42
343}
344
345f
346
347## status: 42
348
349# Hm we do not get the "g" call here?
350
351## STDOUT:
352[ stdin ]:14
353 f
354 ^
355[ stdin ]:11
356 return 42
357 ^~~~~~
358## END