mirror of
https://gitlab.silvrtree.co.uk/martind2000/nci.git
synced 2025-02-10 19:29:15 +00:00
add basic shell command tests
This commit is contained in:
parent
6cce9c9bbf
commit
75734b8d9a
@ -4,22 +4,22 @@ var ParentCommand = require('./spawn').Command,
|
||||
inherits = require('util').inherits;
|
||||
|
||||
function Command(params) {
|
||||
params = params || {};
|
||||
ParentCommand.call(this, params);
|
||||
this.shell = params.shell || '/bin/sh';
|
||||
}
|
||||
|
||||
exports.ShellCommand = Command;
|
||||
exports.Command = Command;
|
||||
|
||||
inherits(Command, ParentCommand);
|
||||
|
||||
|
||||
/**
|
||||
* Executes `params.cmd` (e.g. 'echo 1 && echo 2') in `params.shell`
|
||||
* (e.g. '/bin/sh') with `params.options`
|
||||
* Executes `params.cmd` (e.g. 'echo 1 && echo 2') in a `shell`
|
||||
* (which was set at constructor) with `params.options`
|
||||
*/
|
||||
Command.prototype.run = function(params, callback) {
|
||||
if (!params.shell) return callback(new Error('`shell` is not set'));
|
||||
if (!params.cmd) return callback(new Error('`cmd` is not set'));
|
||||
ParentCommand.prototype.exec.call(this, {
|
||||
cmd: params.shell, args: ['-c', params.cmd], options: params.options
|
||||
ParentCommand.prototype.run.call(this, {
|
||||
cmd: this.shell, args: ['-c', params.cmd], options: params.options
|
||||
}, callback);
|
||||
};
|
||||
|
@ -27,6 +27,7 @@ Command.prototype.run = function(params, callback) {
|
||||
var cmd = spawn(params.cmd, params.args, params.options);
|
||||
cmd.stdout.on('data', function(data) {
|
||||
if (self.isEmit) self.emit('stdout', data);
|
||||
// TODO: join stdout only if flag is set
|
||||
stdout += data;
|
||||
});
|
||||
cmd.stderr.on('data', function(data) {
|
||||
@ -34,11 +35,13 @@ Command.prototype.run = function(params, callback) {
|
||||
cmd.kill();
|
||||
});
|
||||
cmd.on('exit', function(code) {
|
||||
var err = null;
|
||||
if (code !== 0) err = new Error(
|
||||
'Scm command exits with non-zero code: ' + code
|
||||
);
|
||||
callback(err, stdout);
|
||||
if (code !== 0) callback(new Error(
|
||||
'Spawned command exits with non-zero code: ' + code
|
||||
));
|
||||
});
|
||||
// TODO; callback should be called only once (port once from underscore)
|
||||
cmd.on('close', function() {
|
||||
callback(null, stdout);
|
||||
});
|
||||
return cmd;
|
||||
};
|
||||
|
@ -6,7 +6,7 @@
|
||||
"nci": "bin/nci"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "mocha --bail --reporter=spec test"
|
||||
"test": "mocha --bail --reporter=spec test/index"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
|
3
test/commands/index.js
Normal file
3
test/commands/index.js
Normal file
@ -0,0 +1,3 @@
|
||||
'use strict';
|
||||
|
||||
require('./shell');
|
31
test/commands/shell.js
Normal file
31
test/commands/shell.js
Normal file
@ -0,0 +1,31 @@
|
||||
'use strict';
|
||||
|
||||
var expect = require('expect.js'),
|
||||
ShellCommand = require('../../lib/command/shell').Command;
|
||||
|
||||
describe('Shell command', function() {
|
||||
|
||||
var shellCommand;
|
||||
it('Should be created without errors', function() {
|
||||
shellCommand = new ShellCommand({
|
||||
isEmit: true
|
||||
});
|
||||
});
|
||||
|
||||
it('Default shell should be sh', function() {
|
||||
expect(shellCommand.shell).equal('/bin/sh');
|
||||
});
|
||||
|
||||
it('echo "Hello world" should be done', function(done) {
|
||||
var stdout = '';
|
||||
shellCommand.on('stdout', function(data) {
|
||||
stdout += data;
|
||||
});
|
||||
shellCommand.run({cmd: 'echo "Hello world"'}, function(err) {
|
||||
expect(err).not.ok();
|
||||
expect(stdout).equal('Hello world\n');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
});
|
4
test/index.js
Normal file
4
test/index.js
Normal file
@ -0,0 +1,4 @@
|
||||
'use strict';
|
||||
|
||||
require('./commands');
|
||||
require('./scm')
|
Loading…
Reference in New Issue
Block a user