forked from martinmcwhorter/karma-nunit2-reporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
180 lines (134 loc) · 5 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
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
var os = require('os');
var path = require('path');
var fs = require('fs');
var builder = require('xmlbuilder');
var NUnitReporter = function(baseReporterDecorator, config, logger, helper, formatError) {
var log = logger.create('reporter.nunit');
var reporterConfig = config.nunitReporter || {};
var pkgName = reporterConfig.suite || '';
var outputFile = helper.normalizeWinPath(path.resolve(config.basePath, reporterConfig.outputFile
|| 'test-results.xml'));
var xml;
var suites;
var results;
var pendingFileWritings = 0;
var fileWritingFinished = function() {};
var allMessages = [];
var totalSuccess = 0;
var totalFailures = 0;
var totalSkipped = 0;
baseReporterDecorator(this);
this.adapters = [function(msg) {
allMessages.push(msg);
}];
var initliazeXmlForBrowser = function(browser) {
var suite = suites[browser.id] = xml.ele('test-suite', {
name: browser.name
});
results[browser.id] = suite.ele('results');
//suite.ele('properties').ele('property', {name: 'browser.fullName', value: browser.fullName});
};
this.onRunStart = function(browsers) {
suites = Object.create(null);
results = Object.create(null);
xml = builder.create('test-results');
xml.att('name', "Karma Results")
var d = new Date();
var date = d.toISOString().substr(0, 10);
var time = d.toISOString().substr(11, 8);
xml.att('date', date);
xml.att('time', time);
// required attr we don't have data for
xml.att('invalid', 0);
xml.att('ignored', 0);
xml.att('inconclusive', 0);
xml.att('not-run', 0);
xml.att('errors', 0);
xml.ele('environment', {
'nunit-version': 'na', 'clr-version': 'na', 'os-version': os.release(),
platform: os.platform(), cwd: config.basePath, user: 'na', 'user-domain': 'na',
'machine-name': os.hostname()
});
xml.ele('culture-info', { 'current-culture': 'na', 'current-uiculture': 'na' });
// TODO(vojta): remove once we don't care about Karma 0.10
browsers.forEach(initliazeXmlForBrowser);
};
this.onBrowserStart = function(browser) {
initliazeXmlForBrowser(browser);
};
this.onBrowserComplete = function (browser) {
var suite = suites[browser.id];
if (!suite) {
// This browser did not signal `onBrowserStart`. That happens
// if the browser timed out during the start phase.
return;
}
var result = browser.lastResult;
suite.att('type', 'TestFixture');
suite.att('executed', !result.skipped);
suite.att('result', (result.failed) ? 'Failure' : 'Success');
//suite.att('total', result.total);
//suite.att('errors', result.disconnected || result.error ? 1 : 0);
//suite.att('failures', result.failed);
//suite.att('time', (result.netTime || 0) / 1000);
//suite.ele('system-out').dat(allMessages.join() + '\n');
//suite.ele('system-err');
totalSuccess = totalSuccess + result.total;
xml.att('total', totalSuccess);
totalFailures = totalFailures + result.failed;
xml.att('failures', totalFailures);
xml.att('skipped', totalSkipped);
};
this.onRunComplete = function() {
var xmlToOutput = xml;
pendingFileWritings++;
helper.mkdirIfNotExists(path.dirname(outputFile), function() {
fs.writeFile(outputFile, xmlToOutput.end({pretty: true}), function(err) {
if (err) {
log.warn('Cannot write NUnit xml\n\t' + err.message);
} else {
log.debug('NUnit results written to "%s".', outputFile);
}
if (!--pendingFileWritings) {
fileWritingFinished();
}
});
});
suites = xml = null;
allMessages.length = 0;
};
this.specSuccess = this.specSkipped = this.specFailure = function(browser, result) {
var spec = results[browser.id].ele('test-case', {
name: result.description, time: ((result.time || 0) / 1000),
description: (pkgName ? pkgName + ' ' : '') + browser.name + '.' + result.suite.join(' ').replace(/\./g, '_'),
executed: result.skipped ? 'False' : 'True',
success: (result.success || result.skipped) ? 'True' : 'False', // Skipped tests are successful
result: (result.success || result.skipped) ? 'Success' : 'Failure'
});
if (result.skipped) {
//spec.ele('skipped');
totalSkipped++;
}
if (!result.success && !result.skipped) {
// result.log.forEach(function(err) {
// spec.ele('failure', {type: ''}, formatError(err));
// });
var failure = spec.ele('failure')
failure.ele('message').dat(result.log);
failure.ele('stack-trace').dat(result.suite + ' ' + result.description);
}
};
// wait for writing all the xml files, before exiting
this.onExit = function(done) {
if (pendingFileWritings) {
fileWritingFinished = done;
} else {
done();
}
};
};
NUnitReporter.$inject = ['baseReporterDecorator', 'config', 'logger', 'helper', 'formatError'];
// PUBLISH DI MODULE
module.exports = {
'reporter:nunit': ['type', NUnitReporter]
};