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