Skip to content
Open
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
49 changes: 49 additions & 0 deletions e2e/linkRewrite.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,52 @@ describe('production build applies the link-rewrite plugins end-to-end', () => {
);
});
});

// The production build above always resolves to the `main` branch/repo
// because `BUILD_ENV=dev` + a cleared `DEPLOY_PRIME_URL` keeps
// getBuildContext() out of deploy-preview mode. getCurrentBranch() and
// getGitHubRepoUrl() are 100% unit-covered in isolation
// (lib/getCurrentBranch.test.mts, lib/getGitHubRepoUrl.test.mts), but no
// suite asserted that docusaurus.config.ts's assetBaseUrl actually wires
// their deploy-preview output into the rewritten .csv link during a real
// build. This suite exercises exactly that path with deterministic env
// vars, so a regression in that wiring fails here even when the unit
// tests for each helper still pass in isolation.
describe('deploy-preview build wires branch/repo detection into the rewritten .csv link', () => {
let outDir;

before(async () => {
outDir = await mkdtemp(path.join(tmpdir(), 'techdocs-e2e-preview-'));
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',
// Pin branch/repo detection to deterministic values instead of
// relying on the sandbox's ambient git checkout/remote state,
// matching the env vars getCurrentBranch()/getGitHubRepoUrl()
// already read for Netlify/GitHub Actions deploy-preview builds.
GITHUB_REF_NAME: 'e2e-preview-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.

getCurrentBranch() (lib/getCurrentBranch.ts:5-10) reads HEAD || BRANCH || GITHUB_HEAD_REF || GITHUB_REF_NAME, in that order, and this env block spreads ...process.env. Pinning only GITHUB_REF_NAME therefore loses to any ambient higher-precedence var: in a GitHub Actions PR run GITHUB_HEAD_REF is always set, and on Netlify HEAD/BRANCH are. Reproduced: GITHUB_HEAD_REF=pr-head-branch node --test e2e/linkRewrite.test.mjs fails this suite (branch resolves to pr-head-branch, so both the match and the doesNotMatch(/\/blob\/main\//) expectations break — the latter because the URL no longer contains the pinned branch). The first suite in this file already clears DEPLOY_PRIME_URL for exactly this reason; this block needs the same treatment for the branch vars.

Suggested change
GITHUB_REF_NAME: 'e2e-preview-branch',
// Clear the higher-precedence vars getCurrentBranch() reads first
// (Netlify's HEAD/BRANCH, GitHub Actions' GITHUB_HEAD_REF) so an
// ambient value can't override the pinned branch below.
HEAD: '',
BRANCH: '',
GITHUB_HEAD_REF: '',
GITHUB_REF_NAME: 'e2e-preview-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.

getCurrentBranch() checks HEAD || BRANCH || GITHUB_HEAD_REF || GITHUB_REF_NAME in that order (lib/getCurrentBranch.ts:4-10), and ...process.env inherits whatever the invoking environment sets. GitHub Actions always sets GITHUB_HEAD_REF on PR events and Netlify sets HEAD/BRANCH, so any of those would outrank this pinned GITHUB_REF_NAME and break the branch assertion the first time this suite runs in CI. Clear the higher-priority vars, mirroring how the first suite clears DEPLOY_PRIME_URL at line 32:

Suggested change
GITHUB_REF_NAME: 'e2e-preview-branch',
HEAD: '',
BRANCH: '',
GITHUB_HEAD_REF: '',
GITHUB_REF_NAME: 'e2e-preview-branch',

REPOSITORY_URL: 'https://github.com/example-org/example-repo',
},
stdio: 'inherit',
});
});

after(async () => {
if (outDir) await rm(outDir, { recursive: true, force: true });
});

it('rewrites the .csv asset link to the preview branch and repo, not main', async () => {
const html = await readFile(
path.join(outDir, 'analyses/2023/backstage/index.html'),
'utf-8',
);
assert.match(
html,
/https:\/\/github\.com\/example-org\/example-repo\/blob\/e2e-preview-branch\/analyses\/2023\/backstage\/backstage-doc-survey\.csv/,
);
assert.doesNotMatch(html, /\/blob\/main\//);
});
});
Loading