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

165 lines, 63 significant
1## oils_failures_allowed: 1
2
3#### Can read from ENV Dict
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) "Dict"
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
34#### bin/ysh doesn't have exported vars (declare -x)
35
36osh=$SH # this file is run by OSH
37
38case $osh in
39 *osh)
40 echo 'OSH ok'
41 ;;
42esac
43
44var ysh = osh.replace('osh', 'ysh')
45
46# NOT exported
47$ysh -c 'echo sh=$[getVar("SH")]'
48
49## STDOUT:
50OSH ok
51sh=null
52## END
53
54#### Temp bindings A=a B=b my-command push to ENV Obj (ysh:all)
55shopt -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
65shopt -s ysh:upgrade
66
67setglobal ENV.PYTHONPATH = 'foo'
68
69#pp test_ (ENV)
70#export PYTHONPATH=zz
71
72# execute POSIX shell
73sh -c 'echo pythonpath=$PYTHONPATH'
74
75## STDOUT:
76pythonpath=foo
77## END
78
79#### export builtin is disabled in ysh:all, in favor of setglobal
80shopt -s ysh:all
81
82setglobal ENV.ZZ = 'setglobal'
83
84# execute POSIX shell
85sh -c 'echo ZZ=$ZZ'
86
87export ZZ='export' # fails
88
89sh -c 'echo ZZ=$ZZ' # not reached
90
91## status: 1
92## STDOUT:
93ZZ=setglobal
94## END
95
96#### ysh:upgrade can use both export builtin and setglobal ENV
97shopt -s ysh:upgrade
98
99export ZZ='export' # fails
100
101sh -c 'echo ZZ=$ZZ' # not reached
102
103setglobal ENV.ZZ = 'setglobal' # this takes precedence
104
105# execute POSIX shell
106sh -c 'echo ZZ=$ZZ'
107
108## STDOUT:
109ZZ=export
110ZZ=setglobal
111## END
112
113
114#### PS4 environment variable is respected
115shopt -s ysh:upgrade
116
117setglobal ENV.PS4 = '%%% '
118
119$[ENV.SH] -c 'set -x; echo 1; echo 2'
120
121## STDOUT:
1221
1232
124## END
125## STDERR:
126%%% echo 1
127%%% echo 2
128## END
129
130
131#### ENV.HOME is respected
132
133HOME=zz-osh
134echo ~/src
135
136shopt --set ysh:upgrade
137
138setvar ENV.HOME = 'ysh-zz'
139
140# TODO: this should consult ENV.HOME
141echo ~/src
142
143# not set by spec test framework
144#echo $[ENV.HOME]
145
146## STDOUT:
147zz-osh/src
148ysh-zz/src
149## END
150
151#### exec builtin respects ENV
152
153shopt --set ysh:upgrade
154
155#export ZZ=zzz
156setglobal ENV.ZZ = 'zz'
157
158env sh -c 'echo child ZZ=$ZZ'
159
160exec env sh -c 'echo exec ZZ=$ZZ'
161
162## STDOUT:
163child ZZ=zz
164exec ZZ=zz
165## END