Skip to content

Commit

Permalink
fix(core): allow configuration in inflix
Browse files Browse the repository at this point in the history
  • Loading branch information
xiongemi committed Oct 15, 2024
1 parent e0f9a5c commit 884bafe
Show file tree
Hide file tree
Showing 5 changed files with 198 additions and 30 deletions.
3 changes: 2 additions & 1 deletion e2e/nx/src/run.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ describe('Nx Running Tests', () => {
}

expect(runCLI(`echo:fail ${mylib}`, { silenceError: true })).toContain(
`Cannot find configuration for task ${mylib}:echo:fail`
`Cannot find configuration for task ${mylib}:echo`
);

updateJson(`libs/${mylib}/project.json`, (c) => original);
Expand Down Expand Up @@ -346,6 +346,7 @@ describe('Nx Running Tests', () => {
runCLI(`generate @nx/web:app apps/${myapp}`);

runCLI(`build ${myapp}`);
runCLI(`build:production ${myapp}`);
}, 10000);

it('should support project name positional arg non-consecutive to target', () => {
Expand Down
137 changes: 137 additions & 0 deletions packages/nx/src/command-line/run/run-one.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
import type { ProjectGraph } from '../../config/project-graph';
import { parseRunOneOptions } from './run-one';

describe('parseRunOneOptions', () => {
const projectGraph: ProjectGraph = {
nodes: {
project: {
name: 'project',
data: {
root: '',
targets: {
target: {
configurations: {
dev: {},
},
},
'target:target': {
configurations: {
dev: {},
},
},
'hello:world': {},
},
},
type: 'app',
},
},
dependencies: {},
};

it('should split format project:target:configuration', () => {
expect(
parseRunOneOptions(
'',
{
'project:target:configuration': 'project:target:configuration',
},
projectGraph,
{}
)
).toEqual({
configuration: 'configuration',
parsedArgs: {},
project: 'project',
target: 'target',
});

expect(
parseRunOneOptions(
'',
{
'project:target:configuration': 'project:target',
},
projectGraph,
{}
)
).toEqual({
parsedArgs: {},
project: 'project',
target: 'target',
});
});

it('should split format target:configuration for default project', () => {
expect(
parseRunOneOptions(
'',
{
'project:target:configuration': 'target',
},
projectGraph,
{ defaultProject: 'project' }
)
).toEqual({
parsedArgs: {},
project: 'project',
target: 'target',
});
});

it('should parse options with project and target specified', () => {
expect(
parseRunOneOptions(
'',
{
project: 'project',
target: 'target',
},
projectGraph,
{ defaultProject: 'project' }
)
).toEqual({
parsedArgs: {
target: 'target',
},
project: 'project',
target: 'target',
});

expect(
parseRunOneOptions(
'',
{
project: 'project',
target: 'target:configuration',
},
projectGraph,
{ defaultProject: 'project' }
)
).toEqual({
parsedArgs: {
target: 'target:configuration',
},
project: 'project',
target: 'target',
configuration: 'configuration',
});

expect(
parseRunOneOptions(
'',
{
project: 'project',
target: 'target:target',
},
projectGraph,
{ defaultProject: 'project' }
)
).toEqual({
parsedArgs: {
target: 'target:target',
},
project: 'project',
target: 'target:target',
});
});
});
8 changes: 7 additions & 1 deletion packages/nx/src/command-line/run/run-one.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ const targetAliases = {
t: 'test',
};

function parseRunOneOptions(
export function parseRunOneOptions(
cwd: string,
parsedArgs: { [k: string]: any },
projectGraph: ProjectGraph,
Expand Down Expand Up @@ -143,6 +143,12 @@ function parseRunOneOptions(
target = project;
project = defaultProjectName;
}
} else if (parsedArgs.project && parsedArgs.target?.indexOf(':') > -1) {
// infix case
[project, target, configuration] = splitTarget(
`${parsedArgs.project}:${parsedArgs.target}`,
projectGraph
);
} else {
target = parsedArgs.target ?? parsedArgs['project:target:configuration'];
}
Expand Down
8 changes: 6 additions & 2 deletions packages/nx/src/tasks-runner/create-task-graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,13 +315,17 @@ export class ProcessTasks {
): Task {
if (!project.data.targets[target]) {
throw new Error(
`Cannot find configuration for task ${project.name}:${target}`
`Cannot find configuration for task ${project.name}:${target}${
resolvedConfiguration ? ':' + resolvedConfiguration : ''
}`
);
}

if (!project.data.targets[target].executor) {
throw new Error(
`Target "${project.name}:${target}" does not have an executor configured`
`Target "${project.name}:${target}${
resolvedConfiguration ? ':' + resolvedConfiguration : ''
}" does not have an executor configured`
);
}

Expand Down
72 changes: 46 additions & 26 deletions packages/nx/src/utils/split-target.spec.ts
Original file line number Diff line number Diff line change
@@ -1,64 +1,84 @@
import { ProjectGraph } from '../config/project-graph';
import { ProjectGraphBuilder } from '../project-graph/project-graph-builder';
import type { ProjectGraph } from '../config/project-graph';
import { splitTarget } from './split-target';

let projectGraph: ProjectGraph;

describe('splitTarget', () => {
beforeAll(() => {
let builder = new ProjectGraphBuilder();
builder.addNode({
name: 'project',
data: {
root: '',
targets: {
target: {},
'target:target': {},
projectGraph = {
nodes: {
project: {
name: 'project',
data: {
root: '',
targets: {
target: {},
'target:target': {},
},
},
type: 'app',
},
},
type: 'app',
});

projectGraph = builder.getUpdatedProjectGraph();
dependencies: {},
};
});

it('should support only project', () => {
expect(splitTarget('project', projectGraph)).toEqual(['project']);
expect(splitTarget('random', projectGraph)).toEqual(['random']); // return the project name as is
});

it('should project:target', () => {
it('should split format project:target', () => {
expect(splitTarget('project:target', projectGraph)).toEqual([
'project',
'target',
]);

expect(splitTarget('project:random', projectGraph)).toEqual([
'project',
'random',
]); // return the target name as is
});

it('should project:target:configuration', () => {
it('should split format project:target:configuration', () => {
expect(splitTarget('project:target:configuration', projectGraph)).toEqual([
'project',
'target',
'configuration',
]);

expect(splitTarget('project:random:configuration', projectGraph)).toEqual([
'project',
'random',
'configuration',
]); // return the target name as is

expect(splitTarget('project:random:random', projectGraph)).toEqual([
'project',
'random',
'random',
]); // return the target and configuration name as is
});

it('should targets that contain colons when present in the graph', () => {
it('should identify targets that contain colons when present in the graph', () => {
expect(
splitTarget('project:target:target:configuration', projectGraph)
).toEqual(['project', 'target:target', 'configuration']);

expect(splitTarget('project:target:target', projectGraph)).toEqual([
'project',
'target:target',
]);
});

it('should targets that contain colons when not present in the graph but surrounded by quotes', () => {
it('should identify targets that contain colons when not present in the graph but surrounded by quotes', () => {
expect(
splitTarget('project:"other:other":configuration', projectGraph)
).toEqual(['project', 'other:other', 'configuration']);
});

it('should targets that contain colons when not provided graph but surrounded by quotes', () => {
expect(
splitTarget('project:"other:other":configuration', {
nodes: {},
dependencies: {},
} as ProjectGraph)
).toEqual(['project', 'other:other', 'configuration']);
expect(splitTarget('project:"other:other"', projectGraph)).toEqual([
'project',
'other:other',
]);
});
});

0 comments on commit 884bafe

Please sign in to comment.