forked from maty21/mocha-sidebar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mochashim.js
231 lines (195 loc) · 6.91 KB
/
mochashim.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
'use strict';
const config = require('./config');
const fork = require('./fork');
const path = require('path');
const Promise = require('bluebird');
const vscode = require('vscode');
const _runTestsInProcess = require('./inProcess/runtestInProcess');
const _findTestsInProcess = require('./inProcess/findtestsInProccess');
const verboseLog = require('./provider-extensions/constLog');
const outputChannel = vscode.window.createOutputChannel('sideBar-Mocha');
const coverage = require('./lib/coverage/code-coverage');
const { message, TYPES } = require('./worker/process-communication');
const envWithNodePath = (rootPath) => {
return Object.assign({}, process.env, {
NODE_PATH: `${rootPath}${path.sep}node_modules`
}, config.env());
}
const applySubdirectory = (rootPath) => {
const subdirectory = config.subdirectory()
if (subdirectory)
rootPath = path.resolve(rootPath, subdirectory);
return rootPath;
}
const logTestArg = (testFiles, grep, rootPath) => {
console.log(`test arg: ${JSON.stringify({
files: testFiles,
options: config.options(),
grep,
requires: config.requires(),
rootPath
})}`);
}
const forkRunTest = (testFiles, grep, rootPath) => {
const args = {
files: testFiles,
grep,
mochaPath: config.mochaPath()
};
if (config.logVerbose()) {
outputChannel.appendLine(`
test runs with those args:
files: ${JSON.stringify(testFiles)},
options: ${JSON.stringify(config.options())},
grep:${JSON.stringify(grep)},
requires: ${JSON.stringify(config.requires())},
rootpath: ${JSON.stringify(rootPath)}
`)
}
//outputChannel.appendLine(`mocha path : ${config.mochaPath()}`);
return forkWorker('../worker/runtest.js', args, rootPath);
}
const forkFindTests = (rootPath) => {
const args = {
files: {
glob: config.files().glob,
ignore: config.files().ignore
},
mochaPath: config.mochaPath()
};
findingTestLogs();
if (config.logVerbose()) {
// outputChannel.show();
}
return forkWorker('../worker/findtests.js', args, rootPath);
}
const findingTestLogs = () => {
outputChannel.clear();
outputChannel.appendLine(`____________________________________________________________________________`);
outputChannel.appendLine(`trying to searching for tests using these settings: `);
outputChannel.appendLine(`
mocha path: ${config.mochaPath()}
test files location: ${config.files().glob}
files to ignore: ${config.files().ignore}
environmets: ${ JSON.stringify(config.env())}
requires: ${JSON.stringify(config.requires())}
options: ${JSON.stringify(config.options())}
`);
outputChannel.appendLine(`if you find anything wrong please change those default settings`)
outputChannel.appendLine(`____________________________________________________________________________`);
// vscode.window.showErrorMessage(`Failed to run Mocha due to`,'learnMorePanel').then(val=>{
// vscode.commands.executeCommand('vscode.open', vscode.Uri.parse('https://go.microsoft.com/fwlink/?linkid=864631'));
// });
}
const forkWorker = (workerPath, argsObject, rootPath) => {
const jsPath = path.resolve(module.filename, workerPath);
argsObject.options = config.options();
argsObject.requires = config.requires();
argsObject.rootPath = rootPath;
const argsString = JSON.stringify(argsObject);
const options = { env: envWithNodePath(rootPath) };
return fork(jsPath, [argsString], options);
}
const handleError = (err, reject) => {
const qa = 'Q/A';
const gitter = 'Gitter';
vscode.window.showErrorMessage(`Failed to run Mocha due to error message:( ${err.message}) .
error trace can be found in the ouput channel .
for more help:`, qa, gitter).then(val => {
switch (val) {
case qa:
vscode.commands.executeCommand('vscode.open', vscode.Uri.parse('https://github.com/maty21/mocha-sidebar#qa'));
break;
case gitter:
vscode.commands.executeCommand('vscode.open', vscode.Uri.parse('https://gitter.im/mocha-sidebar/Questions'));
break;
// default:
// vscode.commands.executeCommand('vscode.open', vscode.Uri.parse('https://github.com/maty21/mocha-sidebar'))
}
});
outputChannel.appendLine(err.stack);
if (showErrorPopup) {
vscode.window.showErrorMessage(`Failed to run Mocha due to error message:( ${err.message}) .
error trace can be found in the ouput channel .
for more help:`, qa, gitter).then(val => {
switch (val) {
case qa:
vscode.commands.executeCommand('vscode.open', vscode.Uri.parse('https://github.com/maty21/mocha-sidebar#qa'));
break;
case gitter:
vscode.commands.executeCommand('vscode.open', vscode.Uri.parse('https://gitter.im/mocha-sidebar/Questions'));
break;
// default:
// vscode.commands.executeCommand('vscode.open', vscode.Uri.parse('https://github.com/maty21/mocha-sidebar'))
}
});
outputChannel.show();
reject(err);
}
}
const appendMessagesToOutput = (messages) => {
if (messages) {
for (let message of messages) {
outputChannel.appendLine(`${message}`);
}
}
}
const createError = (errorText) => {
return new Error(`Mocha sidebar: ${errorText}. See Mocha output for more info.`);
}
const handleProcessMessages = async (process) => {
return new Promise((resolve, reject) => {
let msg = message(process);
let processMessage = null;
msg.on(TYPES.result, data => {
processMessage = data;
console.log(data);
})
process.stdout.on('data', data => {
if (config.logVerbose()) {
outputChannel.appendLine(data.toString())
}
});
msg.on('error', err => {
let error = JSON.parse(err);
handleError(error, reject)
})
msg.on('exit', async code => {
if (processMessage) {
if (code) {
reject(createError('Process exited with code ' + code));
return;
}
resolve(processMessage);
}
});
});
}
const runTests = async (testFiles, grep, messages) => {
// Allow the user to choose a different subfolder
const rootPath = applySubdirectory(vscode.workspace.rootPath);
logTestArg(testFiles, grep, rootPath);
if (config.coverage().enable && config.coverage().runAfterTest) {
coverage.runViaRequest();
}
// outputChannel.clear();
//outputChannel.appendLine(`Running tests in "${rootPath}"\n`);
appendMessagesToOutput(messages);
let process = await forkRunTest(testFiles, grep, rootPath)
let data = await handleProcessMessages(process)
return data;
}
const findTests = async (rootPath) => {
// Allow the user to choose a different subfolder
//outputChannel.appendLine(`Finding tests in "${rootPath}"\n`);
rootPath = applySubdirectory(rootPath);
//outputChannel.appendLine(`Finding tests in "${rootPath}"\n`);
let process = await forkFindTests(rootPath)
let data = await handleProcessMessages(process)
return data;
}
module.exports = {
runTests,
findTests,
outputChannel,
}