Skip to content

Commit

Permalink
feat(semver): re-use contextual project in post-target
Browse files Browse the repository at this point in the history
  • Loading branch information
edbzn committed Nov 18, 2023
1 parent 029f60e commit 5c73fe6
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 12 deletions.
8 changes: 7 additions & 1 deletion packages/semver/src/executors/version/testing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,14 @@ export function createFakeContext({
project,
projectRoot,
workspaceRoot,
targets = {},
additionalProjects = [],
}: {
cwd?: string;
project: string;
projectRoot: string;
workspaceRoot: string;
targets?: Record<string, TargetConfiguration>;
additionalProjects?: {
project: string;
projectRoot: string;
Expand All @@ -133,11 +135,15 @@ export function createFakeContext({
projects: {
[project]: {
root: projectRoot,
targets: {},
targets,
},
...assembleAdditionalProjects(additionalProjects),
},
},
projectGraph: {
nodes: {},
dependencies: {},
},
} satisfies ExecutorContext;
}

Expand Down
38 changes: 37 additions & 1 deletion packages/semver/src/executors/version/utils/post-target.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ describe(runPostTargets.name, () => {

const context = createFakeContext({
project: 'test',
targets: {
test: {
command: 'exit 0',
},
},
projectRoot: 'libs/test',
workspaceRoot: '/root',
additionalProjects: additionalProjects,
Expand All @@ -74,7 +79,7 @@ describe(runPostTargets.name, () => {
jest.resetAllMocks();
});

it('should successfully execute post targets', (done) => {
it('should execute post targets', (done) => {
mockReadTargetOptions.mockReturnValue({
optionA: 'optionA',
});
Expand Down Expand Up @@ -118,6 +123,37 @@ describe(runPostTargets.name, () => {
});
});

it('should execute post targets without specifying the project name', (done) => {
runPostTargets({
projectName: 'a',
postTargets: ['test'],
templateStringContext: {},
context: createFakeContext({
project: 'a',
targets: {
test: {
command: 'test',
},
},
projectRoot: 'libs/a',
workspaceRoot: '/root',
}),
}).subscribe({
next: nextSpy,
complete: () => {
expect(nextSpy).toBeCalledTimes(1);
expect(mockRunExecutor).toBeCalledTimes(1);
expect(mockRunExecutor.mock.calls[0][0]).toEqual(
expect.objectContaining({
project: 'a',
target: 'test',
}),
);
done();
},
});
});

it('should handle post target failure', (done) => {
mockRunExecutor.mockImplementationOnce(function* () {
yield { success: true };
Expand Down
23 changes: 13 additions & 10 deletions packages/semver/src/executors/version/utils/post-target.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ export function runPostTargets({
return concat(
...postTargets.map((postTargetSchema) =>
defer(async () => {
const target = parseTargetString(postTargetSchema);
// TODO: deprecate specifying the project name in the post target schema.
const target = postTargetSchema.includes(':')
? parseTargetString(postTargetSchema)
: parseTargetString(postTargetSchema, context);

_checkTargetExist(target, context);

Expand Down Expand Up @@ -83,16 +86,16 @@ export function _getTargetOptions({
: _getTargetOptions({ options: _element, context }),
)
: typeof value === 'object'
? _getTargetOptions({
options: value as Record<string, unknown>,
context,
})
: coerce(
createTemplateString(
(value as number | string | boolean).toString(),
? _getTargetOptions({
options: value as Record<string, unknown>,
context,
),
);
})
: coerce(
createTemplateString(
(value as number | string | boolean).toString(),
context,
),
);

return {
...optionsAccumulator,
Expand Down

0 comments on commit 5c73fe6

Please sign in to comment.