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, q) {
|
9 | this.answers = answers;
|
10 | this.question = q;
|
11 | }
|
12 |
|
13 | DummyPrompt.prototype.run = function() {
|
14 | var answer = this.answers[this.question.name];
|
15 | var isSet;
|
16 |
|
17 | switch (this.question.type) {
|
18 | case 'list':
|
19 |
|
20 | isSet = answer !== undefined;
|
21 | break;
|
22 | case 'confirm':
|
23 |
|
24 | isSet = answer || answer === false;
|
25 | break;
|
26 | default:
|
27 |
|
28 | isSet = Boolean(answer);
|
29 | }
|
30 |
|
31 | if (!isSet) {
|
32 | answer = this.question.default;
|
33 |
|
34 | if (answer === undefined && this.question.type === 'confirm') {
|
35 | answer = true;
|
36 | }
|
37 | }
|
38 |
|
39 | return Promise.resolve(answer);
|
40 | };
|
41 |
|
42 | function TestAdapter(answers) {
|
43 | answers = answers || {};
|
44 | this.promptModule = inquirer.createPromptModule();
|
45 |
|
46 | Object.keys(this.promptModule.prompts).forEach(function(promptName) {
|
47 | this.promptModule.registerPrompt(promptName, DummyPrompt.bind(DummyPrompt, answers));
|
48 | }, this);
|
49 |
|
50 | this.diff = sinon.spy();
|
51 | this.log = sinon.spy();
|
52 | _.extend(this.log, events.EventEmitter.prototype);
|
53 |
|
54 |
|
55 | [
|
56 | 'write',
|
57 | 'writeln',
|
58 | 'ok',
|
59 | 'error',
|
60 | 'skip',
|
61 | 'force',
|
62 | 'create',
|
63 | 'invoke',
|
64 | 'conflict',
|
65 | 'identical',
|
66 | 'info',
|
67 | 'table'
|
68 | ].forEach(function(methodName) {
|
69 | this.log[methodName] = sinon.stub().returns(this.log);
|
70 | }, this);
|
71 | }
|
72 |
|
73 | TestAdapter.prototype.prompt = function(questions, cb) {
|
74 | var promise = this.promptModule(questions);
|
75 | promise.then(cb || _.noop);
|
76 |
|
77 | return promise;
|
78 | };
|
79 |
|
80 | module.exports = {
|
81 | DummyPrompt: DummyPrompt,
|
82 | TestAdapter: TestAdapter
|
83 | };
|