|
| 1 | +import test from 'node:test'; |
| 2 | +import assert from 'node:assert/strict'; |
| 3 | +import { execFileSync } from 'node:child_process'; |
| 4 | +import { mkdtemp, rm, writeFile } from 'node:fs/promises'; |
| 5 | +import os from 'node:os'; |
| 6 | +import path from 'node:path'; |
| 7 | + |
| 8 | +import { |
| 9 | + buildReleaseWindows, |
| 10 | + buildRepositoryChangelog, |
| 11 | + collectTagWindowCommits, |
| 12 | + listRepositoryTags, |
| 13 | +} from '../scripts/generate-multi-repo-changelog.mjs'; |
| 14 | + |
| 15 | +function git(repoDir, args, env = {}) { |
| 16 | + return execFileSync('git', ['-C', repoDir, ...args], { |
| 17 | + encoding: 'utf8', |
| 18 | + env: { |
| 19 | + ...process.env, |
| 20 | + ...env, |
| 21 | + }, |
| 22 | + }); |
| 23 | +} |
| 24 | + |
| 25 | +async function createTempRepository(t) { |
| 26 | + const repoDir = await mkdtemp(path.join(os.tmpdir(), 'docs-changelog-')); |
| 27 | + t.after(async () => { |
| 28 | + await rm(repoDir, { recursive: true, force: true }); |
| 29 | + }); |
| 30 | + |
| 31 | + git(repoDir, ['init', '-b', 'main']); |
| 32 | + git(repoDir, ['config', 'user.name', 'Docs Test']); |
| 33 | + git(repoDir, ['config', 'user.email', 'docs-test@example.com']); |
| 34 | + |
| 35 | + return repoDir; |
| 36 | +} |
| 37 | + |
| 38 | +async function commitFile(repoDir, filename, content, date, subject, body = '') { |
| 39 | + await writeFile(path.join(repoDir, filename), content, 'utf8'); |
| 40 | + git(repoDir, ['add', filename]); |
| 41 | + |
| 42 | + const args = ['commit', '-m', subject]; |
| 43 | + if (body.length > 0) { |
| 44 | + args.push('-m', body); |
| 45 | + } |
| 46 | + |
| 47 | + git(repoDir, args, { |
| 48 | + GIT_AUTHOR_DATE: date, |
| 49 | + GIT_COMMITTER_DATE: date, |
| 50 | + }); |
| 51 | +} |
| 52 | + |
| 53 | +test('generator builds adjacent tag windows and skips commits before the earliest tag', async (t) => { |
| 54 | + const repoDir = await createTempRepository(t); |
| 55 | + |
| 56 | + await commitFile(repoDir, 'notes.md', '# init\n', '2026-01-01T00:00:00Z', 'chore: init'); |
| 57 | + git(repoDir, ['tag', '0.1.0']); |
| 58 | + |
| 59 | + await commitFile( |
| 60 | + repoDir, |
| 61 | + 'notes.md', |
| 62 | + '# init\n\nsecond\n', |
| 63 | + '2026-01-02T00:00:00Z', |
| 64 | + 'feat: add second change', |
| 65 | + 'detail for second change', |
| 66 | + ); |
| 67 | + await commitFile( |
| 68 | + repoDir, |
| 69 | + 'notes.md', |
| 70 | + '# init\n\nsecond\n\nthird\n', |
| 71 | + '2026-01-03T00:00:00Z', |
| 72 | + 'fix: prepare release two', |
| 73 | + 'detail for release two', |
| 74 | + ); |
| 75 | + git(repoDir, ['tag', 'v0.2.0']); |
| 76 | + |
| 77 | + await commitFile( |
| 78 | + repoDir, |
| 79 | + 'notes.md', |
| 80 | + '# init\n\nsecond\n\nthird\n\nfourth\n', |
| 81 | + '2026-01-04T00:00:00Z', |
| 82 | + 'docs: release three', |
| 83 | + ); |
| 84 | + git(repoDir, ['tag', 'V0.3.0']); |
| 85 | + |
| 86 | + const tags = listRepositoryTags(repoDir); |
| 87 | + assert.deepEqual( |
| 88 | + tags.map((tag) => tag.name), |
| 89 | + ['0.1.0', 'v0.2.0', 'V0.3.0'], |
| 90 | + ); |
| 91 | + |
| 92 | + const windows = buildReleaseWindows(tags); |
| 93 | + assert.deepEqual( |
| 94 | + windows.map((window) => [window.previousTag.name, window.currentTag.name]), |
| 95 | + [ |
| 96 | + ['0.1.0', 'v0.2.0'], |
| 97 | + ['v0.2.0', 'V0.3.0'], |
| 98 | + ], |
| 99 | + ); |
| 100 | + |
| 101 | + const secondReleaseCommits = collectTagWindowCommits(repoDir, '0.1.0', 'v0.2.0'); |
| 102 | + assert.equal(secondReleaseCommits.length, 2); |
| 103 | + assert.deepEqual( |
| 104 | + secondReleaseCommits.map((commit) => commit.title), |
| 105 | + ['feat: add second change', 'fix: prepare release two'], |
| 106 | + ); |
| 107 | + assert.deepEqual( |
| 108 | + secondReleaseCommits.map((commit) => commit.detail), |
| 109 | + ['detail for second change', 'detail for release two'], |
| 110 | + ); |
| 111 | + |
| 112 | + const changelog = buildRepositoryChangelog( |
| 113 | + { |
| 114 | + repoKey: 'fixture', |
| 115 | + repoPath: repoDir, |
| 116 | + outputPath: 'unused.json', |
| 117 | + }, |
| 118 | + { |
| 119 | + monorepoRoot: '/', |
| 120 | + }, |
| 121 | + ); |
| 122 | + |
| 123 | + assert.equal(changelog.releases.length, 2); |
| 124 | + assert.deepEqual( |
| 125 | + changelog.releases.map((release) => release.tag), |
| 126 | + ['V0.3.0', 'v0.2.0'], |
| 127 | + ); |
| 128 | + assert.equal(changelog.releases[0].commitCount, 1); |
| 129 | + assert.equal(changelog.releases[1].commitCount, 2); |
| 130 | + assert.equal(changelog.releases[0].commits[0].title, 'docs: release three'); |
| 131 | + assert.equal(changelog.releases[1].commits[0].title, 'feat: add second change'); |
| 132 | +}); |
| 133 | + |
| 134 | +test('generator returns a valid empty release list for repositories with a single tag', async (t) => { |
| 135 | + const repoDir = await createTempRepository(t); |
| 136 | + |
| 137 | + await commitFile(repoDir, 'README.md', 'single tag repo\n', '2026-02-01T00:00:00Z', 'feat: first release'); |
| 138 | + git(repoDir, ['tag', 'v1.0.0']); |
| 139 | + |
| 140 | + const changelog = buildRepositoryChangelog( |
| 141 | + { |
| 142 | + repoKey: 'single', |
| 143 | + repoPath: repoDir, |
| 144 | + outputPath: 'unused.json', |
| 145 | + }, |
| 146 | + { |
| 147 | + monorepoRoot: '/', |
| 148 | + }, |
| 149 | + ); |
| 150 | + |
| 151 | + assert.equal(changelog.repoKey, 'single'); |
| 152 | + assert.equal(changelog.releases.length, 0); |
| 153 | +}); |
0 commit comments