Skip to content

Commit

Permalink
work on #742 withLocalConfig - create .yo-rc.json before generator is…
Browse files Browse the repository at this point in the history
… created
  • Loading branch information
gruppjo committed Feb 6, 2015
1 parent f8b7f3e commit 3726d30
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
7 changes: 4 additions & 3 deletions lib/test/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,12 +213,13 @@ exports.restorePrompt = function (generator) {
};

/**
* Provide mocked values to the config
* Provide mocked values to the config, by creating a .yo-rc.json in the testdirectory.
* Needs to be called before the generator is created
* @param {Generator} generator - a Yeoman generator
* @param {Ojbect} localConfig - localConfig - should look just like if called config.getAll()
* @param {Ojbect} localConfig - localConfig - should look just like if you called config.getAll()
*/
exports.mockLocalConfig = function (generator, localConfig) {
generator.config.defaults(localConfig);
fs.writeFileSync('.yo-rc.json', JSON.stringify(localConfig, null, 2));
};

/**
Expand Down
11 changes: 6 additions & 5 deletions lib/test/run-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ RunContext.prototype._run = function () {
this.env.registerStub(this.Generator, namespace);
}

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

this.generator = this.env.create(namespace, {
arguments: this.args,
options: _.extend({
Expand All @@ -87,10 +92,6 @@ 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 @@ -203,7 +204,7 @@ RunContext.prototype.withGenerators = function (dependencies) {

/**
* mock the local configuration with the provided config
* @param {Object} localConfig - should look just like if called config.getAll()
* @param {Object} localConfig - should look just like if you called config.getAll()
* @return {this}
*/
RunContext.prototype.withLocalConfig = function (localConfig) {
Expand Down
6 changes: 4 additions & 2 deletions test/run-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -387,8 +387,10 @@ describe('RunContext', function () {
describe('#withLocalConfig()', function () {
it('provides config to the generator', function (done) {
this.ctx.withLocalConfig({
some: true,
data: 'here'
'*': {
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');
Expand Down

0 comments on commit 3726d30

Please sign in to comment.