76 lines
2.3 KiB
JavaScript
76 lines
2.3 KiB
JavaScript
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('0.0.0');
|
|
|
|
assert.equal(result.status, 1);
|
|
assert.equal(result.stdout, '');
|
|
assert.match(result.stderr, /no section for '0\.0\.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 '2\.0\.0' has no 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');
|
|
assert.doesNotMatch(result.stdout, /Older change/);
|
|
});
|
|
});
|