Skip to content

Commit 7ecc1ec

Browse files
committed
test: Properly test trap functions
Previously, the `core.trap_*` functions were not fully tested because Bats appears to be intercepting the signals (they were only tested by testing for its side effects, like saving global variables). This was fixed by running everything in a subprocess. Since the Bash builtin helpers are not available in this environment, some things like `load` had to be renamed to `source`, or removed entirely.
1 parent 08c54d8 commit 7ecc1ec

File tree

3 files changed

+137
-6
lines changed

3 files changed

+137
-6
lines changed

tests/trap.bats

Lines changed: 137 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,20 @@ load './util/init.sh'
4747
assert_output -p "Passing numbers for the signal specs is prohibited"
4848
}
4949

50-
@test "core.trap_add adds trap function properly" {
50+
@test "core.trap_add adds trap function properly 1" {
51+
BASALT_PACKAGE_DIR=$BASALT_PACKAGE_DIR run bash -c "
52+
cd \"\$BASALT_PACKAGE_DIR/tests\" || { printf '%s\n' 'Failed to cd'; exit 1; }
53+
source ./util/init.sh
54+
55+
somefunction() { printf '%s\n' 'a'; }
56+
core.trap_add 'somefunction' 'USR1'
57+
kill -USR1 \$\$"
58+
59+
assert_success
60+
assert_output "a"
61+
}
62+
63+
@test "core.trap_add adds trap function properly 1 (variable)" {
5164
somefunction() { :; }
5265
core.trap_add 'somefunction' 'USR1'
5366

@@ -56,6 +69,21 @@ load './util/init.sh'
5669
}
5770

5871
@test "core.trap_add adds function properly 2" {
72+
BASALT_PACKAGE_DIR=$BASALT_PACKAGE_DIR run bash -c "
73+
cd \"\$BASALT_PACKAGE_DIR/tests\" || { printf '%s\n' 'Failed to cd'; exit 1; }
74+
source ./util/init.sh
75+
76+
somefunction() { printf '%s\n' 'a'; }
77+
somefunction2() { printf '%s\n' 'b'; }
78+
core.trap_add 'somefunction' 'USR1'
79+
core.trap_add 'somefunction2' 'USR1'
80+
kill -USR1 \$\$"
81+
82+
assert_success
83+
assert_output $'a\nb'
84+
}
85+
86+
@test "core.trap_add adds function properly 2 (variable)" {
5987
somefunction() { :; }
6088
somefunction2() { :; }
6189
core.trap_add 'somefunction' 'USR1'
@@ -65,7 +93,21 @@ load './util/init.sh'
6593
[ "${___global_trap_table___[USR1]}" = $'\x1Csomefunction\x1Csomefunction2' ]
6694
}
6795

68-
@test "core.trap_add adds function properly 3" {
96+
@test "core.trap_add adds function properly 3" {
97+
BASALT_PACKAGE_DIR=$BASALT_PACKAGE_DIR run bash -c "
98+
cd \"\$BASALT_PACKAGE_DIR/tests\" || { printf '%s\n' 'Failed to cd'; exit 1; }
99+
source ./util/init.sh
100+
101+
somefunction() { printf '%s\n' 'a'; }
102+
core.trap_add 'somefunction' 'USR1' 'USR2'
103+
kill -USR1 \$\$
104+
kill -USR2 \$\$"
105+
106+
assert_success
107+
assert_output $'a\na'
108+
}
109+
110+
@test "core.trap_add adds function properly 3 (variable)" {
69111
somefunction() { :; }
70112
core.trap_add 'somefunction' 'USR1' 'USR2'
71113

@@ -102,7 +144,22 @@ load './util/init.sh'
102144
assert_output -p "Function 'nonexistent' is not defined"
103145
}
104146

105-
@test "core.trap_remove removes trap function properly" {
147+
@test "core.trap_remove removes trap function properly 1" {
148+
BASALT_PACKAGE_DIR=$BASALT_PACKAGE_DIR run bash -c "
149+
cd \"\$BASALT_PACKAGE_DIR/tests\" || { printf '%s\n' 'Failed to cd'; exit 1; }
150+
source ./util/init.sh
151+
152+
somefunction() { printf '%s\n' 'a'; }
153+
core.trap_add 'somefunction' 'USR1'
154+
core.trap_remove 'somefunction' 'USR1'
155+
kill -USR1 \$\$"
156+
157+
assert_failure
158+
assert [ "$status" = 138 ] # Matches regular Bash behavior
159+
assert_output ''
160+
}
161+
162+
@test "core.trap_remove removes trap function properly 1 (variable)" {
106163
somefunction() { :; }
107164
core.trap_add 'somefunction' 'USR1'
108165
core.trap_remove 'somefunction' 'USR1'
@@ -111,6 +168,22 @@ load './util/init.sh'
111168
}
112169

113170
@test "core.trap_remove removes trap function properly 2" {
171+
BASALT_PACKAGE_DIR=$BASALT_PACKAGE_DIR run bash -c "
172+
cd \"\$BASALT_PACKAGE_DIR/tests\" || { printf '%s\n' 'Failed to cd'; exit 1; }
173+
source ./util/init.sh
174+
175+
somefunction() { printf '%s\n' 'a'; }
176+
somefunction2() { printf '%s\n' 'b'; }
177+
core.trap_add 'somefunction' 'USR1'
178+
core.trap_add 'somefunction2' 'USR1'
179+
core.trap_remove 'somefunction' 'USR1'
180+
kill -USR1 \$\$"
181+
182+
assert_success
183+
assert_output 'b'
184+
}
185+
186+
@test "core.trap_remove removes trap function properly 2 (variable)" {
114187
somefunction() { :; }
115188
somefunction2() { :; }
116189
core.trap_add 'somefunction' 'USR1'
@@ -122,6 +195,23 @@ load './util/init.sh'
122195
}
123196

124197
@test "core.trap_add removes function properly 3" {
198+
BASALT_PACKAGE_DIR=$BASALT_PACKAGE_DIR run bash -c "
199+
cd \"\$BASALT_PACKAGE_DIR/tests\" || { printf '%s\n' 'Failed to cd'; exit 1; }
200+
source ./util/init.sh
201+
202+
somefunction() { printf '%s\n' 'a'; }
203+
somefunction2() { printf '%s\n' 'b'; }
204+
core.trap_add 'somefunction' 'USR1'
205+
core.trap_add 'somefunction' 'USR2'
206+
core.trap_remove 'somefunction' 'USR1' 'USR2'
207+
kill -USR1 \$\$"
208+
209+
assert_failure
210+
assert [ "$status" = 138 ] # Matches regular Bash behavior
211+
assert_output ''
212+
}
213+
214+
@test "core.trap_add removes function properly 3 (variable)" {
125215
somefunction() { :; }
126216
core.trap_add 'somefunction' 'USR1'
127217
core.trap_add 'somefunction' 'USR2'
@@ -131,7 +221,24 @@ load './util/init.sh'
131221
[ "${___global_trap_table___[USR2]}" = '' ]
132222
}
133223

224+
134225
@test "core.trap_remove removes trap function properly 4" {
226+
BASALT_PACKAGE_DIR=$BASALT_PACKAGE_DIR run bash -c "
227+
cd \"\$BASALT_PACKAGE_DIR/tests\" || { printf '%s\n' 'Failed to cd'; exit 1; }
228+
source ./util/init.sh
229+
230+
somefunction() { printf '%s\n' 'a'; }
231+
somefunction2() { printf '%s\n' 'b'; }
232+
core.trap_add 'somefunction' 'USR1'
233+
core.trap_add 'somefunction2' 'USR1'
234+
core.trap_remove 'somefunction2' 'USR1'
235+
kill -USR1 \$\$"
236+
237+
assert_success
238+
assert_output 'a'
239+
}
240+
241+
@test "core.trap_remove removes trap function properly 4 (variable)" {
135242
somefunction() { :; }
136243
somefunction2() { :; }
137244
core.trap_add 'somefunction' 'USR1'
@@ -142,4 +249,31 @@ load './util/init.sh'
142249
[ "${___global_trap_table___[USR1]}" = $'\x1Csomefunction' ]
143250
}
144251

252+
@test "handling fails if user-provided trap handler fails" {
253+
BASALT_PACKAGE_DIR=$BASALT_PACKAGE_DIR run bash -c "
254+
cd \"\$BASALT_PACKAGE_DIR/tests\"
255+
source ./util/init.sh
256+
257+
somefunction() { return 33; }
258+
core.trap_add 'somefunction' 'USR1'
259+
kill -USR1 \$\$
260+
"
261+
262+
assert_failure
263+
assert_output -p "User-provided trap handler spectacularly failed with exit code 33"
264+
}
265+
266+
@test "handling fails if user-provided function no longer exists" {
267+
BASALT_PACKAGE_DIR=$BASALT_PACKAGE_DIR run bash -c "
268+
cd \"\$BASALT_PACKAGE_DIR/tests\"
269+
source ./util/init.sh
270+
271+
somefunction() { return 33; }
272+
core.trap_add 'somefunction' 'USR1'
273+
unset -f somefunction
274+
kill -USR1 \$\$
275+
"
145276

277+
assert_success
278+
assert_output -p "Trap handler function 'somefunction' that was registered for signal 'USR1' no longer exists"
279+
}

tests/util/init.sh

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ basalt.package-init || exit
55
basalt.package-load || exit
66
basalt.load 'github.com/hyperupcall/bats-all' 'load.bash'
77

8-
load './util/test_util.sh'
9-
108
setup() {
119
cd "$BATS_TEST_TMPDIR"
1210
}

tests/util/test_util.sh

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)