Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 26 additions & 16 deletions .github/workflows/test-and-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,51 @@ on:
push:
branches: [main]

permissions:
contents: read

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: ${{ github.event_name == 'pull_request' }}

jobs:
lint:
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
- name: Use Node.js
uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0
with:
node-version: lts/*
- name: Install Dependencies
run: npm install --no-progress
- name: Lint
run: npm run lint
test:
strategy:
fail-fast: false
matrix:
node: [18.x, 20.x, lts/*, current]
os: [macos-latest, ubuntu-latest, windows-latest]
node: [lts/*, current]
os: [ubuntu-latest, macos-latest]
runs-on: ${{ matrix.os }}
steps:
- name: Checkout Repository
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
fetch-depth: 0
- name: Use Node.js ${{ matrix.node }}
uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0
with:
node-version: ${{ matrix.node }}
- name: Install Dependencies
run: |
npm install --no-progress
run: npm install --no-progress
- name: Run tests
run: |
npm config set script-shell bash
npm run test:ci
run: npm run unit
release:
name: Release
permissions:
contents: write
id-token: write
needs: test
needs: [lint, test]
runs-on: ubuntu-latest
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
steps:
Expand All @@ -49,14 +64,9 @@ jobs:
node-version: lts/*
registry-url: 'https://registry.npmjs.org'
- name: Install dependencies
run: |
npm install --no-progress --no-package-lock --no-save
- name: Build
run: |
npm run build
run: npm install --no-progress --no-package-lock --no-save
- name: Release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_CONFIG_PROVENANCE: true
run: npx semantic-release

17 changes: 6 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@
"scripts": {
"lint": "standard",
"format": "standard --fix",
"build": "true",
"test:ci": "npm run test",
"test": "npm run lint"
"unit": "node --test test.js",
"test": "npm run lint && npm run unit"
},
"author": "Rod <rod@vagg.org> (http://r.va.gg/)",
"license": "MIT",
"dependencies": {
"changelog-maker": "^5.1.3",
"commit-stream": "^2.0.1",
"commit-stream": "^3.0.4",
"pkg-to-id": "^0.0.3",
"split2": "^4.2.0"
},
Expand All @@ -29,14 +28,10 @@
"preferGlobal": true,
"devDependencies": {
"@semantic-release/changelog": "^7.0.0",
"@semantic-release/commit-analyzer": "^13.0.1",
"@semantic-release/git": "^11.0.0",
"@semantic-release/github": "^12.0.6",
"@semantic-release/npm": "^13.1.5",
"@semantic-release/release-notes-generator": "^14.1.0",
"@semantic-release/git": "^11.0.1",
"conventional-changelog-conventionalcommits": "^9.3.1",
"semantic-release": "^25.0.3",
"standard": "^17.0.0"
"semantic-release": "^25.0.9",
"standard": "^17.1.2"
},
"release": {
"branches": [
Expand Down
156 changes: 156 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
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('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
})
})
Loading