@auttam/easycli
Version:
A quick and easy way of creating cli for your npm package.
122 lines (121 loc) • 5.26 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const expect = require('chai').expect;
const index_1 = require("../../index");
var argv = process.argv;
describe('No-Command Mode', () => {
describe("main()", () => {
afterEach(() => {
process.argv = argv;
});
it('is called', () => __awaiter(void 0, void 0, void 0, function* () {
var mainCalled = false;
class SampleProgram extends index_1.Program {
main() {
mainCalled = true;
}
}
yield index_1.Program.run(new SampleProgram());
expect(mainCalled).to.equal(true);
}));
it('is injected with $params', () => __awaiter(void 0, void 0, void 0, function* () {
var injectedParams = null;
class SampleProgram extends index_1.Program {
main(a, b, c, $params) {
injectedParams = $params;
}
}
yield index_1.Program.run(new SampleProgram());
expect(injectedParams).to.not.be.null;
expect(injectedParams).to.have.property("$unknown");
}));
it('is injected with $options', () => __awaiter(void 0, void 0, void 0, function* () {
var injectedOptions = null;
class SampleProgram extends index_1.Program {
main(a, b, c, $options) {
injectedOptions = $options;
}
}
yield index_1.Program.run(new SampleProgram());
expect(injectedOptions).to.not.be.null;
expect(injectedOptions).to.have.property("$unknown");
}));
it('receives parameters appropriately', () => __awaiter(void 0, void 0, void 0, function* () {
var _o = 0, _a = 0, _b = 0, _c = 0, _p = 0;
process.argv = ['node', './', '1', '2', '3', '4', '--x'];
class SampleProgram extends index_1.Program {
main(a, $params, b, c, $options) {
_a = a;
_b = b;
_c = c;
_p = $params;
_o = $options;
}
}
yield index_1.Program.run(new SampleProgram());
expect(_a).to.equal(1);
expect(_b).to.equal(2);
expect(_c).to.equal(3);
expect(_p.$unknown).to.eql([4]);
expect("x" in _o.$unknown).to.equal(true);
}));
it('is called asynchronously', () => __awaiter(void 0, void 0, void 0, function* () {
var executionResult1 = 0;
var executionResult2 = 0;
var executionResult3 = 0;
function asyncFunc(val) {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(val);
}, 300);
});
}
class SampleProgram extends index_1.Program {
main() {
return __awaiter(this, void 0, void 0, function* () {
executionResult1 = yield asyncFunc(100);
return 200;
});
}
onExit(error, execResult) {
return __awaiter(this, void 0, void 0, function* () {
executionResult2 = execResult;
return yield asyncFunc(300);
});
}
}
executionResult3 = yield SampleProgram.run(new SampleProgram());
expect(executionResult1).to.equal(100);
expect(executionResult2).to.equal(200);
expect(executionResult3).to.equal(300);
}));
});
describe("onExit()", () => {
afterEach(() => {
process.argv = argv;
});
it('is called before exiting', () => __awaiter(void 0, void 0, void 0, function* () {
var mainCalled = null, exitCalled = null;
class SampleProgram extends index_1.Program {
main() {
mainCalled = true;
}
onExit() {
exitCalled = true;
}
}
yield index_1.Program.run(new SampleProgram());
expect(mainCalled).to.be.true;
expect(exitCalled).to.be.true;
}));
});
});