From 7da98451439719debbb6daaff7ec042a1d3cdf0a Mon Sep 17 00:00:00 2001 From: Rod Vagg Date: Sat, 28 Nov 2026 21:09:06 +1100 Subject: [PATCH] test: add CLI tests, trim CI matrix, bump deps --- .github/workflows/test-and-release.yml | 42 ++++--- package.json | 17 +-- test.js | 156 +++++++++++++++++++++++++ 3 files changed, 188 insertions(+), 27 deletions(-) create mode 100644 test.js diff --git a/.github/workflows/test-and-release.yml b/.github/workflows/test-and-release.yml index 022a314..301a9a2 100644 --- a/.github/workflows/test-and-release.yml +++ b/.github/workflows/test-and-release.yml @@ -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: @@ -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 - diff --git a/package.json b/package.json index 103910b..f8c29aa 100644 --- a/package.json +++ b/package.json @@ -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 (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" }, @@ -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": [ diff --git a/test.js b/test.js new file mode 100644 index 0000000..1ce2962 --- /dev/null +++ b/test.js @@ -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 ' } = {}) { + 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 `////` 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 ' }) + await commit('net: backport matched by author', { author: 'Alice ' }) + 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 + }) +})