Skip to content

Commit

Permalink
feat: add option "projectName" flag to customize root node and packag…
Browse files Browse the repository at this point in the history
…e name

when we used deptree, one could easily override the dep-tree's name with updating the name property.
in dep-graph this is not possible, so we need a way to override this.
  • Loading branch information
admons committed Aug 31, 2021
1 parent 95d8f07 commit 7e8e71f
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 12 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,5 @@ venv/

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

.DS_Store
5 changes: 4 additions & 1 deletion lib/dependencies/build-dep-graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ type Dependencies = {
};

export function buildDepGraph(
partialDepTree: PartialDepTree
partialDepTree: PartialDepTree,
projectName?: string
): Promise<DepGraph> {
const packageToDepTreeMap = new Map<PackageName, PartialDepTree>();

Expand Down Expand Up @@ -46,5 +47,7 @@ export function buildDepGraph(
dependencies[key] = packageToDepTreeMap.get(key);
});

if (projectName) partialDepTree.name = projectName;

return depTreeToGraph(partialDepTree as DepTree, 'pip');
}
4 changes: 3 additions & 1 deletion lib/dependencies/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface PythonInspectOptions {
command?: string; // `python` command override
allowMissing?: boolean; // Allow skipping packages that are not found in the environment.
args?: string[];
projectName?: string; // Allow providing a project name for the root node and package
}

type Options = api.SingleSubprojectInspectOptions & PythonInspectOptions;
Expand Down Expand Up @@ -54,7 +55,8 @@ export async function getDependencies(
targetFile,
options.allowMissing || false,
includeDevDeps,
options.args
options.args,
options.projectName
),
]);
return { plugin, dependencyGraph };
Expand Down
5 changes: 3 additions & 2 deletions lib/dependencies/inspect-implementation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ export async function inspectInstalledDeps(
targetFile: string,
allowMissing: boolean,
includeDevDeps: boolean,
args?: string[]
args?: string[],
projectName?: string
): Promise<DepGraph> {
const tempDirObj = tmp.dirSync({
unsafeCleanup: true,
Expand Down Expand Up @@ -147,7 +148,7 @@ export async function inspectInstalledDeps(
);

const result = JSON.parse(output) as PartialDepTree;
return buildDepGraph(result);
return buildDepGraph(result, projectName);
} catch (error) {
if (typeof error === 'string') {
const emptyManifestMsg = 'No dependencies detected in manifest.';
Expand Down
39 changes: 31 additions & 8 deletions test/system/inspect.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import * as subProcess from '../../lib/dependencies/sub-process';
import { SpawnSyncReturns } from 'child_process';
import * as depGraphLib from '@snyk/dep-graph';
import * as fs from 'fs';
import * as path from 'path';

// TODO: jestify tap tests in ./inspect.test.js here
describe('inspect', () => {
Expand Down Expand Up @@ -111,12 +112,12 @@ describe('inspect', () => {
describe('dep-graph', () => {
const mockedExecuteSync = jest.spyOn(subProcess, 'executeSync');
const mockedExecute = jest.spyOn(subProcess, 'execute');
const expectedDepGraphPath = path.resolve(
__dirname,
'../fixtures/dence-dep-graph/expected.json'
);

afterEach(() => {
mockedExecuteSync.mockClear();
mockedExecute.mockClear();
});
it('should return dep graph for very dence input', async () => {
beforeEach(() => {
mockedExecuteSync.mockReturnValueOnce({ status: 0 } as SpawnSyncReturns<
Buffer
>);
Expand All @@ -127,11 +128,33 @@ describe('inspect', () => {
'utf8'
)
);
const dirname = 'test/fixtures/pipenv-project';
const manifestFilePath = `${dirname}/Pipfile`;
});

afterEach(() => {
mockedExecuteSync.mockClear();
mockedExecute.mockClear();
});

it('should return dep graph for very dence input', async () => {
const manifestFilePath = `test/fixtures/pipenv-project/Pipfile`;
const result = await inspect('.', manifestFilePath);

const expectedDepGraphData = require('../fixtures/dence-dep-graph/expected.json');
const expectedDepGraphData = require(expectedDepGraphPath);
const expectedDepGraph = depGraphLib.createFromJSON(expectedDepGraphData);

expect(result.dependencyGraph).toEqualDepGraph(expectedDepGraph);
});

it('projectName option should set the dep graph root node name', async () => {
const manifestFilePath = `test/fixtures/pipenv-project/Pipfile`;
const projectName = `${Date.now()}`;
const result = await inspect('.', manifestFilePath, { projectName });

const expectedDepGraphData = JSON.parse(
fs
.readFileSync(expectedDepGraphPath, 'utf8')
.replace(/pip--62-01iNczXpgA9L/g, projectName)
);
const expectedDepGraph = depGraphLib.createFromJSON(expectedDepGraphData);

expect(result.dependencyGraph).toEqualDepGraph(expectedDepGraph);
Expand Down

0 comments on commit 7e8e71f

Please sign in to comment.