Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 67 additions & 8 deletions lib/docusaurusConfigBuildContext.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
},
);

Expand All @@ -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');
Expand All @@ -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', () => {

Copy link
Copy Markdown
Contributor Author

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, so isDeployPreview is true (lib/getBuildContext.ts:17) and the blob/main result comes from the pinned GITHUB_REF_NAME: 'main', not from the non-preview 'main' literal fallback. The assertion itself is correct; only the description misstates which branch of getBuildContext it 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.)

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',

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This pins the branch but not the repo: BRANCH_AND_REPO_ENV empties REPOSITORY_URL, so getGitHubRepoUrl() falls back to git remote get-url origin (lib/getGitHubRepoUrl.ts:26-30). On a fork clone that returns the fork URL and the cncf/techdocs assertion below fails — confirmed by re-running this file with origin set to a fork URL (this test and the getCurrentBranch one fail, 3 pass).

Suggested change
GITHUB_REF_NAME: 'main',
GITHUB_REF_NAME: 'main',
REPOSITORY_URL: 'https://github.com/cncf/techdocs.git',

REPOSITORY_URL: 'https://github.com/cncf/techdocs.git',
});

assert.equal(
Expand All @@ -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', () => {
Expand All @@ -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',

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same fork-clone portability issue as the case above: with REPOSITORY_URL emptied, getGitHubRepoUrl() reads the local origin remote, so this assertion fails on a contributor checkout cloned from a fork.

Suggested change
GITHUB_REF_NAME: 'my-feature-branch',
GITHUB_REF_NAME: 'my-feature-branch',
REPOSITORY_URL: 'https://github.com/cncf/techdocs.git',

// 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',
);
});
});
2 changes: 2 additions & 0 deletions lib/testHelpers/printConfigBuildUrls.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}),
);
Loading