Add rudimentary test for cli definitions.
Use consistent import and quote style.
This commit is contained in:
166
spec/CLI.spec.js
166
spec/CLI.spec.js
@@ -1,29 +1,30 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
var commander = require("../src/cli/utils/commander").default;
|
import commander from '../src/cli/utils/commander';
|
||||||
|
import definitions from '../src/cli/definitions/parse-server';
|
||||||
|
|
||||||
var definitions = {
|
var testDefinitions = {
|
||||||
"arg0": "PROGRAM_ARG_0",
|
'arg0': 'PROGRAM_ARG_0',
|
||||||
"arg1": {
|
'arg1': {
|
||||||
env: "PROGRAM_ARG_1",
|
env: 'PROGRAM_ARG_1',
|
||||||
required: true
|
required: true
|
||||||
},
|
},
|
||||||
"arg2": {
|
'arg2': {
|
||||||
env: "PROGRAM_ARG_2",
|
env: 'PROGRAM_ARG_2',
|
||||||
action: function(value) {
|
action: function(value) {
|
||||||
var value = parseInt(value);
|
var intValue = parseInt(value);
|
||||||
if (!Number.isInteger(value)) {
|
if (!Number.isInteger(intValue)) {
|
||||||
throw "arg2 is invalid";
|
throw 'arg2 is invalid';
|
||||||
}
|
}
|
||||||
return value;
|
return intValue;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"arg3": {},
|
'arg3': {},
|
||||||
"arg4": {
|
'arg4': {
|
||||||
default: "arg4Value"
|
default: 'arg4Value'
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
describe("commander additions", () => {
|
describe('commander additions', () => {
|
||||||
afterEach((done) => {
|
afterEach((done) => {
|
||||||
commander.options = [];
|
commander.options = [];
|
||||||
delete commander.arg0;
|
delete commander.arg0;
|
||||||
@@ -32,107 +33,126 @@ describe("commander additions", () => {
|
|||||||
delete commander.arg3;
|
delete commander.arg3;
|
||||||
delete commander.arg4;
|
delete commander.arg4;
|
||||||
done();
|
done();
|
||||||
})
|
});
|
||||||
|
|
||||||
it("should load properly definitions from args", (done) => {
|
it('should load properly definitions from args', (done) => {
|
||||||
commander.loadDefinitions(definitions);
|
commander.loadDefinitions(testDefinitions);
|
||||||
commander.parse(["node","./CLI.spec.js","--arg0", "arg0Value", "--arg1", "arg1Value", "--arg2", "2", "--arg3", "some"]);
|
commander.parse(['node','./CLI.spec.js','--arg0', 'arg0Value', '--arg1', 'arg1Value', '--arg2', '2', '--arg3', 'some']);
|
||||||
expect(commander.arg0).toEqual("arg0Value");
|
expect(commander.arg0).toEqual('arg0Value');
|
||||||
expect(commander.arg1).toEqual("arg1Value");
|
expect(commander.arg1).toEqual('arg1Value');
|
||||||
expect(commander.arg2).toEqual(2);
|
expect(commander.arg2).toEqual(2);
|
||||||
expect(commander.arg3).toEqual("some");
|
expect(commander.arg3).toEqual('some');
|
||||||
expect(commander.arg4).toEqual("arg4Value");
|
expect(commander.arg4).toEqual('arg4Value');
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should load properly definitions from env", (done) => {
|
it('should load properly definitions from env', (done) => {
|
||||||
commander.loadDefinitions(definitions);
|
commander.loadDefinitions(testDefinitions);
|
||||||
commander.parse([], {
|
commander.parse([], {
|
||||||
"PROGRAM_ARG_0": "arg0ENVValue",
|
'PROGRAM_ARG_0': 'arg0ENVValue',
|
||||||
"PROGRAM_ARG_1": "arg1ENVValue",
|
'PROGRAM_ARG_1': 'arg1ENVValue',
|
||||||
"PROGRAM_ARG_2": "3",
|
'PROGRAM_ARG_2': '3',
|
||||||
});
|
});
|
||||||
expect(commander.arg0).toEqual("arg0ENVValue");
|
expect(commander.arg0).toEqual('arg0ENVValue');
|
||||||
expect(commander.arg1).toEqual("arg1ENVValue");
|
expect(commander.arg1).toEqual('arg1ENVValue');
|
||||||
expect(commander.arg2).toEqual(3);
|
expect(commander.arg2).toEqual(3);
|
||||||
expect(commander.arg4).toEqual("arg4Value");
|
expect(commander.arg4).toEqual('arg4Value');
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should load properly use args over env", (done) => {
|
it('should load properly use args over env', (done) => {
|
||||||
commander.loadDefinitions(definitions);
|
commander.loadDefinitions(testDefinitions);
|
||||||
commander.parse(["node","./CLI.spec.js","--arg0", "arg0Value", "--arg4", "anotherArg4"], {
|
commander.parse(['node','./CLI.spec.js','--arg0', 'arg0Value', '--arg4', 'anotherArg4'], {
|
||||||
"PROGRAM_ARG_0": "arg0ENVValue",
|
'PROGRAM_ARG_0': 'arg0ENVValue',
|
||||||
"PROGRAM_ARG_1": "arg1ENVValue",
|
'PROGRAM_ARG_1': 'arg1ENVValue',
|
||||||
"PROGRAM_ARG_2": "4",
|
'PROGRAM_ARG_2': '4',
|
||||||
});
|
});
|
||||||
expect(commander.arg0).toEqual("arg0Value");
|
expect(commander.arg0).toEqual('arg0Value');
|
||||||
expect(commander.arg1).toEqual("arg1ENVValue");
|
expect(commander.arg1).toEqual('arg1ENVValue');
|
||||||
expect(commander.arg2).toEqual(4);
|
expect(commander.arg2).toEqual(4);
|
||||||
expect(commander.arg4).toEqual("anotherArg4");
|
expect(commander.arg4).toEqual('anotherArg4');
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should fail in action as port is invalid", (done) => {
|
it('should fail in action as port is invalid', (done) => {
|
||||||
commander.loadDefinitions(definitions);
|
commander.loadDefinitions(testDefinitions);
|
||||||
expect(()=> {
|
expect(()=> {
|
||||||
commander.parse(["node","./CLI.spec.js","--arg0", "arg0Value"], {
|
commander.parse(['node','./CLI.spec.js','--arg0', 'arg0Value'], {
|
||||||
"PROGRAM_ARG_0": "arg0ENVValue",
|
'PROGRAM_ARG_0': 'arg0ENVValue',
|
||||||
"PROGRAM_ARG_1": "arg1ENVValue",
|
'PROGRAM_ARG_1': 'arg1ENVValue',
|
||||||
"PROGRAM_ARG_2": "hello",
|
'PROGRAM_ARG_2': 'hello',
|
||||||
});
|
});
|
||||||
}).toThrow("arg2 is invalid");
|
}).toThrow('arg2 is invalid');
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should not override config.json", (done) => {
|
it('should not override config.json', (done) => {
|
||||||
commander.loadDefinitions(definitions);
|
commander.loadDefinitions(testDefinitions);
|
||||||
commander.parse(["node","./CLI.spec.js","--arg0", "arg0Value", "./spec/configs/CLIConfig.json"], {
|
commander.parse(['node','./CLI.spec.js','--arg0', 'arg0Value', './spec/configs/CLIConfig.json'], {
|
||||||
"PROGRAM_ARG_0": "arg0ENVValue",
|
'PROGRAM_ARG_0': 'arg0ENVValue',
|
||||||
"PROGRAM_ARG_1": "arg1ENVValue",
|
'PROGRAM_ARG_1': 'arg1ENVValue',
|
||||||
});
|
});
|
||||||
let options = commander.getOptions();
|
let options = commander.getOptions();
|
||||||
expect(options.arg2).toBe(8888);
|
expect(options.arg2).toBe(8888);
|
||||||
expect(options.arg3).toBe("hello"); //config value
|
expect(options.arg3).toBe('hello'); //config value
|
||||||
expect(options.arg4).toBe('/1');
|
expect(options.arg4).toBe('/1');
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should fail with invalid values in JSON", (done) => {
|
it('should fail with invalid values in JSON', (done) => {
|
||||||
commander.loadDefinitions(definitions);
|
commander.loadDefinitions(testDefinitions);
|
||||||
expect(() => {
|
expect(() => {
|
||||||
commander.parse(["node","./CLI.spec.js","--arg0", "arg0Value", "./spec/configs/CLIConfigFail.json"], {
|
commander.parse(['node','./CLI.spec.js','--arg0', 'arg0Value', './spec/configs/CLIConfigFail.json'], {
|
||||||
"PROGRAM_ARG_0": "arg0ENVValue",
|
'PROGRAM_ARG_0': 'arg0ENVValue',
|
||||||
"PROGRAM_ARG_1": "arg1ENVValue",
|
'PROGRAM_ARG_1': 'arg1ENVValue',
|
||||||
});
|
});
|
||||||
}).toThrow("arg2 is invalid")
|
}).toThrow('arg2 is invalid');
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should fail when too many apps are set", (done) => {
|
it('should fail when too many apps are set', (done) => {
|
||||||
commander.loadDefinitions(definitions);
|
commander.loadDefinitions(testDefinitions);
|
||||||
expect(() => {
|
expect(() => {
|
||||||
commander.parse(["node","./CLI.spec.js","./spec/configs/CLIConfigFailTooManyApps.json"]);
|
commander.parse(['node','./CLI.spec.js','./spec/configs/CLIConfigFailTooManyApps.json']);
|
||||||
}).toThrow("Multiple apps are not supported")
|
}).toThrow('Multiple apps are not supported');
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should load config from apps", (done) => {
|
it('should load config from apps', (done) => {
|
||||||
commander.loadDefinitions(definitions);
|
commander.loadDefinitions(testDefinitions);
|
||||||
commander.parse(["node", "./CLI.spec.js", "./spec/configs/CLIConfigApps.json"]);
|
commander.parse(['node', './CLI.spec.js', './spec/configs/CLIConfigApps.json']);
|
||||||
let options = commander.getOptions();
|
let options = commander.getOptions();
|
||||||
expect(options.arg1).toBe("my_app");
|
expect(options.arg1).toBe('my_app');
|
||||||
expect(options.arg2).toBe(8888);
|
expect(options.arg2).toBe(8888);
|
||||||
expect(options.arg3).toBe("hello"); //config value
|
expect(options.arg3).toBe('hello'); //config value
|
||||||
expect(options.arg4).toBe('/1');
|
expect(options.arg4).toBe('/1');
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should fail when passing an invalid arguement", (done) => {
|
it('should fail when passing an invalid arguement', (done) => {
|
||||||
commander.loadDefinitions(definitions);
|
commander.loadDefinitions(testDefinitions);
|
||||||
expect(() => {
|
expect(() => {
|
||||||
commander.parse(["node", "./CLI.spec.js", "./spec/configs/CLIConfigUnknownArg.json"]);
|
commander.parse(['node', './CLI.spec.js', './spec/configs/CLIConfigUnknownArg.json']);
|
||||||
}).toThrow('error: unknown option myArg')
|
}).toThrow('error: unknown option myArg');
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('definitions', () => {
|
||||||
|
it('should have valid types', () => {
|
||||||
|
for (let key in definitions) {
|
||||||
|
let definition = definitions[key];
|
||||||
|
expect(typeof definition).toBe('object');
|
||||||
|
if (typeof definition.required !== 'undefined') {
|
||||||
|
expect(typeof definition.env).toBe('string');
|
||||||
|
}
|
||||||
|
expect(typeof definition.help).toBe('string');
|
||||||
|
if (typeof definition.required !== 'undefined') {
|
||||||
|
expect(typeof definition.required).toBe('boolean');
|
||||||
|
}
|
||||||
|
if (typeof definition.action !== 'undefined') {
|
||||||
|
expect(typeof definition.action).toBe('function');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user