Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix isIncrement to return only the version requested #28

Merged
merged 1 commit into from
Aug 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 28 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,37 +53,48 @@ class KeepAChangelog extends Plugin {
}

getChangelog(latestVersion) {
const { isIncrement } = this.config;
// Return the unchanged changelog content when no increment is made
if (!isIncrement) {
return this.changelogContent;
}

const { changelog } = this.getContext();
if (changelog) return changelog;

const { filename, strictLatest } = this;

const previousReleaseTitle = strictLatest ? `## [${latestVersion}]` : `## [`;
const hasPreviousReleaseSection = this.changelogContent.includes(previousReleaseTitle);

if (strictLatest && !hasPreviousReleaseSection) {
throw Error(`Missing section for previous release ("${latestVersion}") in ${filename}.`);
}

const startIndex = this.changelogContent.indexOf(this.unreleasedTitle) + this.unreleasedTitle.length;
let endIndex = this.changelogContent.indexOf(previousReleaseTitle, startIndex);
if (!strictLatest && endIndex === -1) {
endIndex = this.changelogContent.length;
const { isIncrement } = this.config;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is where the new code starts. Everything between if (changelog) return changelog; and const { isIncrement } = this.config; is just to satisfy throwing an error if strictLatest is true and the changelog has never had a release before.

const titleToFind = isIncrement ? this.unreleasedTitleRaw : latestVersion;
const changelogContent = this.getChangelogEntryContent(titleToFind);

this.setContext({ changelog: changelogContent });
return changelogContent;
}

getChangelogEntryContent(releaseTitleRaw) {
const { filename, changelogContent, EOL } = this;

const releaseTitleMarkdown = `## [${releaseTitleRaw}]`;
const previousReleaseTitle = `## [`;

const indexOfReleaseTitle = changelogContent.indexOf(releaseTitleMarkdown);

if (indexOfReleaseTitle === -1) {
throw Error(`Missing section for previous release ("${releaseTitleRaw}") in ${filename}.`);
}

const entryContentStartIndex = changelogContent.indexOf(EOL, indexOfReleaseTitle);
let entryContentEndIndex = changelogContent.indexOf(previousReleaseTitle, entryContentStartIndex);
if (entryContentEndIndex === -1) {
entryContentEndIndex = changelogContent.length;
}

const changelogContent = this.changelogContent.substring(startIndex, endIndex).trim();
if (!changelogContent) {
throw Error(`There are no entries under "${this.unreleasedTitleRaw}" section in ${filename}.`);
const changelogEntryContent = changelogContent.substring(entryContentStartIndex, entryContentEndIndex).trim();
if (!changelogEntryContent) {
throw Error(`There are no entries under "${releaseTitleRaw}" section in ${filename}.`);
}

this.setContext({ changelog: changelogContent });
return changelogContent;
return changelogEntryContent;
}

bump(version) {
Expand Down
29 changes: 24 additions & 5 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import fs from 'fs';
import test from 'bron';
import { strict as assert } from 'assert';
import test from 'bron';
import fs from 'fs';
import mock from 'mock-fs';
import { factory, runTasks } from 'release-it/test/util/index.js';
import sinon from 'sinon';
import { factory, runTasks } from 'release-it/test/util';
import Plugin from './index.js';

const initialDryRunFileContents =
Expand Down Expand Up @@ -56,11 +56,30 @@ test('should throw for empty "unreleased" section', async t => {
await assert.rejects(runTasks(plugin), /There are no entries under "Unreleased" section in CHANGELOG-EMPTY\.md/);
});

test('should not throw for empty "unreleased" section when no-increment flag is set', async t => {
test('should throw for missing "unreleased" section when no-increment flag is set if changelog is misformatted', async t => {
const options = { increment: false, [namespace]: { filename: 'CHANGELOG-FOO.md' } };
const plugin = factory(Plugin, { namespace, options });
await assert.rejects(runTasks(plugin), /Missing "Unreleased" section in CHANGELOG-FOO.md/);
});

test('should throw for missing "1.0.0" section when no-increment flag is set', async t => {
const options = { increment: false, [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-MISSING\.md/);
});

test('should find "1.0.0" section when no-increment flag is set when items under version', async t => {
const options = { increment: false, [namespace]: { filename: 'CHANGELOG-EMPTY.md' } };
const plugin = factory(Plugin, { namespace, options });
await runTasks(plugin);
assert.equal(plugin.getChangelog(), readFile('./CHANGELOG-EMPTY.md'));
assert.equal(plugin.getChangelog('1.0.0'), '* Item A\n* Item B');
});

test('should find "1.0.0" section when no-increment flag is set when items under unreleased and version', async t => {
const options = { increment: false, [namespace]: { filename: 'CHANGELOG-FULL.md' } };
const plugin = factory(Plugin, { namespace, options });
await runTasks(plugin);
assert.equal(plugin.getChangelog('1.0.0'), '* Item C\n* Item D');
});

test('should throw for missing section for previous release', async t => {
Expand Down