Skip to content

test: make deploy-preview and detached-HEAD branch coverage deterministic - #401

Merged
nate-double-u merged 1 commit into
mainfrom
quality/test-deterministic-branch-coverage
Nov 12, 2026
Merged

nate-double-u merged 1 commit into
mainfrom
quality/test-deterministic-branch-coverage

Conversation

@hivecommons-hive

Copy link
Copy Markdown
Contributor

Test Improvement

lib/getBuildContext.test.mts and lib/getCurrentBranch.test.mts had tests
for the deploy-preview fallback paths that relied on the ambient git
checkout's real state, so c8 branch coverage for:

  • lib/getBuildContext.ts lines 17-18 (the || main / || repo fallbacks
    in deploy-preview mode)
  • lib/getCurrentBranch.ts line 18 (branch || undefined after an empty
    git branch --show-current, i.e. detached HEAD) and lines 20-21 (the
    execSync catch block)

varied depending on whether the sandbox happened to be on a real branch, and
the execSync-catch branch had no test at all.

This PR makes those branches deterministic instead:

  • runs the deploy-preview fallback test from a fresh non-git tmpdir, which
    forces getCurrentBranch()/getGitHubRepoUrl() to undefined
  • adds a detached-HEAD tmp git repo test for getCurrentBranch()'s
    empty-string -> undefined normalization
  • adds a non-git tmpdir test for getCurrentBranch()'s execSync catch
    block

npm run test:coverage now reports lib/ at 100% statements/branches/
functions (was 97.61% stmts / 87.5% branch before this PR).

Related Issue

Part of #390 (folded tracker for format-check.yml-related findings; this PR
only fixes the getBuildContext/getCurrentBranch branch-coverage finding
folded into it, not the original npm run test:unit CI-wiring gap, which
needs a .github/workflows/ change this lane cannot push)


Filed by quality agent (hold-gated mode). Human review required.

— hive: agent=quality backend=copilot model=claude-opus-4-6 copilot=1.0.88

…stic

getBuildContext.test.mts and getCurrentBranch.test.mts had tests for the
deploy-preview fallback paths that relied on the ambient git checkout's
real state, so c8 branch coverage for the '|| main' / '|| repo' fallbacks
in getBuildContext.ts and the '|| undefined' / execSync-catch branches in
getCurrentBranch.ts varied depending on whether the sandbox happened to be
on a real branch.

Add deterministic coverage instead:
- run the deploy-preview fallback test from a fresh non-git tmpdir, which
  forces getCurrentBranch()/getGitHubRepoUrl() to undefined
- add a detached-HEAD tmp git repo test for getCurrentBranch()'s empty
  string -> undefined normalization
- add a non-git tmpdir test for getCurrentBranch()'s execSync catch block

lib/ is now 100% statements/branches/functions under npm run test:coverage.

Signed-off-by: quality <280983584+hivecommons-hive[bot]@users.noreply.github.com>
@hivecommons-hive

Copy link
Copy Markdown
Contributor Author

Important

Held for human review by the hive's ACMM level gate.

This PR was opened by the "quality" agent while Hive policy required a human checkpoint for that agent. Agent PRs are held at ACMM L3–L5; an outreach PR that targets a repository this hive does not manage is held at every level because it publishes project-facing communication into someone else's project.

Hive will keep the hold label until a human removes it. Operators can make a deliberate one-off release during an ACMM level change with release_level_holds=true, but level changes never release this hold automatically.

@netlify

netlify Bot commented Nov 12, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for cncf-techdocs ready!

Name Link
🔨 Latest commit ca94262
🔍 Latest deploy log https://app.netlify.com/projects/cncf-techdocs/deploys/6af61475b8bf9b07393f17f9
😎 Deploy Preview https://deploy-preview-401--cncf-techdocs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@hivecommons-hive hivecommons-hive Bot left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Conclusion: looks correct to me. Test-only change; I verified each new test against the implementation at head ca94262.

What I checked:

  • lib/getBuildContext.test.mts (new test, lines 94-119): running from a fresh non-git tmpdir deterministically makes execSync('git branch --show-current') throw in lib/getCurrentBranch.ts:14-21 and execSync('git remote get-url origin') throw in lib/getGitHubRepoUrl.ts:25-31, so both return undefined and the || 'main' / || repo fallbacks at lib/getBuildContext.ts:17-18 are exercised exactly as the test asserts. The file-level beforeEach (lines 16-19) already clears HEAD/BRANCH/GITHUB_HEAD_REF/GITHUB_REF_NAME/REPOSITORY_URL, so the env short-circuits in both helpers cannot fire even on CI runners that set GITHUB_REF_NAME.
  • lib/getCurrentBranch.test.mts execSync-failure test: same mechanism; the catch at lib/getCurrentBranch.ts:19-21 is the branch hit, and clearEnv() precedes the call.
  • lib/getCurrentBranch.test.mts detached-HEAD test: git checkout --detach makes git branch --show-current print an empty string, so branch || undefined at lib/getCurrentBranch.ts:18 is the branch hit. The tmp repo sets local user.email/user.name before committing, and execFileSync(..., { cwd: dir }) plus process.chdir in try/finally keep the process state restored.

Two optional, non-blocking notes:

  • The pre-existing loose test "falls back to main/repo arg in deploy-preview mode…" (lines 73-91) is now subsumed by the new deterministic one; it could be dropped in a follow-up, but keeping it is harmless.
  • The mkdtemp dirs are never removed. Fine for throwaway CI/test runs; a t.after(() => rm(dir, …)) would tidy local runs.

The PR body matches the diff (test-only, no workflow change), and Part of #390 correctly leaves the tracker open for the CI-wiring gap this lane cannot push.

— hive: agent=reviewer backend=copilot model=claude-fable-5 copilot=1.0.88

@nate-double-u
nate-double-u merged commit 2484f9a into main Nov 12, 2026
9 checks passed
@nate-double-u
nate-double-u deleted the quality/test-deterministic-branch-coverage branch November 12, 2026 21:08
nate-double-u pushed a commit that referenced this pull request Nov 13, 2026
package.json's test:coverage script (added in #397) reported coverage
but never failed the build on regression: c8's defaults are 0% for
branches/functions/statements and 90% for lines. #396 deliberately
deferred adding thresholds until in-flight coverage PRs merged, to
avoid landing the gate red.

lib/ is now 100% across all four metrics (after #389/#392/#394/#401).
Only scripts/add-frontmatter.mjs remains below 100% branch coverage,
tracked by the still-open #402. Set thresholds at the currently
achieved levels (99/99/97/100) so the gate passes today and catches
any future regression; it can be tightened to --100 once #402 merges.

Signed-off-by: quality <280983584+hivecommons-hive[bot]@users.noreply.github.com>
Co-authored-by: quality <280983584+hivecommons-hive[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant