Skip to content

Commit

Permalink
fix: Create test command for helm snyk plugin
Browse files Browse the repository at this point in the history
 - Include the `testz command for the plugin
 - Include command description
 - Update `Usage` description and `example`
  • Loading branch information
Arthur Granado committed Nov 11, 2019
1 parent 3f12a0c commit 2d4aaf0
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 33 deletions.
59 changes: 42 additions & 17 deletions src/__tests__/test-cli-args.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,66 @@
import { IArgs, parseInputParameters } from "../cli-args";

test("handles dot or actual path as input", () => {
let inputArgs = ["."];
let parsedArgs: IArgs = parseInputParameters(inputArgs);
expect(parsedArgs.inputDirectory).toBe(".");
expect(parsedArgs.debug).toBe(false);
describe("test command", () => {
describe("check required input directory", () => {
test("process exit if there is no <chart-directory> required arg", () => {
const inputArgs = ["test"];
//@ts-ignore
const mockProcessExit = jest.spyOn(process, "exit").mockImplementation(code => {});

inputArgs = ["/some/other/path"];
parsedArgs = parseInputParameters(inputArgs);
expect(parsedArgs.inputDirectory).toBe("/some/other/path");
expect(parsedArgs.debug).toBe(false);
parseInputParameters(inputArgs);
expect(mockProcessExit).toHaveBeenCalledWith(1);
mockProcessExit.mockRestore();
});

inputArgs = ["~/other/path"];
parsedArgs = parseInputParameters(inputArgs);
expect(parsedArgs.inputDirectory).toBe("~/other/path");
expect(parsedArgs.debug).toBe(false);
});
test("handles dot as input", () => {
const inputArgs = ["test", "."];

const parsedArgs = parseInputParameters(inputArgs);

expect(parsedArgs.inputDirectory).toBe(".");
expect(parsedArgs.debug).toBe(false);
});

test("handle absolute path as input", () => {
const inputArgs = ["test", "/some/other/path"];
const parsedArgs = parseInputParameters(inputArgs);

expect(parsedArgs.inputDirectory).toBe("/some/other/path");

expect(parsedArgs.debug).toBe(false);
});

test("handle relative path as input", () => {
const inputArgs = ["test", "~/other/path"];

const parsedArgs = parseInputParameters(inputArgs);

expect(parsedArgs.inputDirectory).toBe("~/other/path");
expect(parsedArgs.debug).toBe(false);
});
});
});

test("yargs causes process exit if no args", () => {
//@ts-ignore
const mockProcessExit = jest.spyOn(process, "exit").mockImplementation(code => {});
const inputArgs = [];
const parsedArgs = parseInputParameters(inputArgs);

parseInputParameters(inputArgs);
expect(mockProcessExit).toHaveBeenCalledWith(1);
mockProcessExit.mockRestore();
});

test("handles debug flag", () => {
const inputArgs = [".", "--debug"];
const inputArgs = ["test", ".", "--debug"];
const parsedArgs: IArgs = parseInputParameters(inputArgs);
expect(parsedArgs.inputDirectory).toBe(".");
expect(parsedArgs.debug).toBe(true);
});

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

const parsedArgs: IArgs = parseInputParameters(inputArgs);

Expand Down
24 changes: 8 additions & 16 deletions src/cli-args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,26 +36,18 @@ function parseInputParameters(inputArgs): IArgs {

const argv = yargs(inputArgs)
.version()
.usage("Usage: helm-snyk <chart-directory> [options]")
.scriptName("helm snyk")
.usage("Usage: $0 <command>")
.command("test <chart-directory> [options]", "Check images in your charts for vulnerabilities")
.help("help")
.alias("help", "h")
.options(getOptions())
.hide("notest")
.demandCommand(1) // because one directory max
.example("helm-snyk . --output=snyk-out.json")
.check(argvObj => {
// argv._ should contain the hyphen-less commands
if (argvObj._.length === 0) {
returnObj.inputDirectory = ".";
} else if (argvObj._.length === 1) {
// todo: make sure this is a legit directory (or ".")
returnObj.inputDirectory = argvObj._[0];
} else {
throw new Error("only one positional argument is allowed and it should be a directory");
}
return true; // from check
})
.strict().argv;
.demandCommand(2)
.example("$0 test . --output=snyk-out.json")
.argv;

returnObj.inputDirectory = argv.chartDirectory;

if (argv.json) {
returnObj.json = argv.json;
Expand Down

0 comments on commit 2d4aaf0

Please sign in to comment.