Skip to content

Commit 9b0259f

Browse files
authored
Merge pull request #5985 from snyk/fix/support-sarif-json-output-files-combination
fix: support using sarif and json output files at the same time
2 parents 4957db7 + 7f28efa commit 9b0259f

File tree

5 files changed

+90
-8
lines changed

5 files changed

+90
-8
lines changed

cliv2/cmd/cliv2/main.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -309,10 +309,22 @@ func defaultCmd(args []string) error {
309309
func runCodeTestCommand(cmd *cobra.Command, args []string) error {
310310
// ensure legacy behavior, where sarif and json can be used interchangeably
311311
globalConfiguration.AddAlternativeKeys(output_workflow.OUTPUT_CONFIG_KEY_SARIF, []string{output_workflow.OUTPUT_CONFIG_KEY_JSON})
312-
globalConfiguration.AddAlternativeKeys(output_workflow.OUTPUT_CONFIG_KEY_SARIF_FILE, []string{output_workflow.OUTPUT_CONFIG_KEY_JSON_FILE})
313312

314-
// ensure legacy behavior, where sarif files with no findings are not written
315-
globalConfiguration.Set(output_workflow.OUTPUT_CONFIG_WRITE_EMPTY_FILE, false)
313+
fileWriters := []output_workflow.FileWriter{
314+
{
315+
NameConfigKey: output_workflow.OUTPUT_CONFIG_KEY_SARIF_FILE,
316+
MimeType: output_workflow.SARIF_MIME_TYPE,
317+
TemplateFiles: output_workflow.ApplicationSarifTemplates,
318+
WriteEmptyContent: true,
319+
},
320+
{
321+
NameConfigKey: output_workflow.OUTPUT_CONFIG_KEY_JSON_FILE,
322+
MimeType: output_workflow.SARIF_MIME_TYPE,
323+
TemplateFiles: output_workflow.ApplicationSarifTemplates,
324+
WriteEmptyContent: false,
325+
},
326+
}
327+
globalConfiguration.Set(output_workflow.OUTPUT_CONFIG_KEY_FILE_WRITERS, fileWriters)
316328

317329
return runCommand(cmd, args)
318330
}

cliv2/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ require (
1717
github.com/snyk/cli-extension-sbom v0.0.0-20250422133603-a5ae6fdf0934
1818
github.com/snyk/container-cli v0.0.0-20250321132345-1e2e01681dd7
1919
github.com/snyk/error-catalog-golang-public v0.0.0-20250520155934-078275889e2c
20-
github.com/snyk/go-application-framework v0.0.0-20250709155813-f556bec6f4f4
20+
github.com/snyk/go-application-framework v0.0.0-20250711115946-e64056bc2173
2121
github.com/snyk/go-httpauth v0.0.0-20240307114523-1f5ea3f55c65
2222
github.com/snyk/snyk-iac-capture v0.6.5
2323
github.com/snyk/snyk-ls v0.0.0-20250708142519-32d15f8b765a

cliv2/go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -804,8 +804,8 @@ github.com/snyk/container-cli v0.0.0-20250321132345-1e2e01681dd7 h1:/2+2piwQtB9f
804804
github.com/snyk/container-cli v0.0.0-20250321132345-1e2e01681dd7/go.mod h1:38w+dcAQp9eG3P5t2eNS9eG0reut10AeJjLv5lJ5lpM=
805805
github.com/snyk/error-catalog-golang-public v0.0.0-20250520155934-078275889e2c h1:rXUCGepwK38Xn00MKwfJRd5ecQ7ylvkudoMFBycIJUk=
806806
github.com/snyk/error-catalog-golang-public v0.0.0-20250520155934-078275889e2c/go.mod h1:Ytttq7Pw4vOCu9NtRQaOeDU2dhBYUyNBe6kX4+nIIQ4=
807-
github.com/snyk/go-application-framework v0.0.0-20250709155813-f556bec6f4f4 h1:ImrcCrDMYcIbT3isQC/4irSG52pt/7dpBo9fOSH8WX4=
808-
github.com/snyk/go-application-framework v0.0.0-20250709155813-f556bec6f4f4/go.mod h1:4DSu9PL2hypUjZhrT+zaMh3H5stAerMVfUs5XP1ST8U=
807+
github.com/snyk/go-application-framework v0.0.0-20250711115946-e64056bc2173 h1:w8uZgvG3arvDPwWd5V3IF1+lU3gLf1sQtoMCFmW37c8=
808+
github.com/snyk/go-application-framework v0.0.0-20250711115946-e64056bc2173/go.mod h1:4DSu9PL2hypUjZhrT+zaMh3H5stAerMVfUs5XP1ST8U=
809809
github.com/snyk/go-httpauth v0.0.0-20240307114523-1f5ea3f55c65 h1:CEQuYv0Go6MEyRCD3YjLYM2u3Oxkx8GpCpFBd4rUTUk=
810810
github.com/snyk/go-httpauth v0.0.0-20240307114523-1f5ea3f55c65/go.mod h1:88KbbvGYlmLgee4OcQ19yr0bNpXpOr2kciOthaSzCAg=
811811
github.com/snyk/policy-engine v0.33.2 h1:ZxD6/RQ4vqUAXa64V72SsGjZ8vmnBgZNGYQxMIqctYo=

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@
4040
"build-cli:prod": "node --max-old-space-size=8192 node_modules/webpack/bin/webpack.js --config webpack.prod.ts",
4141
"watch": "npm run build-cli:dev -- --watch",
4242
"test": "npm run test:unit && npm run test:acceptance && npm run test:tap",
43-
"test:unit": "jest --runInBand --testPathPattern '/test(/jest)?/unit/' --reporters=jest-junit",
44-
"test:acceptance": "jest --maxWorkers=1 --testPathPattern \"/test(/jest)?/acceptance/\" --reporters=jest-junit",
43+
"test:unit": "jest --runInBand --testPathPattern '/test(/jest)?/unit/' --reporters=jest-junit --reporters=default",
44+
"test:acceptance": "jest --maxWorkers=1 --testPathPattern \"/test(/jest)?/acceptance/\" --reporters=jest-junit --reporters=default",
4545
"test:tap": "tap -Rspec --timeout=300 test/tap/*.test.* ",
4646
"test:smoke": "./scripts/run-smoke-tests-locally.sh",
4747
"dev": "ts-node ./src/cli/index.ts"

test/jest/acceptance/snyk-code/snyk-code-user-journey.spec.ts

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ const projectWithCodeIssues = resolve(
4141
'test/fixtures/sast/with_code_issues',
4242
);
4343
const emptyProject = resolve(projectRoot, 'test/fixtures/empty');
44+
const projectWithoutCodeIssues = resolve(
45+
projectRoot,
46+
'test/fixtures/sast-empty',
47+
);
4448

4549
// This method does some basic checks on the given sarif file
4650
function checkSarif(file: string, expectedIgnoredFindings: number): any {
@@ -374,6 +378,72 @@ describe('snyk code test', () => {
374378
}
375379
});
376380

381+
it('works with both --sarif-file-output and --json-file-output', async () => {
382+
const sarifFileName = 'sarifOutput.json';
383+
const jsonFileName = 'jsonOutput.json';
384+
const sarifFilePath = `${projectRoot}/${sarifFileName}`;
385+
const jsonFilePath = `${projectRoot}/${jsonFileName}`;
386+
const path = await ensureUniqueBundleIsUsed(projectWithCodeIssues);
387+
const { stderr, code } = await runSnykCLI(
388+
`code test ${path} --sarif-file-output=${sarifFilePath} --json-file-output=${jsonFilePath}`,
389+
{
390+
env: {
391+
...process.env,
392+
...integrationEnv,
393+
},
394+
},
395+
);
396+
397+
expect(stderr).toBe('');
398+
expect(code).toBe(EXIT_CODE_ACTION_NEEDED);
399+
400+
expect(existsSync(sarifFilePath)).toBe(true);
401+
expect(require(sarifFilePath)).toMatchSchema(sarifSchema);
402+
403+
expect(existsSync(jsonFilePath)).toBe(true);
404+
expect(require(jsonFilePath)).toMatchSchema(sarifSchema);
405+
406+
// cleanup file
407+
try {
408+
unlinkSync(sarifFilePath);
409+
unlinkSync(jsonFilePath);
410+
} catch (error) {
411+
console.error('failed to remove file.', error);
412+
}
413+
});
414+
415+
it('zero findings is handled differently with --sarif-file-output and --json-file-output', async () => {
416+
const sarifFileName = 'sarifOutput.json';
417+
const jsonFileName = 'jsonOutput.json';
418+
const sarifFilePath = `${projectRoot}/${sarifFileName}`;
419+
const jsonFilePath = `${projectRoot}/${jsonFileName}`;
420+
const path = await ensureUniqueBundleIsUsed(projectWithoutCodeIssues);
421+
const { stderr, code } = await runSnykCLI(
422+
`code test ${path} --sarif-file-output=${sarifFilePath} --json-file-output=${jsonFilePath}`,
423+
{
424+
env: {
425+
...process.env,
426+
...integrationEnv,
427+
},
428+
},
429+
);
430+
431+
expect(stderr).toBe('');
432+
expect(code).toBe(EXIT_CODE_SUCCESS);
433+
434+
expect(existsSync(sarifFilePath)).toBe(true);
435+
expect(require(sarifFilePath)).toMatchSchema(sarifSchema);
436+
437+
expect(existsSync(jsonFilePath)).toBe(false);
438+
439+
// cleanup file
440+
try {
441+
unlinkSync(sarifFilePath);
442+
} catch (error) {
443+
console.error('failed to remove file.', error);
444+
}
445+
});
446+
377447
it('works with human readable output', async () => {
378448
const path = await ensureUniqueBundleIsUsed(projectWithCodeIssues);
379449
const { stdout, stderr, code } = await runSnykCLI(

0 commit comments

Comments
 (0)