1148c1243f2e96c3776db4334c412eee51651bb6.svn-base
5.27 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
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
/*!
* Nodeunit
* Copyright (c) 2010 Caolan McMahon
* MIT Licensed
*/
/**
* Module dependencies
*/
var nodeunit = require('../nodeunit'),
utils = require('../utils'),
fs = require('fs'),
path = require('path'),
async = require('../../deps/async'),
AssertionError = require('assert').AssertionError,
child_process = require('child_process'),
ejs = require('../../deps/ejs');
/**
* Reporter info string
*/
exports.info = "jUnit XML test reports";
/**
* Ensures a directory exists using mkdir -p.
*
* @param {String} path
* @param {Function} callback
* @api private
*/
var ensureDir = function (path, callback) {
var mkdir = child_process.spawn('mkdir', ['-p', path]);
mkdir.on('error', function (err) {
callback(err);
callback = function(){};
});
mkdir.on('exit', function (code) {
if (code === 0) callback();
else callback(new Error('mkdir exited with code: ' + code));
});
};
/**
* Returns absolute version of a path. Relative paths are interpreted
* relative to process.cwd() or the cwd parameter. Paths that are already
* absolute are returned unaltered.
*
* @param {String} p
* @param {String} cwd
* @return {String}
* @api public
*/
var abspath = function (p, /*optional*/cwd) {
if (p[0] === '/') return p;
cwd = cwd || process.cwd();
return path.normalize(path.join(cwd, p));
};
/**
* Run all tests within each module, reporting the results to the command-line,
* then writes out junit-compatible xml documents.
*
* @param {Array} files
* @api public
*/
exports.run = function (files, opts, callback) {
if (!opts.output) {
console.error(
'Error: No output directory defined.\n' +
'\tEither add an "output" property to your nodeunit.json config ' +
'file, or\n\tuse the --output command line option.'
);
return;
}
opts.output = abspath(opts.output);
var error = function (str) {
return opts.error_prefix + str + opts.error_suffix;
};
var ok = function (str) {
return opts.ok_prefix + str + opts.ok_suffix;
};
var bold = function (str) {
return opts.bold_prefix + str + opts.bold_suffix;
};
var start = new Date().getTime();
var paths = files.map(function (p) {
return path.join(process.cwd(), p);
});
var modules = {}
var curModule;
nodeunit.runFiles(paths, {
testspec: opts.testspec,
testFullSpec: opts.testFullSpec,
moduleStart: function (name) {
curModule = {
errorCount: 0,
failureCount: 0,
tests: 0,
testcases: [],
name: name
};
modules[name] = curModule;
},
testDone: function (name, assertions) {
var testcase = {name: name};
for (var i=0; i<assertions.length; i++) {
var a = assertions[i];
if (a.failed()) {
a = utils.betterErrors(a);
testcase.failure = {
message: a.message,
backtrace: a.error.stack
};
if (a.error instanceof AssertionError) {
curModule.failureCount++;
}
else {
curModule.errorCount++;
}
break;
}
}
curModule.tests++;
curModule.testcases.push(testcase);
},
done: function (assertions) {
var end = new Date().getTime();
var duration = end - start;
ensureDir(opts.output, function (err) {
var tmpl = __dirname + "/../../share/junit.xml.ejs";
fs.readFile(tmpl, function (err, data) {
if (err) throw err;
var tmpl = data.toString();
for(var k in modules) {
var module = modules[k];
var rendered = ejs.render(tmpl, {
locals: {suites: [module]}
});
var filename = path.join(
opts.output,
module.name + '.xml'
);
console.log('Writing ' + filename);
fs.writeFileSync(filename, rendered, 'utf8');
}
if (assertions.failures()) {
console.log(
'\n' + bold(error('FAILURES: ')) +
assertions.failures() + '/' +
assertions.length + ' assertions failed (' +
assertions.duration + 'ms)'
);
}
else {
console.log(
'\n' + bold(ok('OK: ')) + assertions.length +
' assertions (' + assertions.duration + 'ms)'
);
}
if (callback) callback(assertions.failures() ? new Error('We have got test failures.') : undefined);
});
});
}
});
}