1 | 'use strict';
|
2 | var events = require('events');
|
3 | var _ = require('lodash');
|
4 | var inquirer = require('inquirer');
|
5 | var sinon = require('sinon');
|
6 | var { PassThrough } = require('stream');
|
7 |
|
8 | function DummyPrompt(mockedAnswers, callback, question, _rl, answers) {
|
9 | this.answers = { ...answers, ...mockedAnswers };
|
10 | this.question = question;
|
11 | this.callback = callback || (answers => answers);
|
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 | input: new PassThrough(),
|
47 | output: new PassThrough(),
|
48 | skipTTYChecks: true
|
49 | });
|
50 |
|
51 | Object.keys(this.promptModule.prompts).forEach(function(promptName) {
|
52 | this.promptModule.registerPrompt(
|
53 | promptName,
|
54 | DummyPrompt.bind(DummyPrompt, answers, undefined)
|
55 | );
|
56 | }, this);
|
57 |
|
58 | this.diff = sinon.spy();
|
59 | this.log = sinon.spy();
|
60 | _.extend(this.log, events.EventEmitter.prototype);
|
61 |
|
62 |
|
63 | [
|
64 | 'write',
|
65 | 'writeln',
|
66 | 'ok',
|
67 | 'error',
|
68 | 'skip',
|
69 | 'force',
|
70 | 'create',
|
71 | 'invoke',
|
72 | 'conflict',
|
73 | 'identical',
|
74 | 'info',
|
75 | 'table'
|
76 | ].forEach(function(methodName) {
|
77 | this.log[methodName] = sinon.stub().returns(this.log);
|
78 | }, this);
|
79 | }
|
80 |
|
81 | TestAdapter.prototype.prompt = function(questions, prefilledAnswers) {
|
82 | return this.promptModule(questions, prefilledAnswers);
|
83 | };
|
84 |
|
85 | module.exports = {
|
86 | DummyPrompt: DummyPrompt,
|
87 | TestAdapter: TestAdapter
|
88 | };
|