Skip to content

Commit 7da9845

Browse files
committed
test: add CLI tests, trim CI matrix, bump deps
1 parent c9fec10 commit 7da9845

3 files changed

Lines changed: 188 additions & 27 deletions

File tree

‎.github/workflows/test-and-release.yml‎

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,51 @@ on:
66
push:
77
branches: [main]
88

9+
permissions:
10+
contents: read
11+
12+
concurrency:
13+
group: ${{ github.workflow }}-${{ github.ref }}
14+
cancel-in-progress: ${{ github.event_name == 'pull_request' }}
15+
916
jobs:
17+
lint:
18+
runs-on: ubuntu-latest
19+
steps:
20+
- name: Checkout Repository
21+
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
22+
- name: Use Node.js
23+
uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0
24+
with:
25+
node-version: lts/*
26+
- name: Install Dependencies
27+
run: npm install --no-progress
28+
- name: Lint
29+
run: npm run lint
1030
test:
1131
strategy:
1232
fail-fast: false
1333
matrix:
14-
node: [18.x, 20.x, lts/*, current]
15-
os: [macos-latest, ubuntu-latest, windows-latest]
34+
node: [lts/*, current]
35+
os: [ubuntu-latest, macos-latest]
1636
runs-on: ${{ matrix.os }}
1737
steps:
1838
- name: Checkout Repository
1939
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
20-
with:
21-
fetch-depth: 0
2240
- name: Use Node.js ${{ matrix.node }}
2341
uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0
2442
with:
2543
node-version: ${{ matrix.node }}
2644
- name: Install Dependencies
27-
run: |
28-
npm install --no-progress
45+
run: npm install --no-progress
2946
- name: Run tests
30-
run: |
31-
npm config set script-shell bash
32-
npm run test:ci
47+
run: npm run unit
3348
release:
3449
name: Release
3550
permissions:
3651
contents: write
3752
id-token: write
38-
needs: test
53+
needs: [lint, test]
3954
runs-on: ubuntu-latest
4055
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
4156
steps:
@@ -49,14 +64,9 @@ jobs:
4964
node-version: lts/*
5065
registry-url: 'https://registry.npmjs.org'
5166
- name: Install dependencies
52-
run: |
53-
npm install --no-progress --no-package-lock --no-save
54-
- name: Build
55-
run: |
56-
npm run build
67+
run: npm install --no-progress --no-package-lock --no-save
5768
- name: Release
5869
env:
5970
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
6071
NPM_CONFIG_PROVENANCE: true
6172
run: npx semantic-release
62-

‎package.json‎

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,14 @@
1010
"scripts": {
1111
"lint": "standard",
1212
"format": "standard --fix",
13-
"build": "true",
14-
"test:ci": "npm run test",
15-
"test": "npm run lint"
13+
"unit": "node --test test.js",
14+
"test": "npm run lint && npm run unit"
1615
},
1716
"author": "Rod <rod@vagg.org> (http://r.va.gg/)",
1817
"license": "MIT",
1918
"dependencies": {
2019
"changelog-maker": "^5.1.3",
21-
"commit-stream": "^2.0.1",
20+
"commit-stream": "^3.0.4",
2221
"pkg-to-id": "^0.0.3",
2322
"split2": "^4.2.0"
2423
},
@@ -29,14 +28,10 @@
2928
"preferGlobal": true,
3029
"devDependencies": {
3130
"@semantic-release/changelog": "^7.0.0",
32-
"@semantic-release/commit-analyzer": "^13.0.1",
33-
"@semantic-release/git": "^11.0.0",
34-
"@semantic-release/github": "^12.0.6",
35-
"@semantic-release/npm": "^13.1.5",
36-
"@semantic-release/release-notes-generator": "^14.1.0",
31+
"@semantic-release/git": "^11.0.1",
3732
"conventional-changelog-conventionalcommits": "^9.3.1",
38-
"semantic-release": "^25.0.3",
39-
"standard": "^17.0.0"
33+
"semantic-release": "^25.0.9",
34+
"standard": "^17.1.2"
4035
},
4136
"release": {
4237
"branches": [

‎test.js‎

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
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

Comments
 (0)