This repository has been archived by the owner on May 2, 2021. It is now read-only.
forked from karma-runner/karma-teamcity-reporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
139 lines (114 loc) · 3.97 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
var util = require('util')
var fs = require('fs')
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 TeamcityReporter = function (baseReporterDecorator) {
baseReporterDecorator(this)
var self = this
this.adapters = [fs.writeSync.bind(fs.writeSync, 1)]
this.TEST_IGNORED = "#xx#teamcity[testIgnored name='%s']"
this.SUITE_START = "#xx#teamcity[testSuiteStarted name='%s']"
this.SUITE_END = "#xx#teamcity[testSuiteFinished name='%s']"
this.TEST_START = "#xx#teamcity[testStarted name='%s']"
this.TEST_FAILED = "#xx#teamcity[testFailed name='%s' message='FAILED' details='%s']"
this.TEST_END = "#xx#teamcity[testFinished name='%s' duration='%s']"
this.BLOCK_OPENED = "#xx#teamcity[blockOpened name='%s']"
this.BLOCK_CLOSED = "#xx#teamcity[blockClosed name='%s']"
var reporter = this
var initializeBrowser = function (browser) {
reporter.browserResults[browser.id] = {
name: browser.name,
log: [],
lastSuite: null
}
}
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 = 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
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 = result.description
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()
self.write(line)
if (browserResult.log.length > 0) {
self.write(' ')
}
}
}
}
TeamcityReporter.$inject = ['baseReporterDecorator']
module.exports = {
'reporter:teamcity': ['type', TeamcityReporter]
}