From 1aa6dc26a1846f08082760910b2eac3f0c0e58af Mon Sep 17 00:00:00 2001 From: codex-bot-andresmgsl Date: Wed, 2 Sep 2026 09:19:18 +0000 Subject: [PATCH] test: specify changelog section extraction --- test/changelog-section.test.js | 76 ++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 test/changelog-section.test.js diff --git a/test/changelog-section.test.js b/test/changelog-section.test.js new file mode 100644 index 0000000..18b64bb --- /dev/null +++ b/test/changelog-section.test.js @@ -0,0 +1,76 @@ +const { test } = require('node:test'); +const assert = require('node:assert/strict'); +const { spawnSync } = require('node:child_process'); +const fs = require('node:fs'); +const os = require('node:os'); +const path = require('node:path'); + +const ROOT = path.join(__dirname, '..'); +const SCRIPT = path.join(ROOT, 'scripts', 'changelog-section.sh'); + +function extract(version, changelog = path.join(ROOT, 'CHANGELOG.md')) { + return spawnSync('bash', [SCRIPT, version, changelog], { encoding: 'utf8' }); +} + +function withChangelog(contents, assertion) { + const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'stoke-changelog-test-')); + try { + const changelog = path.join(dir, 'CHANGELOG.md'); + fs.writeFileSync(changelog, contents); + assertion(changelog); + } finally { + fs.rmSync(dir, { recursive: true, force: true }); + } +} + +test('extracts the complete real 1.4.0 changelog section', () => { + const result = extract('1.4.0'); + + assert.equal(result.status, 0, result.stderr); + assert.equal(result.stderr, ''); + assert.equal(result.stdout.split('\n').length - 1, 21); + assert.match(result.stdout, /^### Added$/m); + assert.match(result.stdout, /^### Changed$/m); + assert.match(result.stdout, /^### Fixed$/m); +}); + +test('missing version fails with a reason and no stdout', () => { + const result = extract('1.5.0'); + + assert.equal(result.status, 1); + assert.equal(result.stdout, ''); + assert.match(result.stderr, /no section for '1\.5\.0'/); +}); + +test('heading without a list entry is rejected as empty', () => { + withChangelog('## 2.0.0\n\n### Changed\n\nProse only.\n', (changelog) => { + const result = extract('2.0.0', changelog); + + assert.equal(result.status, 1); + assert.equal(result.stdout, ''); + assert.match(result.stderr, /section for '2\.0\.0' has no changelog entries/); + }); +}); + +test('extraction stops before the next version heading', () => { + withChangelog([ + '## 2.0.0', + '', + '### Added', + '', + '- Current change.', + '', + '## 1.0.0', + '', + '### Added', + '', + '- Older change.', + '', + ].join('\n'), (changelog) => { + const result = extract('2.0.0', changelog); + + assert.equal(result.status, 0, result.stderr); + assert.equal(result.stdout, '### Added\n\n- Current change.\n\n'); + assert.doesNotMatch(result.stdout, /Older change/); + }); +});