1 ## oils_failures_allowed: 1
2
3 #### Can read from ENV Dict
4 shopt -s ysh:upgrade
5
6 pp test_ (type(ENV))
7 #pp test_ (ENV)
8
9 # Set by the spec test harness
10
11 if (ENV.SH ~~ '*osh') {
12 echo ok
13 }
14 #echo SH=$[ENV.SH]
15
16 ## STDOUT:
17 (Str) "Dict"
18 ok
19 ## END
20
21 #### ENV works in different modules
22 shopt -s ysh:upgrade
23
24 setglobal ENV.PS4 = '%%% '
25
26 use $[ENV.REPO_ROOT]/spec/testdata/module2/env.ysh
27
28 ## STDOUT:
29 env.ysh
30 OSH ok
31 ## END
32
33
34 #### bin/ysh doesn't have exported vars (declare -x)
35
36 osh=$SH # this file is run by OSH
37
38 case $osh in
39 *osh)
40 echo 'OSH ok'
41 ;;
42 esac
43
44 var ysh = osh.replace('osh', 'ysh')
45
46 # NOT exported
47 $ysh -c 'echo sh=$[getVar("SH")]'
48
49 ## STDOUT:
50 OSH ok
51 sh=null
52 ## END
53
54 #### Temp bindings A=a B=b my-command push to ENV Obj (ysh:all)
55 shopt -s ysh:all
56
57 _A=a _B=b env | grep '^_' | sort
58
59 ## STDOUT:
60 _A=a
61 _B=b
62 ## END
63
64 #### setglobal ENV.PYTHONPATH = 'foo' changes child process state
65 shopt -s ysh:upgrade
66
67 setglobal ENV.PYTHONPATH = 'foo'
68
69 #pp test_ (ENV)
70 #export PYTHONPATH=zz
71
72 # execute POSIX shell
73 sh -c 'echo pythonpath=$PYTHONPATH'
74
75 ## STDOUT:
76 pythonpath=foo
77 ## END
78
79 #### export builtin is disabled in ysh:all, in favor of setglobal
80 shopt -s ysh:all
81
82 setglobal ENV.ZZ = 'setglobal'
83
84 # execute POSIX shell
85 sh -c 'echo ZZ=$ZZ'
86
87 export ZZ='export' # fails
88
89 sh -c 'echo ZZ=$ZZ' # not reached
90
91 ## status: 1
92 ## STDOUT:
93 ZZ=setglobal
94 ## END
95
96 #### ysh:upgrade can use both export builtin and setglobal ENV
97 shopt -s ysh:upgrade
98
99 export ZZ='export' # fails
100
101 sh -c 'echo ZZ=$ZZ' # not reached
102
103 setglobal ENV.ZZ = 'setglobal' # this takes precedence
104
105 # execute POSIX shell
106 sh -c 'echo ZZ=$ZZ'
107
108 ## STDOUT:
109 ZZ=export
110 ZZ=setglobal
111 ## END
112
113
114 #### PS4 environment variable is respected
115 shopt -s ysh:upgrade
116
117 setglobal ENV.PS4 = '%%% '
118
119 $[ENV.SH] -c 'set -x; echo 1; echo 2'
120
121 ## STDOUT:
122 1
123 2
124 ## END
125 ## STDERR:
126 %%% echo 1
127 %%% echo 2
128 ## END
129
130
131 #### ENV.HOME is respected
132
133 HOME=zz-osh
134 echo ~/src
135
136 shopt --set ysh:upgrade
137
138 setvar ENV.HOME = 'ysh-zz'
139
140 # TODO: this should consult ENV.HOME
141 echo ~/src
142
143 # not set by spec test framework
144 #echo $[ENV.HOME]
145
146 ## STDOUT:
147 zz-osh/src
148 ysh-zz/src
149 ## END
150
151 #### exec builtin respects ENV
152
153 shopt --set ysh:upgrade
154
155 #export ZZ=zzz
156 setglobal ENV.ZZ = 'zz'
157
158 env sh -c 'echo child ZZ=$ZZ'
159
160 exec env sh -c 'echo exec ZZ=$ZZ'
161
162 ## STDOUT:
163 child ZZ=zz
164 exec ZZ=zz
165 ## END