Skip to content

Commit

Permalink
fix: Default output txt and allow JSON output
Browse files Browse the repository at this point in the history
The default output was JSON. However, to follow the snyk CLI standart
the defualt output has to be text and allow the option to JSON output.
  • Loading branch information
Arthur Granado committed Nov 8, 2019
1 parent ae51f28 commit 9683765
Show file tree
Hide file tree
Showing 5 changed files with 278 additions and 42 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
node_modules
dist
.eslintcache
bin
18 changes: 18 additions & 0 deletions src/__tests__/test-cli-args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,21 @@ test("handles debug flag", () => {
expect(parsedArgs.inputDirectory).toBe(".");
expect(parsedArgs.debug).toBe(true);
});

describe("Handle json flag", () => {
test('option --json', () => {
const inputArgs = [".", "--json"];

const parsedArgs: IArgs = parseInputParameters(inputArgs);

expect(parsedArgs.json).toBeTruthy();
});

test('option -j', () => {
const inputArgs = [".", "-j"];

const parsedArgs: IArgs = parseInputParameters(inputArgs);

expect(parsedArgs.json).toBeTruthy();
});
});
208 changes: 208 additions & 0 deletions src/__tests__/test-main-runCommand.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { IExecCommandResult } from "../main";
import { ExecException } from "child_process";
import fs from "fs";
import { async } from "q";

beforeEach(() => {
jest.resetModules();
});

const mainModule = require("../main");

