UNPKG

2.01 kBJavaScriptView Raw
1'use strict';
2var events = require('events');
3var _ = require('lodash');
4var inquirer = require('inquirer');
5var sinon = require('sinon');
6var Promise = require('pinkie-promise');
7
8function DummyPrompt(answers, callback, q) {
9 this.answers = answers;
10 this.question = q;
11 this.callback = callback || (q => q);
12}
13
14DummyPrompt.prototype.run = function() {
15 var answer = this.answers[this.question.name];
16 var isSet;
17
18 switch (this.question.type) {
19 case 'list':
20 // List prompt accepts any answer value including null
21 isSet = answer !== undefined;
22 break;
23 case 'confirm':
24 // Ensure that we don't replace `false` with default `true`
25 isSet = answer || answer === false;
26 break;
27 default:
28 // Other prompts treat all falsy values to default
29 isSet = Boolean(answer);
30 }
31
32 if (!isSet) {
33 answer = this.question.default;
34
35 if (answer === undefined && this.question.type === 'confirm') {
36 answer = true;
37 }
38 }
39
40 return Promise.resolve(this.callback(answer));
41};
42
43function TestAdapter(answers) {
44 answers = answers || {};
45 this.promptModule = inquirer.createPromptModule();
46
47 Object.keys(this.promptModule.prompts).forEach(function(promptName) {
48 this.promptModule.registerPrompt(
49 promptName,
50 DummyPrompt.bind(DummyPrompt, answers, undefined)
51 );
52 }, this);
53
54 this.diff = sinon.spy();
55 this.log = sinon.spy();
56 _.extend(this.log, events.EventEmitter.prototype);
57
58 // Make sure all log methods are defined
59 [
60 'write',
61 'writeln',
62 'ok',
63 'error',
64 'skip',
65 'force',
66 'create',
67 'invoke',
68 'conflict',
69 'identical',
70 'info',
71 'table'
72 ].forEach(function(methodName) {
73 this.log[methodName] = sinon.stub().returns(this.log);
74 }, this);
75}
76
77TestAdapter.prototype.prompt = function(questions, cb) {
78 var promise = this.promptModule(questions);
79 promise.then(cb || _.noop);
80
81 return promise;
82};
83
84module.exports = {
85 DummyPrompt: DummyPrompt,
86 TestAdapter: TestAdapter
87};