Skip to content

[test] Add tests for logger.SlogHandler.Handle and related functions#3381

Draft
github-actions[bot] wants to merge 1 commit intomainfrom
test-coverage/slog-adapter-handle-52cc30f7365d875a
Draft

[test] Add tests for logger.SlogHandler.Handle and related functions#3381
github-actions[bot] wants to merge 1 commit intomainfrom
test-coverage/slog-adapter-handle-52cc30f7365d875a

Conversation

@github-actions
Copy link
Copy Markdown
Contributor

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

Test Coverage Improvement: slog_adapter.go

Function Analyzed

  • Package: internal/logger
  • Functions: SlogHandler.Handle, formatSlogValue, NewSlogLoggerWithHandler
  • Previous Coverage: 0% (all three functions)
  • New Coverage: Handle → 95.2%, formatSlogValue → 100%, NewSlogLoggerWithHandler → 100%
  • Complexity: Medium (Handle has multiple branches: 4 log levels, attribute handling, disabled-logger early return)

Why These Functions?

These three functions in internal/logger/slog_adapter.go had 0% coverage because all existing tests guarded execution with:

if os.Getenv("DEBUG") == "" {
    t.Skip("Skipping test: DEBUG environment variable not set")
}
```

In CI environments where `DEBUG` is not set, the tests were always skipped — leaving `Handle`, `formatSlogValue`, and `NewSlogLoggerWithHandler` completely untested. These are important integration points since `SlogHandler` bridges the gh-aw debug logger with Go's standard `slog.Logger` interface.

### Tests Added

- ✅ `TestSlogHandler_Handle_WithDebugEnabled` — All 4 log level prefixes (`[DEBUG]`, `[INFO]`, `[WARN]`, `[ERROR]`) and unknown level fallback (no prefix)
-`TestSlogHandler_Handle_WhenDisabled`Early return path when logger is not enabled
-`TestSlogHandler_Handle_WithAttributes`String, integer, boolean, float attribute types; multiple attributes; empty message with attributes; no attributes
-`TestFormatSlogValue`Both `slog.Value` and non-`slog.Value` inputs (string, int, bool, float, nil)
-`TestNewSlogLoggerWithHandler_Enabled`Creates working logger, verifies output with namespace and attributes
-`TestNewSlogLoggerWithHandler_Disabled`Verifies no output when logger is disabled
-`TestNewSlogLoggerWithHandler_MultipleMessages`Verifies multiple messages through same logger
-`TestSlogHandler_Handle_AllLevelPrefixes`Focused table-driven test for all 4 standard slog levels
-`TestSlogHandler_Handle_NonStringKeyFallback`Exercises normal attribute key path

### Key Approach

Tests use `t.Setenv("DEBUG", "*")` to **force-enable** the logger rather than skipping. The logger's `enabled` field is computed at construction time via `computeEnabled()`, which reads the `DEBUG` env varso calling `t.Setenv` before `New()` correctly enables the logger and exercises all code paths.

### Coverage Report

```
Before:
  Handle                  0.0%
  formatSlogValue         0.0%
  NewSlogLoggerWithHandler 0.0%

After:
  Handle                  95.2%  (+95.2%)
  formatSlogValue        100.0%  (+100%)
  NewSlogLoggerWithHandler 100.0%  (+100%)
  
  logger package total:  90.7% → 96.0%  (+5.3%)
```

### Test Execution

```
=== RUN   TestSlogHandler_Handle_WithDebugEnabled
--- PASS: TestSlogHandler_Handle_WithDebugEnabled (0.00s)
=== RUN   TestSlogHandler_Handle_WhenDisabled
--- PASS: TestSlogHandler_Handle_WhenDisabled (0.00s)
=== RUN   TestSlogHandler_Handle_WithAttributes
--- PASS: TestSlogHandler_Handle_WithAttributes (0.00s)
=== RUN   TestFormatSlogValue
--- PASS: TestFormatSlogValue (0.00s)
=== RUN   TestNewSlogLoggerWithHandler_Enabled
--- PASS: TestNewSlogLoggerWithHandler_Enabled (0.00s)
=== RUN   TestNewSlogLoggerWithHandler_Disabled
--- PASS: TestNewSlogLoggerWithHandler_Disabled (0.00s)
=== RUN   TestSlogHandler_Handle_AllLevelPrefixes
--- PASS: TestSlogHandler_Handle_AllLevelPrefixes (0.00s)
ok  	github.com/github/gh-aw-mcpg/internal/logger	0.225s

Generated by Test Coverage Improver
Next run will target the next most complex under-tested function

Generated by Test Coverage Improver · ● 6M ·

- Add TestSlogHandler_Handle_WithDebugEnabled: covers all 4 log level
  prefixes (DEBUG/INFO/WARN/ERROR) and unknown level fallback
- Add TestSlogHandler_Handle_WhenDisabled: verifies early return when
  logger is not enabled
- Add TestSlogHandler_Handle_WithAttributes: covers all attribute types
  (string, int, bool, float) and multiple/empty attribute cases
- Add TestFormatSlogValue: covers both slog.Value and non-slog.Value
  inputs including nil, integer, boolean, and string
- Add TestNewSlogLoggerWithHandler_Enabled/Disabled/MultipleMessages:
  covers NewSlogLoggerWithHandler with enabled and disabled loggers
- Add TestSlogHandler_Handle_AllLevelPrefixes: targeted table-driven
  test for all 4 standard slog level prefix cases

Tests use t.Setenv("DEBUG", "*") instead of t.Skip() so they run
in all environments, bringing Handle from 0% → 95.2%, formatSlogValue
from 0% → 100%, and NewSlogLoggerWithHandler from 0% → 100%.

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