Commit 4e877f2
authored
[test-improver] Improve tests for tracing package (#7740)
# Test Improvements: `provider_internal_test.go`
## File Analyzed
- **Test File**: `internal/tracing/provider_internal_test.go`
- **Package**: `internal/tracing`
- **Lines of Code**: 52 → 117 (+65 lines)
## Improvements Made
### 1. Increased Coverage
- ✅ Added `TestGenerateRandomSpanID`: verifies the happy path — produces
a valid non-zero 8-byte span ID and that successive calls yield distinct
values
- ✅ Added `TestGenerateRandomSpanID_Error`: injects a failing
`crypto/rand.Reader` to cover the previously dead error-return path in
`generateRandomSpanID`
- ✅ Added `TestParentContext_RandomSpanIDFailure`: triggers the `genErr
!= nil` branch in `ParentContext` (lines 322–325 of `provider.go`) by
breaking the random source, verifying the original context is returned
unchanged
| Function | Before | After |
|---|---|---|
| `generateRandomSpanID` | 83.3% | 100% |
| `ParentContext` | 92.6% | 100% |
| **Package total** | **95.2%** | **96.2%** |
### 2. Better Testing Patterns
- ✅ Error-path injection via `errorReader` struct (same pattern as
`auth/header_test.go`) to test unreachable crypto-failure branches
- ✅ Tests that must not run in parallel (global `rand.Reader` mutation)
are explicitly left non-parallel with a comment explaining why
- ✅ `require.NoError` / `require.Error` for blocking assertions,
`assert.*` for non-blocking checks — consistent with project conventions
- ✅ `t.Parallel()` on the pure happy-path test that has no global side
effects
## Test Execution
All tests pass:
```
ok github.com/github/gh-aw-mcpg/internal/tracing 0.023s coverage: 96.2% of statements
```
## Why These Changes?
`provider_internal_test.go` was a thin 52-line file that only tested
`mergeOTLPHeaders`. The two functions with coverage gaps —
`generateRandomSpanID` (83.3%) and `ParentContext` (92.6%) — had
unreachable error paths that require `crypto/rand` failure injection.
Since the test file is in package `tracing` (not `tracing_test`), it has
direct access to the unexported `generateRandomSpanID` and can inject
the `rand.Reader` failure needed to exercise both error branches. The
improvements follow the exact same `errorReader` / `rand.Reader` swap
pattern already established in `internal/auth/header_test.go`.
---
*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:
>
> - `index.crates.io`
>> To allow these domains, add them to the `network.allowed` list in
your workflow frontmatter:
>
> ```yaml
> network:
> allowed:
> - defaults
> - "index.crates.io"
> ```
>
> 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/27797269536)
· 795.5 AIC · ⊞ 29.5K ·
[◷](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.60, model: claude-sonnet-4.6, id: 27797269536, workflow_id:
test-improver, run:
https://github.com/github/gh-aw-mcpg/actions/runs/27797269536 -->
<!-- gh-aw-workflow-id: test-improver -->
<!-- gh-aw-workflow-call-id: github/gh-aw-mcpg/test-improver -->1 file changed
Lines changed: 65 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
4 | 7 | | |
5 | 8 | | |
6 | 9 | | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
7 | 14 | | |
8 | 15 | | |
9 | 16 | | |
| |||
50 | 57 | | |
51 | 58 | | |
52 | 59 | | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
0 commit comments