forked from maty21/mocha-sidebar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.js
335 lines (291 loc) · 10.4 KB
/
extension.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
'use strict';
// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
const ChildProcess = require('child_process');
const escapeRegExp = require('escape-regexp');
const fs = require('fs');
const { runTestsOnSave } = require('./config');
const Glob = require('glob').Glob;
const parser = require('./parser');
const path = require('path');
const Promise = require('bluebird');
const Runner = require('./runner');
const config = require('./config');
const vscode = require('vscode');
const changesNotification = require('./changesNotification');
const mochaProvider = require('./mochaProvider');
const mochaLensProvider = require('./provider-extensions/mochaLens')
const access = Promise.promisify(fs.access);
const runner = new Runner();
const { debugAll, debugItem, debugLevel, debugInit } = require('./provider-extensions/runDebug');
const coverage = require('./lib/coverage/code-coverage');
const getOnTerminateFunc = func => {
const noop = function () { };
return config.sideBarOptions().showDebugTestStatus ? func : noop;
}
let lastPattern;
let lastRunResult;
// this method is called when your extension is activated
// your extension is activated the very first time the command is executed
function activate(context) {
const subscriptions = context.subscriptions;
const _mochaProvider = new mochaProvider();
debugInit(_mochaProvider);
vscode.window.registerTreeDataProvider('mocha', _mochaProvider)
if(config.coverage().enable){
coverage.run();
}
const _codeLensProvider = new mochaLensProvider(context, _mochaProvider);
const _changesNotification = new changesNotification(_mochaProvider, _codeLensProvider);
let registerCodeLens = vscode.languages.registerCodeLensProvider(_codeLensProvider.selector, _codeLensProvider);
vscode.commands.executeCommand('setContext', 'runAutoPlay', runTestsOnSave())
let runAutoPlay = runTestsOnSave();
subscriptions.push(registerCodeLens);
subscriptions.push(vscode.commands.registerCommand('mocha-maty.autoPlayStart', (element) => {
if (hasWorkspace()) {
vscode.commands.executeCommand('setContext', 'runAutoPlay', false)
_changesNotification.start();
}
}))
subscriptions.push(vscode.commands.registerCommand('mocha-maty.autoPlayPause', (element) => {
if (hasWorkspace()) {
vscode.commands.executeCommand('setContext', 'runAutoPlay', true)
_changesNotification.pause();
}
}))
subscriptions.push(vscode.commands.registerCommand('mocha-maty.runAllDebug', (element) => {
if (hasWorkspace()) {
debugAll(element, getOnTerminateFunc(_mochaProvider.runAllTests));
}
}))
subscriptions.push(vscode.commands.registerCommand('mocha-maty.debugLevel', (element) => {
if (hasWorkspace()) {
debugLevel(element, getOnTerminateFunc(_mochaProvider.runTestWithoutElement));
}
}))
subscriptions.push(vscode.commands.registerCommand('mocha-maty.debugItem', (element) => {
if (hasWorkspace()) {
debugItem(element, getOnTerminateFunc(_mochaProvider.runTest));
}
}))
subscriptions.push(vscode.commands.registerCommand('mocha-maty.runAllTests', (element) => {
if (hasWorkspace()) {
_mochaProvider.runAllTests(element);
//runAllTests();
}
}))
subscriptions.push(vscode.commands.registerCommand('mocha-maty.runTest', (element) => {
if (hasWorkspace()) {
_mochaProvider.runTest(element);
//runAllTests();
}
}))
subscriptions.push(vscode.commands.registerCommand('mocha-maty.runDescriberLevelTest', (element) => {
if (hasWorkspace()) {
_mochaProvider.runDescriberLevelTest(element);
//runAllTests();
}
}))
subscriptions.push(vscode.commands.registerCommand('mocha-maty.toggleCoverage', (element) => {
if (hasWorkspace()) {
try {
coverage.toggleCoverage();
} catch (e) {
console.log(e);
}
// _mochaProvider.runDescriberLevelTest(element);
//runAllTests();
}
}))
subscriptions.push(vscode.commands.registerCommand('mocha-maty.refreshExplorer', (element) => {
if (hasWorkspace()) {
_mochaProvider.refreshExplorer(element);
_mochaProvider.updateDecorations(vscode.workspace.rootPath);
_codeLensProvider.raiseEventOnUpdate();
//runAllTests();
}
}))
subscriptions.push(vscode.commands.registerCommand('mocha-maty.coverage', (element) => {
if (hasWorkspace()) {
if(config.coverage().enable){
coverage.runViaRequest();
}
}
}))
subscriptions.push(vscode.commands.registerCommand('mocha-maty.setSubdirectory', (element) => {
if (hasWorkspace() && element.path) {
const relativePath = path.relative(vscode.workspace.rootPath, element.path);
config.setSubdirectory(relativePath || element.path);
}
}));
subscriptions.push(vscode.commands.registerCommand('mocha-maty.itemSelection', item => {
if (hasWorkspace()) {
_mochaProvider.itemSelection(item);
//runAllTests();
}
}))
subscriptions.push(vscode.commands.registerCommand('mocha.runAllTests', function () {
if (hasWorkspace()) {
runAllTests();
}
}));
subscriptions.push(vscode.commands.registerCommand('mocha.runTestAtCursor', function () {
if (hasWorkspace()) {
runTestAtCursor();
}
}));
subscriptions.push(vscode.commands.registerCommand('mocha.selectAndRunTest', function () {
if (hasWorkspace()) {
selectAndRunTest();
}
}));
subscriptions.push(vscode.commands.registerCommand('mocha.runFailedTests', function () {
if (hasWorkspace()) {
runFailedTests();
}
}));
subscriptions.push(vscode.commands.registerCommand('mocha.runTestsByPattern', function () {
if (hasWorkspace()) {
runTestsByPattern();
}
}));
subscriptions.push(vscode.commands.registerCommand('mocha.runLastSetAgain', function () {
if (hasWorkspace()) {
runLastSetAgain();
}
}));
const status = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left, -1000);
// status.command = 'extension.selectedLines';
const statusTemplate = (passed, failed) => ` Tests $(check) ${passed} $(x) ${failed} `;
status.text = statusTemplate(0, 0);
subscriptions.push(status);
status.show();
_mochaProvider.onDidChangeTreeData((rootItem) => {
status.text = statusTemplate(_mochaProvider.results.passed.length,
_mochaProvider.results.failed.length);
})
}
exports.activate = activate;
// this method is called when your extension is deactivated
function deactivate() {
}
exports.deactivate = deactivate;
function hasWorkspace() {
const root = vscode.workspace.rootPath;
const validWorkspace = typeof root === "string" && root.length;
// console.log(root);
// console.log(vscode);
//console.log(validWorkspace);
if (!validWorkspace) {
vscode.window.showErrorMessage('Please open a folder before trying to execute Mocha.');
}
return validWorkspace;
}
function fork(jsPath, args, options) {
return findNodeJSPath().then(execPath => new Promise((resolve, reject) => {
resolve(ChildProcess.spawn(
execPath,
[jsPath].concat(args),
options
))
}), err => {
vscode.window.showErrorMessage('Cannot find Node.js installation from environment variable');
throw err;
});
}
function runAllTests() {
runner.loadTestFiles()
.then(
files => {
if (!files.length) {
return vscode.window.showWarningMessage('No tests were found.');
}
runner.runAll();
}
).catch(
err => vscode.window.showErrorMessage(`Failed to run tests due to ${err.message}`)
);
}
function runTestAtCursor() {
const editor = vscode.window.activeTextEditor;
if (!editor) {
return vscode.window.showErrorMessage('No active editors were found.');
} else if (editor.document.languageId !== 'javascript') {
return vscode.window.showErrorMessage('Mocha is only available for JavaScript files.');
}
let detectError = 'No test(s) were detected at the current cursor position.';
let test = null;
try {
test = parser.getTestAtCursor(editor.document.getText(), editor.selection.active);
} catch (e) {
console.error(e);
detectError = `Parsing failed while detecting test(s) at the current cursor position: ${e.message}`;
}
vscode.window.showErrorMessage(test);
return runner.loadTestFiles()
.then(() => {
if (test) {
return runner.runWithGrep(test.label);
} else {
// Only run test from the current file
const currentFile = editor.document.fileName;
runner.tests = runner.tests.filter(t => t.file === currentFile);
return runner.runAll([`WARNING: ${detectError} Running all tests in the current file.`]);
}
})
.catch(err => vscode.window.showErrorMessage(`Failed to run test(s) at the cursor position due to ${err.message}`));
}
function selectAndRunTest() {
const rootPath = vscode.workspace.rootPath;
vscode.window.showQuickPick(
runner.loadTestFiles()
.then(
tests => {
if (!tests.length) {
vscode.window.showWarningMessage(`No tests were found.`);
throw new Error('no tests found');
}
return tests.map(test => ({
detail: path.relative(rootPath, test.file),
label: test.fullName,
test
}));
},
err => {
vscode.window.showErrorMessage(`Failed to find tests due to ${err.message}`);
throw err;
}
)
)
.then(entry => {
if (!entry) { return; }
runner
.runTest(entry.test)
.catch(err => {
vscode.window.showErrorMessage(`Failed to run selected tests due to ${err.message}`);
});
});
}
function runFailedTests() {
runner.runFailed()
.catch(() => vscode.window.showErrorMessage(`Failed to rerun failed tests due to ${err.message}`));
}
function runTestsByPattern() {
return Promise.props({
pattern: vscode.window.showInputBox({
placeHolder: 'Regular expression',
prompt: 'Pattern of tests to run',
value: lastPattern || ''
}),
loadTests: runner.loadTestFiles()
}).then(props => {
const pattern = props.pattern;
if (!pattern) { return; }
lastPattern = pattern;
return runner.runWithGrep(pattern);
}, err => vscode.window.showErrorMessage(`Failed to run tests by pattern due to ${err.message}`));
}
function runLastSetAgain() {
runner.runLastSet()
.catch(() => vscode.window.showErrorMessage(`Failed to rerun last set due to ${err.message}`));
}