@@ -5,53 +5,50 @@ import (
55 "testing"
66)
77
8+ // helper to catch panics
9+ func mustNotPanic (t * testing.T , fn func ()) {
10+ defer func () {
11+ if r := recover (); r != nil {
12+ t .Errorf ("main() panicked: %v" , r )
13+ }
14+ }()
15+ fn ()
16+ }
17+
818func TestMain_WithTestExit (t * testing.T ) {
9- // Test the test exit hook
1019 os .Setenv ("INBOUNDPARSE_TEST_EXIT" , "1" )
1120 defer os .Unsetenv ("INBOUNDPARSE_TEST_EXIT" )
12-
13- // This should return early without error
14- main ()
21+ mustNotPanic (t , main )
1522}
1623
1724func TestMain_WithTestInitOnly (t * testing.T ) {
18- // Test the test init only hook
1925 os .Setenv ("INBOUNDPARSE_TEST_EXIT" , "1" )
2026 os .Setenv ("INBOUNDPARSE_TEST_INIT_ONLY" , "1" )
2127 defer func () {
2228 os .Unsetenv ("INBOUNDPARSE_TEST_EXIT" )
2329 os .Unsetenv ("INBOUNDPARSE_TEST_INIT_ONLY" )
2430 }()
25-
26- // This should return early without error
27- main ()
31+ mustNotPanic (t , main )
2832}
2933
3034func TestMain_WithWebhookURL (t * testing.T ) {
31- // Test with a valid webhook URL
3235 os .Setenv ("WEBHOOK_URL" , "http://test.example.com/webhook" )
3336 os .Setenv ("INBOUNDPARSE_TEST_EXIT" , "1" )
3437 defer func () {
3538 os .Unsetenv ("WEBHOOK_URL" )
3639 os .Unsetenv ("INBOUNDPARSE_TEST_EXIT" )
3740 }()
38-
39- // This should not panic
40- main ()
41+ mustNotPanic (t , main )
4142}
4243
4344func TestMain_WithoutWebhookURL (t * testing.T ) {
44- // Test without webhook URL (should fail config validation)
4545 os .Unsetenv ("WEBHOOK_URL" )
4646 os .Setenv ("INBOUNDPARSE_TEST_EXIT" , "1" )
4747 defer os .Unsetenv ("INBOUNDPARSE_TEST_EXIT" )
48-
49- // This should not panic (config validation happens after test exit check)
50- main ()
48+ mustNotPanic (t , main )
5149}
5250
5351func TestMain_WithVerbose (t * testing.T ) {
54- // Test with verbose logging
5552 os .Setenv ("WEBHOOK_URL" , "http://test.example.com/webhook" )
5653 os .Setenv ("VERBOSE" , "true" )
5754 os .Setenv ("INBOUNDPARSE_TEST_EXIT" , "1" )
@@ -60,13 +57,10 @@ func TestMain_WithVerbose(t *testing.T) {
6057 os .Unsetenv ("VERBOSE" )
6158 os .Unsetenv ("INBOUNDPARSE_TEST_EXIT" )
6259 }()
63-
64- // This should not panic
65- main ()
60+ mustNotPanic (t , main )
6661}
6762
6863func TestMain_WithSentry (t * testing.T ) {
69- // Test with Sentry enabled
7064 os .Setenv ("WEBHOOK_URL" , "http://test.example.com/webhook" )
7165 os .Setenv ("ENABLE_SENTRY" , "true" )
7266 os .Setenv ("SENTRY_DSN" , "https://test@sentry.io/123456" )
@@ -77,13 +71,10 @@ func TestMain_WithSentry(t *testing.T) {
7771 os .Unsetenv ("SENTRY_DSN" )
7872 os .Unsetenv ("INBOUNDPARSE_TEST_EXIT" )
7973 }()
80-
81- // This should not panic
82- main ()
74+ mustNotPanic (t , main )
8375}
8476
8577func TestMain_WithMetrics (t * testing.T ) {
86- // Test with metrics enabled
8778 os .Setenv ("WEBHOOK_URL" , "http://test.example.com/webhook" )
8879 os .Setenv ("ENABLE_METRICS" , "true" )
8980 os .Setenv ("METRICS_ADDR" , "127.0.0.1:9091" )
@@ -94,13 +85,10 @@ func TestMain_WithMetrics(t *testing.T) {
9485 os .Unsetenv ("METRICS_ADDR" )
9586 os .Unsetenv ("INBOUNDPARSE_TEST_EXIT" )
9687 }()
97-
98- // This should not panic
99- main ()
88+ mustNotPanic (t , main )
10089}
10190
10291func TestMain_WithTLS (t * testing.T ) {
103- // Test with TLS configuration
10492 os .Setenv ("WEBHOOK_URL" , "http://test.example.com/webhook" )
10593 os .Setenv ("CERT_FILE" , "/path/to/cert.pem" )
10694 os .Setenv ("KEY_FILE" , "/path/to/key.pem" )
@@ -111,13 +99,10 @@ func TestMain_WithTLS(t *testing.T) {
11199 os .Unsetenv ("KEY_FILE" )
112100 os .Unsetenv ("INBOUNDPARSE_TEST_EXIT" )
113101 }()
114-
115- // This should not panic
116- main ()
102+ mustNotPanic (t , main )
117103}
118104
119105func TestMain_WithCustomListenAddr (t * testing.T ) {
120- // Test with custom listen addresses
121106 os .Setenv ("WEBHOOK_URL" , "http://test.example.com/webhook" )
122107 os .Setenv ("LISTEN_ADDR" , "127.0.0.1:2525" )
123108 os .Setenv ("LISTEN_ADDR_TLS" , "127.0.0.1:465" )
@@ -128,13 +113,10 @@ func TestMain_WithCustomListenAddr(t *testing.T) {
128113 os .Unsetenv ("LISTEN_ADDR_TLS" )
129114 os .Unsetenv ("INBOUNDPARSE_TEST_EXIT" )
130115 }()
131-
132- // This should not panic
133- main ()
116+ mustNotPanic (t , main )
134117}
135118
136119func TestMain_WithWebhookAuth (t * testing.T ) {
137- // Test with webhook authentication
138120 os .Setenv ("WEBHOOK_URL" , "http://test.example.com/webhook" )
139121 os .Setenv ("WEBHOOK_USER" , "testuser" )
140122 os .Setenv ("WEBHOOK_PASS" , "testpass" )
@@ -145,13 +127,10 @@ func TestMain_WithWebhookAuth(t *testing.T) {
145127 os .Unsetenv ("WEBHOOK_PASS" )
146128 os .Unsetenv ("INBOUNDPARSE_TEST_EXIT" )
147129 }()
148-
149- // This should not panic
150- main ()
130+ mustNotPanic (t , main )
151131}
152132
153133func TestMain_WithAuthDisabled (t * testing.T ) {
154- // Test with authentication disabled
155134 os .Setenv ("WEBHOOK_URL" , "http://test.example.com/webhook" )
156135 os .Setenv ("ENABLE_SPF" , "false" )
157136 os .Setenv ("ENABLE_DKIM" , "false" )
@@ -164,13 +143,10 @@ func TestMain_WithAuthDisabled(t *testing.T) {
164143 os .Unsetenv ("ENABLE_DMARC" )
165144 os .Unsetenv ("INBOUNDPARSE_TEST_EXIT" )
166145 }()
167-
168- // This should not panic
169- main ()
146+ mustNotPanic (t , main )
170147}
171148
172149func TestMain_WithCustomServerName (t * testing.T ) {
173- // Test with custom server name
174150 os .Setenv ("WEBHOOK_URL" , "http://test.example.com/webhook" )
175151 os .Setenv ("SERVER_NAME" , "custom.example.com" )
176152 os .Setenv ("INBOUNDPARSE_TEST_EXIT" , "1" )
@@ -179,13 +155,10 @@ func TestMain_WithCustomServerName(t *testing.T) {
179155 os .Unsetenv ("SERVER_NAME" )
180156 os .Unsetenv ("INBOUNDPARSE_TEST_EXIT" )
181157 }()
182-
183- // This should not panic
184- main ()
158+ mustNotPanic (t , main )
185159}
186160
187161func TestMain_WithCustomTimeouts (t * testing.T ) {
188- // Test with custom timeouts
189162 os .Setenv ("WEBHOOK_URL" , "http://test.example.com/webhook" )
190163 os .Setenv ("READ_TIMEOUT" , "60s" )
191164 os .Setenv ("WRITE_TIMEOUT" , "60s" )
@@ -196,13 +169,10 @@ func TestMain_WithCustomTimeouts(t *testing.T) {
196169 os .Unsetenv ("WRITE_TIMEOUT" )
197170 os .Unsetenv ("INBOUNDPARSE_TEST_EXIT" )
198171 }()
199-
200- // This should not panic
201- main ()
172+ mustNotPanic (t , main )
202173}
203174
204175func TestMain_WithCustomMessageSize (t * testing.T ) {
205- // Test with custom message size
206176 os .Setenv ("WEBHOOK_URL" , "http://test.example.com/webhook" )
207177 os .Setenv ("MAX_MESSAGE_SIZE" , "20971520" ) // 20MB
208178 os .Setenv ("INBOUNDPARSE_TEST_EXIT" , "1" )
@@ -211,13 +181,10 @@ func TestMain_WithCustomMessageSize(t *testing.T) {
211181 os .Unsetenv ("MAX_MESSAGE_SIZE" )
212182 os .Unsetenv ("INBOUNDPARSE_TEST_EXIT" )
213183 }()
214-
215- // This should not panic
216- main ()
184+ mustNotPanic (t , main )
217185}
218186
219187func TestMain_WithMetricsAuth (t * testing.T ) {
220- // Test with metrics authentication
221188 os .Setenv ("WEBHOOK_URL" , "http://test.example.com/webhook" )
222189 os .Setenv ("ENABLE_METRICS" , "true" )
223190 os .Setenv ("METRICS_API_KEY" , "test-api-key" )
@@ -232,13 +199,10 @@ func TestMain_WithMetricsAuth(t *testing.T) {
232199 os .Unsetenv ("METRICS_PASSWORD" )
233200 os .Unsetenv ("INBOUNDPARSE_TEST_EXIT" )
234201 }()
235-
236- // This should not panic
237- main ()
202+ mustNotPanic (t , main )
238203}
239204
240205func TestMain_WithSentryConfig (t * testing.T ) {
241- // Test with full Sentry configuration
242206 os .Setenv ("WEBHOOK_URL" , "http://test.example.com/webhook" )
243207 os .Setenv ("ENABLE_SENTRY" , "true" )
244208 os .Setenv ("SENTRY_DSN" , "https://test@sentry.io/123456" )
@@ -253,7 +217,20 @@ func TestMain_WithSentryConfig(t *testing.T) {
253217 os .Unsetenv ("SENTRY_RELEASE" )
254218 os .Unsetenv ("INBOUNDPARSE_TEST_EXIT" )
255219 }()
220+ mustNotPanic (t , main )
221+ }
256222
257- // This should not panic
258- main ()
223+ func TestMain_AppRunError (t * testing.T ) {
224+ os .Setenv ("WEBHOOK_URL" , "http://test.example.com/webhook" )
225+ os .Setenv ("INBOUNDPARSE_TEST_INIT_ONLY" , "1" )
226+ os .Unsetenv ("INBOUNDPARSE_TEST_EXIT" )
227+ defer func () {
228+ os .Unsetenv ("WEBHOOK_URL" )
229+ os .Unsetenv ("INBOUNDPARSE_TEST_INIT_ONLY" )
230+ // catch panics but expect main() to exit
231+ if r := recover (); r != nil {
232+ return
233+ }
234+ }()
235+ mustNotPanic (t , main )
259236}
0 commit comments