Skip to content

Commit

Permalink
Fix bug in Base#run implementation and add more complete test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
SBoudrias committed Jan 12, 2014
1 parent 4adfa1b commit 3642640
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 16 deletions.
14 changes: 6 additions & 8 deletions lib/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -293,8 +293,8 @@ Base.prototype.argument = function argument(name, config) {
* none is given, the same values used to initialize the invoker are
* used to initialize the invoked.
*
* @param {String|Array} args
* @param {Function} cb
* @param {String|Array} [args]
* @param {Function} [cb]
*/

Base.prototype.run = function run(args, cb) {
Expand All @@ -308,20 +308,18 @@ Base.prototype.run = function run(args, cb) {
args = this.args;
}

args = _.isString(args) ? args.split(' ') : args;
cb = cb || function () {};

var runHooks = function () {
self.runHooks(cb);
};

var methods = Object.keys(Object.getPrototypeOf(this));

var resolve = function (method) {
var rules = {
underscore: method.charAt(0) !== '_',
runnable: _.isFunction(Object.getPrototypeOf(self)[method]),
initialize: !/^(constructor|initialize)$/.test(method),
valid: function () {
return this.underscore && this.initialize;
return this.underscore && this.initialize && this.runnable;
}
};

Expand Down Expand Up @@ -363,7 +361,7 @@ Base.prototype.run = function run(args, cb) {
};
};

async.series(methods.map(resolve), runHooks);
async.series(methods.map(resolve), this.runHooks.bind(this, cb));

return this;
};
Expand Down
43 changes: 35 additions & 8 deletions test/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ describe('yeoman.generators.Base', function () {
});
});

describe('.extend', function () {
describe('.extend()', function () {
it('create a new object inheriting the Generator', function () {
assert.ok(new (Base.extend())([], { resolved: 'path/', env: this.env }) instanceof Base);
});
Expand All @@ -77,16 +77,15 @@ describe('yeoman.generators.Base', function () {
});
});

describe('#run', function () {
describe('#run()', function () {
beforeEach(function () {
this.TestGenerator = helpers.createDummyGenerator();
this.TestGenerator.prototype.foo = sinon.spy();
this.execSpy = this.TestGenerator.prototype.exec = sinon.spy();
this.testGen = new this.TestGenerator([], {
resolved: 'ember:all',
resolved: 'generator-ember/all/index.js',
namespace: 'dummy',
env: this.env
});
this.testGen.foo = sinon.spy();
});

it('run all methods in the given generator', function (done) {
Expand All @@ -98,10 +97,38 @@ describe('yeoman.generators.Base', function () {
assert.ok(this.testGen._running);
});

it('run prototype methods', function (done) {
it('run prototype methods (not instances one)', function (done) {
this.testGen.exec = sinon.spy();
this.testGen.run(function () {
assert.ok(this.execSpy.calledOnce);
assert.equal(this.testGen.exec.callCount, 0);
done();
}.bind(this));
});

it('don\'t try running prototype attributes', function (done) {
this.TestGenerator.prototype.prop = 'something';
this.testGen.run(done);
});

it('pass an array of arguments to the called methods', function (done) {
this.testGen.run(['some', 'args'], function () {
assert(this.execSpy.withArgs('some', 'args').calledOnce);
done();
}.bind(this));
});

it('pass string of arguments to the called methods', function (done) {
this.testGen.run('some args', function () {
assert(this.execSpy.withArgs('some', 'args').calledOnce);
done();
}.bind(this));
});

it('pass instance .args property to the called methods', function (done) {
this.testGen.args = ['2', 'args'];
this.testGen.run(function () {
assert.ok(this.TestGenerator.prototype.foo.calledOnce);
assert.equal(this.testGen.foo.callCount, 0);
assert(this.execSpy.withArgs('2', 'args').calledOnce);
done();
}.bind(this));
});
Expand Down

0 comments on commit 3642640

Please sign in to comment.