summaryrefslogtreecommitdiff
path: root/tests/func.tests
blob: 23dff44bed4378f523ffe8e109dee00f54e6425e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
a()
{
	x=$((x - 1))
	return 5
}

b()
{
	x=$((x - 1))
	a
	echo a returns $?
	return 4
}

c()
{
	x=$((x - 1))
	b
	echo b returns $?
	return 3
}

d()
{
	x=$((x - 1))
	c
	echo c returns $?
	return 2
}

e()
{
	d
	echo d returns $?
	echo in e
	x=$((x - 1))
	return $x
}

f()
{
	e
	echo e returned $?
	echo x is $x
	return 0
}

x=30
f

# make sure unsetting a local variable preserves the `local' attribute
f1()
{
	local zz
	zz=abcde
	echo $zz
	unset zz
	zz=defghi
	echo $zz
}

zz=ZZ
echo $zz
f1
echo $zz

unset -f f1
f1()
{
	return 5
}

( f1 )
echo $?

unset -f f1
f1()
{
	sleep 5
	return 5
}

f1 &
wait
echo $?

unset -f f1

f1()
{
	echo $AVAR
	printenv AVAR
}

AVAR=AVAR
echo $AVAR
f1
AVAR=foo f1
echo $AVAR

unset -f f1
# make sure subshells can do a `return' if we're executing in a function
f1()
{
	( return 5 )
	status=$?
	echo $status
	return $status
}

f1
echo $?

declare -F f1	# should print just the name
declare -f f1	# should print the definition, too

# no functions should be exported, right?
declare -xF
declare -xf

# FUNCNAME tests
func2()
{
	echo FUNCNAME = $FUNCNAME
}

func()
{
	echo before: FUNCNAME = $FUNCNAME
	func2
	echo after: FUNCNAME = $FUNCNAME
}

echo before: try to assign to FUNCNAME
FUNCNAME=7

echo outside: FUNCNAME = $FUNCNAME
func
echo outside2: FUNCNAME = $FUNCNAME

# test exported functions (and cached exportstr)
zf()
{
	echo this is zf
}
export -f zf

${THIS_SH} -c 'type -t zf'
${THIS_SH} -c 'type zf'

${THIS_SH} ./func1.sub

# tests for functions whose bodies are not group commands, with and without
# attached redirections
${THIS_SH} ./func2.sub

# test for some posix-specific function behavior
${THIS_SH} ./func3.sub

unset -f myfunction
myfunction() {
    echo "bad shell function redirection"
} >> /dev/null

myfunction
myfunction | cat

segv()
{
	echo foo | return 5
}

segv
echo $?

exit 0