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

349 lines, 142 significant
1## oils_failures_allowed: 7
2
3#### source-guard is an old way of preventing redefinition - could remove it
4shopt --set ysh:upgrade
5
6source-guard 'main' || return 0
7source $REPO_ROOT/spec/testdata/module/common.ysh
8source $REPO_ROOT/spec/testdata/module/module1.ysh
9## STDOUT:
10common
11module1
12## END
13
14#### is-main
15
16# This sources lib.ysh
17$SH $REPO_ROOT/spec/testdata/module/main.ysh
18
19# Run it directly
20$SH $REPO_ROOT/spec/testdata/module/lib.ysh
21
22## STDOUT:
23lib.ysh is not the main module
24hi from main.ysh
25hi from lib.ysh
26## END
27
28#### is-main with -c and stdin
29
30$SH -c 'echo -c; is-main; echo status=$?'
31
32echo 'echo stdin; is-main; echo status=$?' | $SH
33
34## STDOUT:
35-c
36status=0
37stdin
38status=0
39## END
40
41#### use builtin usage
42
43use
44echo no-arg=$?
45
46use foo
47echo one-arg=$?
48
49use --extern foo
50echo extern=$?
51
52use --bad-flag
53echo bad-flag=$?
54
55use too many
56echo too-many=$?
57
58use ///no-builtin
59echo no-builtin=$?
60
61
62## STDOUT:
63no-arg=2
64one-arg=1
65extern=0
66bad-flag=2
67too-many=2
68no-builtin=1
69## END
70
71
72#### use --extern is a no-op, for static analysis
73
74use --extern grep sed awk
75echo status=$?
76
77use --extern zzz
78echo status=$?
79
80## STDOUT:
81status=0
82status=0
83## END
84
85#### use foo.ysh creates a value.Obj, and it's cached on later invocations
86
87shopt --set ysh:upgrade
88
89var caller_no_leak = 42
90
91use $REPO_ROOT/spec/testdata/module2/util.ysh
92
93# This is a value.Obj
94pp test_ (['util', util])
95var id1 = id(util)
96
97var saved_util = util
98
99use $REPO_ROOT/spec/testdata/module2/util.ysh
100pp test_ (['repeated', util])
101var id2 = id(util)
102
103# Create a symlink to test normalization
104
105ln -s $REPO_ROOT/spec/testdata/module2/util.ysh symlink.ysh
106
107use symlink.ysh
108pp test_ (['symlink', symlink])
109var id3 = id(symlink)
110
111#pp test_ ([id1, id2, id3])
112
113# Make sure they are all the same object
114assert [id1 === id2]
115assert [id2 === id3]
116
117# Doesn't leak from util.ysh
118echo "setvar_noleak $[getVar('setvar_noleak')]"
119echo "setglobal_noleak $[getVar('setglobal_noleak')]"
120
121## STDOUT:
122caller_no_leak = null
123(List) ["util",{"MY_INTEGER":42,"log":<Proc>,"die":<Proc>,"setvar_noleak":"util.ysh","setglobal_noleak":"util.ysh","invokableObj":{"x":3,"y":4} ==> {"__invoke__":<Proc>}} ==> {"__invoke__":<BuiltinProc>}]
124(List) ["repeated",{"MY_INTEGER":42,"log":<Proc>,"die":<Proc>,"setvar_noleak":"util.ysh","setglobal_noleak":"util.ysh","invokableObj":{"x":3,"y":4} ==> {"__invoke__":<Proc>}} ==> {"__invoke__":<BuiltinProc>}]
125(List) ["symlink",{"MY_INTEGER":42,"log":<Proc>,"die":<Proc>,"setvar_noleak":"util.ysh","setglobal_noleak":"util.ysh","invokableObj":{"x":3,"y":4} ==> {"__invoke__":<Proc>}} ==> {"__invoke__":<BuiltinProc>}]
126setvar_noleak null
127setglobal_noleak null
128## END
129
130#### procs in a module can call setglobal on globals in that module
131shopt --set ysh:upgrade
132
133use $REPO_ROOT/spec/testdata/module2/globals.ysh
134
135# get() should work on Obj too. Possibly we should get rid of the default
136var myproc = get(propView(globals), 'mutate-g1', null)
137call setVar('mutate-g1', myproc)
138
139# you can mutate it internally, but the mutation isn't VISIBLE. GAH!
140# I wonder if you make Cell a value? or something
141mutate-g1
142echo
143
144# PROBLEM: This is a value.Obj COPY, not the fucking original!!!
145# immutable objects??
146
147#pp test_ ([id(globals.d), globals.d])
148
149call globals.mutateG2()
150echo
151
152#= propView(globals)
153
154# these are not provided
155echo "globals.g1 = $[get(globals, 'g1', null)]"
156echo "globals.g2 = $[get(globals, 'g2', null)]"
157echo
158
159#pp frame_vars_
160# Shouldn't appear here
161echo "importer g1 = $[getVar('g1')]"
162echo "importer g2 = $[getVar('g2')]"
163
164## STDOUT:
165g1 = g1
166g1 = proc mutated
167
168g2 = g2
169g2 = func mutated
170
171globals.g1 = null
172globals.g2 = null
173
174importer g1 = null
175importer g2 = null
176## END
177
178#### no provided names
179shopt --set ysh:upgrade
180
181use $REPO_ROOT/spec/testdata/module2/no-provide.ysh
182
183## status: 1
184## STDOUT:
185## END
186
187#### bad provide type
188shopt --set ysh:upgrade
189
190use $REPO_ROOT/spec/testdata/module2/bad-provide-type.ysh
191
192## status: 1
193## STDOUT:
194## END
195
196#### invalid provide entries
197shopt --set ysh:upgrade
198
199use $REPO_ROOT/spec/testdata/module2/bad-provide.ysh
200
201## status: 1
202## STDOUT:
203## END
204
205#### use foo.ysh creates a value.Obj with __invoke__
206shopt --set ysh:upgrade
207
208use $REPO_ROOT/spec/testdata/module2/util.ysh
209
210# This is a value.Obj
211#pp test_ (util)
212
213util log 'hello'
214util die 'hello there'
215
216## STDOUT:
217caller_no_leak = null
218log hello
219die hello there
220## END
221
222#### module itself is invokable Obj, which can contain invokable obj!
223shopt --set ysh:upgrade
224
225use $REPO_ROOT/spec/testdata/module2/util.ysh
226
227util invokableObj (1)
228
229# Usage error
230#util invokableObj
231
232## STDOUT:
233caller_no_leak = null
234sum = 8
235## END
236
237#### argument binding test
238shopt --set ysh:upgrade
239
240use $REPO_ROOT/spec/testdata/module2/util2.ysh
241
242util2 echo-args w1 w2 w3 w4 (3, 4, 5, 6, n1=7, n2=8, n3=9) {
243 echo hi
244}
245
246echo ---
247
248util2 echo-args w1 w2 (3, 4, n3=9) {
249 echo hi
250}
251
252## STDOUT:
253(List) ["w1","w2"]
254(List) ["w3","w4"]
255
256(List) [3,4]
257(List) [5,6]
258
259(List) [7,8]
260(Dict) {"n3":9}
261
262<Block>
263---
264(List) ["w1","w2"]
265(List) []
266
267(List) [3,4]
268(List) []
269
270(List) [42,43]
271(Dict) {"n3":9}
272
273<Block>
274## END
275
276#### module invoked without any arguments is an error
277shopt --set ysh:upgrade
278
279use $REPO_ROOT/spec/testdata/module2/util.ysh
280
281util
282
283## status: 2
284## STDOUT:
285caller_no_leak = null
286## END
287
288#### module invoked with nonexistent name is error
289shopt --set ysh:upgrade
290
291use $REPO_ROOT/spec/testdata/module2/util.ysh
292
293util zzz
294
295## status: 2
296## STDOUT:
297caller_no_leak = null
298## END
299
300#### circular import is an error?
301
302echo hi
303
304## STDOUT:
305## END
306
307#### Module with runtime error
308
309echo TODO
310
311## STDOUT:
312## END
313
314#### Module with parse error
315
316echo TODO
317
318## STDOUT:
319## END
320
321#### user can inspect __modules__ cache
322
323echo 'TODO: Dict view of realpath() string -> Obj instance'
324
325## STDOUT:
326## END
327
328#### use foo.ysh --names a b
329
330echo TODO
331
332## STDOUT:
333## END
334
335
336#### use foo.ysh --all-provided
337
338echo TODO
339
340## STDOUT:
341## END
342
343
344#### use foo.ysh --all-for-testing
345
346echo TODO
347
348## STDOUT:
349## END