Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
add additionalChangelog functionality + tests
  • Loading branch information
SimonFischer04 committed Nov 12, 2023
commit 76cd4b17d7499a693e839e3ac0318f35b11b1d2c
90 changes: 90 additions & 0 deletions packages/plugin-changelog/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ stuff
### Subsection 2
* New entry 4
* New entry 5`,
changelog_testParseAdditionalChangelog1Without: `## **WORK IN PROGRESS** · Doomsday release
* New entry 1
* New entry 2`,
changelog_testParseAdditionalChangelog1With: `## **WORK IN PROGRESS** · Doomsday release
* New entry 1
* New entry 2
* first Changelog entry added via cli-option
* another CLI-Option Changelog entry`,
};

describe("Changelog plugin", () => {
Expand Down Expand Up @@ -266,6 +274,88 @@ ${fixtures.changelog_subsectionsParse2}`,
* New entry 2`);
});

it("parses all changelog entries correctly if additionalChangelog is set", async () => {
const changelogPlugin = new ChangelogPlugin();
const context = createMockContext({
plugins: [changelogPlugin],
cwd: testFSRoot,
argv: {
additionalChangelog: '* first Changelog entry added via cli-option\n* another CLI-Option Changelog entry',
}
});

await testFS.create({
"CHANGELOG.md": `${fixtures.changelog_testParseHeader}
${fixtures.changelog_testParseAdditionalChangelog1Without}

${fixtures.changelog_testParse2}

${fixtures.changelog_testParse3}`,
});

await changelogPlugin.executeStage(context, DefaultStages.check);
expect(context.errors).toHaveLength(0);

const headline = context.getData<string[]>("changelog_before");
expect(headline).toBe(fixtures.changelog_testParseHeader + "\n");

const entries = context.getData<string[]>("changelog_entries");
expect(entries).toEqual([
fixtures.changelog_testParseAdditionalChangelog1With,
fixtures.changelog_testParse2,
fixtures.changelog_testParse3,
]);

const footer = context.getData<string[]>("changelog_after");
expect(footer).toBe("");

const currentChangelog = context.getData<string[]>("changelog_new");
expect(currentChangelog).toBe(`* New entry 1
* New entry 2
* first Changelog entry added via cli-option
* another CLI-Option Changelog entry`);
});

it("even if no WIP section exists before", async () => {
const changelogPlugin = new ChangelogPlugin();
const context = createMockContext({
plugins: [changelogPlugin],
cwd: testFSRoot,
argv: {
additionalChangelog: '* first Changelog entry added via cli-option\n* another CLI-Option Changelog entry',
}
});

await testFS.create({
"CHANGELOG.md": `${fixtures.changelog_testParseHeader}
${fixtures.changelog_testParse2}

${fixtures.changelog_testParse3}`,
});

await changelogPlugin.executeStage(context, DefaultStages.check);
expect(context.errors).toHaveLength(0);

const headline = context.getData<string[]>("changelog_before");
expect(headline).toBe(fixtures.changelog_testParseHeader + "\n");

const entries = context.getData<string[]>("changelog_entries");
expect(entries).toEqual([
`## **WORK IN PROGRESS**
* first Changelog entry added via cli-option
* another CLI-Option Changelog entry`,
fixtures.changelog_testParse2,
fixtures.changelog_testParse3,
]);

const footer = context.getData<string[]>("changelog_after");
expect(footer).toBe("");

const currentChangelog = context.getData<string[]>("changelog_new");
expect(currentChangelog).toBe(`* first Changelog entry added via cli-option
* another CLI-Option Changelog entry`);
});

it("Detects final newlines correctly", async () => {
const changelogPlugin = new ChangelogPlugin();
const context = createMockContext({
Expand Down
15 changes: 15 additions & 0 deletions packages/plugin-changelog/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@ class ChangelogPlugin implements Plugin {
description: `Add an empty placeholder to the changelog after a release.`,
default: false,
},
additionalChangelog: {
alias: ["additional-changelog"],
type: "string",
description: `Add additional changelog entry/entries to the release.`
}
});
}

Expand Down Expand Up @@ -183,7 +188,17 @@ class ChangelogPlugin implements Plugin {
// But we only output the primary one
const changelogPlaceholder = `${changelogPlaceholderPrefix} ${changelogMarkers[0]}`;

if(context.argv.additionalChangelog) {
let currentChangelogsIndex = entries.findIndex((e) => getPlaceholderRegex().test(e));
if (currentChangelogsIndex == -1) {
entries.unshift('## **WORK IN PROGRESS**');
currentChangelogsIndex = 0
}
entries[currentChangelogsIndex] += `\n${context.argv.additionalChangelog}`;
}

const currentChangelogs = entries.filter((e) => getPlaceholderRegex().test(e));

switch (currentChangelogs.length) {
case 0:
context.cli.error(
Expand Down