|
| 1 | +import { spawnSync } from "node:child_process" |
| 2 | +import fs from "node:fs" |
| 3 | +import os from "node:os" |
| 4 | +import path from "node:path" |
| 5 | +import { afterEach, describe, expect, it } from "vitest" |
| 6 | + |
| 7 | +const scriptPath = path.resolve("scripts/release-notes.mjs") |
| 8 | + |
| 9 | +const changelog = `# Changelog |
| 10 | +
|
| 11 | +## 0.14.0 - 2026-09-01 |
| 12 | +
|
| 13 | +- Add the newest thing. |
| 14 | +
|
| 15 | +## 0.13.2 - 2026-08-17 |
| 16 | +
|
| 17 | +- Accept a \`key\` on \`schedule\`. |
| 18 | +- Add a process administration query. |
| 19 | +
|
| 20 | +## 0.13.0 - 2026-08-16 |
| 21 | +
|
| 22 | +- Release the oldest thing. |
| 23 | +` |
| 24 | + |
| 25 | +let workspace: string | undefined |
| 26 | + |
| 27 | +afterEach(() => { |
| 28 | + if (workspace !== undefined) fs.rmSync(workspace, { recursive: true, force: true }) |
| 29 | + workspace = undefined |
| 30 | +}) |
| 31 | + |
| 32 | +describe("release notes", () => { |
| 33 | + it("writes the changelog entry for the requested version", () => { |
| 34 | + const notes = releaseNotes({ version: "0.13.2" }) |
| 35 | + |
| 36 | + expect(notes).toContain("- Accept a `key` on `schedule`.") |
| 37 | + expect(notes).toContain("- Add a process administration query.") |
| 38 | + }) |
| 39 | + |
| 40 | + it("leaves the neighbouring entries out", () => { |
| 41 | + const notes = releaseNotes({ version: "0.13.2" }) |
| 42 | + |
| 43 | + expect(notes).not.toContain("Add the newest thing") |
| 44 | + expect(notes).not.toContain("Release the oldest thing") |
| 45 | + expect(notes).not.toContain("## 0.13.0") |
| 46 | + }) |
| 47 | + |
| 48 | + it("reads the oldest entry to the end of the file", () => { |
| 49 | + const notes = releaseNotes({ version: "0.13.0" }) |
| 50 | + |
| 51 | + expect(notes).toContain("- Release the oldest thing.") |
| 52 | + }) |
| 53 | + |
| 54 | + it("names the package and the version to install", () => { |
| 55 | + const notes = releaseNotes({ version: "0.13.2" }) |
| 56 | + |
| 57 | + expect(notes).toContain("npm install solid-objects@0.13.2") |
| 58 | + }) |
| 59 | + |
| 60 | + it("links the changelog entry and the correctness boundaries at the tag", () => { |
| 61 | + const notes = releaseNotes({ version: "0.13.2" }) |
| 62 | + |
| 63 | + expect(notes).toContain( |
| 64 | + "https://github.com/cardmagic/solid-objects-js/blob/v0.13.2/CHANGELOG.md#0132---2026-08-17", |
| 65 | + ) |
| 66 | + expect(notes).toContain( |
| 67 | + "https://github.com/cardmagic/solid-objects-js/blob/v0.13.2/docs/correctness.md", |
| 68 | + ) |
| 69 | + }) |
| 70 | + |
| 71 | + it("fails when the changelog has no entry for the version", () => { |
| 72 | + const result = runScript({ version: "9.9.9" }) |
| 73 | + |
| 74 | + expect(result.status).not.toBe(0) |
| 75 | + expect(result.stderr).toContain("9.9.9") |
| 76 | + }) |
| 77 | + |
| 78 | + it("fails when no version is given", () => { |
| 79 | + const result = runScript({ version: undefined }) |
| 80 | + |
| 81 | + expect(result.status).not.toBe(0) |
| 82 | + }) |
| 83 | +}) |
| 84 | + |
| 85 | +function releaseNotes({ version }: { version: string }) { |
| 86 | + const result = runScript({ version }) |
| 87 | + if (result.status !== 0) throw new Error(`the script failed: ${result.stderr}`) |
| 88 | + |
| 89 | + return result.stdout |
| 90 | +} |
| 91 | + |
| 92 | +function runScript({ version }: { version: string | undefined }) { |
| 93 | + const directory = buildWorkspace() |
| 94 | + |
| 95 | + return spawnSync(process.execPath, version === undefined ? [scriptPath] : [scriptPath, version], { |
| 96 | + cwd: directory, |
| 97 | + encoding: "utf8", |
| 98 | + }) |
| 99 | +} |
| 100 | + |
| 101 | +function buildWorkspace() { |
| 102 | + const directory = fs.mkdtempSync(path.join(os.tmpdir(), "release-notes-")) |
| 103 | + fs.writeFileSync(path.join(directory, "CHANGELOG.md"), changelog) |
| 104 | + fs.writeFileSync( |
| 105 | + path.join(directory, "package.json"), |
| 106 | + JSON.stringify({ |
| 107 | + name: "solid-objects", |
| 108 | + repository: { url: "git+https://github.com/cardmagic/solid-objects-js.git" }, |
| 109 | + }), |
| 110 | + ) |
| 111 | + workspace = directory |
| 112 | + |
| 113 | + return directory |
| 114 | +} |
0 commit comments