Skip to content

Commit

Permalink
cleanup(core): add e2e tests for new implicit dependencies definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
AgentEnder committed Dec 22, 2022
1 parent ca87f32 commit d0123c1
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 46 deletions.
19 changes: 19 additions & 0 deletions e2e/nx-run/src/affected-graph.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,8 @@ describe('Nx Affected and Graph Tests', () => {
});

function generateAll() {
removeFile('apps');
removeFile('libs');
runCLI(`generate @nrwl/web:app ${myapp}`);
runCLI(`generate @nrwl/web:app ${myapp2}`);
runCLI(`generate @nrwl/js:lib ${mylib}`);
Expand Down Expand Up @@ -258,6 +260,23 @@ describe('Nx Affected and Graph Tests', () => {
expect(runCLI('affected:apps')).toContain(myapp2);
expect(runCLI('affected:libs')).not.toContain(mylib);
});

it('should detect changes to implicitly dependant projects', () => {
generateAll();
updateProjectConfig(myapp, (config) => ({
...config,
implicitDependencies: ['*', `!${myapp2}`],
}));

runCommand('git commit -m "setup test"');

const affectedApps = runCLI('affected:apps');
const affectedLibs = runCLI('affected:libs');

expect(affectedApps).toContain(myapp);
expect(affectedApps).not.toContain(myapp2);
expect(affectedLibs).toContain(mylib);
});
});

describe('print-affected', () => {
Expand Down
53 changes: 17 additions & 36 deletions packages/nx/src/command-line/run-many.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { createProjectGraphAsync } from '../project-graph/project-graph';
import { TargetDependencyConfig } from '../config/workspace-json-project-json';
import { readNxJson } from '../config/configuration';
import { output } from '../utils/output';
import { findMatchingProjects } from '../utils/find-matching-projects';

export async function runMany(
args: { [k: string]: any },
Expand Down Expand Up @@ -66,30 +67,14 @@ export function projectsToRun(
selectedProjects.set(projectName, projectGraph.nodes[projectName]);
}
} else {
for (const nameOrGlob of nxArgs.projects) {
if (validProjects.has(nameOrGlob)) {
selectedProjects.set(nameOrGlob, projectGraph.nodes[nameOrGlob]);
continue;
} else if (projectGraph.nodes[nameOrGlob]) {
invalidProjects.push(nameOrGlob);
continue;
}

const matchedProjectNames = minimatch.match(
validProjectNames,
nameOrGlob
);

if (matchedProjectNames.length === 0) {
throw new Error(`No projects matching: ${nameOrGlob}`);
const matchingProjects = findMatchingProjects(
nxArgs.projects,
Object.keys(projectGraph.nodes)
);
for (const project of matchingProjects) {
if (!validProjects.has(project)) {
invalidProjects.push(project);
}

matchedProjectNames.forEach((matchedProjectName) => {
selectedProjects.set(
matchedProjectName,
projectGraph.nodes[matchedProjectName]
);
});
}

if (invalidProjects.length > 0) {
Expand All @@ -100,21 +85,17 @@ export function projectsToRun(
}
}

for (const nameOrGlob of nxArgs.exclude ?? []) {
const project = selectedProjects.has(nameOrGlob);
if (project) {
selectedProjects.delete(nameOrGlob);
continue;
}
const excludedProjects = findMatchingProjects(
nxArgs.exclude ?? [],
Array.from(selectedProjects.keys())
);

const matchedProjects = minimatch.match(
Array.from(selectedProjects.keys()),
nameOrGlob
);
for (const excludedProject of excludedProjects) {
const project = selectedProjects.has(excludedProject);

matchedProjects.forEach((matchedProjectName) => {
selectedProjects.delete(matchedProjectName);
});
if (project) {
selectedProjects.delete(excludedProject);
}
}

return Array.from(selectedProjects.values());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,26 @@ describe('workspace-projects', () => {
})
).toEqual(['a', 'b', 'c']);
});

it('should expand glob based implicit dependencies', () => {
expect(
normalizeImplicitDependencies('test-project', ['b*'], {
fileMap: {},
filesToProcess: {},
workspace: {
version: 2,
projects: {
...makeProject('test-project'),
...makeProject('a'),
...makeProject('b'),
...makeProject('b-1'),
...makeProject('b-2'),
...makeProject('c'),
},
},
})
).toEqual(['b', 'b-1', 'b-2']);
});
});
});

Expand Down
20 changes: 11 additions & 9 deletions packages/nx/src/project-graph/build-nodes/workspace-projects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@ import { ProjectGraphBuilder } from '../project-graph-builder';
import { PackageJson } from '../../utils/package-json';
import { readJsonFile } from '../../utils/fileutils';
import { NxJsonConfiguration } from '../../config/nx-json';
import { ProjectConfiguration, TargetConfiguration } from '../../config/workspace-json-project-json';
import {
ProjectConfiguration,
TargetConfiguration,
} from '../../config/workspace-json-project-json';
import { NX_PREFIX } from '../../utils/logger';
import { findMatchingProjects } from 'nx/src/utils/find-matching-projects';

export function buildWorkspaceProjectNodes(
ctx: ProjectGraphProcessorContext,
Expand Down Expand Up @@ -147,12 +151,10 @@ export function normalizeImplicitDependencies(
implicitDependencies: ProjectConfiguration['implicitDependencies'],
context: ProjectGraphProcessorContext
) {
return implicitDependencies?.flatMap((target) => {
if (target === '*') {
return Object.keys(context.workspace.projects).filter(
(projectName) => projectName !== source
);
}
return target;
});
return findMatchingProjects(
implicitDependencies,
Object.keys(context.workspace.projects).filter(
(projectName) => projectName !== source
)
);
}
2 changes: 1 addition & 1 deletion packages/nx/src/utils/assert-workspace-validity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ function detectAndSetInvalidProjectValues(
const projectName = implicit.startsWith('!')
? implicit.substring(1)
: implicit;
return !validProjects[projectName];
return !(implicit === '*' || validProjects[projectName]);
});

if (invalidProjects.length > 0) {
Expand Down
38 changes: 38 additions & 0 deletions packages/nx/src/utils/find-matching-projects.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import minimatch = require('minimatch');

export function findMatchingProjects(
patterns: string[],
projectNames: string[]
): string[] {
const selectedProjects: Set<string> = new Set();
const excludedProjects: Set<string> = new Set();
for (const nameOrGlob of patterns) {
if (projectNames.includes(nameOrGlob)) {
selectedProjects.add(nameOrGlob);
continue;
}

const exclude = nameOrGlob.startsWith('!');
const pattern = exclude ? nameOrGlob.substring(1) : nameOrGlob;

const matchedProjectNames = minimatch.match(projectNames, pattern);

if (matchedProjectNames.length === 0) {
throw new Error(`No projects matching: ${nameOrGlob}`);
}

matchedProjectNames.forEach((matchedProjectName) => {
if (exclude) {
excludedProjects.add(matchedProjectName);
} else {
selectedProjects.add(matchedProjectName);
}
});
}

for (const project of excludedProjects) {
selectedProjects.delete(project);
}

return Array.from(selectedProjects);
}

0 comments on commit d0123c1

Please sign in to comment.