test("validate zero exit code and stdout captured", async () => {
// jest.resetModules();
jest.doMock("child_process", () => {
Expand Down Expand Up @@ -55,3 +59,207 @@ test("validate stderr captured", async () => {
expect(res.stdout.trim()).toBe("");
expect(res.stderr.trim()).toBe("error");
});

describe('Assemble Snyk command', () => {
test("Command without options", () => {
const imageName = 'someImage';
const optionsList = {};

const cmd = mainModule.assembleSnykCommand(imageName, optionsList);

expect(cmd).toEqual(`snyk test --docker ${imageName}`);
});

test("Command with json option", () => {
const imageName = 'someImage';
const optionsList = { json: true };

const cmd = mainModule.assembleSnykCommand(imageName, optionsList);

expect(cmd).toEqual(`snyk test --docker ${imageName} --json`);
});
});

describe('Handle results', () => {
test('Handle text result for simple image', () => {
const optionsList = {};
const commandResult = [
{
image: "MyImage",
result: `
Testing docker.io/bitnami/redis:5.0.5-debian-9-r181...
✗ Low severity vulnerability found in tar
Description: CVE-2005-2541
Info: https://snyk.io/vuln/SNYK-LINUX-TAR-105079
Introduced through: meta-common-packages@meta
From: meta-common-packages@meta > tar@1.29b-1.1
`
}
];
const expectedResult = `Image: ${commandResult[0].image}
${commandResult[0].result}
`

const resultHandled = mainModule.handleResult(commandResult, optionsList)

expect(resultHandled).toEqual(expectedResult);
});

test('Handle text result for multi image', () => {
const optionsList = {};
const commandResult = [
{
image: "MyImage1",
result: `
Testing docker.io/bitnami/redis:5.0.5-debian-9-r181...
✗ Low severity vulnerability found in tar
Description: CVE-2005-2541
Info: https://snyk.io/vuln/SNYK-LINUX-TAR-105079
Introduced through: meta-common-packages@meta
From: meta-common-packages@meta > tar@1.29b-1.1
`
},
{
image: "MyImage2",
result: `
Testing docker.io/bitnami/redis:5.0.5-debian-9-r181...
✗ Low severity vulnerability found in tar
Description: CVE-2005-2541
Info: https://snyk.io/vuln/SNYK-LINUX-TAR-105079
Introduced through: meta-common-packages@meta
From: meta-common-packages@meta > tar@1.29b-1.1
`
}
];
const expectedResult = `Image: ${commandResult[0].image}
${commandResult[0].result}
Image: ${commandResult[1].image}
${commandResult[1].result}
`

const resultHandled = mainModule.handleResult(commandResult, optionsList)

expect(resultHandled).toEqual(expectedResult);
});

test('Handle json result for simple image', () => {
const optionsList = {json: true};
const commandResult = [
{
image: "MyImage",
result: `
Testing docker.io/bitnami/redis:5.0.5-debian-9-r181...
✗ Low severity vulnerability found in tar
Description: CVE-2005-2541
Info: https://snyk.io/vuln/SNYK-LINUX-TAR-105079
Introduced through: meta-common-packages@meta
From: meta-common-packages@meta > tar@1.29b-1.1
`
}
];
const expectedResult = [
{
image: commandResult[0].image,
result: commandResult[0].result
}
];

const resultHandled = mainModule.handleResult(commandResult, optionsList)

expect(resultHandled).toEqual(expectedResult);
});

test('Handle json result for multi image', () => {
const optionsList = {json: true};
const commandResult = [
{
image: "MyImage1",
result: `
Testing docker.io/bitnami/redis:5.0.5-debian-9-r181...
✗ Low severity vulnerability found in tar
Description: CVE-2005-2541
Info: https://snyk.io/vuln/SNYK-LINUX-TAR-105079
Introduced through: meta-common-packages@meta
From: meta-common-packages@meta > tar@1.29b-1.1
`
},
{
image: "MyImage2",
result: `
Testing docker.io/bitnami/redis:5.0.5-debian-9-r181...
✗ Low severity vulnerability found in tar
Description: CVE-2005-2541
Info: https://snyk.io/vuln/SNYK-LINUX-TAR-105079
Introduced through: meta-common-packages@meta
From: meta-common-packages@meta > tar@1.29b-1.1
`
}
];
const expectedResult = [
{
image: commandResult[0].image,
result: commandResult[0].result
},
{
image: commandResult[1].image,
result: commandResult[1].result
}
];

const resultHandled = mainModule.handleResult(commandResult, optionsList)

expect(resultHandled).toEqual(expectedResult);
});
});

describe('Handle output', () => {
test('Output without options', () => {
const optionsList = {};
const expectedOutput = 'summary';
const log = jest.spyOn(global.console, 'log');

mainModule.handleOutput(expectedOutput, optionsList);

expect(log).toHaveBeenCalledWith(expectedOutput);
});

test('Output with "json" option', () => {
const optionsList = { json: true };
const expectedOutput = {
"result": "test"
};
const log = jest.spyOn(global.console, 'log');

mainModule.handleOutput(expectedOutput, optionsList);

expect(log).toHaveBeenCalledWith(JSON.stringify(expectedOutput, null, 2));
});

test('Output with "output" option', () => {
const optionsList = { output: '/tmp/file.html'};
const expectedOutput = 'summary';
const writeFile = jest.spyOn(fs, 'writeFileSync');

mainModule.handleOutput(expectedOutput, optionsList);

expect(writeFile).toHaveBeenCalledWith(optionsList.output, expectedOutput);
});

test('Output with "json" and "output" options', () => {
const optionsList = { output: '/tmp/file.json', json: true };
const expectedOutput = {
"result": "test"
};
const writeFile = jest.spyOn(fs, 'writeFileSync');

mainModule.handleOutput(expectedOutput, optionsList);

expect(writeFile).toHaveBeenCalledWith(optionsList.output, JSON.stringify(expectedOutput, null, 2));
});
});
4 changes: 4 additions & 0 deletions src/cli-args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ function parseInputParameters(inputArgs): IArgs {
})
.strict().argv;

if (argv.json) {
returnObj.json = argv.json;
}

if (argv.output) {
returnObj.output = argv.output;
}
Expand Down

0 comments on commit 9683765

Please sign in to comment.