Commit ab58d67
authored
[test-improver] Improve tests for logger common package (#3114)
# Test Improvements: `internal/logger/common_test.go`
## File Analyzed
- **Test File**: `internal/logger/common_test.go`
- **Package**: `internal/logger`
- **Lines of Code**: ~696 → ~770 (74 lines added)
## Improvements Made
### 1. Better Testify Patterns — Replace `t.Errorf`/`t.Fatal` with
Idiomatic Assertions
**Problem**: Several tests used `t.Errorf("should not be called")`
inside callback closures and `t.Fatal` inside `if err == nil` guards
instead of proper testify patterns.
- ✅ **`TestInitLogFile_InvalidDirectory`** — replaced `if err == nil {
file.Close(); t.Fatal(...) }` with `require.Error(err, ...)` after
closing any file handle
- ✅ **`TestInitLogFile_EmptyFileName`** — same pattern; added `require
:= require.New(t)` so the test uses testify `require.Error` consistently
- ✅ **`TestInitLogger_FileLogger`** — replaced `t.Errorf("Error handler
should not be called")` inside callback with a `errorHandlerCalled`
boolean flag + `assert.False(errorHandlerCalled, ...)`
- ✅ **`TestInitLogger_FileLoggerFallback`** — replaced `t.Errorf` in
setup callback with `setupHandlerCalled` flag + `assert.False`
- ✅ **`TestInitLogger_JSONLLogger`** — same improvement as FileLogger
- ✅ **`TestInitLogger_JSONLLoggerError`** — same improvement as
FileLoggerFallback
- ✅ **`TestInitLogger_MarkdownLogger`** — same improvement as FileLogger
- ✅ **`TestInitLogger_MarkdownLoggerFallback`** — same improvement as
FileLoggerFallback
- ✅ **`TestInitLogger_SetupError`** — replaced `t.Errorf` in
error-handler callback with flag + `assert.False`
The boolean flag pattern makes the intent explicit alongside the other
post-call assertions and avoids mixing raw `t.Errorf` with testify
assertions in the same test.
### 2. Increased Coverage — Direct Tests for `formatLogLine`
**Problem**: The `formatLogLine` function in `common.go` was exercised
only indirectly through `FileLogger.Log()` and `ServerFileLogger`, but
had **no direct unit tests**. The function is responsible for the
canonical log line format used across all file-based loggers.
- ✅ Added `TestFormatLogLine` with subtests covering:
- All four log levels (`INFO`, `WARN`, `ERROR`, `DEBUG`) appear in
brackets
- Category appears in its own bracket
- Format-string interpolation with args (`count=%d name=%s`)
- Full bracket structure regex:
`^\[\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z\] \[INFO\] \[auth\] event
occurred$`
- Timestamp is RFC3339 UTC and falls within the test's time window
- Format string used as-is when no args provided
- Empty category produces `[]` bracket pair
### 3. Cleaner & More Stable Tests
- ✅ Added `"strings"` and `"time"` imports for the new
`TestFormatLogLine` test
- ✅ Consistent testify usage — no more mixing of `t.Errorf` with
`assert`/`require` in the same tests
- ✅ Better resource cleanup — files are closed before `require.Error`
stops the test
## Why These Changes?
`internal/logger/common_test.go` tests the core initialization
primitives (`closeLogFile`, `initLogFile`, `initLogger`) used by all
logger types in the package. The `formatLogLine` function — which
defines the log line format for all operational logs — was entirely
untested directly. The `t.Errorf` callback pattern, while valid Go, was
inconsistent with the testify style used throughout the rest of the
file.
---
*Generated by Test Improver Workflow*
*Focuses on better patterns, increased coverage, and more stable tests*
> Generated by [Test
Improver](https://github.com/github/gh-aw-mcpg/actions/runs/23944702778/agentic_workflow)
·
[◷](https://github.com/search?q=repo%3Agithub%2Fgh-aw-mcpg+%22gh-aw-workflow-id%3A+test-improver%22&type=pullrequests)
<!-- gh-aw-agentic-workflow: Test Improver, engine: copilot, model:
auto, id: 23944702778, workflow_id: test-improver, run:
https://github.com/github/gh-aw-mcpg/actions/runs/23944702778 -->
<!-- gh-aw-workflow-id: test-improver -->1 file changed
Lines changed: 94 additions & 20 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
| 7 | + | |
7 | 8 | | |
8 | 9 | | |
| 10 | + | |
9 | 11 | | |
10 | 12 | | |
11 | 13 | | |
| |||
289 | 291 | | |
290 | 292 | | |
291 | 293 | | |
292 | | - | |
| 294 | + | |
293 | 295 | | |
294 | | - | |
295 | 296 | | |
296 | | - | |
| 297 | + | |
297 | 298 | | |
298 | 299 | | |
299 | 300 | | |
| |||
319 | 320 | | |
320 | 321 | | |
321 | 322 | | |
| 323 | + | |
322 | 324 | | |
323 | 325 | | |
324 | 326 | | |
325 | 327 | | |
326 | 328 | | |
327 | | - | |
| 329 | + | |
328 | 330 | | |
329 | | - | |
330 | 331 | | |
331 | | - | |
| 332 | + | |
332 | 333 | | |
333 | 334 | | |
334 | 335 | | |
| |||
388 | 389 | | |
389 | 390 | | |
390 | 391 | | |
| 392 | + | |
391 | 393 | | |
392 | 394 | | |
393 | 395 | | |
| |||
399 | 401 | | |
400 | 402 | | |
401 | 403 | | |
402 | | - | |
403 | | - | |
| 404 | + | |
404 | 405 | | |
405 | 406 | | |
406 | 407 | | |
407 | 408 | | |
| 409 | + | |
| 410 | + | |
408 | 411 | | |
409 | 412 | | |
410 | 413 | | |
| |||
429 | 432 | | |
430 | 433 | | |
431 | 434 | | |
| 435 | + | |
432 | 436 | | |
433 | 437 | | |
434 | 438 | | |
435 | 439 | | |
436 | | - | |
437 | | - | |
| 440 | + | |
438 | 441 | | |
439 | 442 | | |
440 | 443 | | |
| |||
450 | 453 | | |
451 | 454 | | |
452 | 455 | | |
| 456 | + | |
| 457 | + | |
453 | 458 | | |
454 | 459 | | |
455 | 460 | | |
| |||
465 | 470 | | |
466 | 471 | | |
467 | 472 | | |
| 473 | + | |
468 | 474 | | |
469 | 475 | | |
470 | 476 | | |
| |||
476 | 482 | | |
477 | 483 | | |
478 | 484 | | |
479 | | - | |
480 | | - | |
| 485 | + | |
481 | 486 | | |
482 | 487 | | |
483 | 488 | | |
484 | 489 | | |
| 490 | + | |
| 491 | + | |
485 | 492 | | |
486 | 493 | | |
487 | 494 | | |
| |||
505 | 512 | | |
506 | 513 | | |
507 | 514 | | |
| 515 | + | |
508 | 516 | | |
509 | 517 | | |
510 | 518 | | |
511 | 519 | | |
512 | | - | |
513 | | - | |
| 520 | + | |
514 | 521 | | |
515 | 522 | | |
516 | 523 | | |
| |||
521 | 528 | | |
522 | 529 | | |
523 | 530 | | |
| 531 | + | |
| 532 | + | |
524 | 533 | | |
525 | 534 | | |
526 | 535 | | |
| |||
534 | 543 | | |
535 | 544 | | |
536 | 545 | | |
| 546 | + | |
537 | 547 | | |
538 | 548 | | |
539 | 549 | | |
| |||
546 | 556 | | |
547 | 557 | | |
548 | 558 | | |
549 | | - | |
550 | | - | |
| 559 | + | |
551 | 560 | | |
552 | 561 | | |
553 | 562 | | |
554 | 563 | | |
| 564 | + | |
| 565 | + | |
555 | 566 | | |
556 | 567 | | |
557 | 568 | | |
| |||
577 | 588 | | |
578 | 589 | | |
579 | 590 | | |
| 591 | + | |
580 | 592 | | |
581 | 593 | | |
582 | 594 | | |
583 | 595 | | |
584 | | - | |
585 | | - | |
| 596 | + | |
586 | 597 | | |
587 | 598 | | |
588 | 599 | | |
| |||
598 | 609 | | |
599 | 610 | | |
600 | 611 | | |
| 612 | + | |
| 613 | + | |
601 | 614 | | |
602 | 615 | | |
603 | 616 | | |
| |||
611 | 624 | | |
612 | 625 | | |
613 | 626 | | |
| 627 | + | |
614 | 628 | | |
615 | 629 | | |
616 | 630 | | |
617 | 631 | | |
618 | 632 | | |
619 | 633 | | |
620 | 634 | | |
621 | | - | |
622 | | - | |
| 635 | + | |
623 | 636 | | |
624 | 637 | | |
625 | 638 | | |
626 | 639 | | |
| 640 | + | |
| 641 | + | |
627 | 642 | | |
628 | 643 | | |
629 | 644 | | |
| |||
694 | 709 | | |
695 | 710 | | |
696 | 711 | | |
| 712 | + | |
| 713 | + | |
| 714 | + | |
| 715 | + | |
| 716 | + | |
| 717 | + | |
| 718 | + | |
| 719 | + | |
| 720 | + | |
| 721 | + | |
| 722 | + | |
| 723 | + | |
| 724 | + | |
| 725 | + | |
| 726 | + | |
| 727 | + | |
| 728 | + | |
| 729 | + | |
| 730 | + | |
| 731 | + | |
| 732 | + | |
| 733 | + | |
| 734 | + | |
| 735 | + | |
| 736 | + | |
| 737 | + | |
| 738 | + | |
| 739 | + | |
| 740 | + | |
| 741 | + | |
| 742 | + | |
| 743 | + | |
| 744 | + | |
| 745 | + | |
| 746 | + | |
| 747 | + | |
| 748 | + | |
| 749 | + | |
| 750 | + | |
| 751 | + | |
| 752 | + | |
| 753 | + | |
| 754 | + | |
| 755 | + | |
| 756 | + | |
| 757 | + | |
| 758 | + | |
| 759 | + | |
| 760 | + | |
| 761 | + | |
| 762 | + | |
| 763 | + | |
| 764 | + | |
| 765 | + | |
| 766 | + | |
| 767 | + | |
| 768 | + | |
| 769 | + | |
| 770 | + | |
0 commit comments