Skip to content

[test-improver] Improve tests for logger package#1974

Merged
lpcox merged 3 commits intomainfrom
test-improver/improve-logger-tests-bf4a783d0ffc40d7
Mar 16, 2026
Merged

[test-improver] Improve tests for logger package#1974
lpcox merged 3 commits intomainfrom
test-improver/improve-logger-tests-bf4a783d0ffc40d7

Conversation

@github-actions
Copy link
Contributor

File Analyzed

  • Test File: internal/logger/logger_test.go
  • Package: internal/logger
  • Lines Changed: 92 insertions, 98 deletions (net reduction of 6 lines while adding new tests)

Improvements Made

1. Better Testify Usage

  • ✅ Replaced 29 manual t.Errorf/t.Error/t.Fatal calls with idiomatic testify assertions (assert.Equal, assert.Contains, assert.NotContains, assert.Empty, assert.NotEmpty, assert.True, assert.False, assert.Regexp)
  • ✅ Removed the now-unused "strings" import (was only needed for strings.Contains checks replaced by assert.Contains)
  • ✅ Fixed import ordering: moved "time" into the stdlib block (was mixed with third-party imports)
  • ✅ Replaced if !strings.Contains(output, x) { t.Errorf(...) } patterns with assert.Contains(t, output, x, ...)
  • ✅ Replaced if strings.Contains(logContent, "\033[") { t.Errorf(...) } with assert.NotContains
  • ✅ Used assert.Regexp in TestLogger_TimeDiff for a more precise time-unit check (replaces a fragile two-condition string check)

2. Increased Coverage

  • ✅ Added TestLogger_Printf_WithColors — tests the previously untested color-output branch of Printf (the if l.color != "" branch that writes ANSI escape codes to stderr)
  • ✅ Added TestLogger_Print_WithColors — tests the previously untested color-output branch of Print
  • ✅ Both new tests verify: message content, presence of ANSI color codes (\033[), and presence of the color reset code

3. Cleaner & More Stable Tests

  • ✅ Used t.Cleanup() (instead of defer with manual variable restore) for resetting package-level debugColors and isTTY state in the new color tests — ensures cleanup even if the test panics
  • ✅ Better error messages throughout: assertions now include contextual format strings instead of plain string messages

Why This File?

logger_test.go was the highest-priority target: it imports testify/assert and testify/require but then proceeds to use raw t.Errorf in 29 places — a clear inconsistency that makes the tests harder to read and produces less helpful failure output. The color-output branches of Printf/Print were completely untested despite being real code paths exercised in production.


Generated by Test Improver Workflow
Focuses on better patterns, increased coverage, and more stable tests

Generated by Test Improver ·

- Replace 29 manual t.Errorf/t.Error/t.Fatal calls with testify assertions
  (assert.Equal, assert.Contains, assert.NotContains, assert.Empty,
  assert.NotEmpty, assert.True, assert.False, assert.Regexp)
- Remove unused 'strings' import now that assert.Contains replaces
  strings.Contains checks
- Fix import ordering: move 'time' into the stdlib block
- Add TestLogger_Printf_WithColors and TestLogger_Print_WithColors to cover
  the color-output branch of Printf/Print (previously untested code path)
- Use t.Cleanup for restoring package-level state in color tests instead
  of defer with manual variable restore
- Use assert.Regexp for time-unit check in TestLogger_TimeDiff for a more
  precise and readable assertion

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@lpcox lpcox marked this pull request as ready for review March 16, 2026 01:01
Copilot AI review requested due to automatic review settings March 16, 2026 01:01
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR improves the internal/logger package tests by refactoring assertions to idiomatic testify usage and adding coverage for the ANSI color-output branches in Logger.Printf and Logger.Print.

Changes:

  • Replaced many manual t.Error* checks with testify/assert (and cleaned up imports accordingly).
  • Added new tests to cover the colored stderr output paths for Printf and Print.
  • Made time-diff validation more structured via assert.Regexp.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

lpcox and others added 2 commits March 15, 2026 18:04
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@lpcox lpcox merged commit ed40501 into main Mar 16, 2026
13 checks passed
@lpcox lpcox deleted the test-improver/improve-logger-tests-bf4a783d0ffc40d7 branch March 16, 2026 01:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants