Skip to content

[test-improver] Improve tests for tracing package#7740

Merged
lpcox merged 3 commits into
mainfrom
test-improver/tracing-provider-internal-0adc36df2864ece9
Jun 19, 2026
Merged

[test-improver] Improve tests for tracing package#7740
lpcox merged 3 commits into
mainfrom
test-improver/tracing-provider-internal-0adc36df2864ece9

Conversation

@github-actions

Copy link
Copy Markdown
Contributor

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

Firewall blocked 1 domain

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:

network:
  allowed:
    - defaults
    - "index.crates.io"

See Network Configuration for more information.

Generated by Test Improver · 795.5 AIC · ⊞ 29.5K ·

Add three new tests to provider_internal_test.go (package tracing):

- TestGenerateRandomSpanID: verifies the happy path produces a valid
  non-zero 8-byte span ID and that successive calls yield distinct values.
- TestGenerateRandomSpanID_Error: injects a failing crypto/rand.Reader to
  cover the error return path (previously at 83.3%).
- TestParentContext_RandomSpanIDFailure: triggers the genErr != nil branch
  in ParentContext by breaking the random source, verifying the original
  context is returned unchanged (previously uncovered).

Coverage improvements for internal/tracing:
  generateRandomSpanID: 83.3% → 100%
  ParentContext:        92.6% → 100%
  package total:        95.2% → 96.2%

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@lpcox lpcox marked this pull request as ready for review June 19, 2026 01:28
Copilot AI review requested due to automatic review settings June 19, 2026 01:29

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR expands unit test coverage in internal/tracing by adding targeted tests for previously unexercised error paths in span ID generation and ParentContext, improving confidence in tracing initialization behavior.

Changes:

  • Added a happy-path test for generateRandomSpanID to validate non-zero output.
  • Added an error-path test for generateRandomSpanID by injecting a failing random source.
  • Added an error-path test for ParentContext to confirm it returns the original context when span ID generation fails.
Show a summary per file
File Description
internal/tracing/provider_internal_test.go Adds tests to cover generateRandomSpanID and ParentContext error branches via injected rand.Reader failures.

Copilot's findings

Tip

Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

  • Files reviewed: 1/1 changed files
  • Comments generated: 1

Comment thread internal/tracing/provider_internal_test.go Outdated
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
GitHub Advanced Security started work on behalf of lpcox June 19, 2026 01:40 View session
GitHub Advanced Security finished work on behalf of lpcox June 19, 2026 01:42
@lpcox

lpcox commented Jun 19, 2026

Copy link
Copy Markdown
Collaborator

Copilot AI commented Jun 19, 2026

Copy link
Copy Markdown
Contributor

@copilot fix the failing ci checks https://github.com/github/gh-aw-mcpg/actions/runs/27800176294/job/82268593878?pr=7740

Fixed in the latest commit. The assert.Same call was failing because context.Context is an interface type — testify's assert.Same requires pointer arguments. Replaced it with assert.True(t, ctx == parentCtx, ...) which uses Go's native interface identity comparison (== checks both dynamic type and value pointer).

Copilot finished work on behalf of lpcox June 19, 2026 02:31
Copilot AI requested a review from lpcox June 19, 2026 02:31
@lpcox lpcox merged commit 4e877f2 into main Jun 19, 2026
26 checks passed
@lpcox lpcox deleted the test-improver/tracing-provider-internal-0adc36df2864ece9 branch June 19, 2026 02:53
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.

3 participants