firmament-yargs
Version:
Typescript classes for building CLI node applications
197 lines • 8.74 kB
JavaScript
;
require('reflect-metadata');
const inversify_config_1 = require('../inversify.config');
const chai_1 = require('chai');
const path = require('path');
describe('SpawnAsync (no console out)', function () {
let spawn;
beforeEach(done => {
spawn = inversify_config_1.default.get('Spawn');
spawn.commandUtil.quiet = true;
done();
});
afterEach(done => {
spawn = null;
done();
});
describe('spawnShellCommandAsync (force error)', () => {
it('should report error', done => {
chai_1.expect(spawn).to.not.equal(null);
spawn.forceError = true;
spawn.spawnShellCommandAsync([], null, (err, result) => {
chai_1.expect(result).to.equal(null);
chai_1.expect(err).to.not.equal(null);
chai_1.expect(err.message).to.equal('force error: spawnShellCommandAsync');
});
done();
});
});
describe('spawnShellCommandAsync (null command)', () => {
it('should report error', done => {
chai_1.expect(spawn).to.not.equal(null);
spawn.spawnShellCommandAsync(null, null, (err, result) => {
chai_1.expect(result).to.equal(null);
chai_1.expect(err).to.not.equal(null);
chai_1.expect(err.message).to.equal('Bad argument');
});
done();
});
});
describe('spawnShellCommandAsync (empty array command)', () => {
it('should report error', done => {
chai_1.expect(spawn).to.not.equal(null);
spawn.spawnShellCommandAsync([], null, (err, result) => {
chai_1.expect(result).to.equal(null);
chai_1.expect(err).to.not.equal(null);
chai_1.expect(err.message).to.equal('Bad argument');
});
done();
});
});
});
describe('SpawnAsync (with console out)', function () {
let spawn;
const testScriptName = path.resolve(__dirname, 'test-00.js');
const testErrorScriptName = path.resolve(__dirname, 'test-error-00.js');
let cmd;
beforeEach(done => {
spawn = inversify_config_1.default.get('Spawn');
cmd = ['/usr/bin/env', 'node', testScriptName];
done();
});
describe('spawnShellCommandSync (invalid script,no options,no callback)', () => {
it('should execute script and return', done => {
chai_1.expect(spawn).to.not.equal(null);
cmd[2] = 'happyGoLucky';
let childProcess = spawn.spawnShellCommandAsync(cmd);
chai_1.expect(childProcess).to.not.equal(null);
chai_1.expect(childProcess.spawnfile).to.equal('/usr/bin/env');
childProcess.on('close', function (code) {
chai_1.expect(code).to.not.equal(0);
done();
});
});
});
describe('spawnShellCommandSync (invalid command,no options,no callback)', () => {
it('should execute script and return', done => {
chai_1.expect(spawn).to.not.equal(null);
let childProcess = spawn.spawnShellCommandAsync(['luv', 'muffins', 'forEveryone']);
chai_1.expect(childProcess).to.not.equal(null);
childProcess.on('error', function (err) {
chai_1.expect(err.code).to.equal('ENOENT');
});
childProcess.on('close', function (code) {
chai_1.expect(code).to.not.equal(0);
done();
});
});
});
describe('spawnShellCommandSync (valid command,no options,no callback)', () => {
it('should execute script and return', done => {
chai_1.expect(spawn).to.not.equal(null);
let childProcess = spawn.spawnShellCommandAsync(cmd);
chai_1.expect(childProcess).to.not.equal(null);
chai_1.expect(childProcess.spawnfile).to.equal('/usr/bin/env');
let result = '';
childProcess.stdout.on('data', function (data) {
result += data.toString();
});
childProcess.on('close', function (code) {
chai_1.expect(code).to.equal(0);
chai_1.expect(result).to.equal('test me: 0\ntest me: 1\ntest me: 2\n');
done();
});
});
});
describe('spawnShellCommandSync (valid command,empty options,final callback)', () => {
it('should execute script and return', done => {
chai_1.expect(spawn).to.not.equal(null);
spawn.spawnShellCommandAsync(cmd, {}, (err, result) => {
chai_1.expect(err).to.equal(null);
chai_1.expect(result).to.equal('test me: 0\ntest me: 1\ntest me: 2\n');
done();
});
});
});
describe('spawnShellCommandSync (invalid script,empty options,final callback)', () => {
it('should execute script and return', done => {
chai_1.expect(spawn).to.not.equal(null);
cmd[2] = 'happyGoLucky';
spawn.spawnShellCommandAsync(cmd, {}, (err, result) => {
chai_1.expect(err).to.not.equal(null);
chai_1.expect(result).to.equal('');
chai_1.expect(err.message).to.equal('spawn error: exit code 1');
done();
});
});
});
describe('spawnShellCommandSync (invalid script,empty options,status callback,final callback)', () => {
it('should execute script and return', done => {
chai_1.expect(spawn).to.not.equal(null);
cmd[2] = 'happyGoLucky';
spawn.spawnShellCommandAsync(cmd, {}, () => {
}, (err, result) => {
chai_1.expect(err).to.not.equal(null);
chai_1.expect(result).to.equal('');
chai_1.expect(err.message).to.equal('spawn error: exit code 1');
done();
});
});
});
describe('spawnShellCommandSync (invalid command,empty options,final callback)', () => {
it('should execute script and return', done => {
chai_1.expect(spawn).to.not.equal(null);
spawn.spawnShellCommandAsync(['luv', 'muffins', 'forEveryone'], {}, (err, result) => {
chai_1.expect(err).to.not.equal(null);
chai_1.expect(result).to.equal(null);
chai_1.expect(err.message).to.equal('spawn error: exit code ENOENT');
done();
});
});
});
describe('spawnShellCommandSync (invalid command,empty options,status callback,final callback)', () => {
it('should execute script and return', done => {
chai_1.expect(spawn).to.not.equal(null);
spawn.spawnShellCommandAsync(['luv', 'muffins', 'forEveryone'], {}, () => {
}, (err, result) => {
chai_1.expect(err).to.not.equal(null);
chai_1.expect(result).to.equal(null);
chai_1.expect(err.message).to.equal('spawn error: exit code ENOENT');
done();
});
});
});
describe('spawnShellCommandSync (valid command,empty options,status callback,final callback)', () => {
it('should execute script and return', done => {
chai_1.expect(spawn).to.not.equal(null);
let accumResult = '';
spawn.spawnShellCommandAsync(cmd, {}, (err, result) => {
chai_1.expect(err).to.equal(null);
accumResult += result;
}, (err, result) => {
chai_1.expect(err).to.equal(null);
chai_1.expect(result).to.equal(accumResult);
chai_1.expect(result).to.equal('test me: 0\ntest me: 1\ntest me: 2\n');
done();
});
});
});
describe('spawnShellCommandSync (valid command {non-zero exit code},empty options,status callback,final callback)', () => {
it('should execute script and return', done => {
chai_1.expect(spawn).to.not.equal(null);
let accumResult = '';
cmd = ['/usr/bin/env', 'node', testErrorScriptName];
spawn.spawnShellCommandAsync(cmd, {}, (err, result) => {
chai_1.expect(err).to.equal(null);
accumResult += result;
}, (err, result) => {
chai_1.expect(err).to.not.equal(null);
chai_1.expect(err.message).to.equal('spawn error: exit code 3');
chai_1.expect(result).to.equal(accumResult);
chai_1.expect(result).to.equal('test me: 0\ntest me: 1\n');
done();
});
});
});
});
//# sourceMappingURL=spawn-async.test.js.map