Skip to content

Commit

Permalink
feat: dep-graph json output file
Browse files Browse the repository at this point in the history
When both `--print-deps` and `--json-file-output` are being used
produce a depGraph object in the resulting JSON file.

Does not attempt to fix any existing `--json` output formats to reduce
the potential of breaking changes.

This allows CLI users to see the dependency graph that was resolved
by the plugin code and scanned.
  • Loading branch information
gitphill committed Dec 9, 2024
1 parent 564c860 commit 704e838
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/lib/snyk-test/legacy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ export interface LegacyVulnApiResult extends BasicResultData {
filesystemPolicy?: boolean;
uniqueCount?: any;
remediation?: RemediationChanges;
depGraph?: depGraphLib.DepGraphData;
}

export interface BaseImageRemediation {
Expand Down Expand Up @@ -452,6 +453,10 @@ function convertTestDepGraphResultToLegacy(
remediation: result.remediation,
};

if (options['print-deps'] && options['json-file-output']) {
legacyRes.depGraph = depGraph.toJSON();
}

return legacyRes;
}

Expand Down
42 changes: 42 additions & 0 deletions test/jest/acceptance/cli-json-file-output.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { createProjectFromWorkspace } from '../util/createProject';
import { runSnykCLI } from '../util/runSnykCLI';
import { humanFileSize } from '../../utils';
import { getServerPort } from '../util/getServerPort';
import * as depGraphLib from '@snyk/dep-graph';

jest.setTimeout(1000 * 60);

Expand Down Expand Up @@ -112,4 +113,45 @@ describe('test --json-file-output', () => {
expect(fileExists).toBeFalsy();
expect(code).toEqual(0);
});

describe('print-deps and json-file-output', () => {
it.only('saves JSON output to file with depGraph when --print-deps and --json-file-output are being used', async () => {
const project = await createProjectFromWorkspace('maven-app');
const outputPath = 'json-file-output.json';

const { code } = await runSnykCLI(
`test --print-deps --json-file-output=${outputPath}`,
{
cwd: project.path(),
env,
},
);

expect(code).toEqual(0);
const json = await project.readJSON(outputPath);
expect(json.depGraph).toBeTruthy();
const depGraph = depGraphLib.createFromJSON(json.depGraph);
expect(depGraph.getPkgs()).toContainEqual({
name: 'axis:axis',
version: '1.4',
});
});

it('saves JSON output to file without a depGraph when --print-deps is not used', async () => {
const project = await createProjectFromWorkspace('maven-app');
const outputPath = 'json-file-output.json';

const { code } = await runSnykCLI(
`test --json-file-output=${outputPath}`,
{
cwd: project.path(),
env,
},
);

expect(code).toEqual(0);
const json = await project.readJSON(outputPath);
expect(json.depGraph).toBeUndefined();
});
});
});

0 comments on commit 704e838

Please sign in to comment.