-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathreporters.js
114 lines (100 loc) · 2.41 KB
/
reporters.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
import { Transform } from 'node:stream'
import { fileURLToPath } from 'node:url'
function normalizeFile (file, cwd) {
let res = file
if (file.startsWith('file://')) {
try {
res = fileURLToPath(new URL(file))
} catch (err) {
if (err.code === 'ERR_INVALID_FILE_URL_PATH') {
res = fileURLToPath(new URL(file.replace('file:///', 'file://')))
}
}
}
res = res.replace(cwd, '')
if (res.startsWith('/') || res.startsWith('\\')) {
res = res.slice(1)
}
return res
}
function eventToLine (event) {
return `* __${event.data.name}__, duration ${event.data.details.duration_ms}ms, line ${event.data.line}\n`
}
export class MarkdownReporter extends Transform {
constructor (opts) {
super({
...opts,
objectMode: true
})
this._files = {}
this._cwd = opts?.cwd
}
getFile (path) {
const file = this._files[path] || {
pass: [],
fail: []
}
this._files[path] = file
return file
}
_transform (event, encoding, callback) {
if (!event.data.file) {
callback()
return
}
const path = normalizeFile(event.data.file, this._cwd)
const file = this.getFile(path)
switch (event.type) {
case 'test:pass':
file.pass.push(event)
break
case 'test:fail':
file.fail.push(event)
break
}
callback()
}
_flush (callback) {
this.push('# Summary\n')
for (const [path, file] of Object.entries(this._files)) {
this.push(`## ${path}\n`)
if (file.pass.length > 0) {
this.push('### :white_check_mark: Pass\n')
for (const event of file.pass) {
this.push(eventToLine(event))
}
}
if (file.fail.length > 0) {
this.push('### :x: Fail\n')
for (const event of file.fail) {
this.push(eventToLine(event))
}
}
}
this.push(null)
callback()
}
}
export class GithubWorkflowFailuresReporter extends Transform {
constructor (opts) {
super({
...opts,
objectMode: true
})
this._files = {}
this._cwd = opts?.cwd
}
_transform (event, encoding, callback) {
if (!event.data.file) {
callback()
return
}
const path = normalizeFile(event.data.file, this._cwd)
switch (event.type) {
case 'test:fail':
this.push(`::error file=${path},line=${event.data.line}::${event.data.name}\n`)
break
}
callback()
}
}