2016-04-04 20:02:34 -04:00
|
|
|
'use strict';
|
2016-11-14 14:59:00 +00:00
|
|
|
import commander from '../src/cli/utils/commander';
|
|
|
|
|
import definitions from '../src/cli/definitions/parse-server';
|
2016-11-14 16:23:31 +00:00
|
|
|
import liveQueryDefinitions from '../src/cli/definitions/parse-live-query-server';
|
2016-02-23 10:56:01 -05:00
|
|
|
|
2018-02-17 09:55:30 -05:00
|
|
|
const testDefinitions = {
|
2016-11-14 14:59:00 +00:00
|
|
|
'arg0': 'PROGRAM_ARG_0',
|
|
|
|
|
'arg1': {
|
|
|
|
|
env: 'PROGRAM_ARG_1',
|
2016-02-23 10:56:01 -05:00
|
|
|
required: true
|
|
|
|
|
},
|
2016-11-14 14:59:00 +00:00
|
|
|
'arg2': {
|
|
|
|
|
env: 'PROGRAM_ARG_2',
|
2016-02-23 10:56:01 -05:00
|
|
|
action: function(value) {
|
2018-02-17 09:55:30 -05:00
|
|
|
const intValue = parseInt(value);
|
2016-11-14 14:59:00 +00:00
|
|
|
if (!Number.isInteger(intValue)) {
|
|
|
|
|
throw 'arg2 is invalid';
|
2016-02-24 08:50:04 -05:00
|
|
|
}
|
2016-11-14 14:59:00 +00:00
|
|
|
return intValue;
|
2016-02-23 10:56:01 -05:00
|
|
|
}
|
|
|
|
|
},
|
2016-11-14 14:59:00 +00:00
|
|
|
'arg3': {},
|
|
|
|
|
'arg4': {
|
|
|
|
|
default: 'arg4Value'
|
2016-02-24 08:50:04 -05:00
|
|
|
}
|
2016-11-14 14:59:00 +00:00
|
|
|
};
|
2016-02-23 10:56:01 -05:00
|
|
|
|
2016-11-14 14:59:00 +00:00
|
|
|
describe('commander additions', () => {
|
2016-02-23 10:56:01 -05:00
|
|
|
afterEach((done) => {
|
|
|
|
|
commander.options = [];
|
|
|
|
|
delete commander.arg0;
|
|
|
|
|
delete commander.arg1;
|
|
|
|
|
delete commander.arg2;
|
|
|
|
|
delete commander.arg3;
|
2016-02-24 08:50:04 -05:00
|
|
|
delete commander.arg4;
|
2016-02-23 10:56:01 -05:00
|
|
|
done();
|
2016-11-14 14:59:00 +00:00
|
|
|
});
|
2016-04-04 20:02:34 -04:00
|
|
|
|
2016-11-14 14:59:00 +00:00
|
|
|
it('should load properly definitions from args', (done) => {
|
|
|
|
|
commander.loadDefinitions(testDefinitions);
|
|
|
|
|
commander.parse(['node','./CLI.spec.js','--arg0', 'arg0Value', '--arg1', 'arg1Value', '--arg2', '2', '--arg3', 'some']);
|
|
|
|
|
expect(commander.arg0).toEqual('arg0Value');
|
|
|
|
|
expect(commander.arg1).toEqual('arg1Value');
|
2016-02-23 10:56:01 -05:00
|
|
|
expect(commander.arg2).toEqual(2);
|
2016-11-14 14:59:00 +00:00
|
|
|
expect(commander.arg3).toEqual('some');
|
|
|
|
|
expect(commander.arg4).toEqual('arg4Value');
|
2016-02-23 10:56:01 -05:00
|
|
|
done();
|
|
|
|
|
});
|
2016-04-04 20:02:34 -04:00
|
|
|
|
2016-11-14 14:59:00 +00:00
|
|
|
it('should load properly definitions from env', (done) => {
|
|
|
|
|
commander.loadDefinitions(testDefinitions);
|
2016-02-23 10:56:01 -05:00
|
|
|
commander.parse([], {
|
2016-11-14 14:59:00 +00:00
|
|
|
'PROGRAM_ARG_0': 'arg0ENVValue',
|
|
|
|
|
'PROGRAM_ARG_1': 'arg1ENVValue',
|
|
|
|
|
'PROGRAM_ARG_2': '3',
|
2016-02-23 10:56:01 -05:00
|
|
|
});
|
2016-11-14 14:59:00 +00:00
|
|
|
expect(commander.arg0).toEqual('arg0ENVValue');
|
|
|
|
|
expect(commander.arg1).toEqual('arg1ENVValue');
|
2016-02-23 10:56:01 -05:00
|
|
|
expect(commander.arg2).toEqual(3);
|
2016-11-14 14:59:00 +00:00
|
|
|
expect(commander.arg4).toEqual('arg4Value');
|
2016-02-23 10:56:01 -05:00
|
|
|
done();
|
|
|
|
|
});
|
2016-04-04 20:02:34 -04:00
|
|
|
|
2016-11-14 14:59:00 +00:00
|
|
|
it('should load properly use args over env', (done) => {
|
|
|
|
|
commander.loadDefinitions(testDefinitions);
|
2016-11-30 13:48:49 +00:00
|
|
|
commander.parse(['node','./CLI.spec.js','--arg0', 'arg0Value', '--arg4', ''], {
|
2016-11-14 14:59:00 +00:00
|
|
|
'PROGRAM_ARG_0': 'arg0ENVValue',
|
|
|
|
|
'PROGRAM_ARG_1': 'arg1ENVValue',
|
|
|
|
|
'PROGRAM_ARG_2': '4',
|
2016-11-30 13:48:49 +00:00
|
|
|
'PROGRAM_ARG_4': 'arg4ENVValue'
|
2016-02-23 10:56:01 -05:00
|
|
|
});
|
2016-11-14 14:59:00 +00:00
|
|
|
expect(commander.arg0).toEqual('arg0Value');
|
|
|
|
|
expect(commander.arg1).toEqual('arg1ENVValue');
|
2016-02-23 10:56:01 -05:00
|
|
|
expect(commander.arg2).toEqual(4);
|
2016-11-30 13:48:49 +00:00
|
|
|
expect(commander.arg4).toEqual('');
|
2016-02-23 10:56:01 -05:00
|
|
|
done();
|
|
|
|
|
});
|
2016-04-04 20:02:34 -04:00
|
|
|
|
2016-11-14 14:59:00 +00:00
|
|
|
it('should fail in action as port is invalid', (done) => {
|
|
|
|
|
commander.loadDefinitions(testDefinitions);
|
2016-02-24 08:50:04 -05:00
|
|
|
expect(()=> {
|
2016-11-14 14:59:00 +00:00
|
|
|
commander.parse(['node','./CLI.spec.js','--arg0', 'arg0Value'], {
|
|
|
|
|
'PROGRAM_ARG_0': 'arg0ENVValue',
|
|
|
|
|
'PROGRAM_ARG_1': 'arg1ENVValue',
|
|
|
|
|
'PROGRAM_ARG_2': 'hello',
|
2016-02-24 08:50:04 -05:00
|
|
|
});
|
2016-11-14 14:59:00 +00:00
|
|
|
}).toThrow('arg2 is invalid');
|
2016-04-04 20:02:34 -04:00
|
|
|
done();
|
|
|
|
|
});
|
|
|
|
|
|
2016-11-14 14:59:00 +00:00
|
|
|
it('should not override config.json', (done) => {
|
|
|
|
|
commander.loadDefinitions(testDefinitions);
|
|
|
|
|
commander.parse(['node','./CLI.spec.js','--arg0', 'arg0Value', './spec/configs/CLIConfig.json'], {
|
|
|
|
|
'PROGRAM_ARG_0': 'arg0ENVValue',
|
|
|
|
|
'PROGRAM_ARG_1': 'arg1ENVValue',
|
2016-04-04 20:02:34 -04:00
|
|
|
});
|
2016-12-07 15:17:05 -08:00
|
|
|
const options = commander.getOptions();
|
2016-04-04 20:02:34 -04:00
|
|
|
expect(options.arg2).toBe(8888);
|
2016-11-14 14:59:00 +00:00
|
|
|
expect(options.arg3).toBe('hello'); //config value
|
2016-04-04 20:02:34 -04:00
|
|
|
expect(options.arg4).toBe('/1');
|
|
|
|
|
done();
|
|
|
|
|
});
|
|
|
|
|
|
2016-11-14 14:59:00 +00:00
|
|
|
it('should fail with invalid values in JSON', (done) => {
|
|
|
|
|
commander.loadDefinitions(testDefinitions);
|
2016-04-04 20:02:34 -04:00
|
|
|
expect(() => {
|
2016-11-14 14:59:00 +00:00
|
|
|
commander.parse(['node','./CLI.spec.js','--arg0', 'arg0Value', './spec/configs/CLIConfigFail.json'], {
|
|
|
|
|
'PROGRAM_ARG_0': 'arg0ENVValue',
|
|
|
|
|
'PROGRAM_ARG_1': 'arg1ENVValue',
|
2016-04-04 20:02:34 -04:00
|
|
|
});
|
2016-11-14 14:59:00 +00:00
|
|
|
}).toThrow('arg2 is invalid');
|
2016-04-04 20:02:34 -04:00
|
|
|
done();
|
|
|
|
|
});
|
|
|
|
|
|
2016-11-14 14:59:00 +00:00
|
|
|
it('should fail when too many apps are set', (done) => {
|
|
|
|
|
commander.loadDefinitions(testDefinitions);
|
2016-04-04 20:02:34 -04:00
|
|
|
expect(() => {
|
2016-11-14 14:59:00 +00:00
|
|
|
commander.parse(['node','./CLI.spec.js','./spec/configs/CLIConfigFailTooManyApps.json']);
|
|
|
|
|
}).toThrow('Multiple apps are not supported');
|
2016-04-04 20:02:34 -04:00
|
|
|
done();
|
|
|
|
|
});
|
|
|
|
|
|
2016-11-14 14:59:00 +00:00
|
|
|
it('should load config from apps', (done) => {
|
|
|
|
|
commander.loadDefinitions(testDefinitions);
|
|
|
|
|
commander.parse(['node', './CLI.spec.js', './spec/configs/CLIConfigApps.json']);
|
2016-12-07 15:17:05 -08:00
|
|
|
const options = commander.getOptions();
|
2016-11-14 14:59:00 +00:00
|
|
|
expect(options.arg1).toBe('my_app');
|
2016-04-04 20:02:34 -04:00
|
|
|
expect(options.arg2).toBe(8888);
|
2016-11-14 14:59:00 +00:00
|
|
|
expect(options.arg3).toBe('hello'); //config value
|
2016-04-04 20:02:34 -04:00
|
|
|
expect(options.arg4).toBe('/1');
|
|
|
|
|
done();
|
|
|
|
|
});
|
|
|
|
|
|
2016-11-14 14:59:00 +00:00
|
|
|
it('should fail when passing an invalid arguement', (done) => {
|
|
|
|
|
commander.loadDefinitions(testDefinitions);
|
2016-04-04 20:02:34 -04:00
|
|
|
expect(() => {
|
2016-11-14 14:59:00 +00:00
|
|
|
commander.parse(['node', './CLI.spec.js', './spec/configs/CLIConfigUnknownArg.json']);
|
|
|
|
|
}).toThrow('error: unknown option myArg');
|
2016-02-24 08:50:04 -05:00
|
|
|
done();
|
|
|
|
|
});
|
2016-04-04 20:02:34 -04:00
|
|
|
});
|
2016-11-14 14:59:00 +00:00
|
|
|
|
|
|
|
|
describe('definitions', () => {
|
|
|
|
|
it('should have valid types', () => {
|
2016-12-07 15:17:05 -08:00
|
|
|
for (const key in definitions) {
|
|
|
|
|
const definition = definitions[key];
|
2016-11-14 14:59:00 +00:00
|
|
|
expect(typeof definition).toBe('object');
|
2016-11-14 16:23:31 +00:00
|
|
|
if (typeof definition.env !== 'undefined') {
|
|
|
|
|
expect(typeof definition.env).toBe('string');
|
|
|
|
|
}
|
|
|
|
|
expect(typeof definition.help).toBe('string');
|
2016-11-14 14:59:00 +00:00
|
|
|
if (typeof definition.required !== 'undefined') {
|
2016-11-14 16:23:31 +00:00
|
|
|
expect(typeof definition.required).toBe('boolean');
|
|
|
|
|
}
|
|
|
|
|
if (typeof definition.action !== 'undefined') {
|
|
|
|
|
expect(typeof definition.action).toBe('function');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
2017-09-05 17:51:11 -04:00
|
|
|
|
|
|
|
|
it('should throw when using deprecated facebookAppIds', () => {
|
|
|
|
|
expect(() => {
|
|
|
|
|
definitions.facebookAppIds.action()
|
|
|
|
|
}).toThrow();
|
|
|
|
|
});
|
2016-11-14 16:23:31 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('LiveQuery definitions', () => {
|
|
|
|
|
it('should have valid types', () => {
|
2016-12-07 15:17:05 -08:00
|
|
|
for (const key in liveQueryDefinitions) {
|
|
|
|
|
const definition = liveQueryDefinitions[key];
|
2016-11-14 16:23:31 +00:00
|
|
|
expect(typeof definition).toBe('object');
|
|
|
|
|
if (typeof definition.env !== 'undefined') {
|
2016-11-14 14:59:00 +00:00
|
|
|
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');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
2016-11-14 15:14:48 +00:00
|
|
|
});
|