Skip to content

Commit

Permalink
Added generator#spawnCommandSync()
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesMessinger authored and SBoudrias committed May 7, 2015
1 parent c094740 commit fd336a1
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 22 deletions.
27 changes: 21 additions & 6 deletions lib/actions/spawn_command.js
Expand Up @@ -3,16 +3,31 @@ var _ = require('lodash');
var spawn = require('cross-spawn');

/**
* Normalize a command across OS and spawn it.
*
* @param {String} command
* @param {Array} args
*
* @mixin
* @alias actions/spawn_command
*/
var spawnCommand = module.exports;

module.exports = function spawnCommand(command, args, opt) {
/**
* Normalize a command across OS and spawn it (asynchronously).
*
* @param {String} command
* @param {Array} args
* @param {object} [opt]
*/
spawnCommand.spawnCommand = function spawnCommand(command, args, opt) {
opt = opt || {};
return spawn(command, args, _.defaults(opt, { stdio: 'inherit' }));
};

/**
* Normalize a command across OS and spawn it (synchronously).
*
* @param {String} command
* @param {Array} args
* @param {object} [opt]
*/
spawnCommand.spawnCommandSync = function spawnCommandSync(command, args, opt) {
opt = opt || {};
return spawn.sync(command, args, _.defaults(opt, { stdio: 'inherit' }));
};
2 changes: 1 addition & 1 deletion lib/base.js
Expand Up @@ -188,8 +188,8 @@ _.extend(Base.prototype, require('./actions/install'));
_.extend(Base.prototype, require('./actions/remote'));
_.extend(Base.prototype, deprecate.object('this.<%= name %>() is deprecated. Use require("html-wiring").<%= name %>() instead.', wiring));
_.extend(Base.prototype, require('./actions/help'));
_.extend(Base.prototype, require('./actions/spawn_command'));

Base.prototype.spawnCommand = require('./actions/spawn_command');
Base.prototype.user = require('./actions/user');
Base.prototype.invoke = deprecate(
'generator#invoke() is deprecated. Use generator#composeWith() - see http://yeoman.io/authoring/composability.html',
Expand Down
56 changes: 41 additions & 15 deletions test/spawn_command.js
Expand Up @@ -5,31 +5,57 @@ var assert = require('assert');
var proxyquire = require('proxyquire');
var sinon = require('sinon');

describe('#spawnCommand()', function () {
describe('generators.Base (actions/spawn_command)', function () {
beforeEach(function () {
this.crossSpawn = sinon.spy();
this.crossSpawn.sync = sinon.spy();
this.spawn = proxyquire('../lib/actions/spawn_command', {
'cross-spawn': this.crossSpawn
});
});

it('provide default options', function () {
this.spawn('foo');
sinon.assert.calledWith(this.crossSpawn, 'foo', undefined, { stdio: 'inherit' });
});
describe('#spawnCommand()', function () {
it('provide default options', function () {
this.spawn.spawnCommand('foo');
sinon.assert.calledWith(this.crossSpawn, 'foo', undefined, { stdio: 'inherit' });
});

it('pass arguments', function () {
this.spawn('foo', 'bar');
sinon.assert.calledWith(this.crossSpawn, 'foo', 'bar', { stdio: 'inherit' });
});
it('pass arguments', function () {
this.spawn.spawnCommand('foo', 'bar');
sinon.assert.calledWith(this.crossSpawn, 'foo', 'bar', { stdio: 'inherit' });
});

it('pass options', function () {
this.spawn('foo', undefined, { foo: 1 });
sinon.assert.calledWith(this.crossSpawn, 'foo', undefined, { foo: 1, stdio: 'inherit' });
it('pass options', function () {
this.spawn.spawnCommand('foo', undefined, { foo: 1 });
sinon.assert.calledWith(this.crossSpawn, 'foo', undefined, { foo: 1, stdio: 'inherit' });
});

it('allow overriding default options', function () {
this.spawn.spawnCommand('foo', undefined, { stdio: 'wut' });
sinon.assert.calledWith(this.crossSpawn, 'foo', undefined, { stdio: 'wut' });
});
});

it('allow overriding default options', function () {
this.spawn('foo', undefined, { stdio: 'wut' });
sinon.assert.calledWith(this.crossSpawn, 'foo', undefined, { stdio: 'wut' });
describe('#spawnCommandSync()', function () {
it('provide default options', function () {
this.spawn.spawnCommandSync('foo');
sinon.assert.calledWith(this.crossSpawn.sync, 'foo', undefined, { stdio: 'inherit' });
});

it('pass arguments', function () {
this.spawn.spawnCommandSync('foo', 'bar');
sinon.assert.calledWith(this.crossSpawn.sync, 'foo', 'bar', { stdio: 'inherit' });
});

it('pass options', function () {
this.spawn.spawnCommandSync('foo', undefined, { foo: 1 });
sinon.assert.calledWith(this.crossSpawn.sync, 'foo', undefined, { foo: 1, stdio: 'inherit' });
});

it('allow overriding default options', function () {
this.spawn.spawnCommandSync('foo', undefined, { stdio: 'wut' });
sinon.assert.calledWith(this.crossSpawn.sync, 'foo', undefined, { stdio: 'wut' });
});
});
});

0 comments on commit fd336a1

Please sign in to comment.