Skip to content

Commit

Permalink
Add option changelog.maxVersions
Browse files Browse the repository at this point in the history
  • Loading branch information
ecraig12345 committed Oct 5, 2024
1 parent 3068555 commit a1bf36f
Show file tree
Hide file tree
Showing 10 changed files with 351 additions and 58 deletions.
7 changes: 7 additions & 0 deletions change/beachball-d06fb0b6-4977-44f2-9e5f-cc4f2ca0b5f5.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "minor",
"comment": "Add option changelog.maxVersions",
"packageName": "beachball",
"email": "[email protected]",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ exports[`writeChangelog generates basic changelog: changelog md 1`] = `
<!-- Start content -->
## 1.0.0
## 1.1.0
(date)
Expand All @@ -29,13 +29,13 @@ exports[`writeChangelog generates changelogs with dependent changes in monorepo:
<!-- Start content -->
## 1.3.4
## 1.3.5
(date)
### Patches
- Bump baz to v1.3.4
- Bump baz to v1.4.0
"
`;

Expand All @@ -46,7 +46,7 @@ exports[`writeChangelog generates changelogs with dependent changes in monorepo:
<!-- Start content -->
## 1.3.4
## 1.4.0
(date)
Expand All @@ -63,14 +63,14 @@ exports[`writeChangelog generates changelogs with dependent changes in monorepo:
<!-- Start content -->
## 1.0.0
## 1.1.0
(date)
### Minor changes
- foo comment ([email protected])
- Bump bar to v1.3.4
- Bump bar to v1.3.5
"
`;

Expand All @@ -81,7 +81,7 @@ exports[`writeChangelog generates grouped changelog in monorepo: grouped CHANGEL
<!-- Start content -->
## 1.0.0
## 1.1.0
(date)
Expand All @@ -94,3 +94,30 @@ exports[`writeChangelog generates grouped changelog in monorepo: grouped CHANGEL
- baz comment ([email protected])
"
`;

exports[`writeChangelog trims previous changelog entries over maxVersions: CHANGELOG.md 1`] = `
"# Change Log - foo
<!-- This log was last generated on (date) and should not be manually modified. -->
<!-- Start content -->
## 1.3.0
(date)
### Minor changes
- foo comment 3 ([email protected])
## 1.2.0
(date)
### Minor changes
- foo comment 2 ([email protected])
**Changelog has been truncated. Refer to git history for older versions.**
"
`;
72 changes: 56 additions & 16 deletions src/__functional__/changelog/writeChangelog.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { describe, expect, it, beforeAll, afterAll, afterEach } from '@jest/globals';
import fs from 'fs-extra';
import semver from 'semver';
import { generateChangeFiles, getChange, fakeEmail as author } from '../../__fixtures__/changeFiles';
import {
cleanChangelogJson,
Expand All @@ -19,6 +20,7 @@ import { getDefaultOptions } from '../../options/getDefaultOptions';
import type { BumpInfo } from '../../types/BumpInfo';
import { getMaxChangeType } from '../../changefile/changeTypes';
import { getChangePath } from '../../paths';
import { trimmedVersionsNote } from '../../changelog/renderChangelog';

describe('writeChangelog', () => {
let repositoryFactory: RepositoryFactory;
Expand All @@ -30,10 +32,15 @@ describe('writeChangelog', () => {
initMockLogs();

/**
* Read package infos and change files, fill in default options, and call `writeChangelog`.
* Read package infos and change files, fill in default options, bump versions in package info,
* and call `writeChangelog`.
*
* `calculatedChangeTypes` will be generated based on the max change type of each package's change files,
* and assuming every `dependentChangedBy` package has change type `patch`.
*
* The package info versions and package.json versions on disk (but not dependency ranges) will be
* bumped based on the `calculatedChangeTypes`, so that the changelog results are more realistic,
* and tests involving multiple bumps work more realistically.
*/
async function writeChangelogWrapper(
params: Partial<Pick<BumpInfo, 'dependentChangedBy'>> & {
Expand All @@ -54,6 +61,14 @@ describe('writeChangelog', () => {
calculatedChangeTypes[pkgName] = getMaxChangeType('patch', calculatedChangeTypes[pkgName]);
}

// Bump versions in package info and package.json for more realistic changelogs.
// (This is a much more basic variant of the usual bump process.)
for (const [pkgName, changeType] of Object.entries(calculatedChangeTypes)) {
packageInfos[pkgName].version = semver.inc(packageInfos[pkgName].version, changeType as semver.ReleaseType)!;
const { packageJsonPath, ...packageJson } = packageInfos[pkgName];
fs.writeJsonSync(packageJsonPath, packageJson, { spaces: 2 });
}

await writeChangelog({ dependentChangedBy, calculatedChangeTypes, changeFileChangeInfos, packageInfos }, options);
}

Expand Down Expand Up @@ -118,9 +133,9 @@ describe('writeChangelog', () => {
const changelogJson = readChangelogJson(repo.rootPath);
expect(changelogJson).toEqual({ name: 'foo', entries: [expect.anything()] });
expect(cleanChangelogJson(changelogJson)!.entries[0]).toEqual({
version: '1.0.0',
version: '1.1.0',
date: '(date)',
tag: 'foo_v1.0.0',
tag: 'foo_v1.1.0',
comments: {
minor: [
{ comment: 'new minor comment', package: 'foo', author, commit },
Expand Down Expand Up @@ -191,13 +206,13 @@ describe('writeChangelog', () => {
const fooJson = readChangelogJson(repo.pathTo('packages/foo'));
expect(fooJson).toEqual({ name: 'foo', entries: [expect.anything()] });
expect(cleanChangelogJson(fooJson)!.entries[0]).toEqual({
version: '1.0.0',
version: '1.1.0',
date: '(date)',
tag: 'foo_v1.0.0',
tag: 'foo_v1.1.0',
comments: {
minor: [
{ package: 'foo', comment: 'foo comment', author, commit },
{ package: 'foo', comment: 'Bump bar to v1.3.4', author: 'beachball', commit },
{ package: 'foo', comment: 'Bump bar to v1.3.5', author: 'beachball', commit },
],
},
});
Expand All @@ -206,19 +221,19 @@ describe('writeChangelog', () => {
expect(barJson).toEqual({ name: 'bar', entries: [expect.anything()] });
expect(cleanChangelogJson(barJson)!.entries[0]).toEqual({
comments: {
patch: [{ package: 'bar', comment: 'Bump baz to v1.3.4', author: 'beachball', commit }],
patch: [{ package: 'bar', comment: 'Bump baz to v1.4.0', author: 'beachball', commit }],
},
date: '(date)',
tag: 'bar_v1.3.4',
version: '1.3.4',
tag: 'bar_v1.3.5',
version: '1.3.5',
});

const bazJson = readChangelogJson(repo.pathTo('packages/baz'));
expect(bazJson).toEqual({ name: 'baz', entries: [expect.anything()] });
expect(cleanChangelogJson(bazJson)!.entries[0]).toEqual({
version: '1.3.4',
version: '1.4.0',
date: '(date)',
tag: 'baz_v1.3.4',
tag: 'baz_v1.4.0',
comments: {
minor: [{ package: 'baz', comment: 'baz comment', author, commit }],
},
Expand Down Expand Up @@ -251,7 +266,7 @@ describe('writeChangelog', () => {
minor: [
expect.objectContaining({ comment: 'comment 1', package: 'foo' }),
expect.objectContaining({ comment: 'comment 2', package: 'foo' }),
expect.objectContaining({ comment: 'Bump bar to v1.3.4', package: 'foo' }),
expect.objectContaining({ comment: 'Bump bar to v1.4.0', package: 'foo' }),
],
});

Expand Down Expand Up @@ -306,7 +321,7 @@ describe('writeChangelog', () => {
const fooJson = readChangelogJson(repo.pathTo('packages/foo'));
expect(fooJson).toEqual({ name: 'foo', entries: [expect.anything()] });
expect(fooJson!.entries[0].comments.minor).toContainEqual(
expect.objectContaining({ comment: 'Bump bar to v1.3.4' })
expect.objectContaining({ comment: 'Bump bar to v1.3.5' })
);

// Validate grouped changelog: it shouldn't have dependent entries
Expand All @@ -328,8 +343,8 @@ describe('writeChangelog', () => {
],
},
date: '(date)',
tag: 'foo_v1.0.0',
version: '1.0.0',
tag: 'foo_v1.1.0',
version: '1.1.0',
});
});

Expand Down Expand Up @@ -455,7 +470,7 @@ describe('writeChangelog', () => {
await writeChangelogWrapper({ options });

// CHANGELOG.md is written
expect(readChangelogMd(repo.rootPath)).toContain('## 1.0.0');
expect(readChangelogMd(repo.rootPath)).toContain('## 1.1.0');

// CHANGELOG.json is not written
expect(readChangelogJson(repo.rootPath)).toBeNull();
Expand Down Expand Up @@ -510,4 +525,29 @@ describe('writeChangelog', () => {
entries: [expect.anything(), firstChangelogJson!.entries[0]],
});
});

it('trims previous changelog entries over maxVersions', async () => {
repo = sharedSingleRepo;
const options = getOptions({ changelog: { maxVersions: 2 } });

// Bump and write three times
for (let i = 1; i <= 3; i++) {
fs.emptyDirSync(getChangePath(options));
generateChangeFiles([{ packageName: 'foo', comment: `foo comment ${i}` }], options);
await writeChangelogWrapper({ options });
}

// Read the changelog md and verify that it only has the last two versions
const changelogMd = readChangelogMd(repo.rootPath);
expect(changelogMd).toContain('## 1.3.0');
expect(changelogMd).toContain('## 1.2.0');
expect(changelogMd).not.toContain('## 1.1.0');
expect(changelogMd).toContain(trimmedVersionsNote);
// Do a snapshot to make sure there's no funny formatting
expect(changelogMd).toMatchSnapshot('CHANGELOG.md');

// Same with changelog json
const changelogJson = readChangelogJson(repo.rootPath);
expect(changelogJson!.entries).toHaveLength(2);
});
});
67 changes: 62 additions & 5 deletions src/__tests__/changelog/__snapshots__/renderChangelog.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ Thu, 22 Aug 2019 21:20:40 GMT
# Change Log - foo
This log was last generated on Wed, 21 Aug 2019 21:20:40 GMT and should not be manually modified.
<!-- This log was last generated on Wed, 21 Aug 2019 21:20:40 GMT and should not be manually modified. -->
"
`;

Expand All @@ -73,7 +73,11 @@ Thu, 22 Aug 2019 21:20:40 GMT
## 1.2.0
(content here)
(date)
### Patch changes
- content of 1.2.0
"
`;

Expand All @@ -100,7 +104,11 @@ Thu, 22 Aug 2019 21:20:40 GMT
## 1.2.0
(content here)
(date)
### Patch changes
- content of 1.2.0
"
`;

Expand All @@ -127,7 +135,52 @@ Thu, 22 Aug 2019 21:20:40 GMT
## 1.2.0
(content here)
(date)
### Patch changes
- content of 1.2.0
"
`;

exports[`renderChangelog trims previous versions if over maxVersions 1`] = `
"# Change Log - foo
<!-- This log was last generated on Thu, 22 Aug 2019 21:20:40 GMT and should not be manually modified. -->
<!-- Start content -->
## 1.2.3
Thu, 22 Aug 2019 21:20:40 GMT
### Minor changes
- Awesome change ([email protected])
- Boring change ([email protected])
### Patches
- Fix ([email protected])
- stuff ([email protected])
## 1.2.0
(date)
### Patch changes
- content of 1.2.0
## 1.1.9
(date)
### Patch changes
- content of 1.1.9
**Changelog has been truncated. Refer to git history for older versions.**
"
`;

Expand All @@ -144,6 +197,10 @@ no notes for you
## 1.2.0
(content here)
(date)
### Patch changes
- content of 1.2.0
"
`;
Loading

0 comments on commit a1bf36f

Please sign in to comment.