Skip to content

Commit 4e7cdf8

Browse files
authored
[test-improver] Improve tests for logger package (#5577)
## File Analyzed - **Test Files**: `internal/logger/markdown_logger_test.go`, `internal/logger/server_file_logger_test.go` - **Package**: `internal/logger` ## Improvements Made ### Increased Coverage Eight backward-compatibility wrapper functions had **0% coverage**. These are one-liner delegates that forward calls to the canonical `LogXxxToMarkdown` / `LogXxxToServer` functions. Adding tests for them verifies the delegation chain works correctly and ensures future refactors don't accidentally break the public API. **Functions now covered:** - ✅ `LogInfoMd` → `LogInfoToMarkdown` - ✅ `LogWarnMd` → `LogWarnToMarkdown` - ✅ `LogErrorMd` → `LogErrorToMarkdown` - ✅ `LogDebugMd` → `LogDebugToMarkdown` - ✅ `LogInfoWithServer` → `LogInfoToServer` - ✅ `LogWarnWithServer` → `LogWarnToServer` - ✅ `LogErrorWithServer` → `LogErrorToServer` - ✅ `LogDebugWithServer` → `LogDebugToServer` **Coverage improvement:** - **Previous**: 96.1% - **New**: 97.7% - **Improvement**: +1.6% ### Test Design Each new test: 1. Initialises a real logger in a `t.TempDir()` directory (no side-effects on global state across test runs) 2. Calls all four level wrappers with a formatted message 3. Closes/flushes the logger so the file is written 4. Reads the log file and asserts each expected message is present Tests follow the existing patterns in the file (bound asserters, `require.NoError` for fatal errors, `assert.Contains` for content checks). ## Test Execution ``` === RUN TestLogMdBackwardCompatWrappers --- PASS: TestLogMdBackwardCompatWrappers (0.00s) === RUN TestLogWithServerBackwardCompatWrappers --- PASS: TestLogWithServerBackwardCompatWrappers (0.00s) PASS ok github.com/github/gh-aw-mcpg/internal/logger 0.296s coverage: 97.7% of statements ``` --- *Generated by Test Improver Workflow* *Focuses on better patterns, increased coverage, and more stable tests* > [!WARNING] > <details> > <summary>Firewall blocked 1 domain</summary> > > The following domain was blocked by the firewall during workflow execution: > > - `invalidhostthatdoesnotexist12345.com` >> To allow these domains, add them to the `network.allowed` list in your workflow frontmatter: > > ```yaml > network: > allowed: > - defaults > - "invalidhostthatdoesnotexist12345.com" > ``` > > See [Network Configuration](https://github.github.com/gh-aw/reference/network/) for more information. > > </details> > Generated by [Test Improver](https://github.com/github/gh-aw-mcpg/actions/runs/25769530984/agentic_workflow) · ● 2.2M · [◷](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, version: 1.0.40, model: claude-sonnet-4.6, id: 25769530984, workflow_id: test-improver, run: https://github.com/github/gh-aw-mcpg/actions/runs/25769530984 --> <!-- gh-aw-workflow-id: test-improver -->
2 parents 76d842e + 01b3f00 commit 4e7cdf8

2 files changed

Lines changed: 58 additions & 0 deletions

File tree

internal/logger/markdown_logger_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,3 +377,31 @@ func TestGetEmojiForLevel(t *testing.T) {
377377
})
378378
}
379379
}
380+
381+
// TestLogMdBackwardCompatWrappers verifies the backward-compatibility wrappers
382+
// (LogInfoMd, LogWarnMd, LogErrorMd, LogDebugMd) delegate to their canonical
383+
// counterparts and produce visible output in the markdown log file.
384+
func TestLogMdBackwardCompatWrappers(t *testing.T) {
385+
tmpDir := t.TempDir()
386+
logDir := filepath.Join(tmpDir, "md-logs")
387+
388+
err := InitMarkdownLogger(logDir, "gateway.md")
389+
require.NoError(t, err)
390+
391+
LogInfoMd("test", "md compat info %s", "msg")
392+
LogWarnMd("test", "md compat warn %s", "msg")
393+
LogErrorMd("test", "md compat error %s", "msg")
394+
LogDebugMd("test", "md compat debug %s", "msg")
395+
396+
err = CloseMarkdownLogger()
397+
require.NoError(t, err)
398+
399+
content, err := os.ReadFile(filepath.Join(logDir, "gateway.md"))
400+
require.NoError(t, err)
401+
402+
contentStr := string(content)
403+
assert.Contains(t, contentStr, "md compat info msg", "LogInfoMd should write to markdown log")
404+
assert.Contains(t, contentStr, "md compat warn msg", "LogWarnMd should write to markdown log")
405+
assert.Contains(t, contentStr, "md compat error msg", "LogErrorMd should write to markdown log")
406+
assert.Contains(t, contentStr, "md compat debug msg", "LogDebugMd should write to markdown log")
407+
}

internal/logger/server_file_logger_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,3 +464,33 @@ func TestServerFileLoggerGetOrCreate_FileCreationError(t *testing.T) {
464464
sfl.mu.RUnlock()
465465
assert.False(t, exists, "files map should not contain server1 after creation failure")
466466
}
467+
468+
// TestLogWithServerBackwardCompatWrappers verifies the backward-compatibility wrappers
469+
// (LogInfoWithServer, LogWarnWithServer, LogErrorWithServer, LogDebugWithServer) delegate
470+
// to their canonical counterparts and produce visible output in the log file.
471+
func TestLogWithServerBackwardCompatWrappers(t *testing.T) {
472+
tmpDir := t.TempDir()
473+
logDir := filepath.Join(tmpDir, "server-logs")
474+
475+
err := InitServerFileLogger(logDir)
476+
require.NoError(t, err)
477+
478+
serverID := "compat-server"
479+
480+
LogInfoWithServer(serverID, "test", "compat info %s", "msg")
481+
LogWarnWithServer(serverID, "test", "compat warn %s", "msg")
482+
LogErrorWithServer(serverID, "test", "compat error %s", "msg")
483+
LogDebugWithServer(serverID, "test", "compat debug %s", "msg")
484+
485+
err = CloseServerFileLogger()
486+
require.NoError(t, err)
487+
488+
content, err := os.ReadFile(filepath.Join(logDir, serverID+".log"))
489+
require.NoError(t, err)
490+
491+
contentStr := string(content)
492+
assert.Contains(t, contentStr, "compat info msg", "LogInfoWithServer should write to server log")
493+
assert.Contains(t, contentStr, "compat warn msg", "LogWarnWithServer should write to server log")
494+
assert.Contains(t, contentStr, "compat error msg", "LogErrorWithServer should write to server log")
495+
assert.Contains(t, contentStr, "compat debug msg", "LogDebugWithServer should write to server log")
496+
}

0 commit comments

Comments
 (0)