@@ -47,7 +47,20 @@ load './util/init.sh'
47
47
assert_output -p " Passing numbers for the signal specs is prohibited"
48
48
}
49
49
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)" {
51
64
somefunction () { : ; }
52
65
core.trap_add ' somefunction' ' USR1'
53
66
@@ -56,6 +69,21 @@ load './util/init.sh'
56
69
}
57
70
58
71
@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\n b'
84
+ }
85
+
86
+ @test " core.trap_add adds function properly 2 (variable)" {
59
87
somefunction () { : ; }
60
88
somefunction2 () { : ; }
61
89
core.trap_add ' somefunction' ' USR1'
@@ -65,7 +93,21 @@ load './util/init.sh'
65
93
[ " ${___global_trap_table___[USR1]} " = $' \x1C somefunction\x1C somefunction2' ]
66
94
}
67
95
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\n a'
108
+ }
109
+
110
+ @test " core.trap_add adds function properly 3 (variable)" {
69
111
somefunction () { : ; }
70
112
core.trap_add ' somefunction' ' USR1' ' USR2'
71
113
@@ -102,7 +144,22 @@ load './util/init.sh'
102
144
assert_output -p " Function 'nonexistent' is not defined"
103
145
}
104
146
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)" {
106
163
somefunction () { : ; }
107
164
core.trap_add ' somefunction' ' USR1'
108
165
core.trap_remove ' somefunction' ' USR1'
@@ -111,6 +168,22 @@ load './util/init.sh'
111
168
}
112
169
113
170
@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)" {
114
187
somefunction () { : ; }
115
188
somefunction2 () { : ; }
116
189
core.trap_add ' somefunction' ' USR1'
@@ -122,6 +195,23 @@ load './util/init.sh'
122
195
}
123
196
124
197
@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)" {
125
215
somefunction () { : ; }
126
216
core.trap_add ' somefunction' ' USR1'
127
217
core.trap_add ' somefunction' ' USR2'
@@ -131,7 +221,24 @@ load './util/init.sh'
131
221
[ " ${___global_trap_table___[USR2]} " = ' ' ]
132
222
}
133
223
224
+
134
225
@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)" {
135
242
somefunction () { : ; }
136
243
somefunction2 () { : ; }
137
244
core.trap_add ' somefunction' ' USR1'
@@ -142,4 +249,31 @@ load './util/init.sh'
142
249
[ " ${___global_trap_table___[USR1]} " = $' \x1C somefunction' ]
143
250
}
144
251
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
+ "
145
276
277
+ assert_success
278
+ assert_output -p " Trap handler function 'somefunction' that was registered for signal 'USR1' no longer exists"
279
+ }
0 commit comments