import assert from 'node:assert/strict'; import { execFileSync, spawnSync } from 'node:child_process'; import { mkdtempSync, rmSync, writeFileSync } from 'node:fs'; import { tmpdir } from 'node:os'; import { join } from 'node:path'; import { fileURLToPath } from 'node:url'; const directory = mkdtempSync(join(tmpdir(), 'ci-policy-')); const script = fileURLToPath(new URL('./ci-policy.mjs', import.meta.url)); const git = (...args) => execFileSync('git', args, { cwd: directory, encoding: 'utf8' }).trim(); const check = (variables, expected = 0) => { const result = spawnSync(process.execPath, [script], { cwd: directory, encoding: 'utf8', env: { PATH: process.env.PATH, CI_PIPELINE_SOURCE: 'push', CI_COMMIT_SHA: git('rev-parse', 'HEAD'), CI_COMMIT_BRANCH: 'feature/test', ...variables, }, }); assert.equal(result.status, expected, result.stdout + result.stderr); }; const mr = { CI_PIPELINE_SOURCE: 'merge_request_event', CI_MERGE_REQUEST_SOURCE_BRANCH_NAME: 'feature/test', CI_MERGE_REQUEST_TARGET_BRANCH_NAME: 'develop', CI_MERGE_REQUEST_TITLE: 'feat(test): add policy checks', }; try { git('init', '--initial-branch=main'); git('config', 'user.name', 'CI policy test'); git('config', 'user.email', 'ci-policy@example.invalid'); git('remote', 'add', 'origin', directory); writeFileSync(join(directory, 'package.json'), '{"version":"1.5.0"}\n'); git('add', 'package.json'); git('commit', '-m', 'Legacy history is allowed'); git('branch', 'develop'); git('switch', '-c', 'feature/test'); git('commit', '--allow-empty', '-m', 'feat(test)!: add checks'); check({}); check(mr); check({ ...mr, CI_MERGE_REQUEST_TITLE: 'Draft: feat: test policy' }); check({ ...mr, CI_MERGE_REQUEST_TITLE: '(Draft) feat: test policy' }); check({ ...mr, CI_MERGE_REQUEST_TARGET_BRANCH_NAME: 'main' }, 1); check({ ...mr, CI_MERGE_REQUEST_TITLE: 'Add checks' }, 1); check({ CI_COMMIT_TAG: 'v1.5.0' }, 1); check({ CI_COMMIT_SHA: '0'.repeat(40) }, 1); git('switch', 'develop'); git('commit', '--allow-empty', '-m', 'fix: another change'); git('switch', 'feature/test'); check(mr, 1); git('rebase', 'develop'); check(mr); const before = git('rev-parse', 'HEAD'); git('commit', '--allow-empty', '-m', 'not conventional'); check({ CI_COMMIT_BEFORE_SHA: before }, 1); check(mr, 1); git('commit', '--amend', '--allow-empty', '-m', 'ci: fix the commit title'); check({ CI_COMMIT_BEFORE_SHA: before }); git('switch', 'develop'); git('merge', '--no-ff', 'feature/test', '-m', 'feat(test): add policy checks'); git('switch', '-c', 'release/1.6.0'); writeFileSync(join(directory, 'package.json'), '{"version":"1.6.0"}\n'); git('add', 'package.json'); git('commit', '-m', 'chore(release): prepare 1.6.0'); check({ ...mr, CI_MERGE_REQUEST_SOURCE_BRANCH_NAME: 'release/1.6.0', CI_MERGE_REQUEST_TARGET_BRANCH_NAME: 'main', }); check({ ...mr, CI_MERGE_REQUEST_SOURCE_BRANCH_NAME: 'release/1.6.0' }); git('switch', 'main'); git('merge', '--no-ff', 'release/1.6.0', '-m', 'chore(release): publish 1.6.0'); check({ CI_COMMIT_TAG: 'v1.6.0' }); check({ CI_COMMIT_TAG: 'v1.5.0' }, 1); check({ CI_COMMIT_TAG: 'v01.6.0' }, 1); check({ CI_COMMIT_TAG: 'v1.6.0-rc.1' }, 1); check({ ...mr, CI_MERGE_REQUEST_SOURCE_BRANCH_NAME: 'main' }); git('switch', '-c', 'hotfix/1.6.1'); git('commit', '--allow-empty', '-m', 'fix: production issue'); check({ ...mr, CI_MERGE_REQUEST_SOURCE_BRANCH_NAME: 'hotfix/1.6.1', CI_MERGE_REQUEST_TARGET_BRANCH_NAME: 'main', }); check({ ...mr, CI_MERGE_REQUEST_SOURCE_BRANCH_NAME: 'hotfix/1.6.1' }); // The working branch must not pass by merging develop instead of rebasing. git('switch', 'feature/test'); git('commit', '--allow-empty', '-m', 'feat: keep working'); git('merge', '--no-ff', 'develop', '-m', 'chore: merge develop'); check(mr, 1); console.log('CI policy integration checks passed.'); } finally { rmSync(directory, { recursive: true, force: true }); }