forked from karma-runner/karma-teamcity-reporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
174 lines (141 loc) · 4.99 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
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 hashString = function (s) {
var hash = 0
var i
var chr
var len
if (s === 0) return hash
for (i = 0, len = s.length; i < len; i++) {
chr = s.charCodeAt(i)
hash = ((hash << 5) - hash) + chr
hash |= 0
}
return hash
}
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 TeamcityReporter = function (baseReporterDecorator, teamcityReporter) {
const prependSpecIdInSpecNameEnabled = !!(teamcityReporter && teamcityReporter.prependSpecIdInSpecNameEnabled)
const useSpecFullName = !!(teamcityReporter && teamcityReporter.useSpecFullName)
baseReporterDecorator(this)
var self = this
this.TEST_IGNORED = "##teamcity[testIgnored name='%s' flowId='']"
this.SUITE_START = "##teamcity[testSuiteStarted name='%s' flowId='']"
this.SUITE_END = "##teamcity[testSuiteFinished name='%s' flowId='']"
this.TEST_START = "##teamcity[testStarted name='%s' flowId='']"
this.TEST_FAILED = "##teamcity[testFailed name='%s' message='FAILED' details='%s' flowId='']"
this.TEST_END = "##teamcity[testFinished name='%s' duration='%s' flowId='']"
this.BLOCK_OPENED = "##teamcity[blockOpened name='%s' flowId='']"
this.BLOCK_CLOSED = "##teamcity[blockClosed name='%s' flowId='']"
var reporter = this
var initializeBrowser = function (browser) {
reporter.browserResults[browser.id] = {
name: browser.name,
log: [],
lastSuite: null,
flowId: 'karmaTC' + hashString(browser.name + ((new Date()).getTime())) + browser.id
}
}
const getFormattedSpecName = ({ id, description, fullName }) => {
let formattedSpecName = ''
if (prependSpecIdInSpecNameEnabled) {
formattedSpecName += `[${id.toLocaleUpperCase()}] `
}
if (useSpecFullName) {
formattedSpecName += fullName
} else {
formattedSpecName += description
}
return formattedSpecName
}
this.onRunStart = function (browsers) {
this.write(formatMessage(this.BLOCK_OPENED, 'JavaScript Unit Tests'))
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 = getFormattedSpecName(result)
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 = getFormattedSpecName(result)
log.push(formatMessage(this.TEST_START, testName))
log.push(formatMessage(this.TEST_FAILED, testName, result.log.join('\n\n')))
log.push(formatMessage(this.TEST_END, testName, result.time))
}
this.specSkipped = function (browser, result) {
var log = this.getLog(browser, result)
var testName = getFormattedSpecName(result)
log.push(formatMessage(this.TEST_IGNORED, testName))
}
this.onRunComplete = function () {
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.flushLogs(browserResult)
})
self.write(formatMessage(self.BLOCK_CLOSED, 'JavaScript Unit Tests'))
}
this.getLog = function (browser, result) {
var browserResult = this.browserResults[browser.id]
var suiteName = browser.name
var moduleName = result.suite.join(' ')
if (moduleName) {
suiteName = moduleName.concat('.', suiteName)
}
var log = browserResult.log
if (browserResult.lastSuite !== suiteName) {
if (browserResult.lastSuite) {
log.push(formatMessage(this.SUITE_END, browserResult.lastSuite))
}
this.flushLogs(browserResult)
browserResult.lastSuite = suiteName
log.push(formatMessage(this.SUITE_START, suiteName))
}
return log
}
this.flushLogs = function (browserResult) {
while (browserResult.log.length > 0) {
var line = browserResult.log.shift()
line = line.replace("flowId=''", "flowId='" + browserResult.flowId + "'")
self.write(line)
if (browserResult.log.length > 0) {
self.write(' ')
}
}
}
}
TeamcityReporter.$inject = ['baseReporterDecorator', 'config.teamcityReporter']
module.exports = {
'reporter:teamcity': ['type', TeamcityReporter]
}