Skip to content

Commit 7e0e484

Browse files
authored
Basic test for CLI definitions (#3050)
* Add rudimentary test for cli definitions. Use consistent import and quote style. * nit: style * Fix bug. Add similar tests for live query definitions
2 parents 19271fa + 7b5f89e commit 7e0e484

File tree

1 file changed

+113
-73
lines changed

1 file changed

+113
-73
lines changed

spec/CLI.spec.js

Lines changed: 113 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,31 @@
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';
4+
import liveQueryDefinitions from '../src/cli/definitions/parse-live-query-server';
35

4-
var definitions = {
5-
"arg0": "PROGRAM_ARG_0",
6-
"arg1": {
7-
env: "PROGRAM_ARG_1",
6+
var testDefinitions = {
7+
'arg0': 'PROGRAM_ARG_0',
8+
'arg1': {
9+
env: 'PROGRAM_ARG_1',
810
required: true
911
},
10-
"arg2": {
11-
env: "PROGRAM_ARG_2",
12+
'arg2': {
13+
env: 'PROGRAM_ARG_2',
1214
action: function(value) {
13-
var value = parseInt(value);
14-
if (!Number.isInteger(value)) {
15-
throw "arg2 is invalid";
15+
var intValue = parseInt(value);
16+
if (!Number.isInteger(intValue)) {
17+
throw 'arg2 is invalid';
1618
}
17-
return value;
19+
return intValue;
1820
}
1921
},
20-
"arg3": {},
21-
"arg4": {
22-
default: "arg4Value"
22+
'arg3': {},
23+
'arg4': {
24+
default: 'arg4Value'
2325
}
24-
}
26+
};
2527

26-
describe("commander additions", () => {
28+
describe('commander additions', () => {
2729
afterEach((done) => {
2830
commander.options = [];
2931
delete commander.arg0;
@@ -32,107 +34,145 @@ describe("commander additions", () => {
3234
delete commander.arg3;
3335
delete commander.arg4;
3436
done();
35-
})
37+
});
3638

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");
39+
it('should load properly definitions from args', (done) => {
40+
commander.loadDefinitions(testDefinitions);
41+
commander.parse(['node','./CLI.spec.js','--arg0', 'arg0Value', '--arg1', 'arg1Value', '--arg2', '2', '--arg3', 'some']);
42+
expect(commander.arg0).toEqual('arg0Value');
43+
expect(commander.arg1).toEqual('arg1Value');
4244
expect(commander.arg2).toEqual(2);
43-
expect(commander.arg3).toEqual("some");
44-
expect(commander.arg4).toEqual("arg4Value");
45+
expect(commander.arg3).toEqual('some');
46+
expect(commander.arg4).toEqual('arg4Value');
4547
done();
4648
});
4749

48-
it("should load properly definitions from env", (done) => {
49-
commander.loadDefinitions(definitions);
50+
it('should load properly definitions from env', (done) => {
51+
commander.loadDefinitions(testDefinitions);
5052
commander.parse([], {
51-
"PROGRAM_ARG_0": "arg0ENVValue",
52-
"PROGRAM_ARG_1": "arg1ENVValue",
53-
"PROGRAM_ARG_2": "3",
53+
'PROGRAM_ARG_0': 'arg0ENVValue',
54+
'PROGRAM_ARG_1': 'arg1ENVValue',
55+
'PROGRAM_ARG_2': '3',
5456
});
55-
expect(commander.arg0).toEqual("arg0ENVValue");
56-
expect(commander.arg1).toEqual("arg1ENVValue");
57+
expect(commander.arg0).toEqual('arg0ENVValue');
58+
expect(commander.arg1).toEqual('arg1ENVValue');
5759
expect(commander.arg2).toEqual(3);
58-
expect(commander.arg4).toEqual("arg4Value");
60+
expect(commander.arg4).toEqual('arg4Value');
5961
done();
6062
});
6163

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",
64+
it('should load properly use args over env', (done) => {
65+
commander.loadDefinitions(testDefinitions);
66+
commander.parse(['node','./CLI.spec.js','--arg0', 'arg0Value', '--arg4', 'anotherArg4'], {
67+
'PROGRAM_ARG_0': 'arg0ENVValue',
68+
'PROGRAM_ARG_1': 'arg1ENVValue',
69+
'PROGRAM_ARG_2': '4',
6870
});
69-
expect(commander.arg0).toEqual("arg0Value");
70-
expect(commander.arg1).toEqual("arg1ENVValue");
71+
expect(commander.arg0).toEqual('arg0Value');
72+
expect(commander.arg1).toEqual('arg1ENVValue');
7173
expect(commander.arg2).toEqual(4);
72-
expect(commander.arg4).toEqual("anotherArg4");
74+
expect(commander.arg4).toEqual('anotherArg4');
7375
done();
7476
});
7577

76-
it("should fail in action as port is invalid", (done) => {
77-
commander.loadDefinitions(definitions);
78+
it('should fail in action as port is invalid', (done) => {
79+
commander.loadDefinitions(testDefinitions);
7880
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",
81+
commander.parse(['node','./CLI.spec.js','--arg0', 'arg0Value'], {
82+
'PROGRAM_ARG_0': 'arg0ENVValue',
83+
'PROGRAM_ARG_1': 'arg1ENVValue',
84+
'PROGRAM_ARG_2': 'hello',
8385
});
84-
}).toThrow("arg2 is invalid");
86+
}).toThrow('arg2 is invalid');
8587
done();
8688
});
8789

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",
90+
it('should not override config.json', (done) => {
91+
commander.loadDefinitions(testDefinitions);
92+
commander.parse(['node','./CLI.spec.js','--arg0', 'arg0Value', './spec/configs/CLIConfig.json'], {
93+
'PROGRAM_ARG_0': 'arg0ENVValue',
94+
'PROGRAM_ARG_1': 'arg1ENVValue',
9395
});
9496
let options = commander.getOptions();
9597
expect(options.arg2).toBe(8888);
96-
expect(options.arg3).toBe("hello"); //config value
98+
expect(options.arg3).toBe('hello'); //config value
9799
expect(options.arg4).toBe('/1');
98100
done();
99101
});
100102

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

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

120-
it("should load config from apps", (done) => {
121-
commander.loadDefinitions(definitions);
122-
commander.parse(["node", "./CLI.spec.js", "./spec/configs/CLIConfigApps.json"]);
122+
it('should load config from apps', (done) => {
123+
commander.loadDefinitions(testDefinitions);
124+
commander.parse(['node', './CLI.spec.js', './spec/configs/CLIConfigApps.json']);
123125
let options = commander.getOptions();
124-
expect(options.arg1).toBe("my_app");
126+
expect(options.arg1).toBe('my_app');
125127
expect(options.arg2).toBe(8888);
126-
expect(options.arg3).toBe("hello"); //config value
128+
expect(options.arg3).toBe('hello'); //config value
127129
expect(options.arg4).toBe('/1');
128130
done();
129131
});
130132

131-
it("should fail when passing an invalid arguement", (done) => {
132-
commander.loadDefinitions(definitions);
133+
it('should fail when passing an invalid arguement', (done) => {
134+
commander.loadDefinitions(testDefinitions);
133135
expect(() => {
134-
commander.parse(["node", "./CLI.spec.js", "./spec/configs/CLIConfigUnknownArg.json"]);
135-
}).toThrow('error: unknown option myArg')
136+
commander.parse(['node', './CLI.spec.js', './spec/configs/CLIConfigUnknownArg.json']);
137+
}).toThrow('error: unknown option myArg');
136138
done();
137139
});
138140
});
141+
142+
describe('definitions', () => {
143+
it('should have valid types', () => {
144+
for (let key in definitions) {
145+
let definition = definitions[key];
146+
expect(typeof definition).toBe('object');
147+
if (typeof definition.env !== 'undefined') {
148+
expect(typeof definition.env).toBe('string');
149+
}
150+
expect(typeof definition.help).toBe('string');
151+
if (typeof definition.required !== 'undefined') {
152+
expect(typeof definition.required).toBe('boolean');
153+
}
154+
if (typeof definition.action !== 'undefined') {
155+
expect(typeof definition.action).toBe('function');
156+
}
157+
}
158+
});
159+
});
160+
161+
describe('LiveQuery definitions', () => {
162+
it('should have valid types', () => {
163+
for (let key in liveQueryDefinitions) {
164+
let definition = liveQueryDefinitions[key];
165+
expect(typeof definition).toBe('object');
166+
if (typeof definition.env !== 'undefined') {
167+
expect(typeof definition.env).toBe('string');
168+
}
169+
expect(typeof definition.help).toBe('string');
170+
if (typeof definition.required !== 'undefined') {
171+
expect(typeof definition.required).toBe('boolean');
172+
}
173+
if (typeof definition.action !== 'undefined') {
174+
expect(typeof definition.action).toBe('function');
175+
}
176+
}
177+
});
178+
});

0 commit comments

Comments
 (0)