Skip to content

Commit

Permalink
test: fix comparisons of PkgInfo arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
Konstantin Yegupov committed Dec 4, 2019
1 parent 3c133ff commit 2a7e541
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 23 deletions.
4 changes: 3 additions & 1 deletion src/core/create-from-json.ts
Expand Up @@ -22,7 +22,9 @@ export function createFromJSON(depGraphData: DepGraphData): DepGraph {
const pkgNodes: {[pkgId: string]: Set<string>} = {};

for (const { id, info } of depGraphData.pkgs) {
pkgs[id] = info;
pkgs[id] = info.version
? info
: { ...info, version: undefined };
}

for (const node of depGraphData.graph.nodes) {
Expand Down
4 changes: 2 additions & 2 deletions src/core/dep-graph.ts
Expand Up @@ -163,8 +163,8 @@ class DepGraphImpl implements 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, to support unconventional node IDs, we run our own deep
// comparison.
// 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
24 changes: 12 additions & 12 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 @@ -140,11 +140,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 @@ -352,13 +352,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 @@ -648,11 +648,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 @@ -757,11 +757,11 @@ 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' },
]);
expect(depGraph.getDepPkgs().sort()).toEqual([
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 2a7e541

Please sign in to comment.