OILS / spec / temp-binding.test.sh View on Github | oils.pub

154 lines, 84 significant
1## compare_shells: dash bash zsh mksh ash yash
2## oils_failures_allowed: 0
3
4# forked from spec/ble-idioms
5# the IFS= eval 'local x' bug
6
7#### More eval 'local v='
8case $SH in mksh) exit ;; esac
9
10set -u
11
12f() {
13 # The temp env messes it up
14 tmp1= local x=x
15 tmp2= eval 'local y=y'
16
17 # similar to eval
18 tmp3= . $REPO_ROOT/spec/testdata/define-local-var-z.sh
19
20 # Bug does not appear with only eval
21 #eval 'local v=hello'
22
23 #declare -p v
24 echo x=$x
25 echo y=$y
26 echo z=$z
27}
28
29f
30
31## STDOUT:
32x=x
33y=y
34z=z
35## END
36
37## N-I mksh STDOUT:
38## END
39
40#### Temp bindings with local
41
42f() {
43 local x=x
44 tmp='' local tx=tx
45
46 # Hm both y and ty persist in bash/zsh
47 eval 'local y=y'
48 tmp='' eval 'local ty=ty'
49
50 # Why does this have an effect in OSH? Oh because 'unset' is a special
51 # builtin
52 if true; then
53 x='X' unset x
54 tx='TX' unset tx
55 y='Y' unset y
56 ty='TY' unset ty
57 fi
58
59 #unset y
60 #unset ty
61
62 echo x=$x
63 echo tx=$tx
64 echo y=$y
65 echo ty=$ty
66}
67
68f
69
70## BUG bash/zsh STDOUT:
71x=x
72tx=tx
73y=y
74ty=ty
75## END
76
77## STDOUT:
78x=
79tx=
80y=
81ty=
82## END
83
84#### Temp bindings with unset
85
86# key point:
87# unset looks up the stack
88# local doesn't though
89
90x=42
91unset x
92echo x=$x
93
94echo ---
95
96x=42
97tmp= unset x
98echo x=$x
99
100x=42
101tmp= eval 'unset x'
102echo x=$x
103
104echo ---
105
106shadow() {
107 x=42
108 x=tmp unset x
109 echo x=$x
110
111 x=42
112 x=tmp eval 'unset x'
113 echo x=$x
114}
115
116shadow
117
118echo ---
119
120case $SH in
121 bash) set -o posix ;;
122esac
123shadow
124
125# Now shadow
126
127# unset is a special builtin
128# type unset
129
130## STDOUT:
131x=
132---
133x=
134x=
135---
136x=42
137x=42
138---
139x=42
140x=42
141## END
142
143## BUG mksh/ash/dash/yash STDOUT:
144x=
145---
146x=
147x=
148---
149x=
150x=
151---
152x=
153x=
154## END