-
-
Notifications
You must be signed in to change notification settings - Fork 269
/
report.ts
339 lines (319 loc) · 9.1 KB
/
report.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
import { LoadedConfig } from '@tapjs/config'
import * as CORE from '@tapjs/core'
import { mkdirp } from 'mkdirp'
import { readFileSync } from 'node:fs'
import { writeFile } from 'node:fs/promises'
import { resolve } from 'node:path'
import t from 'tap'
let globCwd = t.testdirName
t.beforeEach(t => (globCwd = t.testdirName))
interface Summary {
lines: { pct: number | 'Unknown' }
functions: { pct: number | 'Unknown' }
statements: { pct: number | 'Unknown' }
branches: { pct: number | 'Unknown' }
}
const mockMainConf: { mainCommand: 'run' | 'report' } = {
mainCommand: 'run',
}
const validCoverageReports = new Set([
'clover',
'cobertura',
'html',
'json',
'json-summary',
'lcov',
'lcovonly',
'none',
'teamcity',
'text',
'text-lcov',
'text-summary',
])
class MockConfig {
#coverageReport: string[]
globCwd: string = globCwd
showFullCoverage?: boolean
constructor(coverageReport: string[] = []) {
this.#coverageReport = coverageReport
}
get(k: string) {
switch (k) {
case 'coverage-report':
return this.#coverageReport
case 'show-full-coverage':
return this.showFullCoverage
default:
throw new Error('should only look up coverage configs')
}
}
}
// set this explicitly in tests
const summaryZero: Summary = {
lines: { pct: 'Unknown' },
functions: { pct: 'Unknown' },
statements: { pct: 'Unknown' },
branches: { pct: 'Unknown' },
}
const summary50: Summary = {
lines: { pct: 50 },
functions: { pct: 'Unknown' },
statements: { pct: 50 },
branches: { pct: 50 },
}
const summary100: Summary = {
lines: { pct: 100 },
functions: { pct: 100 },
statements: { pct: 100 },
branches: { pct: 100 },
}
let summary: Summary = summaryZero
class MockReport {
reporter: string[]
reportsDirectory: string
tempDirectory: string
excludeNodeModules: boolean
summary: Summary = summary
constructor({
reporter,
reportsDirectory,
tempDirectory,
excludeNodeModules,
}: {
reporter: string[]
reportsDirectory: string
tempDirectory: string
excludeNodeModules: boolean
}) {
if (!excludeNodeModules) {
throw new Error('node_modules should always be excluded')
}
this.reporter = reporter
for (const c of this.reporter) {
if (!validCoverageReports.has(c)) {
throw new Error('invalid coverage report: ' + c)
}
}
this.reportsDirectory = reportsDirectory
this.tempDirectory = tempDirectory
this.excludeNodeModules = excludeNodeModules
}
async run() {
await mkdirp(resolve(this.reportsDirectory, 'lcov-report'))
if (this.reporter.includes('html')) {
await writeFile(
resolve(this.reportsDirectory, 'index.html'),
'report'
)
}
if (this.reporter.includes('lcov')) {
await writeFile(
resolve(this.reportsDirectory, 'lcov-report/index.html'),
'report'
)
}
if (this.reporter.includes('text')) {
process.stdout.write('report')
}
if (this.reporter.includes('text-summary')) {
process.stdout.write('summary')
}
}
async getCoverageMapFromAllCoverageFiles() {
return {
getCoverageSummary: () => this.summary,
}
}
}
const mockTap = {
comment: () => {},
debug: () => {},
}
const mockCore = t.createMock(CORE, {
tap: () => mockTap,
})
t.test('omit text report for full coverage run', async t => {
summary = summary100
t.testdir({
'.tap': {
coverage: {
'file.json': '{}',
},
},
})
const comments = t.capture(mockTap, 'comment')
const { report } = (await t.mockImport('../dist/report.js', {
c8: { Report: MockReport },
'@tapjs/core': mockCore,
'../dist/main-config.js': mockMainConf,
})) as typeof import('../dist/report.js')
const config = new MockConfig(['text'])
const logs = t.capture(console, 'log')
await report([], config as unknown as LoadedConfig)
t.strictSame(logs.args(), [], 'show nothing if full coverage')
t.strictSame(comments.args(), [])
})
t.test('show full coverage explicit report', async t => {
summary = summary100
t.testdir({
'.tap': {
coverage: {
'file.json': '{}',
},
},
})
const comments = t.capture(mockTap, 'comment')
const { report } = (await t.mockImport('../dist/report.js', {
c8: { Report: MockReport },
'@tapjs/core': mockCore,
'../dist/main-config.js': { mainCommand: 'report' },
})) as typeof import('../dist/report.js')
const config = new MockConfig(['text'])
const logs = t.capture(console, 'log')
await report([], config as unknown as LoadedConfig)
t.strictSame(logs.args(), [['report']], 'show full coverage report')
t.strictSame(comments.args(), [])
})
t.test('explicit report, full cov report explicit off', async t => {
summary = summary100
t.testdir({
'.tap': {
coverage: {
'file.json': '{}',
},
},
})
const comments = t.capture(mockTap, 'comment')
const { report } = (await t.mockImport('../dist/report.js', {
c8: { Report: MockReport },
'@tapjs/core': mockCore,
'../dist/main-config.js': { mainCommand: 'report' },
})) as typeof import('../dist/report.js')
const config = new MockConfig(['text'])
config.showFullCoverage = false
const logs = t.capture(console, 'log')
await report(['text'], config as unknown as LoadedConfig)
t.strictSame(
logs.args(),
[['summary']],
'show full coverage summary'
)
t.strictSame(comments.args(), [])
})
t.test('run an html report', async t => {
const cases: [string, string][] = [
['html', 'index.html'],
['lcov', 'lcov-report/index.html'],
]
for (const [style, file] of cases) {
t.test(style, async t => {
summary = summary100
t.testdir({
'.tap': {
coverage: {
'file.json': JSON.stringify({}),
},
},
})
const comments = t.capture(mockTap, 'comment')
let openerRan = false
const htmlReport = resolve(globCwd, '.tap/report', file)
const { report } = (await t.mockImport('../dist/report.js', {
c8: { Report: MockReport },
'@tapjs/core': mockCore,
opener: (file: string) => {
t.equal(file, htmlReport)
openerRan = true
},
'../dist/main-config.js': { mainCommand: 'report' },
})) as typeof import('../dist/report.js')
const config = new MockConfig([])
const logs = t.capture(console, 'log')
await report([style], config as unknown as LoadedConfig)
if (style === 'html') t.strictSame(logs.args(), [])
else {
const expect = `lcov report: ${resolve(
t.testdirName,
'.tap/report/lcov.info'
)}`
t.strictSame(logs.args(), [[expect]])
}
t.strictSame(comments.args(), [])
t.equal(openerRan, true)
t.equal(readFileSync(htmlReport, 'utf8'), 'report')
})
}
})
t.test('no coverage files generated', async t => {
summary = summaryZero
t.testdir({
'.tap': {
coverage: {},
},
})
const comments = t.capture(mockTap, 'comment')
const { report } = (await t.mockImport('../dist/report.js', {
c8: { Report: MockReport },
'@tapjs/core': mockCore,
})) as typeof import('../dist/report.js')
const config = new MockConfig([])
const logs = t.capture(console, 'log')
await report([], config as unknown as LoadedConfig)
t.strictSame(logs.args(), [])
t.strictSame(comments.args(), [['No coverage generated']])
t.equal(process.exitCode, 1)
process.exitCode = 0
})
t.test('no coverage summary generated', async t => {
summary = summaryZero
t.testdir({
'.tap': {
coverage: { 'file.json': '{}' },
},
})
const comments = t.capture(mockTap, 'comment')
const { report } = (await t.mockImport('../dist/report.js', {
c8: { Report: MockReport },
'@tapjs/core': mockCore,
})) as typeof import('../dist/report.js')
const config = new MockConfig([])
const logs = t.capture(console, 'log')
await report([], config as unknown as LoadedConfig)
t.strictSame(logs.args(), [])
t.strictSame(comments.args(), [['No coverage generated']])
t.equal(process.exitCode, 1)
process.exitCode = 0
})
t.test('not full coverage', async t => {
summary = summary50
t.testdir({
'.tap': {
coverage: { 'file.json': '{}' },
},
})
const comments = t.capture(mockTap, 'comment')
let openerRan = false
const htmlReport = resolve(globCwd, '.tap/report/index.html')
const { report } = (await t.mockImport('../dist/report.js', {
c8: { Report: MockReport },
'@tapjs/core': mockCore,
opener: (file: string) => {
t.equal(file, htmlReport)
openerRan = true
},
})) as typeof import('../dist/report.js')
const config = new MockConfig([])
const logs = t.capture(console, 'log')
await report(['html'], config as unknown as LoadedConfig)
t.strictSame(logs.args(), [])
t.strictSame(comments.args(), [
['ERROR: incomplete statements coverage (50%)'],
['ERROR: incomplete branches coverage (50%)'],
['ERROR: incomplete functions coverage (0%)'],
['ERROR: incomplete lines coverage (50%)'],
])
t.equal(openerRan, true)
t.equal(readFileSync(htmlReport, 'utf8'), 'report')
t.equal(process.exitCode, 1)
process.exitCode = 0
})