From fd91b7c5581b9024fd8e4a260e9a6e3cd7c9be42 Mon Sep 17 00:00:00 2001 From: PeterSchafer <101886095+PeterSchafer@users.noreply.github.com> Date: Fri, 2 Feb 2024 14:35:12 +0100 Subject: [PATCH] fix: Do not write an empty file when using --json-file-output [CLI-115] (#5030) --- src/cli/main.ts | 2 +- test/acceptance/workspaces/golang-gomodules/main.go | 5 +++++ test/jest/acceptance/cli-json-file-output.spec.ts | 13 +++++++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 test/acceptance/workspaces/golang-gomodules/main.go diff --git a/src/cli/main.ts b/src/cli/main.ts index 615f6a2b725..9c9032d8acd 100755 --- a/src/cli/main.ts +++ b/src/cli/main.ts @@ -459,7 +459,7 @@ async function saveResultsToFile( ) { const flag = `${outputType}-file-output`; const outputFile = options[flag]; - if (outputFile && (jsonResults || jsonPayload)) { + if (outputFile && (jsonResults || !isEmpty(jsonPayload))) { const outputFileStr = outputFile as string; const fullOutputFilePath = getFullPath(outputFileStr); await saveJsonResultsToFile( diff --git a/test/acceptance/workspaces/golang-gomodules/main.go b/test/acceptance/workspaces/golang-gomodules/main.go new file mode 100644 index 00000000000..77d1c0a40a4 --- /dev/null +++ b/test/acceptance/workspaces/golang-gomodules/main.go @@ -0,0 +1,5 @@ +package main + +func main() { + // nothing to see here +} diff --git a/test/jest/acceptance/cli-json-file-output.spec.ts b/test/jest/acceptance/cli-json-file-output.spec.ts index 02efd843b57..5e611d96350 100644 --- a/test/jest/acceptance/cli-json-file-output.spec.ts +++ b/test/jest/acceptance/cli-json-file-output.spec.ts @@ -146,4 +146,17 @@ describe('test --json-file-output', () => { }); expect(fileSize).toBeGreaterThan(500000000); // ~0.5GB }, 120000); + + it('test --json-file-ouput does not write an empty file if no issues are found', async () => { + const project = await createProjectFromWorkspace('golang-gomodules'); + const outputFilename = project.path() + '/shouldnt_be_there.json'; + + const { code } = await runSnykCLI( + `code test --json-file-output=${outputFilename} ${project.path()}`, + ); + + const fileExists = fs.existsSync(outputFilename); + expect(fileExists).toBeFalsy(); + expect(code).toEqual(0); + }); });