-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.js
More file actions
111 lines (106 loc) · 2.6 KB
/
Copy pathindex.js
File metadata and controls
111 lines (106 loc) · 2.6 KB
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
'use strict';
var fs = require('fs'),
util = require('util'),
mkdirp = require("mkdirp"),
path = require("path");
module.exports = function (runner) {
var stack = {};
var title,fd;
var root = process.cwd();
var filePath = process.env.GUNIT_FILE || root + "/gunit.xml"
var stackF;
if(fs.existsSync(filePath)){
fs.unlinkSync(filePath);
}
mkdirp.sync(path.dirname(filePath));
fd = fs.openSync(filePath, 'w');
runner.on('test end', function(test){
var file = getFilePath(test);
// console.log('file-->', file);
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('<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>');
fs.closeSync(fd);
});
function append(str) {
process.stdout.write(str);
process.stdout.write('\n');
fs.writeSync(fd, str + "\n", null, 'utf8');
};
};
function espape(str){
str = str || '';
return str.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''');
}
function getFilePath(testObj){
if(testObj.file){
return testObj.file;
}
if(testObj.parent.title ==''){
return testObj.title;
}
else {
return getFilePath(testObj.parent);
}
}