-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.js
397 lines (354 loc) · 10.9 KB
/
main.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
'use strict';
var fs = require('fs'),
path = require('path'),
Parser = require('./parser.js'),
_requiredPublishOptions = ['server', 'buildName', 'flavor', 'platform', 'teamProject'],
_exePath = null;
/**
* Runs tests through MSTest and reports results
* @constructor
*/
var MSTest = function () {
this._testContainer = '';
this._testMetadata = '';
this._testLists = [];
this._categories = [];
this._tests = [];
this.language = 'en';
this.workingDir = '';
this.noIsolation = false;
this.testSettings = '';
this.runConfig = '';
this.resultsFile = '';
this._publish = null;
this.details = {
adapter: false,
computerName: false,
debugTrace: false,
description: false,
displayText: false,
duration: false,
errorMessage: false,
errorStackTrace: false,
executionID: false,
groups: false,
ID: false,
isAutomated: false,
link: false,
longText: false,
name: false,
outcomeText: false,
owner: false,
parentExeCID: false,
priority: false,
projectName: false,
projectRelativePath: false,
readOnly: false,
spoolMessage: false,
stderr: false,
stdout: false,
storage: false,
testCategoryID: false,
testName: false,
testType: false,
traceInfo: false
// ... Any details you might need
};
this.debugLog = false;
this.useStdErr = false;
};
/**
* Logs out debug information
*/
MSTest.prototype._log = function () {
if (this.debugLog) {
process.stdout.write('[MSTest] ');
console.log.apply(console, arguments);
}
};
/**
* Adds a new test list option
* @param {string} testList
* @returns {MSTest}
*/
MSTest.prototype.addTestList = function (testList) {
if (this._testLists.indexOf(testList) === -1) {
this._testLists.push(testList);
}
return this;
};
/**
* Removes a test list option
* @param {string} testList
* @returns {MSTest}
*/
MSTest.prototype.removeTestList = function (testList) {
var index = this._testLists.indexOf(testList);
if (index !== -1) {
this._testLists.splice(index, 1);
}
return this;
};
/**
* Removes all test list options
* @returns {MSTest}
*/
MSTest.prototype.clearTestLists = function () {
this._testLists = [];
return this;
};
/**
* Sets the only category
* @returns {MSTest}
*/
MSTest.prototype.setCategory = function (category) {
this._categories = [category];
return this;
};
/**
* Adds a new category with an operator
* @param {string} operator
* @param {string} category
*/
MSTest.prototype._appendCategory = function (operator, category) {
var len = this._categories.length;
if (len === 0) {
this._categories = [category];
} else {
this._categories.splice(len, 0, operator, category);
}
};
/**
* Adds another category using "and" (...&category)
* @returns {MSTest}
*/
MSTest.prototype.andCategory = function (category) {
this._appendCategory('&', category);
return this;
};
/**
* Adds another category using "or" (...|category)
* @returns {MSTest}
*/
MSTest.prototype.orCategory = function (category) {
this._appendCategory('|', category);
return this;
};
/**
* Adds another category using "not" (...!category)
* @returns {MSTest}
*/
MSTest.prototype.notCategory = function (category) {
this._appendCategory('!', category);
return this;
};
/**
* Adds another category using "and not" (...&!category)
* @returns {MSTest}
*/
MSTest.prototype.andNotCategory = function (category) {
this._appendCategory('&!', category);
return this;
};
/**
* Removes a category from the list
* @param {string} category
* @returns {MSTest}
*/
MSTest.prototype.removeCategory = function (category) {
var index = this._categories.indexOf(category);
if (index > 0) {
// Remove the category and it's previous operator
this._categories.splice(index - 1, 2);
} else if (index === 0) {
// It was the first one, just pull it out
this._categories.shift();
}
return this;
};
/**
* Adds a test case to run
* @param {string} test
* @returns {MSTest}
*/
MSTest.prototype.addTest = function (test) {
if (this._tests.indexOf(test)) {
this._tests.push(test);
}
return this;
};
/**
* Removes a test case to run (only removes from the explicit list, does not blacklist)
* @param {string} test
* @returns {MSTest}
*/
MSTest.prototype.removeTest = function (test) {
var index = this._tests.indexOf(test);
if (index !== -1) {
this._tests.splice(index, 1);
}
return this;
};
/**
* Clears tests, will run all tests
* @returns {MSTest}
*/
MSTest.prototype.clearTests = function () {
this._tests = [];
return this;
};
/**
* Prepares the tests to be published to a TFS server
* @param {object} options
* @param {string} options.server TFS server (i.e. http://TFSMachine:8080)
* @param {string} options.buildName Name of the build. Check /publishbuild for how to get this value
* @param {string} options.flavor Must match the value set in the build (i.e. debug, release, etc.)
* @param {string} options.platform Must match the value set in the build (i.e. AnyCPU, x86, etc.)
* @param {string} options.teamProject Name of the team project the build belongs to
* @param {string} [options.resultsFile] Name of the results file to publish. Only set for publishing old results
* @returns {MSTest}
*/
MSTest.prototype.publish = function (options) {
_requiredPublishOptions.forEach(function (val) {
if (!options.hasOwnProperty(val)) {
throw new Error('All of the following options must be given to publish: ' + _requiredPublishOptions);
}
});
this._publish = options;
return this;
};
/**
* Clears publish settings
* @returns {MSTest}
*/
MSTest.prototype.dontPublish = function () {
this._publish = null;
return this;
};
/**
* Runs the tests with the current settings
* @param {object} [options]
* @param {function(TestResult)} [options.eachTest] Called for every test result
* @param {function(TestResult[])} [options.done] Called after all the tests have completed
* @param {function(Error)} [options.error] Called if there was an error in the parser
* @returns {MSTest}
*/
MSTest.prototype.runTests = function (options) {
options = options || {};
options.eachTest = options.eachTest || function () { };
options.done = options.done || function () { };
options.error = options.error || function () { };
var msTestArgs = ['/nologo']; // Less to parse
// Figure out where we're getting the tests from
if (this.testContainer) {
msTestArgs.push('/testcontainer:"' + this.testContainer + '"');
} else if (this.testMetadata) {
msTestArgs.push('/testmetadata:"' + this.testMetadata + '"');
} else {
throw new Error('Must specify a test container or test metadata');
}
// Append any category filters
if (this._categories.length > 0) {
msTestArgs.push('/category:"' + this._categories.join('') + '"');
}
// Add any test filters
for (var i = 0, len = this._tests.length; i < len; i++) {
msTestArgs.push('/test:' + this._tests[i]);
}
// Run tests inside the mstest.exe process
if (this.noIsolation) {
msTestArgs.push('/noisolation');
}
// Specify a test settings file
if (this.testSettings) {
msTestArgs.push('/testsettings:"' + this.testSettings + '"');
}
// Specify the run configuration file
if (this.runConfig) {
msTestArgs.push('/runconfig:"' + this.runConfig + '"');
}
// Save the results somewhere special
if (this.resultsFile) {
msTestArgs.push('/resultsfile:"' + this.resultsFile + '"');
}
// Use standard error to ouput error information that may occur attempting to run the test
if (this.useStdErr) {
msTestArgs.push('/usestderr');
}
// Add all extra details
for (var detail in this.details) {
if (this.details[detail]) {
msTestArgs.push('/detail:' + detail.toLowerCase());
}
}
// Add any publish settings
if (this._publish) {
msTestArgs.push('/publish:' + this._publish.server);
msTestArgs.push('/publishbuild:' + this._publish.buildName);
msTestArgs.push('/flavor:' + this._publish.flavor);
msTestArgs.push('/platform:' + this._publish.platform);
msTestArgs.push('/teamproject:' + this._publish.teamProject + '');
if (this._publish.resultsFile) {
msTestArgs.push('/publishresultsfile:"' + this._publish.resultsFile + '"');
}
}
this._log('mstest.exe path: ' + this.exePath);
this._log('Arguments: ' + msTestArgs.join(' '));
this._log('CMD: ' + this.exePath + ' ' + msTestArgs.join(' '));
// Fire it up!
var parser = new Parser(this.exePath, msTestArgs, this.workingDir, Object.keys(this.details), this.language);
parser.on('test', options.eachTest);
parser.on('done', options.done);
parser.on('error', options.error);
return this;
};
Object.defineProperty(MSTest.prototype, 'testContainer', {
get: function () { return this._testContainer; },
set: function (value) {
// Clear the metadata path since we can't have both
this._log('Can only have testContainer or testMetadata, clearing testMetadata');
this._testMetadata = '';
this._testContainer = value;
}
});
Object.defineProperty(MSTest.prototype, 'testMetadata', {
get: function () { return this._testMetadata; },
set: function (value) {
// Clear the testcontainer since we can't have both
this._log('Can only have testContainer or testMetadata, clearing testContainer');
this._testContainer = '';
this._testMetadata = value;
}
});
// Get the path to mstest.exe
Object.defineProperty(MSTest.prototype, 'exePath', {
get: function () {
if (_exePath !== null) {
return _exePath;
}
// Environment variables for VS tools
var vsToolsVariables = [
process.env.VS140COMNTOOLS,
process.env.VS120COMNTOOLS,
process.env.VS110COMNTOOLS,
process.env.VS100COMNTOOLS
],
vsTools = null;
for (var i = 0, len = vsToolsVariables.length; i < len; i++) {
var toolPath = vsToolsVariables[i];
if (toolPath && toolPath !== '') {
vsTools = toolPath;
break;
}
}
if (!vsTools) {
throw new Error('Could not find path to Visual Studio tools');
}
_exePath = path.join(vsTools, '../IDE', 'mstest.exe');
if (!fs.existsSync(_exePath)) {
throw new Error('Could not find mstest.exe at ' + _exePath);
}
return _exePath;
}
});
module.exports = MSTest;