Skip to content

Commit dd4f30a

Browse files
authored
Merge pull request #13 from snyk/fix/dep-tree-with-null-dep
fix: depTreeToGraph - skip invalid null dependencies
2 parents f94c5c3 + 4a77dc6 commit dd4f30a

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

src/legacy/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,9 @@ async function buildGraph(
6161
const hash = crypto.createHash('sha1');
6262

6363
const deps = depTree.dependencies || {};
64-
const depNames = _.keys(deps).sort();
65-
for (const depName of depNames) {
64+
// filter-out invalid null deps (shouldn't happen - but did...)
65+
const depNames = _.keys(deps).filter((d) => !!deps[d]);
66+
for (const depName of depNames.sort()) {
6667
const dep = deps[depName];
6768

6869
const subtreeHash = await buildGraph(builder, dep, depName);

test/legacy/from-dep-tree.test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,3 +289,29 @@ describe('depTreeToGraph cycle with root', () => {
289289
expect(restoredGraph.getPkgs().sort()).toEqual(depGraph.getPkgs().sort());
290290
});
291291
});
292+
293+
describe('depTreeToGraph with (invalid) null dependency', () => {
294+
const depTree = {
295+
name: 'pine',
296+
version: '4',
297+
dependencies: {
298+
foo: {
299+
version: '1',
300+
},
301+
bar: null,
302+
baz: {
303+
version: '3',
304+
},
305+
},
306+
};
307+
308+
let depGraph: types.DepGraph;
309+
test('create', async () => {
310+
depGraph = await depGraphLib.legacy.depTreeToGraph(depTree, 'composer');
311+
expect(_.sortBy(depGraph.getPkgs(), 'name')).toEqual(_.sortBy([
312+
{name: 'pine', version: '4'},
313+
{name: 'foo', version: '1'},
314+
{name: 'baz', version: '3'},
315+
], 'name'));
316+
});
317+
});

0 commit comments

Comments
 (0)