Skip to content

Commit

Permalink
work on #742 withLocalConfig - basic tests & implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
gruppjo committed Jan 23, 2015
1 parent 461a181 commit f8b7f3e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/test/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,15 @@ exports.restorePrompt = function (generator) {
generator.env.adapter.prompt.restoreDefaultPrompts();
};

/**
* Provide mocked values to the config
* @param {Generator} generator - a Yeoman generator
* @param {Ojbect} localConfig - localConfig - should look just like if called config.getAll()
*/
exports.mockLocalConfig = function (generator, localConfig) {
generator.config.defaults(localConfig);
};

/**
* Create a simple, dummy generator
*/
Expand Down
16 changes: 16 additions & 0 deletions lib/test/run-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ var RunContext = module.exports = function RunContext(Generator, settings) {
this.args = [];
this.options = {};
this.answers = {};
this.localConfig = null;
this.dependencies = [];
this.Generator = Generator;
this.settings = _.extend({ tmpdir: true }, settings);
Expand Down Expand Up @@ -86,6 +87,10 @@ RunContext.prototype._run = function () {

helpers.mockPrompt(this.generator, this.answers);

if (this.localConfig) { // only mock local config when withLocalConfig was called
helpers.mockLocalConfig(this.generator, this.localConfig);
}

this.generator.on('error', this.emit.bind(this, 'error'));
this.generator.once('end', function () {
helpers.restorePrompt(this.generator);
Expand Down Expand Up @@ -196,6 +201,17 @@ RunContext.prototype.withGenerators = function (dependencies) {
return this;
};

/**
* mock the local configuration with the provided config
* @param {Object} localConfig - should look just like if called config.getAll()
* @return {this}
*/
RunContext.prototype.withLocalConfig = function (localConfig) {
assert(_.isObject(localConfig), 'config should be an object');
this.localConfig = localConfig;
return this;
};

/**
* Add a callback to be called after the generator has ran
* @deprecated `onEnd` is deprecated, use .on('end', onEndHandler) instead.
Expand Down
13 changes: 13 additions & 0 deletions test/run-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -383,4 +383,17 @@ describe('RunContext', function () {
}.bind(this));
});
});

describe('#withLocalConfig()', function () {
it('provides config to the generator', function (done) {
this.ctx.withLocalConfig({
some: true,
data: 'here'
}).on('ready', function () {
assert.equal(this.ctx.generator.config.get('some'), true);
assert.equal(this.ctx.generator.config.get('data'), 'here');
done();
}.bind(this));
});
});
});

0 comments on commit f8b7f3e

Please sign in to comment.