Repository navigation
test: make deploy-preview and detached-HEAD branch coverage deterministic - #401
Conversation
…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>
|
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 Hive will keep the |
✅ Deploy Preview for cncf-techdocs ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
There was a problem hiding this comment.
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 makesexecSync('git branch --show-current')throw inlib/getCurrentBranch.ts:14-21andexecSync('git remote get-url origin')throw inlib/getGitHubRepoUrl.ts:25-31, so both returnundefinedand the|| 'main'/|| repofallbacks atlib/getBuildContext.ts:17-18are exercised exactly as the test asserts. The file-levelbeforeEach(lines 16-19) already clearsHEAD/BRANCH/GITHUB_HEAD_REF/GITHUB_REF_NAME/REPOSITORY_URL, so the env short-circuits in both helpers cannot fire even on CI runners that setGITHUB_REF_NAME.lib/getCurrentBranch.test.mtsexecSync-failure test: same mechanism; thecatchatlib/getCurrentBranch.ts:19-21is the branch hit, andclearEnv()precedes the call.lib/getCurrentBranch.test.mtsdetached-HEAD test:git checkout --detachmakesgit branch --show-currentprint an empty string, sobranch || undefinedatlib/getCurrentBranch.ts:18is the branch hit. The tmp repo sets localuser.email/user.namebefore committing, andexecFileSync(..., { cwd: dir })plusprocess.chdirin 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
mkdtempdirs are never removed. Fine for throwaway CI/test runs; at.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
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>
Test Improvement
lib/getBuildContext.test.mtsandlib/getCurrentBranch.test.mtshad testsfor the deploy-preview fallback paths that relied on the ambient git
checkout's real state, so c8 branch coverage for:
lib/getBuildContext.tslines 17-18 (the|| main/|| repofallbacksin deploy-preview mode)
lib/getCurrentBranch.tsline 18 (branch || undefinedafter an emptygit branch --show-current, i.e. detached HEAD) and lines 20-21 (theexecSynccatch 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:
forces
getCurrentBranch()/getGitHubRepoUrl()toundefinedgetCurrentBranch()'sempty-string ->
undefinednormalizationgetCurrentBranch()'sexecSynccatchblock
npm run test:coveragenow reportslib/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