Skip to content
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
2 changes: 2 additions & 0 deletions .github/workflows/build-lint-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ jobs:
- run: yarn install --frozen-lockfile
- run: yarn setup:postinstall
- run: yarn build
# below step will fail if there're still changes to commit after "yarn build"
# see if dist/index.js modified after "yarn build".
- run: git diff --quiet || { echo 'working directory dirty after "yarn build"'; exit 1; }
- run: yarn lint
- run: yarn test
Expand Down
22 changes: 20 additions & 2 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64800,11 +64800,29 @@ async function updatePackageChangelog(packageMetadata, updateSpecification, root
repoUrl: repositoryUrl,
formatter: formatChangelog,
});
if (!newChangelogContent) {
if (newChangelogContent) {
return await external_fs_.promises.writeFile(changelogPath, newChangelogContent);
}
const hasUnReleased = hasUnreleasedChanges(changelogContent, repositoryUrl);
if (!hasUnReleased) {
const packageName = packageMetadata.manifest.name;
throw new Error(`"updateChangelog" returned an empty value for package ${packageName ? `"${packageName}"` : `at "${packagePath}"`}.`);
}
return await external_fs_.promises.writeFile(changelogPath, newChangelogContent);
return undefined;
}
/**
* Checks if there are unreleased changes in the changelog.
* @param changelogContent - The string formatted changelog.
* @param repositoryUrl - The repository url.
* @returns The boolean true if there are unreleased changes, otherwise false.
*/
function hasUnreleasedChanges(changelogContent, repositoryUrl) {
const changelog = (0,auto_changelog_dist.parseChangelog)({
changelogContent,
repoUrl: repositoryUrl,
formatter: formatChangelog,
});
return Object.keys(changelog.getUnreleasedChanges()).length !== 0;
}
/**
* Updates the given manifest per the update specification as follows:
Expand Down
103 changes: 101 additions & 2 deletions src/package-operations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ jest.mock('@metamask/action-utils/dist/file-utils', () => {
jest.mock('@metamask/auto-changelog', () => {
return {
updateChangelog: jest.fn(),
parseChangelog: jest.fn(),
};
});

Expand Down Expand Up @@ -260,6 +261,7 @@ describe('package-operations', () => {
const readFileMock = jest.spyOn(fs.promises, 'readFile');

const updateChangelogMock = jest.spyOn(autoChangelog, 'updateChangelog');
const parseChangelogMock = jest.spyOn(autoChangelog, 'parseChangelog');

const getMockPackageMetadata = (
dirPath: string,
Expand Down Expand Up @@ -354,6 +356,7 @@ describe('package-operations', () => {
getMockWritePath(dir, 'CHANGELOG.md'),
mockNewChangelog,
);
expect(parseChangelogMock).toHaveBeenCalledTimes(0);
});

it('re-throws changelog read error', async () => {
Expand Down Expand Up @@ -437,8 +440,19 @@ describe('package-operations', () => {
const changelogContent = 'I am a changelog.';
readFileMock.mockImplementationOnce(async () => changelogContent);

// This will cause an error
// no new changelog content and no unreleased changes will cause an error
updateChangelogMock.mockImplementation(async () => '');
const actualChangelog = jest.requireActual(
'@metamask/auto-changelog/dist/changelog',
);
parseChangelogMock.mockImplementationOnce(() => {
return {
...actualChangelog,
getUnreleasedChanges() {
return {};
},
};
});

const packageMetadata = getMockPackageMetadata(dir, manifest);
const updateSpecification = {
Expand Down Expand Up @@ -472,6 +486,74 @@ describe('package-operations', () => {
repoUrl,
formatter: expect.any(Function),
});
expect(parseChangelogMock).toHaveBeenCalledTimes(1);
expect(parseChangelogMock).toHaveBeenCalledWith({
changelogContent,
repoUrl,
formatter: expect.any(Function),
});
});

it('succeeds if updated changelog is empty, but there are unreleased changes', async () => {
const originalVersion = '1.0.0';
const newVersion = '1.0.1';
const dir = mockDirs[0];
const name = packageNames[0];
const manifest = getMockManifest(name, originalVersion);

const repoUrl = 'https://fake';
const changelogContent = 'I am a changelog.';
readFileMock.mockImplementationOnce(async () => changelogContent);

updateChangelogMock.mockImplementation(async () => '');
const actualChangelog = jest.requireActual(
'@metamask/auto-changelog/dist/changelog',
);
parseChangelogMock.mockImplementationOnce(() => {
return {
...actualChangelog,
getUnreleasedChanges() {
return {
Fixed: ['Something'],
};
},
};
});

const packageMetadata = getMockPackageMetadata(dir, manifest);
const updateSpecification = {
newVersion,
packagesToUpdate: new Set(packageNames),
repositoryUrl: repoUrl,
shouldUpdateChangelog: true,
synchronizeVersions: false,
};

await updatePackage(packageMetadata, updateSpecification);
expect(writeFileMock).toHaveBeenCalledTimes(1);
expect(writeFileMock).toHaveBeenNthCalledWith(
1,
getMockWritePath(dir, 'package.json'),
jsonStringify({
...cloneDeep(manifest),
[ManifestFieldNames.Version]: newVersion,
}),
);
expect(updateChangelogMock).toHaveBeenCalledTimes(1);
expect(updateChangelogMock).toHaveBeenCalledWith({
changelogContent,
currentVersion: newVersion,
isReleaseCandidate: true,
projectRootDirectory: dir,
repoUrl,
formatter: expect.any(Function),
});
expect(parseChangelogMock).toHaveBeenCalledTimes(1);
expect(parseChangelogMock).toHaveBeenCalledWith({
changelogContent,
repoUrl,
formatter: expect.any(Function),
});
});

it('throws if updated changelog is empty, and handles missing package name', async () => {
Expand All @@ -486,8 +568,19 @@ describe('package-operations', () => {
const changelogContent = 'I am a changelog.';
readFileMock.mockImplementationOnce(async () => changelogContent);

// This will cause an error
// no new changelog content and no unreleased changes will cause an error
updateChangelogMock.mockImplementation(async () => '');
const actualChangelog = jest.requireActual(
'@metamask/auto-changelog/dist/changelog',
);
parseChangelogMock.mockImplementationOnce(() => {
return {
...actualChangelog,
getUnreleasedChanges() {
return {};
},
};
});

const packageMetadata = getMockPackageMetadata(dir, manifest);
const updateSpecification = {
Expand Down Expand Up @@ -521,6 +614,12 @@ describe('package-operations', () => {
repoUrl,
formatter: expect.any(Function),
});
expect(parseChangelogMock).toHaveBeenCalledTimes(1);
expect(parseChangelogMock).toHaveBeenCalledWith({
changelogContent,
repoUrl,
formatter: expect.any(Function),
});
});

it('updates a package without synchronizing dependency versions', async () => {
Expand Down
31 changes: 28 additions & 3 deletions src/package-operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
validatePolyrepoPackageManifest,
writeJsonFile,
} from '@metamask/action-utils';
import { updateChangelog } from '@metamask/auto-changelog';
import { parseChangelog, updateChangelog } from '@metamask/auto-changelog';
import { promises as fs } from 'fs';
import pathUtils from 'path';
import prettier from 'prettier';
Expand Down Expand Up @@ -276,7 +276,13 @@ async function updatePackageChangelog(
repoUrl: repositoryUrl,
formatter: formatChangelog,
});
if (!newChangelogContent) {

if (newChangelogContent) {
return await fs.writeFile(changelogPath, newChangelogContent);
}

const hasUnReleased = hasUnreleasedChanges(changelogContent, repositoryUrl);
if (!hasUnReleased) {
const packageName = packageMetadata.manifest.name;
throw new Error(
`"updateChangelog" returned an empty value for package ${
Expand All @@ -285,7 +291,26 @@ async function updatePackageChangelog(
);
}

return await fs.writeFile(changelogPath, newChangelogContent);
return undefined;
}

/**
* Checks if there are unreleased changes in the changelog.
* @param changelogContent - The string formatted changelog.
* @param repositoryUrl - The repository url.
* @returns The boolean true if there are unreleased changes, otherwise false.
*/
function hasUnreleasedChanges(
changelogContent: string,
repositoryUrl: string,
): boolean {
const changelog = parseChangelog({
changelogContent,
repoUrl: repositoryUrl,
formatter: formatChangelog,
});

return Object.keys(changelog.getUnreleasedChanges()).length !== 0;
}

/**
Expand Down