From eeceeb9e22b005891e62004f3d6ac4785975d81a Mon Sep 17 00:00:00 2001 From: Guillaume Roux Date: Thu, 12 Nov 2026 13:54:20 +0100 Subject: [PATCH] refactor(create-release-branch): let a mode name its prepare step The prepare step of a mode is now an object with a title, a result, and a run function. The core shows the title and the result that the mode gives, so the "Updating changelogs" labels move to the legacy mode. --- .../create-release-branch/src/core/mode.ts | 19 ++++++++++-- .../src/core/release-steps.test.ts | 31 ++++++++++--------- .../src/core/release-steps.ts | 7 ++--- .../src/core/run-release.test.ts | 3 +- .../src/modes/legacy/legacy-mode.test.ts | 17 ++++++++-- .../src/modes/legacy/legacy-mode.ts | 7 +++-- .../tests/unit/helpers.ts | 20 ++++++++++-- 7 files changed, 75 insertions(+), 29 deletions(-) diff --git a/packages/create-release-branch/src/core/mode.ts b/packages/create-release-branch/src/core/mode.ts index 67ef1eef..144c8b7b 100644 --- a/packages/create-release-branch/src/core/mode.ts +++ b/packages/create-release-branch/src/core/mode.ts @@ -32,13 +32,26 @@ export type ModePlanResult = | { plan: ModePlan } | { 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; +}; + /** * 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 @@ -49,7 +62,7 @@ export type ModePlanResult = */ export type Mode = { name: ModeName; - prepare(): Promise; + prepare: ModePrepareStep; listCandidates(choices?: ReleaseChoices): Promise; plan(choices: ReleaseChoices): Promise>; apply(plan: ModePlan, newRootVersion: string): Promise; diff --git a/packages/create-release-branch/src/core/release-steps.test.ts b/packages/create-release-branch/src/core/release-steps.test.ts index f0bef8c1..af403e03 100644 --- a/packages/create-release-branch/src/core/release-steps.test.ts +++ b/packages/create-release-branch/src/core/release-steps.test.ts @@ -5,6 +5,7 @@ import { describe, expect, it, vi } from 'vitest'; import { buildMockMode, + buildMockPrepareStep, buildMockPackage, buildMockProject, createNoopCliOutput, @@ -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'); @@ -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, @@ -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, @@ -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"', ]); @@ -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', @@ -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 () => { @@ -268,7 +271,7 @@ describe('release-steps', () => { project: buildMockProject(), releaseType: 'ordinary', mode: buildMockMode({ - prepare: vi.fn().mockResolvedValue(notices), + prepare: buildMockPrepareStep(vi.fn().mockResolvedValue(notices)), }), output: buildRecordingOutput(calls), }); @@ -276,8 +279,8 @@ describe('release-steps', () => { 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', diff --git a/packages/create-release-branch/src/core/release-steps.ts b/packages/create-release-branch/src/core/release-steps.ts index f2d875c1..14ef9dd5 100644 --- a/packages/create-release-branch/src/core/release-steps.ts +++ b/packages/create-release-branch/src/core/release-steps.ts @@ -80,11 +80,8 @@ export async function startRelease({ ); 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 diff --git a/packages/create-release-branch/src/core/run-release.test.ts b/packages/create-release-branch/src/core/run-release.test.ts index 588529f6..360b0db6 100644 --- a/packages/create-release-branch/src/core/run-release.test.ts +++ b/packages/create-release-branch/src/core/run-release.test.ts @@ -4,6 +4,7 @@ import { describe, expect, it, vi } from 'vitest'; import { buildMockPackage, + buildMockPrepareStep, buildMockProject, writtenText, } from '../../tests/unit/helpers.js'; @@ -35,7 +36,7 @@ function buildContext({ }): ReleaseContext { const mode: Mode = { name: 'legacy', - prepare: vi.fn(), + prepare: buildMockPrepareStep(), listCandidates: vi.fn(), plan: vi.fn(), apply: vi.fn(), diff --git a/packages/create-release-branch/src/modes/legacy/legacy-mode.test.ts b/packages/create-release-branch/src/modes/legacy/legacy-mode.test.ts index 558e6c45..ebe997a2 100644 --- a/packages/create-release-branch/src/modes/legacy/legacy-mode.test.ts +++ b/packages/create-release-branch/src/modes/legacy/legacy-mode.test.ts @@ -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({ @@ -35,7 +48,7 @@ describe('legacy mode', () => { formatter: 'prettier', }); - await mode.prepare(); + await mode.prepare.run(); expect( projectModule.updateChangelogsForChangedPackages, @@ -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 () => { diff --git a/packages/create-release-branch/src/modes/legacy/legacy-mode.ts b/packages/create-release-branch/src/modes/legacy/legacy-mode.ts index 4f09b07b..e74c59be 100644 --- a/packages/create-release-branch/src/modes/legacy/legacy-mode.ts +++ b/packages/create-release-branch/src/modes/legacy/legacy-mode.ts @@ -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) { diff --git a/packages/create-release-branch/tests/unit/helpers.ts b/packages/create-release-branch/tests/unit/helpers.ts index 2fad5182..20e6f023 100644 --- a/packages/create-release-branch/tests/unit/helpers.ts +++ b/packages/create-release-branch/tests/unit/helpers.ts @@ -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, @@ -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. * @@ -207,7 +223,7 @@ export function buildMockMode( ): Mode { return { name: 'legacy', - prepare: vi.fn().mockResolvedValue([]), + prepare: buildMockPrepareStep(), listCandidates: vi.fn().mockResolvedValue([]), plan: vi.fn(), apply: vi.fn().mockResolvedValue([]),