Skip to content

Commit

Permalink
fix: Do not write an empty file when using --json-file-output [CLI-11…
Browse files Browse the repository at this point in the history
…5] (#5030)
  • Loading branch information
PeterSchafer committed Feb 2, 2024
1 parent 4b8c991 commit fd91b7c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/cli/main.ts
Expand Up @@ -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(
Expand Down
5 changes: 5 additions & 0 deletions test/acceptance/workspaces/golang-gomodules/main.go
@@ -0,0 +1,5 @@
package main

func main() {
// nothing to see here
}
13 changes: 13 additions & 0 deletions test/jest/acceptance/cli-json-file-output.spec.ts
Expand Up @@ -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);
});
});

0 comments on commit fd91b7c

Please sign in to comment.