diff --git a/e2e/linkRewrite.test.mjs b/e2e/linkRewrite.test.mjs index 03069482..921bfe3c 100644 --- a/e2e/linkRewrite.test.mjs +++ b/e2e/linkRewrite.test.mjs @@ -258,6 +258,60 @@ describe('deploy-preview build wires branch/repo detection into the rewritten .c }); }); +// 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/, + ); + }); +}); + // `lib/remarkRewriteLinks/index.test.mts` unit-tests the `.mdx`-specific // stripping branches in isolation ("maps (../)+docs/.../foo.mdx to // /docs/.../foo/" and "strips .mdx from site-absolute /docs/.../foo.mdx"),