Skip to content
Draft
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
19 changes: 16 additions & 3 deletions packages/create-release-branch/src/core/mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,26 @@ export type ModePlanResult<Data> =
| { plan: ModePlan<Data> }
| { errors: ReleaseChoiceError[] };

/**
* A step that a mode runs on the first run, before the "Initialize Release"
* commit.
*
* @property title - What the step does, shown while it runs.
* @property result - What the step did, shown when it ends.
* @property run - Changes the project, and returns the notices to print.
*/
export type ModePrepareStep = {
title: string;
result: string;
run: () => Promise<Notice[]>;
};

/**
* Decides which packages can be released, plans the release from the release
* choices, and writes it to the project.
*
* @property name - The name of the mode.
* @property prepare - Changes the project on the first run, before the
* "Initialize Release" commit, and returns the notices to print.
* @property prepare - The step that prepares the project on the first run.
* @property listCandidates - Lists the packages that can be released, including
* the packages that the given choices pull in.
* @property plan - Plans the release from release choices that passed the
Expand All @@ -49,7 +62,7 @@ export type ModePlanResult<Data> =
*/
export type Mode<Data> = {
name: ModeName;
prepare(): Promise<Notice[]>;
prepare: ModePrepareStep;
listCandidates(choices?: ReleaseChoices): Promise<Candidate[]>;
plan(choices: ReleaseChoices): Promise<ModePlanResult<Data>>;
apply(plan: ModePlan<Data>, newRootVersion: string): Promise<Notice[]>;
Expand Down
31 changes: 17 additions & 14 deletions packages/create-release-branch/src/core/release-steps.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { describe, expect, it, vi } from 'vitest';

import {
buildMockMode,
buildMockPrepareStep,
buildMockPackage,
buildMockProject,
createNoopCliOutput,
Expand Down Expand Up @@ -117,8 +118,8 @@ describe('release-steps', () => {
describe('startRelease', () => {
it('prepares and commits only on the first run', async () => {
const project = buildMockProject();
const prepare = vi.fn().mockResolvedValue([]);
const mode = buildMockMode({ prepare });
const run = vi.fn().mockResolvedValue([]);
const mode = buildMockMode({ prepare: buildMockPrepareStep(run) });
const determineReleaseVersionSpy = vi
.spyOn(releaseBranchModule, 'determineReleaseVersion')
.mockReturnValue('2.0.0');
Expand Down Expand Up @@ -150,7 +151,7 @@ describe('release-steps', () => {
'ordinary',
);
expect(ensureReleaseBranchSpy).toHaveBeenCalledWith(project, '2.0.0');
expect(prepare).toHaveBeenCalledTimes(1);
expect(run).toHaveBeenCalledTimes(1);
expect(commitAllChangesSpy).toHaveBeenCalledTimes(1);
expect(commitAllChangesSpy).toHaveBeenCalledWith(
project.directoryPath,
Expand All @@ -163,10 +164,12 @@ describe('release-steps', () => {
const project = buildMockProject();
const calls: string[] = [];
const mode = buildMockMode({
prepare: vi.fn().mockImplementation(async () => {
calls.push('prepare');
return [];
}),
prepare: buildMockPrepareStep(
vi.fn().mockImplementation(async () => {
calls.push('prepare');
return [];
}),
),
});
vi.spyOn(
releaseBranchModule,
Expand Down Expand Up @@ -199,9 +202,9 @@ describe('release-steps', () => {
'start Creating the release branch',
'ensureReleaseBranch',
'end Creating the release branch',
'start Updating changelogs',
'start Preparing the project',
'prepare',
'end Updating changelogs',
'end Preparing the project',
'commitAllChanges /path/to/project Initialize Release 2.0.0',
'done Committed "Initialize Release 2.0.0"',
]);
Expand Down Expand Up @@ -233,7 +236,7 @@ describe('release-steps', () => {
},
);

it('ends the "Updating changelogs" step with "Updated changelogs"', async () => {
it('ends the prepare step with the result that the mode gives', async () => {
const { output, stepLines } = buildOutputWithStepLines();
vi.spyOn(releaseBranchModule, 'determineReleaseVersion').mockReturnValue(
'2.0.0',
Expand All @@ -249,7 +252,7 @@ describe('release-steps', () => {
output,
});

expect(stepLines()[1]).toBe('Updated changelogs');
expect(stepLines()[1]).toBe('Prepared the project');
});

it('prints the notices from prepare after the step', async () => {
Expand All @@ -268,16 +271,16 @@ describe('release-steps', () => {
project: buildMockProject(),
releaseType: 'ordinary',
mode: buildMockMode({
prepare: vi.fn().mockResolvedValue(notices),
prepare: buildMockPrepareStep(vi.fn().mockResolvedValue(notices)),
}),
output: buildRecordingOutput(calls),
});

expect(calls).toStrictEqual([
'start Creating the release branch',
'end Creating the release branch',
'start Updating changelogs',
'end Updating changelogs',
'start Preparing the project',
'end Preparing the project',
'warnList Title 1: a, b',
'warnList Title 2: c',
'commitAllChanges',
Expand Down
7 changes: 2 additions & 5 deletions packages/create-release-branch/src/core/release-steps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,8 @@ export async function startRelease<Data>({
);

if (firstRun) {
const notices = await output.step(
'Updating changelogs',
async () => await mode.prepare(),
{ result: () => 'Updated changelogs' },
);
const { title, result, run } = mode.prepare;
const notices = await output.step(title, run, { result: () => result });
printNotices(output, notices);
const commitMessage = `Initialize Release ${newReleaseVersion}`;
// `prepare` may change nothing, and the release branch still needs this
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { describe, expect, it, vi } from 'vitest';

import {
buildMockPackage,
buildMockPrepareStep,
buildMockProject,
writtenText,
} from '../../tests/unit/helpers.js';
Expand Down Expand Up @@ -35,7 +36,7 @@ function buildContext({
}): ReleaseContext {
const mode: Mode<unknown> = {
name: 'legacy',
prepare: vi.fn(),
prepare: buildMockPrepareStep(),
listCandidates: vi.fn(),
plan: vi.fn(),
apply: vi.fn(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,19 @@ const notices: Notice[] = [

describe('legacy mode', () => {
describe('createLegacyMode', () => {
it('names its prepare step after the changelog update', () => {
const mode = createLegacyMode({
project: buildMockProject(),
defaultBranch: 'main',
formatter: 'prettier',
});

expect(mode.prepare).toMatchObject({
title: 'Updating changelogs',
result: 'Updated changelogs',
});
});

it('updates the changelogs of changed packages when preparing', async () => {
const project = buildMockProject();
const mode = createLegacyMode({
Expand All @@ -35,7 +48,7 @@ describe('legacy mode', () => {
formatter: 'prettier',
});

await mode.prepare();
await mode.prepare.run();

expect(
projectModule.updateChangelogsForChangedPackages,
Expand All @@ -52,7 +65,7 @@ describe('legacy mode', () => {
formatter: 'prettier',
});

expect(await mode.prepare()).toStrictEqual(notices);
expect(await mode.prepare.run()).toStrictEqual(notices);
});

it('lists the legacy candidates for the given choices', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,11 @@ export function createLegacyMode({
return {
name: 'legacy',

async prepare() {
return await updateChangelogsForChangedPackages({ project, formatter });
prepare: {
title: 'Updating changelogs',
result: 'Updated changelogs',
run: async () =>
await updateChangelogsForChangedPackages({ project, formatter }),
},

async listCandidates(choices) {
Expand Down
20 changes: 18 additions & 2 deletions packages/create-release-branch/tests/unit/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { vi } from 'vitest';

import { createCliOutput } from '../../src/core/cli-output.js';
import type { CliOutput } from '../../src/core/cli-output.js';
import type { Mode } from '../../src/core/mode.js';
import type { Mode, ModePrepareStep } from '../../src/core/mode.js';
import {
PackageManifestDependenciesFieldNames,
PackageManifestFieldNames,
Expand Down Expand Up @@ -196,6 +196,22 @@ export function createNoopCliOutput(): CliOutput {
});
}

/**
* Builds the prepare step of a mode for use in tests.
*
* @param run - The function that runs the step.
* @returns The mock prepare step.
*/
export function buildMockPrepareStep(
run: ModePrepareStep['run'] = vi.fn().mockResolvedValue([]),
): ModePrepareStep {
return {
title: 'Preparing the project',
result: 'Prepared the project',
run,
};
}

/**
* Builds a mode for use in tests. All methods are mock functions.
*
Expand All @@ -207,7 +223,7 @@ export function buildMockMode(
): Mode<string> {
return {
name: 'legacy',
prepare: vi.fn().mockResolvedValue([]),
prepare: buildMockPrepareStep(),
listCandidates: vi.fn().mockResolvedValue([]),
plan: vi.fn(),
apply: vi.fn().mockResolvedValue([]),
Expand Down
Loading