-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
122 lines (112 loc) · 3.77 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
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
'use strict';
const fs = require('fs');
const util = require('util');
const path = require("path");
const mkdirp = require("mkdirp");
const xmlescape = require('xml-escape');
module.exports = function (runner, options) {
var stack = {};
var title, fd;
var stackF;
var root = process.cwd();
var useFullFilePath = false;
var filePath = "./xunit.xml";
//get output file option if any
if (options && options.reporterOptions) {
if (options.reporterOptions.output) {
mkdirp.sync(path.dirname(options.reporterOptions.output));
filePath = options.reporterOptions.output;
}
if (options.reporterOptions.useFullFilePath) {
useFullFilePath = true;
}
}
//remove old output file if any
if (fs.existsSync(filePath)) {
fs.unlinkSync(filePath);
}
//create outputdir if neeede
mkdirp.sync(path.dirname(filePath));
//Init ouptut file
fd = fs.openSync(filePath, 'w');
runner.on('test end', function (test) {
var file = getFilePath(test);
if (!useFullFilePath) {
file = file.substr(file.indexOf(root) + root.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('<testExecutions 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"/>',
xmlescape(test.titleId), test.duration
));
break;
default :
append(util.format(
'<testCase name="%s" duration="%d">',
xmlescape(test.titleId), test.duration != undefined ? test.duration : 0
));
switch (test.state) {
case 'failed':
append(util.format(
'<failure message="%s"><![CDATA[%s]]></failure>',
xmlescape(test.message), test.stack
));
break;
case 'skipped':
append(util.format(
'<skipped message="%s"></skipped>', xmlescape(test.title)
));
break;
}
append('</testCase>');
}
});
append('</file>');
});
append('</testExecutions>');
fs.closeSync(fd);
});
function append(str) {
fs.writeSync(fd, str + "\n", null, 'utf8');
}
};
function getFilePath(testObj) {
if (testObj.file) {
return testObj.file;
}
if (testObj.parent.title == '') {
return testObj.title;
}
else {
return getFilePath(testObj.parent);
}
}