-
Notifications
You must be signed in to change notification settings - Fork 2
/
mxunit.js
374 lines (304 loc) · 11 KB
/
mxunit.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
var fix = require('./string-fix');
var request = require('request');
var xcolor = require('xcolor');
var _ = require('underscore');
var async = require('async');
var utils = require('./utils');
var TestResult = require('./TestResult');
var TestSuiteResult = require('./TestSuiteResult');
var lineWidth = 80;
var httpTimeout = 120000; // in milliseconds
var batchMode = false;
var urlFilterParam = null;
var suites = {}
, suiteCount = 0
;
function isEmpty(obj) {
if (obj == null) return true;
if (obj.length && obj.length > 0) return false;
if (obj.length === 0) return true;
for (var key in obj) {
if (hasOwnProperty.call(obj, key)) return false;
}
return true;
}
function log(str, callback) {
var args = arguments;
if( typeof callback == "function" ){
args = callback(fix, lineWidth);
// for simple strings, return as a single array item
if( typeof args == "string" ){
args = [args];
// if we do not have an array, ignore the output
} else if( !Array.isArray(args) ) {
args = [];
}
// prepend the string to the beginning of the string
args.unshift(str);
}
xcolor.log.apply(this, args);
}
function writeSuiteName(suiteName, desc){
let description = (typeof desc === "string") ? ' ' + desc + ' ' : '';
log('\n\n{{blue}}%s{{/color}}{{white}}%s{{/color}}', fix.padr(suiteName, lineWidth-description.length), description);
console.log(fix.repeat('-', lineWidth));
}
function writeSuiteResultStatus(suiteResult, executionTime){
console.log(fix.repeat('-', lineWidth));
let msg = "SUITE [in " + utils.timeConversion(executionTime) + "]:";
if( suiteResult.hasSuccess() ){
log('{{bg green}}{{white}} %s%s {{/color}}', msg, fix.padl('PASS: ' + suiteResult.totalPassed(), lineWidth - msg.length - 2));
} else {
let totalFailed = suiteResult.totalFailed()
, totalFailedMsg = totalFailed ? `: ${totalFailed}` : '';
;
log('{{bg red}}{{white}} %s%s {{/color}}', msg, fix.padl('FAIL' + totalFailedMsg, lineWidth - msg.length - 2));
}
}
function processTestResult(test, results, executionTime){
/*
* When the unit test returns an execution time, we will show that number instead because it reflect the actual
* time of the test w/out the HTTP overhead.
*/
if( ("TIME" in results) && Number.isInteger(parseInt(results.TIME)) ){
executionTime = parseInt(results.TIME);
}
let executionMsg = "[" + utils.timeConversion(executionTime) + "] ";
let paddingOffset = lineWidth - 9 - executionMsg.length;
if (results.TESTSTATUS === 'Passed'){
log(fix.padr(test, paddingOffset) + ' %s[{{bg green}}{{white}} PASS {{/color}}]', executionMsg);
return new TestResult(test, true);
} else {
let errorMessage;
log(fix.padr(test, paddingOffset) + ' %s[{{bg red}}{{white}} FAIL {{/color}}]', executionMsg);
//console.log("Test Results -> %o", results);
if( results.hasOwnProperty("ERROR") ){
errorMessage = (results.ERROR.Message || "").toString().trim() || "An unknown error occurred!";
if( errorMessage.length ){
log('{{red}} => %s{{/color}}', errorMessage);
}
if( (results.ERROR.Detail || "").toString().trim().length ){
log('{{red}} => %s{{/color}}', results.ERROR.Detail);
}
}
return new TestResult(test, false, errorMessage);
}
}
function getHttpError(err, resp, httpTimeout){
let statusCode = "";
let errorMessage = "";
if( err.code === 'ETIMEDOUT' ){
statusCode = "TIMEDOUT";
if( err.connect === true ){
errorMessage = "The remote connection to the server has timed out.";
} else {
errorMessage = `The HTTP request was not completed before the ${httpTimeout} connection timeout period.`;
}
} else {
statusCode = resp.statusCode;
if( resp.statusMessage && resp.statusMessage.length ){
errorMessage = resp.statusMessage;
} else {
errorMessage = "This test suite could not be run due to an error trying to with th HTTP request. Please check the above URL for any errors.";
}
}
return {
statusCode: statusCode
, errorMessage: errorMessage
};
}
function appendUrlParam(url, name, value){
return url + ((url.indexOf("?") === -1) ? "?" : "&") + name + "=" + encodeURIComponent(value);
}
module.exports = {
loadList: function(url, callback){
if( batchMode ){
url = appendUrlParam(url, "batch", true);
}
if( urlFilterParam ){
url = appendUrlParam(url, "filter", urlFilterParam);
}
request({url: url, timeout: httpTimeout}, function(err, resp, body){
if (!err && resp.statusCode === 200){
suites = JSON.parse(body);
suiteCount = Object.keys(suites).length;
}else{
suites = {};
suiteCount = 0;
}
if (callback){
callback(!isEmpty(suites));
}
});
}
, log: log
, getLineWidth: function (){
return lineWidth;
}
, setLineWidth: function (width){
if( !Number.isInteger(width) ){
throw "Line width must be an integer!";
}
lineWidth = width;
}
, getHttpTimeout: function (){
return httpTimeout/1000;
}
, setHttpTimeout: function (timeout){
if( !Number.isInteger(timeout) ){
throw "The HTTP timeout must be an integer!";
}
httpTimeout = timeout * 1000;
}
, getBatchMode: function (){
return batchMode;
}
, setBatchMode: function (status){
batchMode = !!status;
}
, getFilter: function (){
return urlFilterParam;
}
, setFilter: function (filter){
urlFilterParam = filter;
}
, runAll: function(hostname, callback){
let startTime = utils.getEpochMilliseconds();
function queueSuite(hostname, suiteName, idx){
let suiteNameDesc = "(" + idx + " of " + suiteCount + ")";
return function(cb){
module.exports[batchMode ? "runSuiteFull" : "runSuite"](hostname, suiteName, function(results){
cb(null, results);
}, suiteNameDesc);
}
}
var queuedSuites = []
, idx = 0
;
for( var suite in suites ){
queuedSuites.push( queueSuite(hostname, suite, ++idx) );
}
async.series(queuedSuites, function(err, results){
let allSuccess = !results.some((TestSuiteResult) => !TestSuiteResult.hasSuccess());
let totalPassed = results.reduce((total, suite) => total + suite.totalPassed(), 0)
, totalFailed = results.reduce((total, suite) => total + suite.totalFailed(), 0)
, totalPassFailMsg = `PASS: ${totalPassed} / FAIL: ${totalFailed}`
;
console.log('\n');
if( allSuccess ){
log('{{bg green}}' + fix.repeat(' ', lineWidth) + '{{/color}}');
log('{{bg green}}{{white}} ALL SUITES:%s {{/color}}', fix.padl(totalPassFailMsg, lineWidth - 13));
log('{{bg green}}' + fix.repeat(' ', lineWidth) + '{{/color}}');
} else {
log('{{bg red}}' + fix.repeat(' ', lineWidth) + '{{/color}}');
log('{{bg red}}{{white}} ALL SUITES:%s {{/color}}', fix.padl(totalPassFailMsg, lineWidth - 13));
log('{{bg red}}' + fix.repeat(' ', lineWidth) + '{{/color}}');
log("\n{{red}}The following summary contains a list of all errors found when running the unit tests:{{/color}}\n");
results.forEach(function (suiteResults, idx, results){
if( !suiteResults.hasSuccess() ){
// output the suite's name
log('{{red}}%s{{/color}}', suiteResults.name);
if( !!suiteResults.message ){
log('{{red}}=> %s{{/color}}', suiteResults.message);
}
let errors = suiteResults.errors();
errors.forEach(function (testResult, idx, errors){
log('{{red}} • %s{{/color}}', testResult.name);
log('{{red}} => %s{{/color}}', testResult.message);
});
}
})
}
let totalMs = utils.getEpochMilliseconds() - startTime;
log('\n\n{{bg yellow}}{{black}} TOTAL EXECUTION TIME:%s {{/color}}', fix.padl(utils.timeConversion(totalMs), lineWidth - 23));
if( callback ){
callback(allSuccess);
}
});
}
, runSuiteFull: function(hostname, suiteName, callback, desc){
let startTime = utils.getEpochMilliseconds();
var path = 'http://' + hostname + '/' + suiteName.split('.').join('/') + '.cfc?method=runTestRemote&output=json';
writeSuiteName(suiteName, desc);
const updateLine = (msg) => process.stdout.write("\r" + (msg||""));
const workingUpdate = () => updateLine("Running test suite... " + utils.timerFormat(utils.getEpochMilliseconds() - startTime));
// start updating the screen with the runtime
const workingUpdateJob = setInterval(workingUpdate, 1000);
let workingJobFinished = function (){
clearInterval(workingUpdateJob);
updateLine(); // reset the line
};
request({url: path, timeout: httpTimeout}, function(err, resp, body){
workingJobFinished();
// the entire suite ran
if( !err && (resp.statusCode === 200) ){
try {
var results = JSON.parse(body);
var hasParsedBody = true;
} catch (e){
var suiteResults = new TestSuiteResult(suiteName, false, "Error processing JSON response!");
console.log("HTTP Response -> %o", body);
hasParsedBody = false;
}
if( hasParsedBody ){
var suiteResults = new TestSuiteResult(suiteName, true)
results
// sort the results so they come back in order
.sort((a, b) => (a.TESTNAME||"").localeCompare((b.TESTNAME||"")))
.forEach((test, idx, results) => {
suiteResults.add(processTestResult(test.TESTNAME, test));
})
;
}
writeSuiteResultStatus(suiteResults, (utils.getEpochMilliseconds() - startTime));
callback(suiteResults);
} else {
workingJobFinished();
let httpError = getHttpError(err, resp, httpTimeout);
log("\n\n{{red}}[%s] %s{{/color}}\n", httpError.statusCode, path);
log("%s\n", httpError.errorMessage);
let suiteResults = new TestSuiteResult(suiteName, false, httpError.errorMessage);
writeSuiteResultStatus(suiteResults, (utils.getEpochMilliseconds() - startTime));
callback(suiteResults);
}
});
}
, runSuite: function(hostname, suiteName, callback, desc){
let startTime = utils.getEpochMilliseconds();
let suiteResults = new TestSuiteResult(suiteName, true)
function queueTest(hostname, suiteName, testName){
return function(cb){
module.exports.runTest(hostname, suiteName, testName, function(results){
suiteResults.add(results);
if( !results.hasSuccess() ){
suiteResults.success = false;
}
cb(null, results);
});
}
}
writeSuiteName(suiteName, desc);
var tests = suites[suiteName];
var queuedTests = [];
for (var test in tests){
queuedTests.push( queueTest(hostname, suiteName, tests[test]) );
}
async.series(queuedTests, function(err, results){
writeSuiteResultStatus(suiteResults, (utils.getEpochMilliseconds() - startTime));
callback(suiteResults);
});
}
, runTest: function(hostname, cfc, test, callback){
let startTime = utils.getEpochMilliseconds();
var path = 'http://' + hostname + '/' + cfc.split('.').join('/') + '.cfc?method=runTestRemote&output=json&testMethod=' + test;
request({url: path, timeout: httpTimeout}, function(err, resp, body){
let httpExecution = utils.getEpochMilliseconds() - startTime;
if (!err && resp.statusCode === 200){
callback(processTestResult(test, JSON.parse(body)[0], httpExecution));
} else {
let httpError = getHttpError(err, resp, httpTimeout);
callback(processTestResult(test, {TESTSTATUS: "Failed", ERROR: {Message: httpError.errorMessage}}, httpExecution));
}
});
}
};