Skip to content

Commit

Permalink
fix(js): filter project dependencies when calculating topological ord…
Browse files Browse the repository at this point in the history
…ering (#26491)
  • Loading branch information
mpsanchis authored Jun 11, 2024
1 parent b9b89b2 commit 0018842
Show file tree
Hide file tree
Showing 2 changed files with 153 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe('sortProjectsTopologically', () => {
expect(result).toEqual([projectGraph.nodes.project1]);
});

it('should return projects in the correct order', () => {
it('should return [2,1] if 1 depends on 2', () => {
const projectGraph = {
dependencies: {
project1: [
Expand Down Expand Up @@ -111,4 +111,146 @@ describe('sortProjectsTopologically', () => {
const result = sortProjectsTopologically(projectGraph, projectNodes);
expect(result).toEqual(projectNodes);
});

it('should return [3,2,1] if 1 depends on 2 and 2 depends on 3', () => {
const projectGraph = {
dependencies: {
project1: [
{
source: 'project1',
target: 'project2',
type: 'static',
},
],
project2: [
{
source: 'project2',
target: 'project3',
type: 'static',
},
],
},
nodes: {
project1: {
name: 'project1',
data: {
root: '',
},
type: 'app' as const,
},
project2: {
name: 'project2',
data: {
root: '',
},
type: 'app' as const,
},
project3: {
name: 'project3',
data: {
root: '',
},
type: 'app' as const,
},
},
};
const projectNodes = [
projectGraph.nodes.project1,
projectGraph.nodes.project2,
projectGraph.nodes.project3,
];
const result = sortProjectsTopologically(projectGraph, projectNodes);
expect(result).toEqual([
projectGraph.nodes.project3,
projectGraph.nodes.project2,
projectGraph.nodes.project1,
]);
});

it('should return [1,2,3,4] if 1 has zero dependencies, 2 has one, 3 has two, and 4 has three', () => {
const projectGraph = {
dependencies: {
project1: [],
project2: [
{
source: 'project2',
target: 'project1',
type: 'static',
},
],
project3: [
{
source: 'project3',
target: 'project1',
type: 'static',
},
{
source: 'project3',
target: 'project2',
type: 'static',
},
],
project4: [
{
source: 'project4',
target: 'project3',
type: 'static',
},
{
source: 'project4',
target: 'project2',
type: 'static',
},
{
source: 'project4',
target: 'project1',
type: 'static',
},
],
},
nodes: {
project1: {
name: 'project1',
data: {
root: '',
},
type: 'app' as const,
},
project2: {
name: 'project2',
data: {
root: '',
},
type: 'app' as const,
},
project3: {
name: 'project3',
data: {
root: '',
},
type: 'app' as const,
},
project4: {
name: 'project4',
data: {
root: '',
},
type: 'app' as const,
},
},
};
const projectNodes = [
projectGraph.nodes.project1,
projectGraph.nodes.project2,
projectGraph.nodes.project3,
projectGraph.nodes.project4,
];
const result = sortProjectsTopologically(projectGraph, projectNodes);
expect(result).toEqual([
projectGraph.nodes.project1,
projectGraph.nodes.project2,
projectGraph.nodes.project3,
projectGraph.nodes.project4,
]);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,16 @@ export function sortProjectsTopologically(
sortedProjects.push(node);

// Process each project that depends on the current node
filteredDependencies.forEach((dep) => {
const dependentNode = projectGraph.nodes[dep.source];
const count = edges.get(dependentNode) - 1;
edges.set(dependentNode, count);
if (count === 0) {
processQueue.push(dependentNode);
}
});
filteredDependencies
.filter((dep) => dep.target === node.name)
.forEach((dep) => {
const dependentNode = projectGraph.nodes[dep.source];
const count = edges.get(dependentNode) - 1;
edges.set(dependentNode, count);
if (count === 0) {
processQueue.push(dependentNode);
}
});
}

/**
Expand Down

0 comments on commit 0018842

Please sign in to comment.