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

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