forked from david-driscoll/karma-msbuild-reporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
143 lines (116 loc) · 4.36 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
var util = require('util');
var escapeMessage = function (message) {
if(message === null || message === undefined) {
return '';
}
return message.toString().
replace(/\|/g, '||').
replace(/\'/g, '|\'').
replace(/\n/g, '|n').
replace(/\r/g, '|r').
replace(/\u0085/g, '|x').
replace(/\u2028/g, '|l').
replace(/\u2029/g, '|p').
replace(/\[/g, '|[').
replace(/\]/g, '|]');
};
var formatMessage = function() {
var args = Array.prototype.slice.call(arguments);
for (var i = args.length - 1; i > 0; i--) {
args[i] = escapeMessage(args[i]);
}
return util.format.apply(null, args) + '\n';
};
var MSBuildReporter = function(baseReporterDecorator) {
baseReporterDecorator(this);
this.TEST_IGNORED = 'testIgnored name=\'%s\'';
this.SUITE_START = 'testSuiteStarted name=\'%s\'';
this.SUITE_END = 'testSuiteFinished name=\'%s\'';
this.TEST_START = 'testStarted name=\'%s\'';
this.TEST_FAILED = '%s: error: Test \'%s\' Failed: %s';
this.TEST_END = 'testFinished name=\'%s\' duration=\'%s\'';
this.BROWSER_START = 'browserStart name=\'%s\'';
this.BROWSER_END = 'browserEnd name=\'%s\'';
this.REG_LINENUMBER = /\?([0-9a-z]+?)\:(\d+)$/;
var reporter = this;
var initializeBrowser = function (browser) {
reporter.browserResults[browser.id] = {
name: browser.name,
log: [],
lastSuite: null
};
};
this.onRunStart = function (browsers) {
this.browserResults = {};
// Support Karma 0.10 (TODO: remove)
browsers.forEach(initializeBrowser);
};
this.onBrowserStart = function (browser) {
initializeBrowser(browser);
};
this.specSuccess = function(browser, result) {
var log = this.getLog(browser, result);
var testName = result.description;
log.push(formatMessage(this.TEST_START, testName));
log.push(formatMessage(this.TEST_END, testName, result.time));
};
this.specFailure = function(browser, result) {
var log = this.getLog(browser, result);
var testName = result.description;
var self = this;
log.push(formatMessage(this.TEST_START, testName));
console.log(result);
result.log.forEach(function (item) {
console.log(self.formatErrorMessageForTest(item, testName));
});
//log.push(formatMessage(this.TEST_FAILED, testName, result.log.join("\\n").replace(/\n/g, '$(NewLine)')));
log.push(formatMessage(this.TEST_END, testName, result.time));
};
this.formatErrorMessageForTest = function (message, testName) {
var cleanedMessage = message.replace(/\\n/g, '');
var messageInfo = cleanedMessage.split(' at ');
return formatMessage(this.TEST_FAILED, this.extractFileAndLinenumber(messageInfo[1]), testName, messageInfo[0].replace(/\n/g, '').trim());
};
this.extractFileAndLinenumber = function (message) {
var path = message.indexOf('absolute') > -1 ? message.split('absolute')[1] : message;
var lineNumMatch = this.REG_LINENUMBER.exec(path);
if (lineNumMatch) {
return path.replace(lineNumMatch[0], '') + ' ' + '(' + lineNumMatch[2] + ')';
}
};
this.specSkipped = function(browser, result) {
var log = this.getLog(browser, result);
var testName = result.description;
log.push(formatMessage(this.TEST_IGNORED, testName));
};
this.onRunComplete = function() {
var self = this;
Object.keys(this.browserResults).forEach(function(browserId) {
var browserResult = self.browserResults[browserId];
var log = browserResult.log;
if(browserResult.lastSuite) {
log.push(formatMessage(self.SUITE_END, browserResult.lastSuite));
}
self.write(formatMessage(self.BROWSER_START, browserResult.name));
self.write(log.join(''));
self.write(formatMessage(self.BROWSER_END, browserResult.name));
});
};
this.getLog = function(browser, result) {
var browserResult = this.browserResults[browser.id];
var suiteName = result.suite.join(' ');
var log = browserResult.log;
if(browserResult.lastSuite !== suiteName) {
if(browserResult.lastSuite) {
log.push(formatMessage(this.SUITE_END, browserResult.lastSuite));
}
browserResult.lastSuite = suiteName;
log.push(formatMessage(this.SUITE_START, suiteName));
}
return log;
};
};
MSBuildReporter.$inject = ['baseReporterDecorator'];
module.exports = {
'reporter:msbuild': ['type', MSBuildReporter]
};