Skip to content

Commit

Permalink
Update to match latest yeoman-environment (providing support for yeom…
Browse files Browse the repository at this point in the history
…an-generator 0.23)
  • Loading branch information
SBoudrias committed May 3, 2016
1 parent 6be1e06 commit 4051bd8
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 47 deletions.
20 changes: 13 additions & 7 deletions lib/adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ var events = require('events');
var _ = require('lodash');
var inquirer = require('inquirer');
var sinon = require('sinon');
var Promise = require('pinkie-promise');

function DummyPrompt(answers, q) {
this.answers = answers;
this.question = q;
}

DummyPrompt.prototype.run = function (cb) {
DummyPrompt.prototype.run = function () {
var answer = this.answers[this.question.name];
var isSet;

Expand All @@ -35,17 +36,15 @@ DummyPrompt.prototype.run = function (cb) {
}
}

setImmediate(function () {
cb(answer);
});
return Promise.resolve(answer);
};

function TestAdapter(answers) {
answers = answers || {};
this.prompt = inquirer.createPromptModule();
this.promptModule = inquirer.createPromptModule();

Object.keys(this.prompt.prompts).forEach(function (promptName) {
this.prompt.registerPrompt(promptName, DummyPrompt.bind(DummyPrompt, answers));
Object.keys(this.promptModule.prompts).forEach(function (promptName) {
this.promptModule.registerPrompt(promptName, DummyPrompt.bind(DummyPrompt, answers));
}, this);

this.diff = sinon.spy();
Expand All @@ -71,6 +70,13 @@ function TestAdapter(answers) {
}, this);
}

TestAdapter.prototype.prompt = function (questions, cb) {
var promise = this.promptModule(questions);
promise.then(cb || _.noop);

return promise;
};

module.exports = {
DummyPrompt: DummyPrompt,
TestAdapter: TestAdapter
Expand Down
4 changes: 2 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ exports.testDirectory = function (dir, cb) {
*/

exports.mockPrompt = function (generator, answers) {
var promptModule = generator.env.adapter.prompt;
var promptModule = generator.env.adapter.promptModule;
answers = answers || {};
var DummyPrompt = adapter.DummyPrompt;

Expand All @@ -120,7 +120,7 @@ exports.mockPrompt = function (generator, answers) {
* @param {Generator} generator
*/
exports.restorePrompt = function (generator) {
generator.env.adapter.prompt.restoreDefaultPrompts();
generator.env.adapter.promptModule.restoreDefaultPrompts();
};

/**
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@
},
"license": "MIT",
"dependencies": {
"inquirer": "^0.12.0",
"inquirer": "^1.0.2",
"lodash": "^4.3.0",
"mkdirp": "^0.5.1",
"pinkie-promise": "^2.0.1",
"rimraf": "^2.4.4",
"sinon": "^1.17.2",
"yeoman-environment": "^1.5.2",
"yeoman-generator": "^0.22.5"
"yeoman-generator": "^0.23.0"
}
}
67 changes: 38 additions & 29 deletions test/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,92 +68,101 @@ describe('yeoman-test', function () {
helpers.mockPrompt(this.generator, { answer: 'foo' });
});

it('uses default values', function (done) {
this.generator.prompt([{ name: 'respuesta', type: 'input', default: 'bar' }], function (answers) {
assert.equal(answers.respuesta, 'bar');
done();
});
it('uses default values', function () {
return this.generator.prompt([{ name: 'respuesta', type: 'input', default: 'bar' }])
.then(function (answers) {
assert.equal(answers.respuesta, 'bar');
});
});

it('uses default values when no answer is passed', function (done) {
it('uses default values when no answer is passed', function () {
var generator = env.instantiate(helpers.createDummyGenerator());
helpers.mockPrompt(generator);
generator.prompt([{ name: 'respuesta', message: 'foo', type: 'input', default: 'bar' }], function (answers) {
return generator.prompt([
{ name: 'respuesta', message: 'foo', type: 'input', default: 'bar' }
]).then(function (answers) {
assert.equal(answers.respuesta, 'bar');
done();
});
});

it('supports `null` answer for `list` type', function (done) {
it('supports `null` answer for `list` type', function () {
var generator = env.instantiate(helpers.createDummyGenerator());

helpers.mockPrompt(generator, {
respuesta: null
});

generator.prompt([{ name: 'respuesta', message: 'foo', type: 'list', default: 'bar' }], function (answers) {
return generator.prompt([
{ name: 'respuesta', message: 'foo', type: 'list', default: 'bar' }
]).then(function (answers) {
assert.equal(answers.respuesta, null);
done();
});
});

it('treats `null` as no answer for `input` type', function (done) {
it('treats `null` as no answer for `input` type', function () {
var generator = env.instantiate(helpers.createDummyGenerator());

helpers.mockPrompt(generator, {
respuesta: null
});

generator.prompt([{ name: 'respuesta', message: 'foo', type: 'input', default: 'bar' }], function (answers) {
return generator.prompt([
{ name: 'respuesta', message: 'foo', type: 'input', default: 'bar' }
]).then(function (answers) {
assert.equal(answers.respuesta, 'bar');
done();
});
});

it('uses `true` as the default value for `confirm` type', function (done) {
it('uses `true` as the default value for `confirm` type', function () {
var generator = env.instantiate(helpers.createDummyGenerator());
helpers.mockPrompt(generator, {});
generator.prompt([{ name: 'respuesta', message: 'foo', type: 'confirm' }], function (answers) {

return generator.prompt([
{ name: 'respuesta', message: 'foo', type: 'confirm' }
]).then(function (answers) {
assert.equal(answers.respuesta, true);
done();
});
});

it('supports `false` answer for `confirm` type', function (done) {
it('supports `false` answer for `confirm` type', function () {
var generator = env.instantiate(helpers.createDummyGenerator());
helpers.mockPrompt(generator, { respuesta: false });
generator.prompt([{ name: 'respuesta', message: 'foo', type: 'confirm' }], function (answers) {

return generator.prompt([
{ name: 'respuesta', message: 'foo', type: 'confirm' }
]).then(function (answers) {
assert.equal(answers.respuesta, false);
done();
});
});

it('prefers mocked values over defaults', function (done) {
this.generator.prompt([{ name: 'answer', type: 'input', default: 'bar' }], function (answers) {
it('prefers mocked values over defaults', function () {
return this.generator.prompt([
{ name: 'answer', type: 'input', default: 'bar' }
]).then(function (answers) {
assert.equal(answers.answer, 'foo');
done();
});
});

it('can be call multiple time on the same generator', function (done) {
it('can be call multiple time on the same generator', function () {
var generator = env.instantiate(helpers.createDummyGenerator());
helpers.mockPrompt(generator, { foo: 1 });
helpers.mockPrompt(generator, { foo: 2 });
generator.prompt({ message: 'bar', name: 'foo' }, function (answers) {
return generator.prompt({ message: 'bar', name: 'foo' }).then(function (answers) {
assert.equal(answers.foo, 2);
done();
});
});

it('keep prompt method asynchronous', function (done) {
it('keep prompt method asynchronous', function () {
var spy = sinon.spy();

this.generator.prompt({ name: 'answer', type: 'input' }, function () {
var promise = this.generator.prompt(
{ name: 'answer', type: 'input' }
).then(function () {
sinon.assert.called(spy);
done();
});

spy();
return promise;
});
});

Expand Down
12 changes: 5 additions & 7 deletions test/run-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -409,28 +409,26 @@ describe('RunContext', function () {
.on('end', done);
});

it('is chainable', function (done) {
it('is chainable', function () {
this.Dummy.prototype.askFor = function () {
var cb = this.async();
this.prompt([{
return this.prompt([{
name: 'yeoman',
type: 'input',
message: 'Hey!'
}, {
name: 'yo',
type: 'input',
message: 'Yo!'
}], function (answers) {
}]).then(function (answers) {
assert.equal(answers.yeoman, 'yes please');
assert.equal(answers.yo, 'yo man');
cb();
});
};

this.ctx
return this.ctx
.withPrompts({ yeoman: 'yes please' })
.withPrompts({ yo: 'yo man' })
.on('end', done);
.toPromise();
});
});

Expand Down

0 comments on commit 4051bd8

Please sign in to comment.