Skip to content

Commit

Permalink
Fix up whitespace + tests (fixes #11)
Browse files Browse the repository at this point in the history
  • Loading branch information
webpro committed Nov 29, 2020
1 parent bcc80e3 commit 1254944
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 32 deletions.
8 changes: 4 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class KeepAChangelog extends Plugin {
if (updatedChangelog.includes(latestVersionLink)) {
return updatedChangelog.replace(latestVersionLink, `${releaseLink}${this.EOL}${latestVersionLink}`);
} else {
return `${updatedChangelog}${this.EOL}${releaseLink}${this.EOL}`;
return `${updatedChangelog}${this.EOL}${releaseLink}`;
}
}

Expand All @@ -97,15 +97,15 @@ class KeepAChangelog extends Plugin {
if (isDryRun || keepUnreleased) return;
const { version } = this.getContext();
const formattedDate = getFormattedDate();
const unreleasedTitle = addUnreleased ? this.unreleasedTitle : '';
const releaseTitle = `${unreleasedTitle}${this.EOL}${this.EOL}## [${version}] - ${formattedDate}`;
const unreleasedTitle = addUnreleased ? this.unreleasedTitle + this.EOL + this.EOL : '';
const releaseTitle = `${unreleasedTitle}## [${version}] - ${formattedDate}`;
let changelog = this.changelogContent.replace(this.unreleasedTitle, releaseTitle);

if (addVersionUrl) {
changelog = this.addVersionUrls(changelog);
}

fs.writeFileSync(this.changelogPath, changelog + this.EOL);
fs.writeFileSync(this.changelogPath, changelog.trim() + this.EOL);
}
}

Expand Down
55 changes: 27 additions & 28 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,25 @@ const initialDryRunFileContents =
'\n\n## [Unreleased]\n\n* Item A\n* Item B\n\n## [1.0.0] - 2020-05-02\n\n* Item C\n* Item D';

mock({
'./CHANGELOG-FOO.md': '\n\n## [FOO]\n\n* Item A\n* Item B',
'./CHANGELOG-UNRELEASED.md': '\n\n## [Unreleased]\n\n* Item A\n* Item B',
'./CHANGELOG-EMPTY.md': '\n\n## [Unreleased]\n\n\n\n## [1.0.0]\n\n* Item A\n* Item B',
'./CHANGELOG-FULL.md': '\n\n## [Unreleased]\n\n* Item A\n* Item B\n\n## [1.0.0] - 2020-05-02\n\n* Item C\n* Item D',
'./CHANGELOG-FOO.md': '## [FOO]\n\n* Item A\n* Item B',
'./CHANGELOG-MISSING.md': '## [Unreleased]\n\n* Item A\n* Item B',
'./CHANGELOG-EMPTY.md': '## [Unreleased]\n\n\n\n## [1.0.0]\n\n* Item A\n* Item B',
'./CHANGELOG-FULL.md':
'# Changelog\n\n## [Unreleased]\n\n* Item A\n* Item B\n\n## [1.0.0] - 2020-05-02\n\n* Item C\n* Item D',
'./CHANGELOG-NO-STRICT.md': '## [Unreleased]\n\n* Item A\n* Item B\n\n## [1.0.0] - 2020-05-02\n\n* Item C\n* Item D',
'./CHANGELOG-DRYRUN.md': initialDryRunFileContents,
'./CHANGELOG-LESS_NEW_LINES.md':
'\n\n## [Unreleased]\n* Item A\n* Item B\n## [1.0.0] - 2020-05-02\n* Item C\n* Item D',
'./CHANGELOG-LESS_NEW_LINES.md': '## [Unreleased]\n* Item A\n* Item B\n## [1.0.0] - 2020-05-02\n* Item C\n* Item D',
'./CHANGELOG-EOL.md':
'\r\n\r\n## [Unreleased]\r\n\r\n* Item A\r\n* Item B\r\n\r\n## [1.0.0] - 2020-05-02\r\n\r\n* Item C\r\n* Item D',
'./CHANGELOG-UNRELEASED.md': '## [Unreleased]\n\n* Item A\n* Item B\n\n## [1.0.0] - 2020-05-02\n\n* Item C\n* Item D',
'./CHANGELOG-VERSION_URL.md':
'\n\n## [Unreleased]\n\n* Item A\n* Item B\n\n## [1.0.0] - 2020-05-02\n\n* Item C\n* Item D\n\n[Unreleased]: https://github.com/release-it/release-it/compare/1.0.0..HEAD\n[1.0.0]: https://github.com/release-it/release-it/compare/0.0.0...1.0.0',
'## [Unreleased]\n\n* Item A\n* Item B\n\n## [1.0.0] - 2020-05-02\n\n* Item C\n* Item D\n\n[Unreleased]: https://github.com/release-it/release-it/compare/1.0.0..HEAD\n[1.0.0]: https://github.com/release-it/release-it/compare/0.0.0...1.0.0',
'./CHANGELOG-VERSION_URL_UNRELEASED.md':
'\n\n## [Unreleased]\n\n* Item A\n* Item B\n\n## [1.0.0] - 2020-05-02\n\n* Item C\n* Item D\n\n[Unreleased]: https://github.com/user/project/compare/1.0.0..HEAD\n[1.0.0]: https://github.com/user/project/compare/0.0.0...1.0.0',
'## [Unreleased]\n\n* Item A\n* Item B\n\n## [1.0.0] - 2020-05-02\n\n* Item C\n* Item D\n\n[Unreleased]: https://github.com/user/project/compare/1.0.0..HEAD\n[1.0.0]: https://github.com/user/project/compare/0.0.0...1.0.0',
'./CHANGELOG-VERSION_URL_NEW.md': '## [Unreleased]\n\n* Item A\n* Item B'
});

