Skip to content

Commit 28c1824

Browse files
Add rudimentary test for cli definitions.
Use consistent import and quote style.
1 parent 19271fa commit 28c1824

File tree

1 file changed

+93
-73
lines changed

1 file changed

+93
-73
lines changed

spec/CLI.spec.js

Lines changed: 93 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,30 @@
11
'use strict';
2-
var commander = require("../src/cli/utils/commander").default;
2+
import commander from '../src/cli/utils/commander';
3+
import definitions from '../src/cli/definitions/parse-server';
34

4-
var definitions = {
5-
"arg0": "PROGRAM_ARG_0",
6-
"arg1": {
7-
env: "PROGRAM_ARG_1",
5+
var testDefinitions = {
6+
'arg0': 'PROGRAM_ARG_0',
7+
'arg1': {
8+
env: 'PROGRAM_ARG_1',
89
required: true
910
},
10-
"arg2": {
11-
env: "PROGRAM_ARG_2",
11+
'arg2': {
12+
env: 'PROGRAM_ARG_2',
1213
action: function(value) {
13-
var value = parseInt(value);
14-
if (!Number.isInteger(value)) {
15-
throw "arg2 is invalid";
14+
var intValue = parseInt(value);
15+
if (!Number.isInteger(intValue)) {
16+
throw 'arg2 is invalid';
1617
}
17-
return value;
18+
return intValue;
1819
}
1920
},
20-
"arg3": {},
21-
"arg4": {
22-
default: "arg4Value"
21+
'arg3': {},
22+
'arg4': {
23+
default: 'arg4Value'
2324
}
24-
}
25+
};
2526

26-
describe("commander additions", () => {
27+
describe('commander additions', () => {
2728
afterEach((done) => {
2829
commander.options = [];
2930
delete commander.arg0;
@@ -32,107 +33,126 @@ describe("commander additions", () => {
3233
delete commander.arg3;
3334
delete commander.arg4;
3435
done();
35-
})
36+
});
3637

37-
it("should load properly definitions from args", (done) => {
38-
commander.loadDefinitions(definitions);
39-
commander.parse(["node","./CLI.spec.js","--arg0", "arg0Value", "--arg1", "arg1Value", "--arg2", "2", "--arg3", "some"]);
40-
expect(commander.arg0).toEqual("arg0Value");
41-
expect(commander.arg1).toEqual("arg1Value");
38+
it('should load properly definitions from args', (done) => {
39+
commander.loadDefinitions(testDefinitions);
40+
commander.parse(['node','./CLI.spec.js','--arg0', 'arg0Value', '--arg1', 'arg1Value', '--arg2', '2', '--arg3', 'some']);
41+
expect(commander.arg0).toEqual('arg0Value');
42+
expect(commander.arg1).toEqual('arg1Value');
4243
expect(commander.arg2).toEqual(2);
43-
expect(commander.arg3).toEqual("some");
44-
expect(commander.arg4).toEqual("arg4Value");
44+
expect(commander.arg3).toEqual('some');
45+
expect(commander.arg4).toEqual('arg4Value');
4546
done();
4647
});
4748

48-
it("should load properly definitions from env", (done) => {
49-
commander.loadDefinitions(definitions);
49+
it('should load properly definitions from env', (done) => {
50+
commander.loadDefinitions(testDefinitions);
5051
commander.parse([], {
51-
"PROGRAM_ARG_0": "arg0ENVValue",
52-
"PROGRAM_ARG_1": "arg1ENVValue",
53-
"PROGRAM_ARG_2": "3",
52+
'PROGRAM_ARG_0': 'arg0ENVValue',
53+
'PROGRAM_ARG_1': 'arg1ENVValue',
54+
'PROGRAM_ARG_2': '3',
5455
});
55-
expect(commander.arg0).toEqual("arg0ENVValue");
56-
expect(commander.arg1).toEqual("arg1ENVValue");
56+
expect(commander.arg0).toEqual('arg0ENVValue');
57+
expect(commander.arg1).toEqual('arg1ENVValue');
5758
expect(commander.arg2).toEqual(3);
58-
expect(commander.arg4).toEqual("arg4Value");
59+
expect(commander.arg4).toEqual('arg4Value');
5960
done();
6061
});
6162

62-
it("should load properly use args over env", (done) => {
63-
commander.loadDefinitions(definitions);
64-
commander.parse(["node","./CLI.spec.js","--arg0", "arg0Value", "--arg4", "anotherArg4"], {
65-
"PROGRAM_ARG_0": "arg0ENVValue",
66-
"PROGRAM_ARG_1": "arg1ENVValue",
67-
"PROGRAM_ARG_2": "4",
63+
it('should load properly use args over env', (done) => {
64+
commander.loadDefinitions(testDefinitions);
65+
commander.parse(['node','./CLI.spec.js','--arg0', 'arg0Value', '--arg4', 'anotherArg4'], {
66+
'PROGRAM_ARG_0': 'arg0ENVValue',
67+
'PROGRAM_ARG_1': 'arg1ENVValue',
68+
'PROGRAM_ARG_2': '4',
6869
});
69-
expect(commander.arg0).toEqual("arg0Value");
70-
expect(commander.arg1).toEqual("arg1ENVValue");
70+
expect(commander.arg0).toEqual('arg0Value');
71+
expect(commander.arg1).toEqual('arg1ENVValue');
7172
expect(commander.arg2).toEqual(4);
72-
expect(commander.arg4).toEqual("anotherArg4");
73+
expect(commander.arg4).toEqual('anotherArg4');
7374
done();
7475
});
7576

76-
it("should fail in action as port is invalid", (done) => {
77-
commander.loadDefinitions(definitions);
77+
it('should fail in action as port is invalid', (done) => {
78+
commander.loadDefinitions(testDefinitions);
7879
expect(()=> {
79-
commander.parse(["node","./CLI.spec.js","--arg0", "arg0Value"], {
80-
"PROGRAM_ARG_0": "arg0ENVValue",
81-
"PROGRAM_ARG_1": "arg1ENVValue",
82-
"PROGRAM_ARG_2": "hello",
80+
commander.parse(['node','./CLI.spec.js','--arg0', 'arg0Value'], {
81+
'PROGRAM_ARG_0': 'arg0ENVValue',
82+
'PROGRAM_ARG_1': 'arg1ENVValue',
83+
'PROGRAM_ARG_2': 'hello',
8384
});
84-
}).toThrow("arg2 is invalid");
85+
}).toThrow('arg2 is invalid');
8586
done();
8687
});
8788

88-
it("should not override config.json", (done) => {
89-
commander.loadDefinitions(definitions);
90-
commander.parse(["node","./CLI.spec.js","--arg0", "arg0Value", "./spec/configs/CLIConfig.json"], {
91-
"PROGRAM_ARG_0": "arg0ENVValue",
92-
"PROGRAM_ARG_1": "arg1ENVValue",
89+
it('should not override config.json', (done) => {
90+
commander.loadDefinitions(testDefinitions);
91+
commander.parse(['node','./CLI.spec.js','--arg0', 'arg0Value', './spec/configs/CLIConfig.json'], {
92+
'PROGRAM_ARG_0': 'arg0ENVValue',
93+
'PROGRAM_ARG_1': 'arg1ENVValue',
9394
});
9495
let options = commander.getOptions();
9596
expect(options.arg2).toBe(8888);
96-
expect(options.arg3).toBe("hello"); //config value
97+
expect(options.arg3).toBe('hello'); //config value
9798
expect(options.arg4).toBe('/1');
9899
done();
99100
});
100101

101-
it("should fail with invalid values in JSON", (done) => {
102-
commander.loadDefinitions(definitions);
102+
it('should fail with invalid values in JSON', (done) => {
103+
commander.loadDefinitions(testDefinitions);
103104
expect(() => {
104-
commander.parse(["node","./CLI.spec.js","--arg0", "arg0Value", "./spec/configs/CLIConfigFail.json"], {
105-
"PROGRAM_ARG_0": "arg0ENVValue",
106-
"PROGRAM_ARG_1": "arg1ENVValue",
105+
commander.parse(['node','./CLI.spec.js','--arg0', 'arg0Value', './spec/configs/CLIConfigFail.json'], {
106+
'PROGRAM_ARG_0': 'arg0ENVValue',
107+
'PROGRAM_ARG_1': 'arg1ENVValue',
107108
});
108-
}).toThrow("arg2 is invalid")
109+
}).toThrow('arg2 is invalid');
109110
done();
110111
});
111112

112-
it("should fail when too many apps are set", (done) => {
113-
commander.loadDefinitions(definitions);
113+
it('should fail when too many apps are set', (done) => {
114+
commander.loadDefinitions(testDefinitions);
114115
expect(() => {
115-
commander.parse(["node","./CLI.spec.js","./spec/configs/CLIConfigFailTooManyApps.json"]);
116-
}).toThrow("Multiple apps are not supported")
116+
commander.parse(['node','./CLI.spec.js','./spec/configs/CLIConfigFailTooManyApps.json']);
117+
}).toThrow('Multiple apps are not supported');
117118
done();
118119
});
119120

120-
it("should load config from apps", (done) => {
121-
commander.loadDefinitions(definitions);
122-
commander.parse(["node", "./CLI.spec.js", "./spec/configs/CLIConfigApps.json"]);
121+
it('should load config from apps', (done) => {
122+
commander.loadDefinitions(testDefinitions);
123+
commander.parse(['node', './CLI.spec.js', './spec/configs/CLIConfigApps.json']);
123124
let options = commander.getOptions();
124-
expect(options.arg1).toBe("my_app");
125+
expect(options.arg1).toBe('my_app');
125126
expect(options.arg2).toBe(8888);
126-
expect(options.arg3).toBe("hello"); //config value
127+
expect(options.arg3).toBe('hello'); //config value
127128
expect(options.arg4).toBe('/1');
128129
done();
129130
});
130131

131-
it("should fail when passing an invalid arguement", (done) => {
132-
commander.loadDefinitions(definitions);
132+
it('should fail when passing an invalid arguement', (done) => {
133+
commander.loadDefinitions(testDefinitions);
133134
expect(() => {
134-
commander.parse(["node", "./CLI.spec.js", "./spec/configs/CLIConfigUnknownArg.json"]);
135-
}).toThrow('error: unknown option myArg')
135+
commander.parse(['node', './CLI.spec.js', './spec/configs/CLIConfigUnknownArg.json']);
136+
}).toThrow('error: unknown option myArg');
136137
done();
137138
});
138139
});
140+
141+
describe('definitions', () => {
142+
it('should have valid types', () => {
143+
for (let key in definitions) {
144+
let definition = definitions[key];
145+
expect(typeof definition).toBe('object');
146+
if (typeof definition.required !== 'undefined') {
147+
expect(typeof definition.env).toBe('string');
148+
}
149+
expect(typeof definition.help).toBe('string');
150+
if (typeof definition.required !== 'undefined') {
151+
expect(typeof definition.required).toBe('boolean');
152+
}
153+
if (typeof definition.action !== 'undefined') {
154+
expect(typeof definition.action).toBe('function');
155+
}
156+
}
157+
});
158+
});

0 commit comments

Comments
 (0)