1 | 'use strict';
|
2 | var events = require('events');
|
3 | var _ = require('lodash');
|
4 | var inquirer = require('inquirer');
|
5 | var sinon = require('sinon');
|
6 | var Promise = require('pinkie-promise');
|
7 |
|
8 | function DummyPrompt(answers, callback, q) {
|
9 | this.answers = answers;
|
10 | this.question = q;
|
11 | this.callback = callback || (q => q);
|
12 | }
|
13 |
|
14 | DummyPrompt.prototype.run = function() {
|
15 | var answer = this.answers[this.question.name];
|
16 | var isSet;
|
17 |
|
18 | switch (this.question.type) {
|
19 | case 'list':
|
20 |
|
21 | isSet = answer !== undefined;
|
22 | break;
|
23 | case 'confirm':
|
24 |
|
25 | isSet = answer || answer === false;
|
26 | break;
|
27 | default:
|
28 |
|
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 |
|
43 | function 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 |
|
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 |
|
77 | TestAdapter.prototype.prompt = function(questions, cb) {
|
78 | var promise = this.promptModule(questions);
|
79 | promise.then(cb || _.noop);
|
80 |
|
81 | return promise;
|
82 | };
|
83 |
|
84 | module.exports = {
|
85 | DummyPrompt: DummyPrompt,
|
86 | TestAdapter: TestAdapter
|
87 | };
|