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

152 lines, 89 significant
1## oils_failures_allowed: 1
2## compare_shells: bash dash mksh
3
4#### Locals don't leak
5f() {
6 local f_var=f_var
7}
8f
9echo $f_var
10## stdout:
11
12#### Globals leak
13f() {
14 f_var=f_var
15}
16f
17echo $f_var
18## stdout: f_var
19
20#### Return statement
21f() {
22 echo one
23 return 42
24 echo two
25}
26f
27## stdout: one
28## status: 42
29
30#### Dynamic Scope
31f() {
32 echo $g_var
33}
34g() {
35 local g_var=g_var
36 f
37}
38g
39## stdout: g_var
40
41#### Dynamic Scope Mutation (wow this is bad)
42f() {
43 g_var=f_mutation
44}
45g() {
46 local g_var=g_var
47 echo "g_var=$g_var"
48 f
49 echo "g_var=$g_var"
50}
51g
52echo g_var=$g_var
53## STDOUT:
54g_var=g_var
55g_var=f_mutation
56g_var=
57## END
58
59#### Assign local separately
60f() {
61 local f
62 f='new-value'
63 echo "[$f]"
64}
65f
66## stdout: [new-value]
67## status: 0
68
69#### Assign a local and global on same line
70myglobal=
71f() {
72 local mylocal
73 mylocal=L myglobal=G
74 echo "[$mylocal $myglobal]"
75}
76f
77echo "[$mylocal $myglobal]"
78## stdout-json: "[L G]\n[ G]\n"
79## status: 0
80
81#### Return without args gives previous
82f() {
83 ( exit 42 )
84 return
85}
86f
87echo status=$?
88## STDOUT:
89status=42
90## END
91
92#### return "" (a lot of disagreement)
93f() {
94 echo f
95 return ""
96}
97
98f
99echo status=$?
100## STDOUT:
101f
102status=0
103## END
104## status: 0
105
106## OK dash status: 2
107## OK dash STDOUT:
108f
109## END
110
111## BUG mksh STDOUT:
112f
113status=1
114## END
115
116## BUG bash STDOUT:
117f
118status=2
119## END
120
121#### return $empty
122f() {
123 echo f
124 empty=
125 return $empty
126}
127
128f
129echo status=$?
130## STDOUT:
131f
132status=0
133## END
134
135#### Subshell function
136
137f() ( return 42; )
138# BUG: OSH raises invalid control flow! I think we should just allow 'return'
139# but maybe not 'break' etc.
140g() ( return 42 )
141# bash warns here but doesn't cause an error
142# g() ( break )
143
144f
145echo status=$?
146g
147echo status=$?
148
149## STDOUT:
150status=42
151status=42
152## END