Skip to content

[test] Add tests for logger.ToolsLogger.writeToFile and LogToolsForServer#3427

Draft
github-actions[bot] wants to merge 1 commit intomainfrom
test-coverage/tools-logger-write-to-file-bb271d3ac424373e
Draft

[test] Add tests for logger.ToolsLogger.writeToFile and LogToolsForServer#3427
github-actions[bot] wants to merge 1 commit intomainfrom
test-coverage/tools-logger-write-to-file-bb271d3ac424373e

Conversation

@github-actions
Copy link
Copy Markdown
Contributor

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

Test Coverage Improvement: ToolsLogger.writeToFile & LogToolsForServer

Function Analyzed

  • Package: internal/logger
  • File: internal/logger/tools_logger.go
  • Functions targeted: writeToFile (line 107) and LogToolsForServer (line 141)
  • Previous Coverage: writeToFile 63.6% · LogToolsForServer 66.7%
  • New Coverage: writeToFile 90.9% · LogToolsForServer 100.0%

Why These Functions?

writeToFile performed an atomic write via a temp-file-plus-rename pattern with three distinct error branches — none of which were exercised by the existing test suite. LogToolsForServer had its warning-log path (triggered when LogTools returns an error) completely untested. Together they had the lowest coverage of any non-trivial functions in the testable packages.

Tests Added

Test What it covers
TestWriteToFile_Success Happy-path: temp file written, renamed, original cleaned up, valid JSON on disk
TestWriteToFile_WriteFileFails os.WriteFile fails → "failed to write temp file" error returned
TestWriteToFile_RenameFails os.Rename fails (EISDIR — target is a directory) → "failed to rename temp file" error + temp file removed
TestLogToolsForServer_ErrorIsLogged Global logger pointed at bad path so LogTools errors → log.Printf("WARNING: …") branch hit
TestLogToolsForServer_FallbackSkipsErrors Logger in fallback mode silently discards without reaching error branch

Coverage Report

Before:  writeToFile 63.6% · LogToolsForServer  66.7%
After:   writeToFile 90.9% · LogToolsForServer 100.0%
```

The remaining ~9% of `writeToFile` is the `json.MarshalIndent` error branch (line 113), which cannot be reached with valid `ToolsData` structs at runtime.

### Test Execution

```
=== RUN   TestWriteToFile_Success
--- PASS: TestWriteToFile_Success (0.00s)
=== RUN   TestWriteToFile_WriteFileFails
--- PASS: TestWriteToFile_WriteFileFails (0.00s)
=== RUN   TestWriteToFile_RenameFails
--- PASS: TestWriteToFile_RenameFails (0.00s)
=== RUN   TestLogToolsForServer_ErrorIsLogged
--- PASS: TestLogToolsForServer_ErrorIsLogged (0.00s)
=== RUN   TestLogToolsForServer_FallbackSkipsErrors
--- PASS: TestLogToolsForServer_FallbackSkipsErrors (0.00s)
PASS
ok  github.com/github/gh-aw-mcpg/internal/logger 0.006s

Full logger package suite: ok github.com/github/gh-aw-mcpg/internal/logger


Generated by Test Coverage Improver

Generated by Test Coverage Improver · ● 6.9M ·

- TestWriteToFile_Success: verifies atomic write (temp→rename) produces valid JSON
- TestWriteToFile_WriteFileFails: covers the 'failed to write temp file' error branch
- TestWriteToFile_RenameFails: covers the rename-failure cleanup path (EISDIR on Linux)
- TestLogToolsForServer_ErrorIsLogged: covers the warning log path when LogTools errors
- TestLogToolsForServer_FallbackSkipsErrors: verifies fallback mode silently skips writes

Before: writeToFile 63.6%, LogToolsForServer 66.7%
After:  writeToFile 90.9%, LogToolsForServer 100.0%

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