bitcore-node
Version:
A blockchain indexing node with extended capabilities using bitcore
251 lines • 11.2 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const chai_1 = require("chai");
const mocha_1 = require("mocha");
const sinon_1 = __importDefault(require("sinon"));
const parseArgv_1 = __importDefault(require("../../../src/utils/parseArgv"));
(0, mocha_1.describe)('parseArgv Util', () => {
let argv;
const sandbox = sinon_1.default.createSandbox();
beforeEach(function () {
argv = sandbox.stub(process, 'argv');
});
afterEach(function () {
sandbox.restore();
});
(0, mocha_1.describe)('legacy (string) args', function () {
it('should parse required legacy arg', function () {
argv.value(['--DEBUG', '1']);
const args = (0, parseArgv_1.default)(['DEBUG'], []);
(0, chai_1.expect)(!!args.DEBUG).to.equal(true);
});
it('should parse required legacy arg with intuitively falsy value as true', function () {
argv.value(['--DEBUG', '0']);
const args = (0, parseArgv_1.default)(['DEBUG'], []);
(0, chai_1.expect)(!!args.DEBUG).to.equal(true);
});
it('should parse required legacy arg with string value', function () {
argv.value(['--CONFIG', '../hello/world']);
const args = (0, parseArgv_1.default)(['CONFIG'], []);
(0, chai_1.expect)(!!args.CONFIG).to.equal(true);
(0, chai_1.expect)(args.CONFIG).to.equal('../hello/world');
});
it('should parse required legacy arg without value', function () {
argv.value(['--DEBUG']);
try {
(0, parseArgv_1.default)(['DEBUG'], []);
throw new Error('should have thrown');
}
catch (err) {
(0, chai_1.expect)(err.message).to.equal('DEBUG is missing a value of string type');
}
});
it('should parse optional legacy arg', function () {
argv.value(['--DEBUG', '1']);
const args = (0, parseArgv_1.default)([], ['DEBUG']);
(0, chai_1.expect)(!!args.DEBUG).to.equal(true);
});
it('should parse optional legacy arg with intuitively falsy value as true', function () {
argv.value(['--DEBUG', '0']);
const args = (0, parseArgv_1.default)([], ['DEBUG']);
(0, chai_1.expect)(!!args.DEBUG).to.equal(true);
});
it('should parse optional legacy arg without value', function () {
argv.value(['--DEBUG']);
try {
(0, parseArgv_1.default)([], ['DEBUG']);
throw new Error('should have thrown');
}
catch (err) {
(0, chai_1.expect)(err.message).to.equal('DEBUG is missing a value of string type');
}
});
});
(0, mocha_1.describe)('string', function () {
it('should parse required arg', function () {
argv.value(['--CONFIG', 'hello world!']);
const args = (0, parseArgv_1.default)([{ arg: 'CONFIG', type: 'string' }], []);
(0, chai_1.expect)(args.CONFIG).to.equal('hello world!');
});
it('should throw if missing required arg', function () {
argv.value([]);
try {
(0, parseArgv_1.default)([{ arg: 'CONFIG', type: 'string' }], []);
throw new Error('should have thrown');
}
catch (err) {
(0, chai_1.expect)(err.message).to.equal('CONFIG is a required command argument');
}
});
it('should throw if required arg has missing val', function () {
argv.value(['--CONFIG']);
try {
(0, parseArgv_1.default)([{ arg: 'CONFIG', type: 'string' }], []);
throw new Error('should have thrown');
}
catch (err) {
(0, chai_1.expect)(err.message).to.equal('CONFIG is missing a value of string type');
}
});
it('should parse optional arg', function () {
argv.value(['--CONFIG', 'hello world!']);
const args = (0, parseArgv_1.default)([], [{ arg: 'CONFIG', type: 'string' }]);
(0, chai_1.expect)(args.CONFIG).to.equal('hello world!');
});
it('should throw if optional arg is missing val', function () {
argv.value(['--CONFIG']);
try {
(0, parseArgv_1.default)([], [{ arg: 'CONFIG', type: 'string' }]);
throw new Error('should have thrown');
}
catch (err) {
(0, chai_1.expect)(err.message).to.equal('CONFIG is missing a value of string type');
}
});
});
(0, mocha_1.describe)('boolean', function () {
it('should parse optional arg', function () {
argv.value(['--DEBUG', '1']);
const args = (0, parseArgv_1.default)([], [{ arg: 'DEBUG', type: 'bool' }]);
(0, chai_1.expect)(args.DEBUG).to.equal(true);
});
it('should parse optional arg with intuitively falsy value as false', function () {
argv.value(['--DEBUG', '0']);
const args = (0, parseArgv_1.default)([], [{ arg: 'DEBUG', type: 'bool' }]);
(0, chai_1.expect)(args.DEBUG).to.equal(false);
});
it('should parse optional arg without value', function () {
argv.value(['--DEBUG']);
const args = (0, parseArgv_1.default)([], [{ arg: 'DEBUG', type: 'bool' }]);
(0, chai_1.expect)(args.DEBUG).to.equal(true);
});
});
(0, mocha_1.describe)('int', function () {
it('should parse required arg', function () {
argv.value(['--DAYS', '123']);
const args = (0, parseArgv_1.default)([{ arg: 'DAYS', type: 'int' }], []);
(0, chai_1.expect)(args.DAYS).to.equal(123);
});
it('should throw if missing required arg', function () {
argv.value([]);
try {
(0, parseArgv_1.default)([{ arg: 'DAYS', type: 'int' }], []);
throw new Error('should have thrown');
}
catch (err) {
(0, chai_1.expect)(err.message).to.equal('DAYS is a required command argument');
}
});
it('should throw if missing required arg value', function () {
argv.value(['--DAYS']);
try {
(0, parseArgv_1.default)([{ arg: 'DAYS', type: 'int' }], []);
throw new Error('should have thrown');
}
catch (err) {
(0, chai_1.expect)(err.message).to.equal('DAYS is missing a value of int type');
}
});
it('should throw if required arg is the wrong type', function () {
argv.value(['--DAYS', 'true']);
try {
(0, parseArgv_1.default)([{ arg: 'DAYS', type: 'int' }], []);
throw new Error('should have thrown');
}
catch (err) {
(0, chai_1.expect)(err.message).to.equal('Invalid arg type. Expected int but got "true"');
}
});
it('should parse optional arg', function () {
argv.value(['--DAYS', '123.34']); // float gets parsed as an int
const args = (0, parseArgv_1.default)([], [{ arg: 'DAYS', type: 'int' }]);
(0, chai_1.expect)(args.DAYS).to.equal(123);
});
it('should throw if missing arg value', function () {
argv.value(['--DAYS']);
try {
(0, parseArgv_1.default)([{ arg: 'DAYS', type: 'int' }], []);
throw new Error('should have thrown');
}
catch (err) {
(0, chai_1.expect)(err.message).to.equal('DAYS is missing a value of int type');
}
});
it('should throw if arg is the wrong type', function () {
argv.value(['--DAYS', 'true']);
try {
(0, parseArgv_1.default)([{ arg: 'DAYS', type: 'int' }], []);
throw new Error('should have thrown');
}
catch (err) {
(0, chai_1.expect)(err.message).to.equal('Invalid arg type. Expected int but got "true"');
}
});
});
(0, mocha_1.describe)('float/number', function () {
it('should parse required arg', function () {
argv.value(['--DAYS', '123.23']);
const args = (0, parseArgv_1.default)([{ arg: 'DAYS', type: 'number' }], []);
(0, chai_1.expect)(args.DAYS).to.equal(123.23);
});
it('should throw if missing required arg', function () {
argv.value([]);
try {
(0, parseArgv_1.default)([{ arg: 'DAYS', type: 'number' }], []);
throw new Error('should have thrown');
}
catch (err) {
(0, chai_1.expect)(err.message).to.equal('DAYS is a required command argument');
}
});
it('should throw if missing required arg value', function () {
argv.value(['--DAYS']);
try {
(0, parseArgv_1.default)([{ arg: 'DAYS', type: 'number' }], []);
throw new Error('should have thrown');
}
catch (err) {
(0, chai_1.expect)(err.message).to.equal('DAYS is missing a value of number type');
}
});
it('should throw if required arg is the wrong type', function () {
argv.value(['--DAYS', 'true']);
try {
(0, parseArgv_1.default)([{ arg: 'DAYS', type: 'number' }], []);
throw new Error('should have thrown');
}
catch (err) {
(0, chai_1.expect)(err.message).to.equal('Invalid arg type. Expected float but got "true"');
}
});
it('should parse optional arg', function () {
argv.value(['--DAYS', '123.34']);
const args = (0, parseArgv_1.default)([], [{ arg: 'DAYS', type: 'number' }]);
(0, chai_1.expect)(args.DAYS).to.equal(123.34);
});
it('should throw if missing arg value', function () {
argv.value(['--DAYS']);
try {
(0, parseArgv_1.default)([{ arg: 'DAYS', type: 'number' }], []);
throw new Error('should have thrown');
}
catch (err) {
(0, chai_1.expect)(err.message).to.equal('DAYS is missing a value of number type');
}
});
it('should throw if arg is the wrong type', function () {
argv.value(['--DAYS', 'true']);
try {
(0, parseArgv_1.default)([{ arg: 'DAYS', type: 'number' }], []);
throw new Error('should have thrown');
}
catch (err) {
(0, chai_1.expect)(err.message).to.equal('Invalid arg type. Expected float but got "true"');
}
});
});
});
//# sourceMappingURL=parseArgv.unit.js.map