Skip to content

[test] Add tests for logger.ServerFileLogger.Close#3159

Draft
github-actions[bot] wants to merge 1 commit intomainfrom
test-coverage/server-file-logger-close-17d6cd30b61c6b8d
Draft

[test] Add tests for logger.ServerFileLogger.Close#3159
github-actions[bot] wants to merge 1 commit intomainfrom
test-coverage/server-file-logger-close-17d6cd30b61c6b8d

Conversation

@github-actions
Copy link
Copy Markdown
Contributor

@github-actions github-actions bot commented Apr 4, 2026

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 ·

Increase test coverage for the ServerFileLogger.Close method from
58.8% to 94.1%, while also bringing Log(), getOrCreateLogger(), and
all public server-log helpers to 100% coverage.

Tests added:
- TestServerFileLoggerClose_NilReceiver: nil-receiver safety check
- TestServerFileLoggerClose_EmptyFiles: no-op close with empty maps
- TestServerFileLoggerClose_SyncError: Sync() failure propagation and
  firstErr tracking via a pre-closed file descriptor
- TestServerFileLoggerClose_FirstErrorTracking: first-error-wins
  behaviour across multiple failing files
- TestServerFileLoggerClose_ValidFiles: happy path (all files close OK)
- TestServerFileLoggerClose_CloseTwice: idempotent close safety
- TestServerFileLoggerLog_NilReceiver: nil-receiver safety for Log()
- TestServerFileLoggerLog_SyncError: Sync() failure inside Log()
- TestServerFileLoggerGetOrCreate_FileCreationError: os.OpenFile
  failure in getOrCreateLogger fallback path

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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.

0 participants