forked from mageddo/mocha-sonar-generic-test-coverage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
85 lines (80 loc) · 2.02 KB
/
index.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
var fs = require('fs'),
util = require('util');
module.exports = function (runner) {
var stack = {};
var title;
runner.on('test end', function(test){
var file = test.file.substr(test.file.indexOf(process.cwd()) + process.cwd().length + 1);
stackF = stack[file];
if(!stackF){
stackF = stack[file] = [];
}
var mtest = {
title: test.title,
titleId: title + ': ' + test.title,
suite: title,
stack: test.stack,
message: test.message,
file: file,
duration: test.duration,
state: test.state != undefined ? test.state : 'skipped'
};
stackF.push(mtest);
});
runner.on('suite', function(test){
title = test.title;
});
runner.on('fail', function(test, err){
test.stack = err.stack;
test.message = err.message;
});
runner.on('end', function() {
append('<unitTest version="1">');
Object.keys(stack).forEach(function(file){
append(util.format(' <file path="%s">', file));
stack[file].forEach(function(test){
switch(test.state){
case 'passed':
append(util.format(
' <testCase name="%s" duration="%d"/>',
espape(test.titleId), test.duration
));
break;
default :
append(util.format(
' <testCase name="%s" duration="%d">',
espape(test.titleId), test.duration != undefined ? test.duration : 0
));
switch(test.state){
case 'failed':
append(util.format(
' <failure message="%s"><![CDATA[%s]]></failure>',
espape(test.message), test.stack
));
break;
case 'skipped':
append(util.format(
' <skipped message="%s"></skipped>', espape(test.title)
));
break;
}
append(' </testCase>');
}
});
append(' </file>');
});
append('</unitTest>');
});
};
function append(str) {
process.stdout.write(str);
process.stdout.write('\n');
};
function espape(str){
str = str || '';
return str.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''');
}