-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathprintApiCoverage.js
67 lines (58 loc) · 2.15 KB
/
printApiCoverage.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
const { CONTENT_API } = require('@dcl/catalyst-api-specs')
const chalk = require('chalk')
const columnify = require('columnify')
const fs = require('fs')
const path = require('path')
const jestConfig = require('./jest.config')
function printApiCoverage() {
const entries = new Set()
// Combine API coverage results files with parallel processing in the CI
fs.readdirSync(path.join(__dirname, 'api-coverage')).forEach(file => {
if (file.startsWith('api-coverage') && file.endsWith('.csv')) {
const content = fs.readFileSync(path.resolve(__dirname, 'api-coverage', file))
for (const entry of content.toString().split("\n")) {
entries.add(entry)
}
}
})
const coverage = Array.from(entries).map((l) => {
const [ path, method, status ] = l.split(",")
return { path, method, status }
})
const tableRows = []
let someMissing = false
for (const apiPath in CONTENT_API.paths) {
for (const method in CONTENT_API.paths[apiPath]) {
for (const status in CONTENT_API.paths[apiPath][method].responses) {
const replacedPath = apiPath
.replace(/\{.*?\}/, '[^\\/]*')
.replace(/\{.*?\}/, '[^\\/]*') // Replace a second time for URLs with two query params
const testablePath = new RegExp(`^${replacedPath}$`)
let covered = false
for (const tested of coverage) {
if (testablePath.test(tested.path) && tested.method === method.toUpperCase() && tested.status === status) {
covered = true
break
}
}
if (!covered) {
someMissing = true
}
const color = covered ? chalk.green : chalk.red
const row = {
route: color(`${method.toUpperCase()} ${apiPath}`),
statuses: color(status),
status: covered ? 'COVERED' : 'MISSING'
}
tableRows.push(row)
}
}
}
console.info('API Coverage:')
console.info(columnify(tableRows, { columnSplitter: ' | ' }))
// Throw an error if some endpoint/method is not tested
if (someMissing) {
throw new Error('There are some endpoints that are not fully tested')
}
}
module.exports = printApiCoverage