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

309 lines, 141 significant
1## oils_failures_allowed: 0
2
3#### Can read from ENV Obj
4shopt -s ysh:upgrade
5
6pp test_ (type(ENV))
7#pp test_ (ENV)
8
9# Set by the spec test harness
10
11if (ENV.SH ~~ '*osh') {
12 echo ok
13}
14#echo SH=$[ENV.SH]
15
16## STDOUT:
17(Str) "Obj"
18ok
19## END
20
21#### ENV works in different modules
22shopt -s ysh:upgrade
23
24setglobal ENV.PS4 = '%%% '
25
26use $[ENV.REPO_ROOT]/spec/testdata/module2/env.ysh
27
28## STDOUT:
29env.ysh
30OSH ok
31## END
32
33#### bin/ysh doesn't have exported vars (declare -x)
34
35osh=$SH # this file is run by OSH
36
37case $osh in
38 *osh)
39 echo 'OSH ok'
40 ;;
41esac
42
43var ysh = osh.replace('osh', 'ysh')
44
45# NOT exported
46$ysh -c 'echo sh=$[getVar("SH")]'
47
48## STDOUT:
49OSH ok
50sh=null
51## END
52
53#### Temp bindings A=a B=b my-command push to ENV Obj (ysh:all)
54shopt -s ysh:all
55
56_A=a _B=b env | grep '^_' | sort
57
58## STDOUT:
59_A=a
60_B=b
61## END
62
63#### Nested temp bindings
64
65f2() {
66 echo " f2 AA=$AA BB=$BB"
67 env | egrep 'AA|BB'
68}
69
70f1() {
71 echo "> f1 AA=$AA"
72 AA=aaaa BB=bb f2
73 echo "< f1 AA=$AA"
74}
75
76AA=a f1
77
78#
79# Now with ysh:upgrade
80#
81
82shopt --set ysh:upgrade
83echo
84
85proc p2 {
86 echo " p2 AA=$[get(ENV, 'AA')] BB=$[get(ENV, 'BB')]"
87 env | egrep 'AA|BB'
88}
89
90proc p1 {
91 echo "> p1 AA=$[get(ENV, 'AA')]"
92 AA=aaaa BB=bb p2
93 echo "< p1 AA=$[get(ENV, 'AA')]"
94}
95
96AA=a p1
97
98#
99# Now with ysh:all
100#
101
102shopt --set ysh:all
103echo
104
105AA=a p1
106
107## STDOUT:
108> f1 AA=a
109 f2 AA=aaaa BB=bb
110AA=aaaa
111BB=bb
112< f1 AA=a
113
114> p1 AA=a
115 p2 AA=aaaa BB=bb
116AA=aaaa
117BB=bb
118< p1 AA=a
119
120> p1 AA=a
121 p2 AA=aaaa BB=bb
122AA=aaaa
123BB=bb
124< p1 AA=a
125## END
126
127#### Temp bindings can use locals in the same frame,(don't introduce new frame)
128
129# OSH: FOO can use x, but FOO is also bound
130shfunc() {
131 local x='zzz'
132 # There is no FOO here, because the argument to echo is evaluated first
133 FOO=$x echo "shfunc x=$x FOO=${FOO:-}"
134 FOO=$x eval 'echo shfunc x=$x FOO=$FOO'
135}
136
137shfunc
138echo
139
140shopt --set ysh:upgrade
141
142# YSH: FOO can use x, but FOO is also bound
143proc p {
144 var x = 'zzz'
145 # There is no ENV.FOO here, because the argument to echo is evaluated first
146 FOO=$x echo "ysh x=$x FOO=${FOO:-} ENV.FOO=$[get(ENV, 'FOO')]"
147 FOO=$x eval 'echo ysh x=$x FOO=${FOO:-} ENV.FOO=$[get(ENV, "FOO")]'
148 FOO=$x redir { echo "ysh x=$x FOO=${FOO:-} ENV.FOO=$[get(ENV, 'FOO')]" }
149}
150
151p
152
153## STDOUT:
154shfunc x=zzz FOO=
155shfunc x=zzz FOO=zzz
156
157ysh x=zzz FOO= ENV.FOO=null
158ysh x=zzz FOO= ENV.FOO=zzz
159ysh x=zzz FOO= ENV.FOO=zzz
160## END
161
162
163#### setglobal ENV.PYTHONPATH = 'foo' changes child process state
164shopt -s ysh:upgrade
165
166setglobal ENV.PYTHONPATH = 'foo'
167
168#pp test_ (ENV)
169#export PYTHONPATH=zz
170
171# execute POSIX shell
172sh -c 'echo pythonpath=$PYTHONPATH'
173
174## STDOUT:
175pythonpath=foo
176## END
177
178#### export builtin is disabled in ysh:all, in favor of setglobal
179shopt -s ysh:all
180
181setglobal ENV.ZZ = 'setglobal'
182
183# execute POSIX shell
184sh -c 'echo ZZ=$ZZ'
185
186export ZZ='export' # fails
187
188sh -c 'echo ZZ=$ZZ' # not reached
189
190## status: 1
191## STDOUT:
192ZZ=setglobal
193## END
194
195#### ysh:upgrade can use both export builtin and setglobal ENV
196shopt -s ysh:upgrade
197
198export ZZ='export' # fails
199
200sh -c 'echo ZZ=$ZZ' # not reached
201
202setglobal ENV.ZZ = 'setglobal' # this takes precedence
203
204# execute POSIX shell
205sh -c 'echo ZZ=$ZZ'
206
207## STDOUT:
208ZZ=export
209ZZ=setglobal
210## END
211
212
213#### PS4 environment variable is respected
214shopt -s ysh:upgrade
215
216setglobal ENV.PS4 = '%%% '
217
218$[ENV.SH] -c 'set -x; echo 1; echo 2'
219
220## STDOUT:
2211
2222
223## END
224## STDERR:
225%%% echo 1
226%%% echo 2
227## END
228
229
230#### ENV.HOME is respected
231
232HOME=zz-osh
233echo ~/src
234
235shopt --set ysh:upgrade
236
237setvar ENV.HOME = 'ysh-zz'
238
239# TODO: this should consult ENV.HOME
240echo ~/src
241
242# not set by spec test framework
243#echo $[ENV.HOME]
244
245## STDOUT:
246zz-osh/src
247ysh-zz/src
248## END
249
250#### exec builtin respects ENV
251
252shopt --set ysh:upgrade
253
254#export ZZ=zzz
255setglobal ENV.ZZ = 'zz'
256
257env sh -c 'echo child ZZ=$ZZ'
258
259exec env sh -c 'echo exec ZZ=$ZZ'
260
261## STDOUT:
262child ZZ=zz
263exec ZZ=zz
264## END
265
266#### setglobal quirk - do we need setenv?
267shopt --set ysh:all
268
269proc p {
270 # quirk: MOST visible Dict is mutated
271 setglobal ENV.perm = 'perm'
272}
273
274FOO=bar p
275
276# quirk: that Dict is gone
277# we could add 'setenv' to work around this
278pp test_ (get(ENV, 'perm'))
279
280p
281pp test_ (get(ENV, 'perm'))
282
283
284## STDOUT:
285(Null) null
286(Str) "perm"
287## END
288
289#### try to corrupt ENV var from user code
290shopt --set ysh:all
291
292setglobal ENV.AA = 'aa'
293
294proc p {
295 # this doesn't do anything, because Mem still have self.env_object
296 setglobal ENV = null
297
298 # TODO: there could be other ways to mess it up, and hit e_die()
299 # Right now, it's not possible to mutate 'prototype'. But if so we could
300 # mess up ENV.
301}
302
303FOO=bar p
304
305= ENV.AA
306
307## STDOUT:
308(Str) 'aa'
309## END