const readFile = file => fs.readFileSync(file).toString().trim();
const readFile = file => fs.readFileSync(file).toString();

const namespace = 'keep-a-changelog';

Expand All @@ -49,12 +51,9 @@ test('should throw for empty "unreleased" section', async t => {
});

test('should throw for missing section for previous release', async t => {
const options = { [namespace]: { filename: 'CHANGELOG-UNRELEASED.md' } };
const options = { [namespace]: { filename: 'CHANGELOG-MISSING.md' } };
const plugin = factory(Plugin, { namespace, options });
await assert.rejects(
runTasks(plugin),
/Missing section for previous release \("1\.0\.0"\) in CHANGELOG-UNRELEASED\.md/
);
await assert.rejects(runTasks(plugin), /Missing section for previous release \("1\.0\.0"\) in CHANGELOG-MISSING\.md/);
});

test('should find very first changelog with disabled strict latest option', async t => {
Expand All @@ -71,17 +70,17 @@ test('should write changelog', async t => {
assert.equal(plugin.getChangelog(), '* Item A\n* Item B');
assert.match(
readFile('./CHANGELOG-FULL.md'),
/## \[1\.0\.1] - [0-9]{4}-[0-9]{2}-[0-9]{2}\n\n\* Item A\n\* Item B\n\n## \[1\.0\.0] - 2020-05-02\n\n\* Item C\n*\* Item D/
/^# Changelog\n\n## \[1\.0\.1] - [0-9]{4}-[0-9]{2}-[0-9]{2}\n\n\* Item A\n\* Item B\n\n## \[1\.0\.0] - 2020-05-02\n\n\* Item C\n*\* Item D\n$/
);
});

test('should write changelog even with `strictLatest: true`', async t => {
const options = { [namespace]: { filename: 'CHANGELOG-FULL.md', strictLatest: false } };
test('should write changelog even with `strictLatest: false`', async t => {
const options = { [namespace]: { filename: 'CHANGELOG-NO-STRICT.md', strictLatest: false } };
const plugin = factory(Plugin, { namespace, options });
await runTasks(plugin);
assert.equal(plugin.getChangelog(), '* Item A\n* Item B');
assert.match(
readFile('./CHANGELOG-FULL.md'),
readFile('./CHANGELOG-NO-STRICT.md'),
/## \[1\.0\.1] - [0-9]{4}-[0-9]{2}-[0-9]{2}\n\n\* Item A\n\* Item B\n\n## \[1\.0\.0] - 2020-05-02\n\n\* Item C\n*\* Item D/
);
});
Expand All @@ -90,14 +89,14 @@ test('should not write changelog in dry run', async t => {
const options = { 'dry-run': true, [namespace]: { filename: 'CHANGELOG-DRYRUN.md' } };
const plugin = factory(Plugin, { namespace, options });
await runTasks(plugin);
assert.equal(fs.readFileSync('./CHANGELOG-DRYRUN.md').toString(), initialDryRunFileContents);
assert.equal(readFile('./CHANGELOG-DRYRUN.md'), initialDryRunFileContents);
});

test('should not write changelog with keep unreleased option', async t => {
const options = { [namespace]: { filename: 'CHANGELOG-DRYRUN.md', keepUnreleased: true } };
const plugin = factory(Plugin, { namespace, options });
await runTasks(plugin);
assert.equal(fs.readFileSync('./CHANGELOG-DRYRUN.md').toString(), initialDryRunFileContents);
assert.equal(readFile('./CHANGELOG-DRYRUN.md'), initialDryRunFileContents);
});

test('should find changelog even if less new lines is used', async t => {
Expand All @@ -107,29 +106,29 @@ test('should find changelog even if less new lines is used', async t => {
assert.equal(plugin.getChangelog(), '* Item A\n* Item B');
});

test('should write changelog (with different EOL)', async t => {
test('should write changelog with different EOL', async t => {
const options = { [namespace]: { filename: 'CHANGELOG-EOL.md' } };
const plugin = factory(Plugin, { namespace, options });
await runTasks(plugin);
assert.equal(plugin.getChangelog(), '* Item A\r\n* Item B');
assert.match(
readFile('./CHANGELOG-EOL.md'),
/^## \[1\.0\.1] - [0-9]{4}-[0-9]{2}-[0-9]{2}\r\n\r\n\* Item A\r\n\* Item B\r\n\r\n## \[1\.0\.0] - 2020-05-02\r\n\r\n\* Item C\r\n*\* Item D/
/^## \[1\.0\.1] - [0-9]{4}-[0-9]{2}-[0-9]{2}\r\n\r\n\* Item A\r\n\* Item B\r\n\r\n## \[1\.0\.0] - 2020-05-02\r\n\r\n\* Item C\r\n*\* Item D\r\n/
);
});

test('should write changelog and add unreleased section', async t => {
const options = { [namespace]: { filename: 'CHANGELOG-FULL.md', addUnreleased: true } };
const options = { [namespace]: { filename: 'CHANGELOG-UNRELEASED.md', addUnreleased: true } };
const plugin = factory(Plugin, { namespace, options });
await runTasks(plugin);
assert.equal(plugin.getChangelog(), '* Item A\n* Item B');
assert.match(
readFile('./CHANGELOG-FULL.md'),
readFile('./CHANGELOG-UNRELEASED.md'),
/^## \[Unreleased]\n\n## \[1\.0\.1] - [0-9]{4}-[0-9]{2}-[0-9]{2}\n\n\* Item A\n\* Item B\n\n## \[1\.0\.0] - 2020-05-02\n\n\* Item C\n*\* Item D/
);
});

test('should add unreleased section and links to the end of the file', async t => {
test('should add links to the end of the file', async t => {
const options = { [namespace]: { filename: 'CHANGELOG-VERSION_URL.md', addVersionUrl: true } };
const plugin = factory(Plugin, { namespace, options });
plugin.config.setContext({
Expand All @@ -143,7 +142,7 @@ test('should add unreleased section and links to the end of the file', async t =
assert.equal(plugin.getChangelog(), '* Item A\n* Item B');
assert.match(
readFile('./CHANGELOG-VERSION_URL.md'),
/^## \[1\.0\.1] - [0-9]{4}-[0-9]{2}-[0-9]{2}\n\n\* Item A\n\* Item B\n\n## \[1\.0\.0] - 2020-05-02\n\n\* Item C\n*\* Item D\n\n\[Unreleased]: https:\/\/github\.com\/release-it\/release-it\/compare\/1\.0\.1\.\.\.HEAD\n\[1\.0\.1]: https:\/\/github\.com\/release-it\/release-it\/compare\/1\.0\.0\.\.\.1\.0\.1\n\[1\.0\.0]: https:\/\/github\.com\/release-it\/release-it\/compare\/0\.0\.0\.\.\.1\.0\.0/
/^## \[1\.0\.1] - [0-9]{4}-[0-9]{2}-[0-9]{2}\n\n\* Item A\n\* Item B\n\n## \[1\.0\.0] - 2020-05-02\n\n\* Item C\n*\* Item D\n\n\[Unreleased]: https:\/\/github\.com\/release-it\/release-it\/compare\/1\.0\.1\.\.\.HEAD\n\[1\.0\.1]: https:\/\/github\.com\/release-it\/release-it\/compare\/1\.0\.0\.\.\.1\.0\.1\n\[1\.0\.0]: https:\/\/github\.com\/release-it\/release-it\/compare\/0\.0\.0\.\.\.1\.0\.0\n$/
);
});

Expand All @@ -163,7 +162,7 @@ test('should add unreleased section and links to the end of the file', async t =
assert.equal(plugin.getChangelog(), '* Item A\n* Item B');
assert.match(
readFile('./CHANGELOG-VERSION_URL_UNRELEASED.md'),
/^## \[Unreleased]\n\n## \[1\.0\.1] - [0-9]{4}-[0-9]{2}-[0-9]{2}\n\n\* Item A\n\* Item B\n\n## \[1\.0\.0] - 2020-05-02\n\n\* Item C\n*\* Item D\n\n\[Unreleased]: https:\/\/github\.com\/user\/project\/compare\/1\.0\.1\.\.\.HEAD\n\[1\.0\.1]: https:\/\/github\.com\/user\/project\/compare\/1\.0\.0\.\.\.1\.0\.1\n\[1\.0\.0]: https:\/\/github\.com\/user\/project\/compare\/0\.0\.0\.\.\.1\.0\.0/
/^## \[Unreleased]\n\n## \[1\.0\.1] - [0-9]{4}-[0-9]{2}-[0-9]{2}\n\n\* Item A\n\* Item B\n\n## \[1\.0\.0] - 2020-05-02\n\n\* Item C\n*\* Item D\n\n\[Unreleased]: https:\/\/github\.com\/user\/project\/compare\/1\.0\.1\.\.\.HEAD\n\[1\.0\.1]: https:\/\/github\.com\/user\/project\/compare\/1\.0\.0\.\.\.1\.0\.1\n\[1\.0\.0]: https:\/\/github\.com\/user\/project\/compare\/0\.0\.0\.\.\.1\.0\.0\n/
);
});

Expand All @@ -184,6 +183,6 @@ test('should add link to the end of a new changelog', async t => {
assert.equal(plugin.getChangelog(), '* Item A\n* Item B');
assert.match(
readFile('./CHANGELOG-VERSION_URL_NEW.md'),
/^## \[1\.0\.0] - [0-9]{4}-[0-9]{2}-[0-9]{2}\n\n\* Item A\n\* Item B\n\n\[Unreleased]: https:\/\/github\.com\/user\/project\/compare\/1\.0\.0\.\.\.HEAD\n\[1.0.0]: https:\/\/github\.com\/user\/project\/compare\/0\.0\.0\.\.\.1\.0\.0/
/^## \[1\.0\.0] - [0-9]{4}-[0-9]{2}-[0-9]{2}\n\n\* Item A\n\* Item B\n\n\[Unreleased]: https:\/\/github\.com\/user\/project\/compare\/1\.0\.0\.\.\.HEAD\n\[1.0.0]: https:\/\/github\.com\/user\/project\/compare\/0\.0\.0\.\.\.1\.0\.0\n$/
);
});

0 comments on commit 1254944

Please sign in to comment.