Skip to content

Commit fc9ff9a

Browse files
author
Konstantin Yegupov
authored
Merge pull request #46 from snyk/feat/update-tests-and-types
feat: update tests and correct type definition
2 parents 9e42e65 + 2a7e541 commit fc9ff9a

File tree

7 files changed

+39
-29
lines changed

7 files changed

+39
-29
lines changed

README.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -49,21 +49,21 @@ export interface DepGraph {
4949
};
5050
readonly rootPkg: {
5151
name: string;
52-
version: string | null;
52+
version?: string;
5353
};
5454
// all unique packages in the graph (including root package)
5555
getPkgs(): Array<{
5656
name: string;
57-
version: string | null;
57+
version?: string;
5858
}>;
5959
// all unique packages in the graph, except the root package
6060
getDepPkgs(): Array<{
6161
name: string;
62-
version: string | null;
62+
version?: string;
6363
}>;
6464
pkgPathsToRoot(pkg: Pkg): Array<Array<{
6565
name: string;
66-
version: string | null;
66+
version?: string;
6767
}>>;
6868
countPathsToRoot(pkg: Pkg): number;
6969
toJSON(): DepGraphData;
@@ -89,7 +89,7 @@ export interface DepGraphData {
8989
id: string;
9090
info: {
9191
name: string;
92-
version: string | null;
92+
version?: string;
9393
};
9494
}>;
9595
graph: {

src/core/create-from-json.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ export function createFromJSON(depGraphData: DepGraphData): DepGraph {
2222
const pkgNodes: {[pkgId: string]: Set<string>} = {};
2323

2424
for (const { id, info } of depGraphData.pkgs) {
25-
// TODO: avoid this, instead just use `info` as is
26-
pkgs[id] = info.version ? info : { ...info, version: null } as any;
25+
pkgs[id] = info.version
26+
? info
27+
: { ...info, version: undefined };
2728
}
2829

2930
for (const node of depGraphData.graph.nodes) {

src/core/dep-graph.ts

+4
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,10 @@ class DepGraphImpl implements types.DepGraphInternal {
176176
otherDepGraph = createFromJSON(other.toJSON()) as types.DepGraphInternal;
177177
}
178178

179+
// In theory, for the graphs created by standard means, `_.isEquals(this._data, otherDepGraph._data)`
180+
// should suffice, since node IDs will be generated in a predictable way.
181+
// However, there might be different versions of graph and inconsistencies
182+
// in the ordering of the arrays, so we perform a deep comparison.
179183
return this.nodeEquals(this, this.rootNodeId, otherDepGraph, otherDepGraph.rootNodeId, compareRoot);
180184
}
181185

src/legacy/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ function addLabel(dep: DepTreeDep, key: string, value: string) {
4545
async function depTreeToGraph(depTree: DepTree, pkgManagerName: string): Promise<types.DepGraph> {
4646
const rootPkg = {
4747
name: depTree.name!,
48-
version: depTree.version,
48+
version: depTree.version || undefined,
4949
};
5050

5151
const pkgManagerInfo: types.PkgManager = {

test/core/create-from-json.test.ts

+14-14
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,26 @@ describe('fromJSON simple', () => {
1717
});
1818

1919
test('getPkgs()', () => {
20-
expect(graph.getPkgs().sort(helpers.depSort)).toEqual([
20+
helpers.expectSamePkgs(graph.getPkgs(), [
2121
{ name: 'a', version: '1.0.0' },
2222
{ name: 'b', version: '1.0.0' },
2323
{ name: 'c', version: '1.0.0' },
2424
{ name: 'd', version: '0.0.1' },
2525
{ name: 'd', version: '0.0.2' },
2626
{ name: 'e', version: '5.0.0' },
2727
{ name: 'root', version: '0.0.0' },
28-
].sort(helpers.depSort));
28+
]);
2929
});
3030

3131
test('getDepPkgs()', () => {
32-
expect(graph.getDepPkgs().sort(helpers.depSort)).toEqual([
32+
helpers.expectSamePkgs(graph.getDepPkgs(), [
3333
{ name: 'a', version: '1.0.0' },
3434
{ name: 'b', version: '1.0.0' },
3535
{ name: 'c', version: '1.0.0' },
3636
{ name: 'd', version: '0.0.1' },
3737
{ name: 'd', version: '0.0.2' },
3838
{ name: 'e', version: '5.0.0' },
39-
].sort(helpers.depSort));
39+
]);
4040
});
4141

4242
test('getPathsToRoot', () => {
@@ -151,11 +151,11 @@ test('fromJSON a pkg and a node share same id', () => {
151151

152152
const depGraph = depGraphLib.createFromJSON(graphJson);
153153

154-
expect(depGraph.getPkgs().sort()).toEqual([
154+
helpers.expectSamePkgs(depGraph.getPkgs(), [
155155
{ name: 'toor', version: '1.0.0' },
156156
{ name: 'foo', version: '2' },
157157
].sort());
158-
expect(depGraph.getDepPkgs()).toEqual([
158+
helpers.expectSamePkgs(depGraph.getDepPkgs(), [
159159
{ name: 'foo', version: '2' },
160160
]);
161161

@@ -363,13 +363,13 @@ test('fromJSON with a cycle', () => {
363363

364364
const depGraph = depGraphLib.createFromJSON(graphJson);
365365

366-
expect(depGraph.getPkgs().sort()).toEqual([
366+
helpers.expectSamePkgs(depGraph.getPkgs(), [
367367
{ name: 'toor', version: '1.0.0' },
368368
{ name: 'foo', version: '2' },
369369
{ name: 'bar', version: '3' },
370370
{ name: 'baz', version: '4' },
371371
]);
372-
expect(depGraph.getDepPkgs().sort()).toEqual([
372+
helpers.expectSamePkgs(depGraph.getDepPkgs(), [
373373
{ name: 'foo', version: '2' },
374374
{ name: 'bar', version: '3' },
375375
{ name: 'baz', version: '4' },
@@ -659,11 +659,11 @@ test('fromJSON root has several instances', () => {
659659
};
660660

661661
const depGraph = depGraphLib.createFromJSON(graphJson);
662-
expect(depGraph.getPkgs().sort()).toEqual([
662+
helpers.expectSamePkgs(depGraph.getPkgs(), [
663663
{name: 'toor', version: '1.0.0'},
664664
{name: 'foo', version: '2'},
665665
].sort());
666-
expect(depGraph.getDepPkgs().sort()).toEqual([
666+
helpers.expectSamePkgs(depGraph.getDepPkgs(), [
667667
{name: 'foo', version: '2'},
668668
].sort());
669669
expect(depGraph.countPathsToRoot({name: 'toor', version: '1.0.0'})).toBe(2);
@@ -768,12 +768,12 @@ test('fromJSON a pkg missing version field', () => {
768768
};
769769

770770
const depGraph = depGraphLib.createFromJSON(graphJson as any);
771-
expect(depGraph.getPkgs().sort()).toEqual([
771+
helpers.expectSamePkgs(depGraph.getPkgs(), [
772772
{ name: 'toor', version: '1.0.0' },
773-
{ name: 'foo', version: null },
773+
{ name: 'foo' },
774774
]);
775-
expect(depGraph.getDepPkgs().sort()).toEqual([
776-
{ name: 'foo', version: null },
775+
helpers.expectSamePkgs(depGraph.getDepPkgs(), [
776+
{ name: 'foo' },
777777
]);
778778
});
779779

test/helpers.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import * as _ from 'lodash';
22
import * as fs from 'fs';
33
import * as path from 'path';
4+
import { PkgInfo } from '../src';
45

56
export function loadFixture(name: string) {
67
return JSON.parse(fs.readFileSync(path.join(__dirname, `fixtures/${name}`), 'utf8'));
78
}
89

9-
export function depSort(a: any, b: any) {
10+
function depSort(a: any, b: any) {
1011
if (a.name < b.name) {
1112
return -1;
1213
} else if (a.name > b.name) {
@@ -20,6 +21,10 @@ export function depSort(a: any, b: any) {
2021
return 0;
2122
}
2223

24+
export function expectSamePkgs(actual: PkgInfo[], expected: PkgInfo[]) {
25+
return expect(actual.sort(depSort)).toEqual(expected.sort(depSort));
26+
}
27+
2328
export function depTreesEqual(a: any, b: any) {
2429
if (a.name !== b.name || a.version !== b.version) {
2530
return false;

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

+6-6
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,23 @@ describe('depTreeToGraph simple dysmorphic', () => {
2929
});
3030

3131
test('getPkgs', async () => {
32-
expect(depGraph.getPkgs().sort(helpers.depSort)).toEqual([
32+
helpers.expectSamePkgs(depGraph.getPkgs(), [
3333
{ name: 'a', version: '1.0.0' },
3434
{ name: 'b', version: '1.0.0' },
3535
{ name: 'c', version: '1.0.0' },
3636
{ name: 'd', version: '0.0.1' },
3737
{ name: 'd', version: '0.0.2' },
3838
{ name: 'e', version: '5.0.0' },
3939
{ name: 'root', version: '0.0.0' },
40-
].sort(helpers.depSort));
41-
expect(depGraph.getDepPkgs().sort(helpers.depSort)).toEqual([
40+
]);
41+
helpers.expectSamePkgs(depGraph.getDepPkgs(), [
4242
{ name: 'a', version: '1.0.0' },
4343
{ name: 'b', version: '1.0.0' },
4444
{ name: 'c', version: '1.0.0' },
4545
{ name: 'd', version: '0.0.1' },
4646
{ name: 'd', version: '0.0.2' },
4747
{ name: 'e', version: '5.0.0' },
48-
].sort(helpers.depSort));
48+
]);
4949
});
5050

5151
test('getPathsToRoot', async () => {
@@ -228,8 +228,8 @@ describe('depTreeToGraph with funky pipes in the version', () => {
228228
const graphJson = depGraph.toJSON();
229229
const restoredGraph = await depGraphLib.createFromJSON(graphJson);
230230

231-
expect(restoredGraph.getPkgs().sort()).toEqual(depGraph.getPkgs().sort());
232-
expect(restoredGraph.getDepPkgs().sort()).toEqual(depGraph.getDepPkgs().sort());
231+
helpers.expectSamePkgs(restoredGraph.getPkgs(), depGraph.getPkgs());
232+
helpers.expectSamePkgs(restoredGraph.getDepPkgs(), depGraph.getDepPkgs());
233233
});
234234
});
235235

0 commit comments

Comments
 (0)