Repository navigation
test: cover assetBaseUrl deploy-preview wiring in docusaurus.config.ts #419
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
cbc4c5d
9fb675c
cefa95a
51b8f58
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -6,23 +6,36 @@ 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<string, string>) { | ||||||||
| const result = spawnSync( | ||||||||
| process.execPath, | ||||||||
| ['--experimental-strip-types', harness], | ||||||||
| { | ||||||||
| 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<string, string>) { | |||||||
| 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', | ||||||||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This pins the branch but not the repo:
Suggested change
|
||||||||
| 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', | ||||||||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same fork-clone portability issue as the case above: with
Suggested change
|
||||||||
| // 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', | ||||||||
| ); | ||||||||
| }); | ||||||||
| }); | ||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Info-level naming nit: this title says "non-preview", but the case sets
DEPLOY_PRIME_URL, soisDeployPreviewis true (lib/getBuildContext.ts:17) and theblob/mainresult comes from the pinnedGITHUB_REF_NAME: 'main', not from the non-preview'main'literal fallback. The assertion itself is correct; only the description misstates which branch ofgetBuildContextit exercises. Suggested title:'falls back to DEPLOY_PRIME_URL, omits editUrl, and uses the pinned main branch in the asset URL for deploy previews'. (PR is in its busy window, so leaving this as a suggestion rather than pushing.)