diff --git a/lib/command/shell.js b/lib/command/shell.js index 73a6927..bc71cd3 100644 --- a/lib/command/shell.js +++ b/lib/command/shell.js @@ -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); }; diff --git a/lib/command/spawn.js b/lib/command/spawn.js index 71bd9c8..d2e6710 100644 --- a/lib/command/spawn.js +++ b/lib/command/spawn.js @@ -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; }; diff --git a/package.json b/package.json index 6412bbe..0aca218 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "nci": "bin/nci" }, "scripts": { - "test": "mocha --bail --reporter=spec test" + "test": "mocha --bail --reporter=spec test/index" }, "repository": { "type": "git", diff --git a/test/commands/index.js b/test/commands/index.js new file mode 100644 index 0000000..bd72714 --- /dev/null +++ b/test/commands/index.js @@ -0,0 +1,3 @@ +'use strict'; + +require('./shell'); diff --git a/test/commands/shell.js b/test/commands/shell.js new file mode 100644 index 0000000..e2691ce --- /dev/null +++ b/test/commands/shell.js @@ -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(); + }); + }); + +}); diff --git a/test/index.js b/test/index.js new file mode 100644 index 0000000..b0a54db --- /dev/null +++ b/test/index.js @@ -0,0 +1,4 @@ +'use strict'; + +require('./commands'); +require('./scm')