Commit e8c466f
authored
[test] Add tests for logger.ServerFileLogger.Close (#3159)
## Test Coverage Improvement: `ServerFileLogger.Close`
### Function Analyzed
- **Package**: `internal/logger`
- **Function**: `ServerFileLogger.Close`
- **File**: `internal/logger/server_file_logger.go:108`
- **Previous Coverage**: 58.8%
- **New Coverage**: 94.1%
- **Complexity**: High (nil-receiver guard, loop over file map,
sync+close error handling with first-error-wins tracking)
### Why This Function?
`ServerFileLogger.Close` had the lowest real test coverage (58.8%) among
non-env-gated functions in the testable subset of the codebase. Its
complexity comes from:
- Nil-receiver guard (callable on `nil *ServerFileLogger`)
- Iterating a map of open log files, running two operations per file
- First-error-wins tracking: `firstErr` captures the first Sync or Close
failure without being overwritten by later errors
- Unconditional map clearing even when errors occur
All of these paths except the "Sync succeeds but Close fails" branch
(which requires NFS-level failure to trigger) are now exercised.
### Tests Added
| Test | Path Covered |
|------|--------------|
| `TestServerFileLoggerClose_NilReceiver` | Nil-receiver guard in
`Close()` |
| `TestServerFileLoggerClose_EmptyFiles` | Loop body never entered; maps
are still reset |
| `TestServerFileLoggerClose_SyncError` | `file.Sync()` error →
`firstErr` set |
| `TestServerFileLoggerClose_FirstErrorTracking` | Multiple files both
failing; first-error-wins over multiple iterations |
| `TestServerFileLoggerClose_ValidFiles` | Happy-path close (all files
flush and close cleanly) |
| `TestServerFileLoggerClose_CloseTwice` | Idempotent: second call is a
no-op with empty maps |
| `TestServerFileLoggerLog_NilReceiver` | Nil-receiver guard in `Log()`
|
| `TestServerFileLoggerLog_SyncError` | `file.Sync()` error inside
`Log()` does not panic |
| `TestServerFileLoggerGetOrCreate_FileCreationError` | `os.OpenFile`
failure in `getOrCreateLogger()` gracefully falls back to `LogDebug` |
### Coverage Report
````
Before:
Close 58.8%
Log 84.6%
getOrCreateLogger 90.9%
After:
Close 94.1% (+35.3 pp)
Log 100.0% (+15.4 pp)
getOrCreateLogger 100.0% (+9.1 pp)
```
Overall `internal/logger` package: **88.8% → 90.7%**
### Test Execution
```
=== RUN TestServerFileLoggerClose_NilReceiver
--- PASS: TestServerFileLoggerClose_NilReceiver (0.00s)
=== RUN TestServerFileLoggerClose_EmptyFiles
--- PASS: TestServerFileLoggerClose_EmptyFiles (0.00s)
=== RUN TestServerFileLoggerClose_SyncError
--- PASS: TestServerFileLoggerClose_SyncError (0.00s)
=== RUN TestServerFileLoggerClose_FirstErrorTracking
--- PASS: TestServerFileLoggerClose_FirstErrorTracking (0.00s)
=== RUN TestServerFileLoggerClose_ValidFiles
--- PASS: TestServerFileLoggerClose_ValidFiles (0.00s)
=== RUN TestServerFileLoggerClose_CloseTwice
--- PASS: TestServerFileLoggerClose_CloseTwice (0.00s)
=== RUN TestServerFileLoggerLog_NilReceiver
--- PASS: TestServerFileLoggerLog_NilReceiver (0.00s)
=== RUN TestServerFileLoggerLog_SyncError
--- PASS: TestServerFileLoggerLog_SyncError (0.00s)
=== RUN TestServerFileLoggerGetOrCreate_FileCreationError
--- PASS: TestServerFileLoggerGetOrCreate_FileCreationError (0.00s)
PASS
ok github.com/github/gh-aw-mcpg/internal/logger 0.006s
````
All existing tests continue to pass.
---
*Generated by Test Coverage Improver*
*Next run should target `writeToFile` (63.6%) or `logEntry` (66.7%)*
> Generated by [Test Coverage
Improver](https://github.com/github/gh-aw-mcpg/actions/runs/23978202716/agentic_workflow)
·
[◷](https://github.com/search?q=repo%3Agithub%2Fgh-aw-mcpg+%22gh-aw-workflow-id%3A+test-coverage-improver%22&type=pullrequests)
<!-- gh-aw-agentic-workflow: Test Coverage Improver, engine: copilot,
model: auto, id: 23978202716, workflow_id: test-coverage-improver, run:
https://github.com/github/gh-aw-mcpg/actions/runs/23978202716 -->
<!-- gh-aw-workflow-id: test-coverage-improver -->1 file changed
+201
-0
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
| 4 | + | |
4 | 5 | | |
5 | 6 | | |
6 | 7 | | |
| |||
263 | 264 | | |
264 | 265 | | |
265 | 266 | | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
0 commit comments