Skip to content
Merged
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
76 changes: 76 additions & 0 deletions lib/docusaurusConfigBuildContext.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { describe, it } from 'node:test';
import { spawnSync } from 'node:child_process';
import path from 'node:path';
import url from 'node:url';
import getCurrentBranch from './getCurrentBranch.ts';
import getGitHubRepoUrl from './getGitHubRepoUrl.ts';

// 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
Expand Down Expand Up @@ -140,4 +142,78 @@ describe('docusaurus.config.ts (isProd-dependent branches)', () => {
'https://github.com/some-fork/techdocs/blob/my-feature-branch',
);
});

// Every case above pins HEAD/BRANCH/GITHUB_HEAD_REF/GITHUB_REF_NAME and
// REPOSITORY_URL, which the production code (and getCurrentBranch()/
// getGitHubRepoUrl()) treat as equivalent to "unset" because it checks
// truthiness, not key presence. That keeps every case above deterministic
// by skipping the real `git branch --show-current` / `git remote get-url
// origin` fallbacks. Those fallbacks are 100% unit-covered in isolation
// (lib/getCurrentBranch.test.mts, lib/getGitHubRepoUrl.test.mts), but no
// suite asserted they're actually wired into docusaurus.config.ts's
// assetBaseUrl when a deploy-preview build runs with those vars cleared,
// e.g. a bare `netlify build` outside CI. A regression that stopped
// passing their return values through to assetBaseUrl would pass every
// unit test above while still breaking that real build.
it('wires the real git branch/remote into the asset URL for a deploy-preview build with branch/repo env vars cleared', (t) => {
const keys = [
'HEAD',
'BRANCH',
'GITHUB_HEAD_REF',
'GITHUB_REF_NAME',
'REPOSITORY_URL',
] as const;
const saved: Record<string, string | undefined> = {};
for (const key of keys) {
saved[key] = process.env[key];
delete process.env[key];
}

let realBranch: string | undefined;
let realRepo: string | undefined;
const savedCwd = process.cwd();
try {
// readConfigUrls spawns the harness with cwd: repoRoot, so the real
// `git` fallback it exercises reads repoRoot's git state. Match that
// here (rather than this process's own cwd) so both sides agree
// regardless of where the test runner was invoked from.
process.chdir(repoRoot);
// Computed via the same already-unit-tested helpers docusaurus.config.ts
// calls, not reimplemented here: this test proves the *wiring*, not the
// helpers' own git-fallback correctness.
realBranch = getCurrentBranch();
realRepo = getGitHubRepoUrl();
} finally {
process.chdir(savedCwd);
for (const key of keys) {
if (saved[key] === undefined) delete process.env[key];
else process.env[key] = saved[key];
}
}

// If this environment can't produce a real branch/remote (detached HEAD,
// no origin, no .git directory), both getCurrentBranch()/
// getGitHubRepoUrl() and docusaurus.config.ts collapse to the same
// hardcoded defaults, so the assertion below would pass vacuously
// without ever exercising the git fallback. Skip rather than claim a
// pass that proves nothing.
if (!realBranch || !realRepo) {
t.skip(
'no real git branch/remote available in this environment; cannot prove the fallback wiring here',
);
return;
}

const { assetBaseUrl } = readConfigUrls({
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: '',
});
Comment on lines +207 to +215

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.

Verified: the case does pass HEAD/BRANCH/etc. through as empty strings rather than omitting the keys, and this matches the BRANCH_AND_REPO_ENV convention used by every other case in this file (all rely on the helpers' truthiness check, documented in the comment above BRANCH_AND_REPO_ENV). Changing that to true key-deletion would mean reworking readConfigUrls's shared harness used by all 10+ existing cases, which is out of scope for this test-only addition and risks regressing the rest of the file.

Changed in commit 85daf30: renamed the test and reworded the comment above it to say the vars are "cleared" rather than claiming "no env overrides," so the test no longer overstates what it does. Left the empty-string mechanism itself unchanged since it is the file's existing, intentional convention, not something unique to this test.

Full suite (77 tests), npm run test:coverage (100% lines/branches/functions/statements), npx prettier --check, and npx cspell all still pass on the updated file.


assert.equal(assetBaseUrl, `${realRepo}/blob/${realBranch}`);
});
});