|
| 1 | +import assert from 'node:assert/strict' |
| 2 | +import { execFileSync } from 'node:child_process' |
| 3 | +import { |
| 4 | + mkdtempSync, |
| 5 | + mkdirSync, |
| 6 | + readFileSync, |
| 7 | + rmSync, |
| 8 | + writeFileSync, |
| 9 | +} from 'node:fs' |
| 10 | +import { tmpdir } from 'node:os' |
| 11 | +import { join } from 'node:path' |
| 12 | +import { test } from 'node:test' |
| 13 | +import { fileURLToPath } from 'node:url' |
| 14 | + |
| 15 | +type Configuration = { |
| 16 | + 'repository': { type: string; url: string } |
| 17 | + 'release-it': { |
| 18 | + plugins: { |
| 19 | + '@release-it/conventional-changelog': { |
| 20 | + preset: { types: { type: string; section: string }[] } |
| 21 | + } |
| 22 | + } |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +const script = fileURLToPath( |
| 27 | + new URL('./generate-release-notes.ts', import.meta.url), |
| 28 | +) |
| 29 | +const configuration = JSON.parse( |
| 30 | + readFileSync(new URL('../package.json', import.meta.url), 'utf8'), |
| 31 | +) as Configuration |
| 32 | + |
| 33 | +test('combines the announcement, configured changelog, and new contributors from the selected release branch', () => { |
| 34 | + const githubNotes = |
| 35 | + "## What's Changed\n* unrelated generated list\n\n## New Contributors\n* @newcomer made their first contribution in https://github.com/margelo/react-native-nitro-sqlite/pull/1\n\n**Full Changelog**: contributor-section-end" |
| 36 | + withFixture(githubNotes, (directory) => { |
| 37 | + const notes = generate(directory) |
| 38 | + assert.match(notes, /^Custom announcement\.\n\n---\n\n/) |
| 39 | + const fixSection = configuration['release-it'].plugins[ |
| 40 | + '@release-it/conventional-changelog' |
| 41 | + ].preset.types.find((entry) => entry.type === 'fix')?.section |
| 42 | + assert.ok(fixSection) |
| 43 | + assert.ok(notes.includes(fixSection)) |
| 44 | + assert.match(notes, /repair runtime connections/) |
| 45 | + assert.match(notes, /v1\.0\.0\.\.\.v1\.0\.1/) |
| 46 | + assert.match( |
| 47 | + notes, |
| 48 | + /## New Contributors\n\* @newcomer made their first contribution in https:\/\/github\.com\/margelo\/react-native-nitro-sqlite\/pull\/1/, |
| 49 | + ) |
| 50 | + assert.doesNotMatch( |
| 51 | + notes, |
| 52 | + /main only feature|initial release fixture|unrelated generated list|contributor-section-end/, |
| 53 | + ) |
| 54 | + }) |
| 55 | +}) |
| 56 | + |
| 57 | +test('regenerates a published release from its previous tag and states when no new contributors are detected', () => { |
| 58 | + withFixture("## What's Changed\n* existing contributor", (directory) => { |
| 59 | + git(directory, 'tag', 'v1.0.1') |
| 60 | + const notes = generate(directory) |
| 61 | + assert.match(notes, /repair runtime connections/) |
| 62 | + assert.match(notes, /v1\.0\.0\.\.\.v1\.0\.1/) |
| 63 | + assert.match( |
| 64 | + notes, |
| 65 | + /## New Contributors\n\nNo new contributors were detected for this release\./, |
| 66 | + ) |
| 67 | + }) |
| 68 | +}) |
| 69 | + |
| 70 | +test('rejects a release increment before generating notes', () => { |
| 71 | + withFixture('', (directory) => { |
| 72 | + assert.throws( |
| 73 | + () => generate(directory, 'patch'), |
| 74 | + /An exact stable release version is required/, |
| 75 | + ) |
| 76 | + }) |
| 77 | +}) |
| 78 | + |
| 79 | +function generate(directory: string, version = '1.0.1'): string { |
| 80 | + const destination = join(directory, 'notes.md') |
| 81 | + execFileSync(process.execPath, [script, version, destination], { |
| 82 | + cwd: directory, |
| 83 | + env: { |
| 84 | + ...process.env, |
| 85 | + PATH: `${join(directory, 'bin')}:${process.env.PATH}`, |
| 86 | + }, |
| 87 | + stdio: 'pipe', |
| 88 | + }) |
| 89 | + return readFileSync(destination, 'utf8') |
| 90 | +} |
| 91 | + |
| 92 | +function withFixture(body: string, run: (directory: string) => void): void { |
| 93 | + const directory = mkdtempSync(join(tmpdir(), 'nitrosqlite-release-notes-')) |
| 94 | + try { |
| 95 | + git(directory, 'init', '-b', 'main') |
| 96 | + git(directory, 'config', 'user.name', 'Christoph Pader') |
| 97 | + git(directory, 'config', 'user.email', 'release-test@example.invalid') |
| 98 | + // Fixture commits stay inside this temporary repository and need no signing key in CI. |
| 99 | + git(directory, 'config', 'commit.gpgsign', 'false') |
| 100 | + writeFileSync( |
| 101 | + join(directory, 'package.json'), |
| 102 | + JSON.stringify({ |
| 103 | + 'name': 'release-notes-fixture', |
| 104 | + 'version': '1.0.1', |
| 105 | + 'repository': configuration.repository, |
| 106 | + 'release-it': configuration['release-it'], |
| 107 | + }), |
| 108 | + ) |
| 109 | + mkdirSync(join(directory, 'docs/releases'), { recursive: true }) |
| 110 | + writeFileSync( |
| 111 | + join(directory, 'docs/releases/v1.0.1.md'), |
| 112 | + 'Custom announcement.\n', |
| 113 | + ) |
| 114 | + git(directory, 'add', '.') |
| 115 | + git(directory, 'commit', '-m', 'chore: initial release fixture') |
| 116 | + git(directory, 'tag', 'v1.0.0') |
| 117 | + git(directory, 'branch', 'release/1.0') |
| 118 | + git(directory, 'commit', '--allow-empty', '-m', 'feat: main only feature') |
| 119 | + git(directory, 'switch', 'release/1.0') |
| 120 | + git( |
| 121 | + directory, |
| 122 | + 'commit', |
| 123 | + '--allow-empty', |
| 124 | + '-m', |
| 125 | + 'fix: repair runtime connections', |
| 126 | + ) |
| 127 | + mkdirSync(join(directory, 'bin')) |
| 128 | + writeFileSync( |
| 129 | + join(directory, 'bin/gh'), |
| 130 | + `#!/usr/bin/env node\nconst args = process.argv.slice(2);\nif (!args.includes('previous_tag_name=v1.0.0') || !args.includes('tag_name=v1.0.1')) throw new Error('Incorrect GitHub release range');\nprocess.stdout.write(${JSON.stringify(JSON.stringify({ body }))});\n`, |
| 131 | + { mode: 0o755 }, |
| 132 | + ) |
| 133 | + run(directory) |
| 134 | + } finally { |
| 135 | + rmSync(directory, { recursive: true, force: true }) |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +function git(directory: string, ...args: string[]): void { |
| 140 | + execFileSync('git', args, { cwd: directory, stdio: 'pipe' }) |
| 141 | +} |
0 commit comments