Skip to content

[test-improver] Improve tests for version.BuildVersionString#8386

Draft
github-actions[bot] wants to merge 1 commit into
mainfrom
test-improver/version-build-string-coverage-a9e2b0bb6d792dd5
Draft

[test-improver] Improve tests for version.BuildVersionString#8386
github-actions[bot] wants to merge 1 commit into
mainfrom
test-improver/version-build-string-coverage-a9e2b0bb6d792dd5

Conversation

@github-actions

@github-actions github-actions Bot commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

File analyzed

internal/version/version_test.go (and production internal/version/version.go)

Problem

BuildVersionString had two uncovered branches — the inner bodies executed when debug.ReadBuildInfo() returns a non-empty vcs.revision (lines 92–94) or vcs.time (lines 100–102). These are the "auto-detect from binary build info" fallback paths that run when the caller passes empty gitCommit or buildDate.

BuildVersionString  89.5%  (package total 95.1%)

Changes

internal/version/version.go

Extracted debug.ReadBuildInfo into a package-level variable:

var readBuildInfo = debug.ReadBuildInfo

BuildVersionString now calls readBuildInfo() instead of debug.ReadBuildInfo() directly. This is a zero-cost indirection — the variable holds the exact same function pointer at runtime, but allows tests to inject a deterministic replacement without any API changes.

internal/version/version_test.go

Added TestBuildVersionString_FallbackFromBuildInfo with five subtests that override readBuildInfo and restore it via t.Cleanup:

Subtest What it covers
uses truncated vcs.revision when gitCommit is empty line 93 — commit appended from build info
uses vcs.time when buildDate is empty line 101 — date appended from build info
uses both vcs fields when both are empty lines 93 + 101 together
build info unavailable (ok=false) else branch — nothing appended
build info available but no vcs settings inner if not taken — nothing appended

Coverage

Metric Before After
BuildVersionString 89.5% 100%
Package total 95.1% 100%

Test output

ok  github.com/github/gh-aw-mcpg/internal/version   0.003s  coverage: 100.0% of statements

All existing tests continue to pass.

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 · 82.2 AIC · ⊞ 5.6K ·

Add tests for the two branches in BuildVersionString where empty
gitCommit or buildDate fall back to debug.ReadBuildInfo(). These
branches were previously uncovered (lines 92-94, 100-102).

To enable injection without API changes, extract debug.ReadBuildInfo
into a package-level var readBuildInfo. Tests override this var to
supply deterministic mock build info, covering:
- vcs.revision used when gitCommit is empty
- vcs.time used when buildDate is empty
- both fields used when both are empty
- build info unavailable (ok=false)
- build info available but no vcs settings

Coverage: BuildVersionString 89.5% → 100%, package 95.1% → 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