|
| 1 | +import { test, before, after } from 'node:test' |
| 2 | +import assert from 'node:assert/strict' |
| 3 | +import { execFile } from 'node:child_process' |
| 4 | +import { mkdtemp, readFile, rm } from 'node:fs/promises' |
| 5 | +import { tmpdir, devNull } from 'node:os' |
| 6 | +import { join } from 'node:path' |
| 7 | +import { promisify, stripVTControlCharacters } from 'node:util' |
| 8 | +import { fileURLToPath } from 'node:url' |
| 9 | + |
| 10 | +const execFileP = promisify(execFile) |
| 11 | +const bin = fileURLToPath(new URL('./branch-diff.js', import.meta.url)) |
| 12 | + |
| 13 | +// Isolate git from the user's config (signing, hooks, default branch) so |
| 14 | +// fixtures are identical everywhere. |
| 15 | +const env = { |
| 16 | + ...process.env, |
| 17 | + GIT_CONFIG_GLOBAL: devNull, |
| 18 | + GIT_CONFIG_NOSYSTEM: '1', |
| 19 | + NO_COLOR: '1' |
| 20 | +} |
| 21 | + |
| 22 | +let repo |
| 23 | +let tick = 0 |
| 24 | + |
| 25 | +async function git (...args) { |
| 26 | + const { stdout } = await execFileP('git', args, { cwd: repo, env }) |
| 27 | + return stdout.trim() |
| 28 | +} |
| 29 | + |
| 30 | +// Each commit gets a later date than the last so `--until` (used by |
| 31 | +// `--end-ref`) is deterministic. |
| 32 | +async function commit (summary, { body = [], author = 'Alice <alice@example.com>' } = {}) { |
| 33 | + const date = new Date(Date.UTC(2026, 0, 1) + (++tick) * 60_000).toISOString() |
| 34 | + const [, name, email] = author.match(/^(.+) <(.+)>$/) |
| 35 | + await execFileP('git', ['commit', '--allow-empty', '-q', '-m', summary, ...body.flatMap((b) => ['-m', b])], { |
| 36 | + cwd: repo, |
| 37 | + env: { |
| 38 | + ...env, |
| 39 | + GIT_AUTHOR_NAME: name, |
| 40 | + GIT_AUTHOR_EMAIL: email, |
| 41 | + GIT_COMMITTER_NAME: name, |
| 42 | + GIT_COMMITTER_EMAIL: email, |
| 43 | + GIT_AUTHOR_DATE: date, |
| 44 | + GIT_COMMITTER_DATE: date |
| 45 | + } |
| 46 | + }) |
| 47 | +} |
| 48 | + |
| 49 | +async function branchDiff (...args) { |
| 50 | + const { stdout } = await execFileP(process.execPath, [bin, ...args], { cwd: repo, env }) |
| 51 | + return stripVTControlCharacters(stdout).split('\n').filter(Boolean) |
| 52 | +} |
| 53 | + |
| 54 | +// Summaries from `simple` format lines: `* [sha] - summary (author) [url]`. |
| 55 | +async function summaries (...args) { |
| 56 | + return (await branchDiff(...args)).map((line) => line.match(/^\* \[\w+\] - (.+) \(.+?\)(?: \S+)?$/)[1]) |
| 57 | +} |
| 58 | + |
| 59 | +// PR-URLs avoid the `/<user>/<project>/<type>/<n>` shape so commit-stream |
| 60 | +// assigns no `ghIssue` and label collection makes no GitHub API calls. |
| 61 | +before(async () => { |
| 62 | + repo = await mkdtemp(join(tmpdir(), 'branch-diff-test-')) |
| 63 | + await git('init', '-q', '-b', 'main') |
| 64 | + await commit('build: shared base') |
| 65 | + |
| 66 | + await git('checkout', '-q', '-b', 'release') |
| 67 | + await commit('fs: backport with matching PR-URL', { body: ['PR-URL: https://example.com/pr/1'] }) |
| 68 | + await commit('net: backport matched by author') |
| 69 | + await commit('doc: same summary, different PR-URL', { body: ['PR-URL: https://example.com/pr/3'] }) |
| 70 | + await commit('test: only on release') |
| 71 | + |
| 72 | + await git('checkout', '-q', 'main') |
| 73 | + await commit('fs: backport with matching PR-URL', { body: ['PR-URL: https://example.com/pr/1/'] }) |
| 74 | + await commit('net: backport matched by author') |
| 75 | + await commit('doc: same summary, different PR-URL', { body: ['PR-URL: https://example.com/pr/4'] }) |
| 76 | + await commit('net: backport matched by author', { author: 'Bob <alice@example.com>' }) |
| 77 | + await commit('net: backport matched by author', { author: 'Alice <alice@elsewhere.example>' }) |
| 78 | + await commit('crypto: only on main') |
| 79 | + await commit('2026-01-02, Version 1.0.0 (Current)') |
| 80 | + await commit('Working on v1.0.1') |
| 81 | + await commit('lib: after end ref') |
| 82 | +}) |
| 83 | + |
| 84 | +after(() => rm(repo, { recursive: true, force: true })) |
| 85 | + |
| 86 | +test('lists commits on the comparison branch missing from the base branch', async () => { |
| 87 | + assert.deepEqual(await summaries('release', 'main'), [ |
| 88 | + 'lib: after end ref', |
| 89 | + 'Working on v1.0.1', |
| 90 | + '2026-01-02, Version 1.0.0 (Current)', |
| 91 | + 'crypto: only on main', |
| 92 | + 'net: backport matched by author', // different email |
| 93 | + 'net: backport matched by author', // different name |
| 94 | + 'doc: same summary, different PR-URL' |
| 95 | + ]) |
| 96 | +}) |
| 97 | + |
| 98 | +test('compares in the reverse direction', async () => { |
| 99 | + assert.deepEqual(await summaries('main', 'release'), [ |
| 100 | + 'test: only on release', |
| 101 | + 'doc: same summary, different PR-URL' |
| 102 | + ]) |
| 103 | +}) |
| 104 | + |
| 105 | +test('--end-ref stops at the given ref', async () => { |
| 106 | + const list = await summaries('release', 'main', '--end-ref=main~1') |
| 107 | + assert.equal(list[0], 'Working on v1.0.1') |
| 108 | + assert.ok(!list.includes('lib: after end ref')) |
| 109 | +}) |
| 110 | + |
| 111 | +test('--filter-release drops release commits', async () => { |
| 112 | + const list = await summaries('release', 'main', '--filter-release') |
| 113 | + assert.ok(!list.includes('Working on v1.0.1')) |
| 114 | + assert.ok(!list.includes('2026-01-02, Version 1.0.0 (Current)')) |
| 115 | + assert.equal(list.length, 5) |
| 116 | +}) |
| 117 | + |
| 118 | +test('--format=sha and --reverse', async () => { |
| 119 | + const shas = await branchDiff('release', 'main', '--format=sha') |
| 120 | + assert.equal(shas.length, 7) |
| 121 | + assert.ok(shas.every((s) => /^[0-9a-f]{10}$/.test(s))) |
| 122 | + assert.equal(shas[0], (await git('rev-parse', 'main')).slice(0, 10)) |
| 123 | + assert.deepEqual(await branchDiff('release', 'main', '--format=sha', '--reverse'), shas.toReversed()) |
| 124 | +}) |
| 125 | + |
| 126 | +test('--group orders commits by subsystem', async () => { |
| 127 | + const list = await summaries('release', 'main', '--group', '--filter-release') |
| 128 | + assert.deepEqual(list, [ |
| 129 | + 'crypto: only on main', |
| 130 | + 'doc: same summary, different PR-URL', |
| 131 | + 'lib: after end ref', |
| 132 | + 'net: backport matched by author', |
| 133 | + 'net: backport matched by author' |
| 134 | + ]) |
| 135 | +}) |
| 136 | + |
| 137 | +test('--version prints the package version', async () => { |
| 138 | + const { version } = JSON.parse(await readFile(new URL('./package.json', import.meta.url))) |
| 139 | + assert.deepEqual(await branchDiff('--version'), [`v ${version}`]) |
| 140 | +}) |
| 141 | + |
| 142 | +test('missing branch names is an error', async () => { |
| 143 | + await assert.rejects(branchDiff('main'), (err) => { |
| 144 | + assert.equal(err.code, 1) |
| 145 | + assert.match(err.stderr, /Must supply two branch names to compare/) |
| 146 | + return true |
| 147 | + }) |
| 148 | +}) |
| 149 | + |
| 150 | +test('unknown ref is an error', async () => { |
| 151 | + await assert.rejects(branchDiff('nope', 'main'), (err) => { |
| 152 | + assert.equal(err.code, 1) |
| 153 | + assert.match(err.stderr, /git merge-base nope main" exited with 128/) |
| 154 | + return true |
| 155 | + }) |
| 156 | +}) |
0 commit comments