forked from test-summary/action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_parser.ts
312 lines (249 loc) · 8.01 KB
/
test_parser.ts
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
import * as fs from "fs"
import * as util from "util"
import xml2js from "xml2js"
export enum TestStatus {
Pass = (1 << 0),
Fail = (1 << 1),
Skip = (1 << 2)
}
export interface TestCounts {
passed: number
failed: number
skipped: number
}
export interface TestResult {
counts: TestCounts
suites: TestSuite[]
/** If the test runner itself fails, it will set an exception. */
exception?: string
}
export interface TestSuite {
name?: string
timestamp?: string
filename?: string
cases: TestCase[]
}
export interface TestCase {
status: TestStatus
name?: string
description?: string
details?: string
duration?: string
}
export async function parseTap(data: string): Promise<TestResult> {
const lines = data.trim().split(/\r?\n/)
let version = 12
let header = 0
let trailer = false
if (lines.length > 0 && lines[header].match(/^TAP version 13$/)) {
version = 13
header++
}
if (lines.length > 0 && lines[header].match(/^1\.\./)) {
// TODO: capture the plan for validation
header++
}
let testMax = 0
let num = 0
const suites: TestSuite[] = [ ]
let exception: string | undefined = undefined
let cases = [ ]
let suitename: string | undefined = undefined
const counts = {
passed: 0,
failed: 0,
skipped: 0
}
for (let i = header; i < lines.length; i++) {
const line = lines[i]
let found
let status = TestStatus.Skip
let name: string | undefined = undefined
let description: string | undefined = undefined
let details: string | undefined = undefined
if (found = line.match(/^\s*#(.*)/)) {
if (!found[1]) {
continue
}
/* a comment starts a new suite */
if (cases.length > 0) {
suites.push({
name: suitename,
cases: cases
})
suitename = undefined
cases = [ ]
}
if (suitename)
suitename += " " + found[1].trim()
else
suitename = found[1].trim()
continue
} else if (found = line.match(/^ok(?:\s+(\d+))?\s*-?\s*([^#]*?)\s*#\s*[Ss][Kk][Ii][Pp]\S*(?:\s+(.*?)\s*)?$/)) {
num = parseInt(found[1])
status = TestStatus.Skip
name = (found[2] && found[2].length > 0) ? found[2] : undefined
description = found[3]
counts.skipped++
} else if (found = line.match(/^ok(?:\s+(\d+))?\s*-?\s*(?:(.*?)\s*)?$/)) {
num = parseInt(found[1])
status = TestStatus.Pass
name = found[2]
counts.passed++
} else if (found = line.match(/^not ok(?:\s+(\d+))?\s*-?\s*([^#]*?)\s*#\s*[Tt][Oo][Dd][Oo](?:\s+(.*?)\s*)?$/)) {
num = parseInt(found[1])
status = TestStatus.Skip
name = (found[2] && found[2].length > 0) ? found[2] : undefined
description = found[3]
counts.skipped++
} else if (found = line.match(/^not ok(?:\s+(\d+))?\s*-?\s*-?\s*(?:(.*?)\s*)?$/)) {
num = parseInt(found[1])
status = TestStatus.Fail
name = found[2]
counts.failed++
} else if (line.match(/^Bail out\!/)) {
const message = (line.match(/^Bail out\!(.*)/))
if (message) {
exception = message[1].trim()
}
break
} else if (line.match(/^$/)) {
continue
} else if (line.match(/^1\.\.\d+/)) {
// TODO: capture the plan for validation
trailer = true
continue
} else {
throw new Error(`unknown TAP line ${i + 1}: '${line}'`)
}
if (isNaN(num)) {
num = ++testMax
} else if (num > testMax) {
testMax = num
}
if ((i + 1) < lines.length && lines[i + 1].match(/^ ---$/)) {
i++
while (i < lines.length && !lines[i + 1].match(/^ \.\.\.$/)) {
const detail = (lines[i + 1].match(/^ (.*)/))
if (!detail) {
throw new Error("invalid yaml in test case details")
}
if (details)
details += "\n" + detail[1]
else
details = detail[1]
i++
}
if (i >= lines.length) {
throw new Error("truncated yaml in test case details")
}
i++
}
if (trailer) {
throw new Error("unexpected test results after trailer")
}
cases.push({
status: status,
name: name,
description: description,
details: details
})
}
suites.push({
name: suitename,
cases: cases
})
return {
counts: counts,
suites: suites,
exception: exception
}
}
async function parseJunitXml(xml: any): Promise<TestResult> {
let testsuites
if ('testsuites' in xml) {
testsuites = xml.testsuites.testsuite || [ ]
} else if ('testsuite' in xml) {
testsuites = [ xml.testsuite ]
} else {
throw new Error("expected top-level testsuites or testsuite node")
}
if (!Array.isArray(testsuites)) {
throw new Error("expected array of testsuites")
}
const suites: TestSuite[] = [ ]
const counts = {
passed: 0,
failed: 0,
skipped: 0
}
for (const testsuite of testsuites) {
const cases = [ ]
if (!Array.isArray(testsuite.testcase)) {
continue
}
for (const testcase of testsuite.testcase) {
let status = TestStatus.Pass
const id = testcase.$.id
const classname = testcase.$.classname
const name = testcase.$.name
const duration = testcase.$.time
let details: string | undefined = undefined
if (testcase.skipped) {
status = TestStatus.Skip
counts.skipped++
} else if (testcase.failure || testcase.error) {
status = TestStatus.Fail
details = (testcase.failure || testcase.error)[0]._
counts.failed++
} else {
counts.passed++
}
cases.push({
status: status,
name: name,
description: classname,
details: details,
duration: duration
})
}
suites.push({
name: testsuite.$.name,
timestamp: testsuite.$.timestamp,
filename: testsuite.$.file,
cases: cases
})
}
return {
counts: counts,
suites: suites
}
}
export async function parseJunit(data: string): Promise<TestResult> {
const parser = util.promisify(xml2js.parseString)
const xml: any = await parser(data)
return await parseJunitXml(xml)
}
export async function parseTapFile(filename: string): Promise<TestResult> {
const readfile = util.promisify(fs.readFile)
return await parseTap(await readfile(filename, "utf8"))
}
export async function parseJunitFile(filename: string): Promise<TestResult> {
const readfile = util.promisify(fs.readFile)
return await parseJunit(await readfile(filename, "utf8"))
}
export async function parseFile(filename: string): Promise<TestResult> {
const readfile = util.promisify(fs.readFile)
const parser = util.promisify(xml2js.parseString)
const data = await readfile(filename, "utf8")
if (data.match(/^TAP version 13\r?\n/) ||
data.match(/^ok /) ||
data.match(/^not ok /)) {
return await parseTap(data)
}
const xml: any = await parser(data)
if (xml.testsuites || xml.testsuite) {
return await parseJunitXml(xml)
}
throw new Error(`unknown test file type for '${filename}'`)
}