diff --git a/lib/docusaurusConfigBuildContext.test.mts b/lib/docusaurusConfigBuildContext.test.mts index bb9a201e..4e12a015 100644 --- a/lib/docusaurusConfigBuildContext.test.mts +++ b/lib/docusaurusConfigBuildContext.test.mts @@ -6,15 +6,28 @@ import url from 'node:url'; // docusaurus.config.ts is excluded from the c8 coverage gate (it can't be // imported into a running test process without reloading its top-level -// `getBuildContext(repo)` call for each env combination), so its three -// `isProd`-dependent fields (`url`, and the `editUrl` on both the docs -// preset and the analyses plugin) have no unit coverage at all. Exercise -// them here via a fresh child process per env combination, using the same -// test-only harness pattern as docusaurusConfigPreprocessor.test.mts. +// `getBuildContext(repo)` call for each env combination), so its +// `isProd`/`isDeployPreview`-dependent fields (`url`, the `editUrl` on both +// the docs preset and the analyses plugin, and the rewrite plugin's +// `assetBaseUrl` option) have no unit coverage at all. Exercise them here +// via a fresh child process per env combination, using the same test-only +// harness pattern as docusaurusConfigPreprocessor.test.mts. const __dirname = path.dirname(url.fileURLToPath(import.meta.url)); const repoRoot = path.join(__dirname, '..'); const harness = path.join(__dirname, 'testHelpers', 'printConfigBuildUrls.mjs'); +// getCurrentBranch()/getGitHubRepoUrl() also read these env vars (and, absent +// those, real `git` state from repoRoot), so every case below must pin them +// explicitly to stay deterministic regardless of the branch/CI env this test +// itself runs under. +const BRANCH_AND_REPO_ENV = { + HEAD: '', + BRANCH: '', + GITHUB_HEAD_REF: '', + GITHUB_REF_NAME: '', + REPOSITORY_URL: '', +}; + function readConfigUrls(env: Record) { const result = spawnSync( process.execPath, @@ -22,7 +35,7 @@ function readConfigUrls(env: Record) { { cwd: repoRoot, encoding: 'utf-8', - env: { ...process.env, ...env }, + env: { ...process.env, ...BRANCH_AND_REPO_ENV, ...env }, }, ); @@ -36,18 +49,24 @@ function readConfigUrls(env: Record) { url: string; docsEditUrl?: string; analysesEditUrl?: string; + assetBaseUrl: string; }; } describe('docusaurus.config.ts (isProd-dependent branches)', () => { - it('uses the production site URL and GitHub editUrl links when BUILD_ENV=production', () => { + it('uses the production site URL and GitHub editUrl/asset links when BUILD_ENV=production', () => { const { url: siteUrl, docsEditUrl, analysesEditUrl, + assetBaseUrl, } = readConfigUrls({ BUILD_ENV: 'production', DEPLOY_PRIME_URL: '', + // Pinned (rather than left to fall back to this checkout's real + // `origin` remote) so the test is deterministic on a fork clone, not + // only a checkout whose `origin` is cncf/techdocs. + REPOSITORY_URL: 'https://github.com/cncf/techdocs.git', }); assert.equal(siteUrl, 'https://cncf-techdocs.netlify.app'); @@ -56,16 +75,23 @@ describe('docusaurus.config.ts (isProd-dependent branches)', () => { analysesEditUrl, 'https://github.com/cncf/techdocs/tree/main/analyses', ); + assert.equal(assetBaseUrl, 'https://github.com/cncf/techdocs/blob/main'); }); - it('falls back to DEPLOY_PRIME_URL and omits editUrl for non-production deploy previews', () => { + it('falls back to DEPLOY_PRIME_URL, omits editUrl, and keeps the main-branch asset URL for non-preview, non-production builds', () => { const { url: siteUrl, docsEditUrl, analysesEditUrl, + assetBaseUrl, } = readConfigUrls({ BUILD_ENV: 'deploy-preview', DEPLOY_PRIME_URL: 'https://deploy-preview-1--cncf-techdocs.netlify.app', + // Pinned explicitly (rather than left to fall back to this checkout's + // real branch/remote) so the test is deterministic on any contributor + // checkout, not only a detached-HEAD CI checkout of cncf/techdocs' 'main'. + GITHUB_REF_NAME: 'main', + REPOSITORY_URL: 'https://github.com/cncf/techdocs.git', }); assert.equal( @@ -74,6 +100,7 @@ describe('docusaurus.config.ts (isProd-dependent branches)', () => { ); assert.equal(docsEditUrl, undefined); assert.equal(analysesEditUrl, undefined); + assert.equal(assetBaseUrl, 'https://github.com/cncf/techdocs/blob/main'); }); it('falls back to localhost when neither production nor DEPLOY_PRIME_URL is set', () => { @@ -84,4 +111,36 @@ describe('docusaurus.config.ts (isProd-dependent branches)', () => { assert.equal(siteUrl, 'http://localhost:3000'); }); + + it('wires getCurrentBranch() into the rewrite plugin asset URL for a real deploy preview build', () => { + const { assetBaseUrl } = readConfigUrls({ + BUILD_ENV: 'deploy-preview', + DEPLOY_PRIME_URL: 'https://deploy-preview-42--cncf-techdocs.netlify.app', + GITHUB_REF_NAME: 'my-feature-branch', + // Pinned (see the production/deploy-preview cases above) so this + // assertion of a hardcoded cncf/techdocs URL stays deterministic on a + // fork clone, whose real `origin` remote getGitHubRepoUrl() would + // otherwise fall back to. + REPOSITORY_URL: 'https://github.com/cncf/techdocs.git', + }); + + assert.equal( + assetBaseUrl, + 'https://github.com/cncf/techdocs/blob/my-feature-branch', + ); + }); + + it('wires getGitHubRepoUrl() into the rewrite plugin asset URL for a forked deploy preview build', () => { + const { assetBaseUrl } = readConfigUrls({ + BUILD_ENV: 'deploy-preview', + DEPLOY_PRIME_URL: 'https://deploy-preview-42--cncf-techdocs.netlify.app', + GITHUB_REF_NAME: 'my-feature-branch', + REPOSITORY_URL: 'https://github.com/some-fork/techdocs.git', + }); + + assert.equal( + assetBaseUrl, + 'https://github.com/some-fork/techdocs/blob/my-feature-branch', + ); + }); }); diff --git a/lib/testHelpers/printConfigBuildUrls.mjs b/lib/testHelpers/printConfigBuildUrls.mjs index cff066d1..aacd2973 100644 --- a/lib/testHelpers/printConfigBuildUrls.mjs +++ b/lib/testHelpers/printConfigBuildUrls.mjs @@ -13,5 +13,7 @@ console.log( url: config.url, docsEditUrl: config.presets[0][1].docs.editUrl, analysesEditUrl: config.plugins[0][1].editUrl, + assetBaseUrl: + config.presets[0][1].docs.beforeDefaultRemarkPlugins[0][1].assetBaseUrl, }), );