Skip to content

Commit

Permalink
Merge pull request #46 from snyk/feat/update-tests-and-types
Browse files Browse the repository at this point in the history
feat: update tests and correct type definition
  • Loading branch information
Konstantin Yegupov committed Dec 4, 2019
2 parents 9e42e65 + 2a7e541 commit fc9ff9a
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 29 deletions.
10 changes: 5 additions & 5 deletions README.md
Expand Up @@ -49,21 +49,21 @@ export interface DepGraph {
};
readonly rootPkg: {
name: string;
version: string | null;
version?: string;
};
// all unique packages in the graph (including root package)
getPkgs(): Array<{
name: string;
version: string | null;
version?: string;
}>;
// all unique packages in the graph, except the root package
getDepPkgs(): Array<{
name: string;
version: string | null;
version?: string;
}>;
pkgPathsToRoot(pkg: Pkg): Array<Array<{
name: string;
version: string | null;
version?: string;
}>>;
countPathsToRoot(pkg: Pkg): number;
toJSON(): DepGraphData;
Expand All @@ -89,7 +89,7 @@ export interface DepGraphData {
id: string;
info: {
name: string;
version: string | null;
version?: string;
};
}>;
graph: {
Expand Down
5 changes: 3 additions & 2 deletions src/core/create-from-json.ts
Expand Up @@ -22,8 +22,9 @@ export function createFromJSON(depGraphData: DepGraphData): DepGraph {
const pkgNodes: {[pkgId: string]: Set<string>} = {};

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

for (const node of depGraphData.graph.nodes) {
Expand Down
4 changes: 4 additions & 0 deletions src/core/dep-graph.ts
Expand Up @@ -176,6 +176,10 @@ class DepGraphImpl implements types.DepGraphInternal {
otherDepGraph = createFromJSON(other.toJSON()) as types.DepGraphInternal;
}

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

Expand Down
2 changes: 1 addition & 1 deletion src/legacy/index.ts
Expand Up @@ -45,7 +45,7 @@ function addLabel(dep: DepTreeDep, key: string, value: string) {
async function depTreeToGraph(depTree: DepTree, pkgManagerName: string): Promise<types.DepGraph> {
const rootPkg = {
name: depTree.name!,
version: depTree.version,
version: depTree.version || undefined,
};

const pkgManagerInfo: types.PkgManager = {
Expand Down
28 changes: 14 additions & 14 deletions test/core/create-from-json.test.ts
Expand Up @@ -17,26 +17,26 @@ describe('fromJSON simple', () => {
});

test('getPkgs()', () => {
expect(graph.getPkgs().sort(helpers.depSort)).toEqual([
helpers.expectSamePkgs(graph.getPkgs(), [
{ name: 'a', version: '1.0.0' },
{ name: 'b', version: '1.0.0' },
{ name: 'c', version: '1.0.0' },
{ name: 'd', version: '0.0.1' },
{ name: 'd', version: '0.0.2' },
{ name: 'e', version: '5.0.0' },
{ name: 'root', version: '0.0.0' },
].sort(helpers.depSort));
]);
});

test('getDepPkgs()', () => {
expect(graph.getDepPkgs().sort(helpers.depSort)).toEqual([
helpers.expectSamePkgs(graph.getDepPkgs(), [
{ name: 'a', version: '1.0.0' },
{ name: 'b', version: '1.0.0' },
{ name: 'c', version: '1.0.0' },
{ name: 'd', version: '0.0.1' },
{ name: 'd', version: '0.0.2' },
{ name: 'e', version: '5.0.0' },
].sort(helpers.depSort));
]);
});

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

const depGraph = depGraphLib.createFromJSON(graphJson);

expect(depGraph.getPkgs().sort()).toEqual([
helpers.expectSamePkgs(depGraph.getPkgs(), [
{ name: 'toor', version: '1.0.0' },
{ name: 'foo', version: '2' },
].sort());
expect(depGraph.getDepPkgs()).toEqual([
helpers.expectSamePkgs(depGraph.getDepPkgs(), [
{ name: 'foo', version: '2' },
]);

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

const depGraph = depGraphLib.createFromJSON(graphJson);

expect(depGraph.getPkgs().sort()).toEqual([
helpers.expectSamePkgs(depGraph.getPkgs(), [
{ name: 'toor', version: '1.0.0' },
{ name: 'foo', version: '2' },
{ name: 'bar', version: '3' },
{ name: 'baz', version: '4' },
]);
expect(depGraph.getDepPkgs().sort()).toEqual([
helpers.expectSamePkgs(depGraph.getDepPkgs(), [
{ name: 'foo', version: '2' },
{ name: 'bar', version: '3' },
{ name: 'baz', version: '4' },
Expand Down Expand Up @@ -659,11 +659,11 @@ test('fromJSON root has several instances', () => {
};

const depGraph = depGraphLib.createFromJSON(graphJson);
expect(depGraph.getPkgs().sort()).toEqual([
helpers.expectSamePkgs(depGraph.getPkgs(), [
{name: 'toor', version: '1.0.0'},
{name: 'foo', version: '2'},
].sort());
expect(depGraph.getDepPkgs().sort()).toEqual([
helpers.expectSamePkgs(depGraph.getDepPkgs(), [
{name: 'foo', version: '2'},
].sort());
expect(depGraph.countPathsToRoot({name: 'toor', version: '1.0.0'})).toBe(2);
Expand Down Expand Up @@ -768,12 +768,12 @@ test('fromJSON a pkg missing version field', () => {
};

const depGraph = depGraphLib.createFromJSON(graphJson as any);
expect(depGraph.getPkgs().sort()).toEqual([
helpers.expectSamePkgs(depGraph.getPkgs(), [
{ name: 'toor', version: '1.0.0' },
{ name: 'foo', version: null },
{ name: 'foo' },
]);
expect(depGraph.getDepPkgs().sort()).toEqual([
{ name: 'foo', version: null },
helpers.expectSamePkgs(depGraph.getDepPkgs(), [
{ name: 'foo' },
]);
});

Expand Down
7 changes: 6 additions & 1 deletion test/helpers.ts
@@ -1,12 +1,13 @@
import * as _ from 'lodash';
import * as fs from 'fs';
import * as path from 'path';
import { PkgInfo } from '../src';

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

export function depSort(a: any, b: any) {
function depSort(a: any, b: any) {
if (a.name < b.name) {
return -1;
} else if (a.name > b.name) {
Expand All @@ -20,6 +21,10 @@ export function depSort(a: any, b: any) {
return 0;
}

export function expectSamePkgs(actual: PkgInfo[], expected: PkgInfo[]) {
return expect(actual.sort(depSort)).toEqual(expected.sort(depSort));
}

export function depTreesEqual(a: any, b: any) {
if (a.name !== b.name || a.version !== b.version) {
return false;
Expand Down
12 changes: 6 additions & 6 deletions test/legacy/from-dep-tree.test.ts
Expand Up @@ -29,23 +29,23 @@ describe('depTreeToGraph simple dysmorphic', () => {
});

test('getPkgs', async () => {
expect(depGraph.getPkgs().sort(helpers.depSort)).toEqual([
helpers.expectSamePkgs(depGraph.getPkgs(), [
{ name: 'a', version: '1.0.0' },
{ name: 'b', version: '1.0.0' },
{ name: 'c', version: '1.0.0' },
{ name: 'd', version: '0.0.1' },
{ name: 'd', version: '0.0.2' },
{ name: 'e', version: '5.0.0' },
{ name: 'root', version: '0.0.0' },
].sort(helpers.depSort));
expect(depGraph.getDepPkgs().sort(helpers.depSort)).toEqual([
]);
helpers.expectSamePkgs(depGraph.getDepPkgs(), [
{ name: 'a', version: '1.0.0' },
{ name: 'b', version: '1.0.0' },
{ name: 'c', version: '1.0.0' },
{ name: 'd', version: '0.0.1' },
{ name: 'd', version: '0.0.2' },
{ name: 'e', version: '5.0.0' },
].sort(helpers.depSort));
]);
});

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

expect(restoredGraph.getPkgs().sort()).toEqual(depGraph.getPkgs().sort());
expect(restoredGraph.getDepPkgs().sort()).toEqual(depGraph.getDepPkgs().sort());
helpers.expectSamePkgs(restoredGraph.getPkgs(), depGraph.getPkgs());
helpers.expectSamePkgs(restoredGraph.getDepPkgs(), depGraph.getDepPkgs());
});
});

Expand Down

0 comments on commit fc9ff9a

Please sign in to comment.