Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"dependencies": {
"@metamask/action-utils": "^0.0.2",
"@metamask/utils": "^2.1.0",
"date-fns": "^2.29.1",
"debug": "^4.3.4",
"execa": "^5.0.0",
"glob": "^8.0.3",
Expand Down
20 changes: 10 additions & 10 deletions src/functional.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe('create-release-branch (functional)', () => {
packages: {
$root$: {
name: '@scope/monorepo',
version: '2022.1.1',
version: '20220101.1.0',
directoryPath: '.',
},
a: {
Expand Down Expand Up @@ -41,7 +41,7 @@ describe('create-release-branch (functional)', () => {
workspaces: {
'.': ['packages/*'],
},
today: new Date('2022-06-24'),
today: new Date(2022, 5, 24),
},
async (environment) => {
await environment.updateJsonFile('package.json', {
Expand Down Expand Up @@ -88,7 +88,7 @@ describe('create-release-branch (functional)', () => {

expect(await environment.readJsonFile('package.json')).toStrictEqual({
name: '@scope/monorepo',
version: '2022.6.24',
version: '20220624.2.0',
private: true,
workspaces: ['packages/*'],
scripts: { foo: 'bar' },
Expand Down Expand Up @@ -138,7 +138,7 @@ describe('create-release-branch (functional)', () => {
packages: {
$root$: {
name: '@scope/monorepo',
version: '2022.1.1',
version: '20220101.1.0',
directoryPath: '.',
},
a: {
Expand Down Expand Up @@ -238,7 +238,7 @@ describe('create-release-branch (functional)', () => {
packages: {
$root$: {
name: '@scope/monorepo',
version: '2022.1.1',
version: '20220101.1.0',
directoryPath: '.',
},
a: {
Expand All @@ -250,7 +250,7 @@ describe('create-release-branch (functional)', () => {
workspaces: {
'.': ['packages/*'],
},
today: new Date('2022-06-24'),
today: new Date(2022, 5, 24),
},
async (environment) => {
await environment.runTool({
Expand All @@ -262,9 +262,9 @@ describe('create-release-branch (functional)', () => {
});

// Tests four things:
// * The latest commit should be called "Release YYYY-MM-DD"
// * The latest commit should be called "Release YYYY-MM-DD (RN)"
// * The latest commit should be the current commit (HEAD)
// * The latest branch should be called "release/YYYY-MM-DD"
// * The latest branch should be called "release/YYYY-MM-DD/N"
// * The latest branch should point to the latest commit
const [latestCommitSubject, latestCommitId, latestCommitRevsMarker] =
(
Expand All @@ -284,9 +284,9 @@ describe('create-release-branch (functional)', () => {
'--max-count=1',
])
).stdout;
expect(latestCommitSubject).toStrictEqual('Release 2022-06-24');
expect(latestCommitSubject).toStrictEqual('Release 2022-06-24 (R2)');
expect(latestCommitRevs).toContain('HEAD');
expect(latestCommitRevs).toContain('release/2022-06-24');
expect(latestCommitRevs).toContain('release/2022-06-24/2');
expect(latestBranchCommitId).toStrictEqual(latestCommitId);
},
);
Expand Down
80 changes: 76 additions & 4 deletions src/initial-parameters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ describe('initial-parameters', () => {
project,
tempDirectoryPath: '/path/to/temp',
reset: true,
today: new Date('2022-06-22'),
today: new Date(2022, 5, 22),
});
});

Expand Down Expand Up @@ -125,9 +125,81 @@ describe('initial-parameters', () => {
);
});

it('uses the current date if the TODAY environment variable was not provided', async () => {
it('returns initial parameters including reset: false, derived from a command-line argument of "--reset true"', async () => {
const project = buildMockProject();
const today = new Date('2022-01-01');
when(jest.spyOn(commandLineArgumentsModule, 'readCommandLineArguments'))
.calledWith(['arg1', 'arg2'])
.mockResolvedValue({
projectDirectory: '/path/to/project',
tempDirectory: '/path/to/temp',
reset: true,
});
jest
.spyOn(envModule, 'getEnvironmentVariables')
.mockReturnValue({ TODAY: undefined, EDITOR: undefined });
when(jest.spyOn(projectModule, 'readProject'))
.calledWith('/path/to/project')
.mockResolvedValue(project);

const config = await determineInitialParameters(
['arg1', 'arg2'],
'/path/to/somewhere',
);

expect(config.reset).toBe(true);
});

it('returns initial parameters including reset: false, derived from a command-line argument of "--reset false"', async () => {
const project = buildMockProject();
when(jest.spyOn(commandLineArgumentsModule, 'readCommandLineArguments'))
.calledWith(['arg1', 'arg2'])
.mockResolvedValue({
projectDirectory: '/path/to/project',
tempDirectory: '/path/to/temp',
reset: false,
});
jest
.spyOn(envModule, 'getEnvironmentVariables')
.mockReturnValue({ TODAY: undefined, EDITOR: undefined });
when(jest.spyOn(projectModule, 'readProject'))
.calledWith('/path/to/project')
.mockResolvedValue(project);

const config = await determineInitialParameters(
['arg1', 'arg2'],
'/path/to/somewhere',
);

expect(config.reset).toBe(false);
});

it("returns initial parameters including today's date, derived from the TODAY environment variable", async () => {
const project = buildMockProject();
when(jest.spyOn(commandLineArgumentsModule, 'readCommandLineArguments'))
.calledWith(['arg1', 'arg2'])
.mockResolvedValue({
projectDirectory: '/path/to/project',
tempDirectory: '/path/to/temp',
reset: true,
});
jest
.spyOn(envModule, 'getEnvironmentVariables')
.mockReturnValue({ TODAY: '2022-01-01', EDITOR: undefined });
when(jest.spyOn(projectModule, 'readProject'))
.calledWith('/path/to/project')
.mockResolvedValue(project);

const config = await determineInitialParameters(
['arg1', 'arg2'],
'/path/to/somewhere',
);

expect(config.today).toStrictEqual(new Date(2022, 0, 1));
});

it('uses the current date if TODAY is undefined', async () => {
const project = buildMockProject();
const today = new Date(2022, 0, 1);
when(jest.spyOn(commandLineArgumentsModule, 'readCommandLineArguments'))
.calledWith(['arg1', 'arg2'])
.mockResolvedValue({
Expand All @@ -153,7 +225,7 @@ describe('initial-parameters', () => {

it('uses the current date if TODAY is not a parsable date', async () => {
const project = buildMockProject();
const today = new Date('2022-01-01');
const today = new Date(2022, 0, 1);
when(jest.spyOn(commandLineArgumentsModule, 'readCommandLineArguments'))
.calledWith(['arg1', 'arg2'])
.mockResolvedValue({
Expand Down
5 changes: 3 additions & 2 deletions src/initial-parameters.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import os from 'os';
import path from 'path';
import { getEnvironmentVariables } from './env';
import { parseISO as parseDateAsISO } from 'date-fns';
import { readCommandLineArguments } from './command-line-arguments';
import { getEnvironmentVariables } from './env';
import { readProject, Project } from './project';

interface InitialParameters {
Expand Down Expand Up @@ -37,7 +38,7 @@ export async function determineInitialParameters(
)
: path.resolve(cwd, inputs.tempDirectory);
const parsedTodayTimestamp =
TODAY === undefined ? NaN : new Date(TODAY).getTime();
TODAY === undefined ? NaN : parseDateAsISO(TODAY).getTime();
const today = isNaN(parsedTodayTimestamp)
? new Date()
: new Date(parsedTodayTimestamp);
Expand Down
67 changes: 46 additions & 21 deletions src/monorepo-workflow-operations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,21 +89,19 @@ function buildMockReleaseSpecification({
*
* @param overrides - The properties you want to override in the mock release
* plan.
* @param overrides.releaseName - The name of the new release. For a polyrepo or
* a monorepo with fixed versions, this will be a version string with the shape
* `<major>.<minor>.<patch>`; for a monorepo with independent versions, this
* will be a version string with the shape `<year>.<month>.<day>-<build
* number>`.
* @param overrides.releaseDate - The date of the release.
* @param overrides.releaseNumber - The number of the release.
* @param overrides.packages - Information about all of the packages in the
* project. For a polyrepo, this consists of the self-same package; for a
* monorepo it consists of the root package and any workspace packages.
* @returns The mock release specification.
*/
function buildMockReleasePlan({
releaseName = 'release-name',
releaseDate = new Date(),
releaseNumber = 1,
packages = [],
}: Partial<ReleasePlan> = {}): ReleasePlan {
return { releaseName, packages };
return { releaseDate, releaseNumber, packages };
}

/**
Expand Down Expand Up @@ -178,8 +176,9 @@ async function setupFollowMonorepoWorkflow({
const releaseSpecification = buildMockReleaseSpecification({
path: releaseSpecificationPath,
});
const releaseName = 'some-release-name';
const releasePlan = buildMockReleasePlan({ releaseName });
const releaseDate = new Date(2022, 0, 1);
const releaseNumber = 12345;
const releasePlan = buildMockReleasePlan({ releaseDate, releaseNumber });
const projectDirectoryPath = '/path/to/project';
const project = buildMockProject({ directoryPath: projectDirectoryPath });
const today = new Date();
Expand Down Expand Up @@ -221,12 +220,21 @@ async function setupFollowMonorepoWorkflow({
}

if (errorUponExecutingReleasePlan) {
executeReleasePlanSpy.mockRejectedValue(errorUponExecutingReleasePlan);
when(executeReleasePlanSpy)
.calledWith(project, releasePlan, stderr)
.mockRejectedValue(errorUponExecutingReleasePlan);
} else {
executeReleasePlanSpy.mockResolvedValue();
when(executeReleasePlanSpy)
.calledWith(project, releasePlan, stderr)
.mockResolvedValue(undefined);
}

captureChangesInReleaseBranchSpy.mockResolvedValue();
when(captureChangesInReleaseBranchSpy)
.calledWith(projectDirectoryPath, {
releaseDate,
releaseNumber,
})
.mockResolvedValue();

if (doesReleaseSpecFileExist) {
await fs.promises.writeFile(
Expand All @@ -246,7 +254,8 @@ async function setupFollowMonorepoWorkflow({
executeReleasePlanSpy,
captureChangesInReleaseBranchSpy,
releasePlan,
releaseName,
releaseDate,
releaseNumber,
releaseSpecificationPath,
};
}
Expand Down Expand Up @@ -295,7 +304,8 @@ describe('monorepo-workflow-operations', () => {
stderr,
captureChangesInReleaseBranchSpy,
projectDirectoryPath,
releaseName,
releaseDate,
releaseNumber,
} = await setupFollowMonorepoWorkflow({
sandbox,
doesReleaseSpecFileExist: false,
Expand All @@ -313,7 +323,10 @@ describe('monorepo-workflow-operations', () => {

expect(captureChangesInReleaseBranchSpy).toHaveBeenCalledWith(
projectDirectoryPath,
releaseName,
{
releaseDate,
releaseNumber,
},
);
});
});
Expand Down Expand Up @@ -688,7 +701,8 @@ describe('monorepo-workflow-operations', () => {
stderr,
captureChangesInReleaseBranchSpy,
projectDirectoryPath,
releaseName,
releaseDate,
releaseNumber,
} = await setupFollowMonorepoWorkflow({
sandbox,
doesReleaseSpecFileExist: true,
Expand All @@ -705,7 +719,10 @@ describe('monorepo-workflow-operations', () => {

expect(captureChangesInReleaseBranchSpy).toHaveBeenCalledWith(
projectDirectoryPath,
releaseName,
{
releaseDate,
releaseNumber,
},
);
});
});
Expand Down Expand Up @@ -849,7 +866,8 @@ describe('monorepo-workflow-operations', () => {
stderr,
captureChangesInReleaseBranchSpy,
projectDirectoryPath,
releaseName,
releaseDate,
releaseNumber,
} = await setupFollowMonorepoWorkflow({
sandbox,
doesReleaseSpecFileExist: false,
Expand All @@ -867,7 +885,10 @@ describe('monorepo-workflow-operations', () => {

expect(captureChangesInReleaseBranchSpy).toHaveBeenCalledWith(
projectDirectoryPath,
releaseName,
{
releaseDate,
releaseNumber,
},
);
});
});
Expand Down Expand Up @@ -1247,7 +1268,8 @@ describe('monorepo-workflow-operations', () => {
stderr,
captureChangesInReleaseBranchSpy,
projectDirectoryPath,
releaseName,
releaseDate,
releaseNumber,
} = await setupFollowMonorepoWorkflow({
sandbox,
doesReleaseSpecFileExist: true,
Expand All @@ -1265,7 +1287,10 @@ describe('monorepo-workflow-operations', () => {

expect(captureChangesInReleaseBranchSpy).toHaveBeenCalledWith(
projectDirectoryPath,
releaseName,
{
releaseDate,
releaseNumber,
},
);
});
});
Expand Down
8 changes: 4 additions & 4 deletions src/monorepo-workflow-operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ export async function followMonorepoWorkflow({
});
await executeReleasePlan(project, releasePlan, stderr);
await removeFile(releaseSpecificationPath);
await captureChangesInReleaseBranch(
project.directoryPath,
releasePlan.releaseName,
);
await captureChangesInReleaseBranch(project.directoryPath, {
releaseDate: releasePlan.releaseDate,
releaseNumber: releasePlan.releaseNumber,
});
}
Loading