Skip to content

Commit

Permalink
[FIX] npm translator: Remove deduped optional dependencies from tree
Browse files Browse the repository at this point in the history
In some cases optional (pending) dependencies that have been deduped
have not been removed from the dependency tree, no matter whether the
"includeDeduped" flag was set or not.
  • Loading branch information
RandomByte committed Feb 14, 2019
1 parent 4d8527a commit 3481154
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 5 deletions.
8 changes: 6 additions & 2 deletions lib/translators/npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -461,8 +461,12 @@ class NpmTranslator {
`(child of ${parent.id})`);

const idx = parent.dependencies.indexOf(project);
const dedupedProject = this.createDedupedProject(project);
parent.dependencies.splice(idx, 1, dedupedProject);
if (this.includeDeduped) {
const dedupedProject = this.createDedupedProject(project);
parent.dependencies.splice(idx, 1, dedupedProject);
} else {
parent.dependencies.splice(idx, 1);
}
}

if (project.dependencies) {
Expand Down
96 changes: 93 additions & 3 deletions test/lib/translators/npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ test("AppG: project with npm 'optionalDependencies' should not fail if optional
test("AppCycleA: cyclic dev deps", (t) => {
const applicationCycleAPath = path.join(cycleDepsBasePath, "application.cycle.a");

return npmTranslator.generateDependencyTree(applicationCycleAPath, {includeDeduped: false}).then((parsedTree) => {
return npmTranslator.generateDependencyTree(applicationCycleAPath).then((parsedTree) => {
t.deepEqual(parsedTree, applicationCycleATree, "Parsed correctly");
});
});
Expand All @@ -72,9 +72,16 @@ test("AppCycleA: cyclic dev deps - include deduped", (t) => {
});
});

test("AppCycleB: cyclic npm deps - Cycle via devDependency on second level - include deduped", (t) => {
const applicationCycleBPath = path.join(cycleDepsBasePath, "application.cycle.b");
return npmTranslator.generateDependencyTree(applicationCycleBPath, {includeDeduped: true}).then((parsedTree) => {
t.deepEqual(parsedTree, applicationCycleBTreeIncDeduped, "Parsed correctly");
});
});

test("AppCycleB: cyclic npm deps - Cycle via devDependency on second level", (t) => {
const applicationCycleBPath = path.join(cycleDepsBasePath, "application.cycle.b");
return npmTranslator.generateDependencyTree(applicationCycleBPath).then((parsedTree) => {
return npmTranslator.generateDependencyTree(applicationCycleBPath, {includeDeduped: false}).then((parsedTree) => {
t.deepEqual(parsedTree, applicationCycleBTree, "Parsed correctly");
});
});
Expand All @@ -100,9 +107,16 @@ test("AppCycleD: cyclic npm deps - Cycles everywhere", (t) => {
});
});

test("AppCycleE: cyclic npm deps - Cycle via devDependency", (t) => {
test("AppCycleE: cyclic npm deps - Cycle via devDependency - include deduped", (t) => {
const applicationCycleEPath = path.join(cycleDepsBasePath, "application.cycle.e");
return npmTranslator.generateDependencyTree(applicationCycleEPath, {includeDeduped: true}).then((parsedTree) => {
t.deepEqual(parsedTree, applicationCycleETreeIncDeduped, "Parsed correctly");
});
});

test("AppCycleE: cyclic npm deps - Cycle via devDependency", (t) => {
const applicationCycleEPath = path.join(cycleDepsBasePath, "application.cycle.e");
return npmTranslator.generateDependencyTree(applicationCycleEPath, {includeDeduped: false}).then((parsedTree) => {
t.deepEqual(parsedTree, applicationCycleETree, "Parsed correctly");
});
});
Expand Down Expand Up @@ -394,6 +408,48 @@ const applicationCycleBTree = {
}
]
},
{
"id": "module.e",
"version": "1.0.0",
"path": path.join(cycleDepsBasePath, "module.e"),
"dependencies": [
{
"id": "module.d",
"version": "1.0.0",
"path": path.join(cycleDepsBasePath, "module.d"),
"dependencies": []
}
]
}
]
};

const applicationCycleBTreeIncDeduped = {
"id": "application.cycle.b",
"version": "1.0.0",
"path": path.join(cycleDepsBasePath, "application.cycle.b"),
"dependencies": [
{
"id": "module.d",
"version": "1.0.0",
"path": path.join(cycleDepsBasePath, "module.d"),
"dependencies": [
{
"id": "module.e",
"version": "1.0.0",
"path": path.join(cycleDepsBasePath, "module.e"),
"dependencies": [
{
"id": "module.d",
"version": "1.0.0",
"path": path.join(cycleDepsBasePath, "module.d"),
"dependencies": [],
"deduped": true
}
]
}
]
},
{
"id": "module.e",
"version": "1.0.0",
Expand Down Expand Up @@ -860,6 +916,40 @@ const applicationCycleDTree = {
};

const applicationCycleETree = {
"id": "application.cycle.e",
"version": "1.0.0",
"path": path.join(cycleDepsBasePath, "application.cycle.e"),
"dependencies": [
{
"id": "module.l",
"version": "1.0.0",
"path": path.join(cycleDepsBasePath, "module.l"),
"dependencies": [
{
"id": "module.m",
"version": "1.0.0",
"path": path.join(cycleDepsBasePath, "module.m"),
"dependencies": []
}
]
},
{
"id": "module.m",
"version": "1.0.0",
"path": path.join(cycleDepsBasePath, "module.m"),
"dependencies": [
{
"id": "module.l",
"version": "1.0.0",
"path": path.join(cycleDepsBasePath, "module.l"),
"dependencies": []
}
]
}
]
};

const applicationCycleETreeIncDeduped = {
"id": "application.cycle.e",
"version": "1.0.0",
"path": path.join(cycleDepsBasePath, "application.cycle.e"),
Expand Down

0 comments on commit 3481154

Please sign in to comment.