From 3c4521f67cf6e665a0f507d82ef5eb7119c8b1f6 Mon Sep 17 00:00:00 2001 From: quality <280983584+hivecommons-hive[bot]@users.noreply.github.com> Date: Mon, 23 Nov 2026 18:23:17 -0500 Subject: [PATCH] test: cover getBuildContext repo/main fallback in the real e2e build getBuildContext.ts's getCurrentBranch() || 'main' and getGitHubRepoUrl() || repo fallback branches were 100% unit-covered in isolation but never exercised by a real docusaurus build: every existing deploy-preview e2e case either sets GITHUB_REF_NAME / REPOSITORY_URL directly, or clears them while still running inside this checkout's real git repo, so the git fallback inside getCurrentBranch()/getGitHubRepoUrl() always returned a truthy value and the || fallbacks in getBuildContext.ts never ran. Adds a describe block that runs a deploy-preview build with every git-fallback env var cleared and GIT_DIR/GIT_WORK_TREE pointed at a nonexistent path, so the real git commands fail safely without touching this checkout's actual .git, then asserts the rewritten asset link falls back to the hard-coded repo constant and main branch. Signed-off-by: quality <280983584+hivecommons-hive[bot]@users.noreply.github.com> --- e2e/linkRewrite.test.mjs | 54 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/e2e/linkRewrite.test.mjs b/e2e/linkRewrite.test.mjs index c7bff49c..3f04c02f 100644 --- a/e2e/linkRewrite.test.mjs +++ b/e2e/linkRewrite.test.mjs @@ -257,3 +257,57 @@ describe('deploy-preview build wires branch/repo detection into the rewritten .c assert.doesNotMatch(html, /\/blob\/main\//); }); }); + +// The deploy-preview suite above always has either an env var +// (GITHUB_REF_NAME/REPOSITORY_URL) or a working `git` fallback available, so +// `getCurrentBranch()`/`getGitHubRepoUrl()` always return a truthy value. +// `getBuildContext.ts`'s own `getCurrentBranch() || 'main'` and +// `getGitHubRepoUrl() || repo` fallbacks (lines 15-16) only run when those +// helpers return `undefined`, which is 100% unit-covered in isolation +// (lib/getBuildContext.test.mts) but never exercised by a real build, since +// no suite here invokes `docusaurus build` with every env-var input cleared +// *and* the `git` fallback broken. This suite forces that by pointing +// GIT_DIR/GIT_WORK_TREE at a nonexistent path, so `git branch --show-current` +// and `git remote get-url origin` both fail safely (without touching this +// checkout's real `.git`), making getCurrentBranch()/getGitHubRepoUrl() +// return `undefined` and asserting the build falls back to `repo`/`main`. +describe('deploy-preview build falls back to repo/main when branch and remote detection both fail', () => { + let outDir; + + before(async () => { + outDir = await mkdtemp( + path.join(tmpdir(), 'techdocs-e2e-preview-fallback-'), + ); + execFileSync('npx', ['docusaurus', 'build', '--out-dir', outDir], { + cwd: repoRoot, + env: { + ...process.env, + BUILD_ENV: 'deploy-preview', + DEPLOY_PRIME_URL: 'https://deploy-preview-1--cncf-techdocs.netlify.app', + HEAD: '', + BRANCH: '', + GITHUB_HEAD_REF: '', + GITHUB_REF_NAME: '', + REPOSITORY_URL: '', + GIT_DIR: '/nonexistent-dir-techdocs-e2e', + GIT_WORK_TREE: '/nonexistent-dir-techdocs-e2e', + }, + stdio: 'inherit', + }); + }); + + after(async () => { + if (outDir) await rm(outDir, { recursive: true, force: true }); + }); + + it('rewrites the .csv asset link to the hard-coded repo and main branch', async () => { + const html = await readFile( + path.join(outDir, 'analyses/2023/backstage/index.html'), + 'utf-8', + ); + assert.match( + html, + /https:\/\/github\.com\/cncf\/techdocs\/blob\/main\/analyses\/2023\/backstage\/backstage-doc-survey\.csv/, + ); + }); +});