Skip to content

Commit

Permalink
feat: getPkgNodes(pkg) to get package nodes data
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-go committed Nov 27, 2019
1 parent 7a1a4f0 commit 0786b06
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 15 deletions.
25 changes: 20 additions & 5 deletions src/core/dep-graph.ts
Expand Up @@ -7,7 +7,7 @@ export {
DepGraphImpl,
};

interface Node {
interface GraphNode {
pkgId: string;
info?: types.NodeInfo;
}
Expand Down Expand Up @@ -48,7 +48,7 @@ class DepGraphImpl implements types.DepGraphInternal {
this._pkgManager = pkgManager;

this._rootNodeId = rootNodeId;
this._rootPkgId = (graph.node(rootNodeId) as Node).pkgId;
this._rootPkgId = (graph.node(rootNodeId) as GraphNode).pkgId;

this._pkgList = _.values(pkgs);
this._depPkgsList = this._pkgList
Expand All @@ -75,6 +75,21 @@ class DepGraphImpl implements types.DepGraphInternal {
return this._depPkgsList;
}

public getPkgNodes(pkg: types.Pkg): types.Node[] {
const pkgId = DepGraphImpl.getPkgId(pkg);

const nodes: types.Node[] = [];
for (const nodeId of Array.from(this._pkgNodes[pkgId])) {
const graphNode = this.getGraphNode(nodeId);

nodes.push({
info: graphNode.info || {},
});
}

return nodes;
}

public getNode(nodeId: string): types.NodeInfo {
return this.getGraphNode(nodeId).info || {};
}
Expand Down Expand Up @@ -171,7 +186,7 @@ class DepGraphImpl implements types.DepGraphInternal {
const deps = (this._graph.successors(nodeId) || [])
.map((depNodeId) => ({ nodeId: depNodeId }));

const node = this._graph.node(nodeId) as Node;
const node = this._graph.node(nodeId) as GraphNode;
const elem: types.GraphNode = {
nodeId,
pkgId: node.pkgId,
Expand Down Expand Up @@ -265,8 +280,8 @@ class DepGraphImpl implements types.DepGraphInternal {
return true;
}

private getGraphNode(nodeId: string): Node {
const node = this._graph.node(nodeId) as Node;
private getGraphNode(nodeId: string): GraphNode {
const node = this._graph.node(nodeId) as GraphNode;
if (!node) {
throw new Error(`no such node: ${nodeId}`);
}
Expand Down
7 changes: 7 additions & 0 deletions src/core/types.ts
Expand Up @@ -48,6 +48,12 @@ export interface NodeInfo {
};
}

export interface Node {
// id: string; - can be added later once it's useful as input to other methods
// pkg: PkgInfo; - it can be nice to return this, consider when exposing more node related methods
info: NodeInfo;
}

export interface GraphNode {
nodeId: string;
pkgId: string;
Expand Down Expand Up @@ -85,6 +91,7 @@ export interface DepGraph {
readonly rootPkg: PkgInfo;
getPkgs(): PkgInfo[];
getDepPkgs(): PkgInfo[];
getPkgNodes(pkg: Pkg): Node[];
toJSON(): DepGraphData;
pkgPathsToRoot(pkg: Pkg): PkgInfo[][];
countPathsToRoot(pkg: Pkg): number;
Expand Down
11 changes: 11 additions & 0 deletions test/core/create-from-json.test.ts
Expand Up @@ -69,6 +69,17 @@ describe('fromJSON simple', () => {
],
]);
});

test('getPkgNodes', () => {
expect(graph.getPkgNodes({ name: 'root', version: '0.0.0' })).toHaveLength(1);
expect(graph.getPkgNodes({ name: 'a', version: '1.0.0' })).toHaveLength(1);

const cNodes = graph.getPkgNodes({ name: 'c', version: '1.0.0' });
expect(cNodes).toHaveLength(2);
expect(cNodes[0].info).toEqual(cNodes[1].info);

expect(() => graph.getPkgNodes({ name: 'no-such-pkg', version: '1.3.7'})).toThrow();
});
});

test('fromJSON with pkgManager.repositories', () => {
Expand Down
6 changes: 3 additions & 3 deletions test/legacy/__snapshots__/from-dep-tree.test.ts.snap
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`snapshots with labels 1`] = `
exports[`with labels matches snapshot 1`] = `
Object {
"graph": Object {
"nodes": Array [
Expand Down Expand Up @@ -144,7 +144,7 @@ Object {
}
`;

exports[`snapshots with versionProvenance 1`] = `
exports[`with versionProvenance matches snapshot 1`] = `
Object {
"graph": Object {
"nodes": Array [
Expand Down Expand Up @@ -663,7 +663,7 @@ Object {
}
`;

exports[`snapshots without versionProvenance 1`] = `
exports[`without versionProvenance matches snapshot 1`] = `
Object {
"graph": Object {
"nodes": Array [
Expand Down
53 changes: 46 additions & 7 deletions test/legacy/from-dep-tree.test.ts
Expand Up @@ -323,22 +323,61 @@ describe('depTreeToGraph with (invalid) null dependency', () => {
});
});

describe('snapshots', () => {
test('with versionProvenance', async () => {
describe('with versionProvenance', () => {
let depGraph: types.DepGraph;

beforeAll(async () => {
const depTree = helpers.loadFixture('maven-dep-tree.json');
const depGraph = await depGraphLib.legacy.depTreeToGraph(depTree, 'maven');
depGraph = await depGraphLib.legacy.depTreeToGraph(depTree, 'maven');
});

it('matches snapshot', () => {
expect(depGraph.toJSON()).toMatchSnapshot();
});

test('without versionProvenance', async () => {
it ('getPkgNodes() returns versionProvenance', () => {
const commonsIoNodes = depGraph.getPkgNodes({name: 'commons-io:commons-io', version: '2.2'});
expect(commonsIoNodes[0].info.versionProvenance!.type).toEqual('dependencyManagement');

const ognlNodes = depGraph.getPkgNodes({name: 'ognl:ognl', version: '3.0.6'});
expect(ognlNodes[0].info.versionProvenance).toEqual({
type: 'property',
property: { name: 'ognl.version' },
// tslint:disable:max-line-length
location: 'https://maven-central.storage-download.googleapis.com/repos/central/data/org/apache/struts/struts2-parent/2.3.20/struts2-parent-2.3.20.pom',
});
});
});

describe('without versionProvenance', () => {
let depGraph: types.DepGraph;

beforeAll(async () => {
const depTree = helpers.loadFixture('goof-dep-tree.json');
const depGraph = await depGraphLib.legacy.depTreeToGraph(depTree, 'npm');
depGraph = await depGraphLib.legacy.depTreeToGraph(depTree, 'npm');
});

it('matches snapshot', () => {
expect(depGraph.toJSON()).toMatchSnapshot();
});
});

describe('with labels', () => {
let depGraph: types.DepGraph;

test('with labels', async () => {
beforeAll(async () => {
const depTree = helpers.loadFixture('labelled-dep-tree.json');
const depGraph = await depGraphLib.legacy.depTreeToGraph(depTree, 'maven');
depGraph = await depGraphLib.legacy.depTreeToGraph(depTree, 'maven');
});

it('matches snapshot', () => {
expect(depGraph.toJSON()).toMatchSnapshot();
});

it('getPkgNodes() returns labels', () => {
let dNodes = depGraph.getPkgNodes({name: 'd', version: '2.0.0'});
dNodes = _.sortBy(dNodes, 'id');
expect(dNodes[0].info.labels).toEqual({key: 'value1'});
expect(dNodes[1].info.labels).toEqual({key: 'value2'});
});
});

0 comments on commit 0786b06

Please sign in to comment.