-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgenerate-codecov-json.js
62 lines (50 loc) · 2.03 KB
/
generate-codecov-json.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
// From https://github.com/sersoft-gmbh/swift-coverage-action
// See https://github.com/codecov/uploader/issues/223 for why this file exists
/*
name: generate-codecov-json.js
purpose: convert .xcresult coverage data to codecov json format
example: node generate-codecov-json.js --archive-path SOME_PATH/SOME.xcresult
note:
you'll need to build and test w/ coverage. example:
> xcodebuild -scheme xcode-poc -sdk iphonesimulator -derivedDataPath Build/ -destination 'id=09CE93EF-52C4-49AA-8C7F-B10B8CC016E0' -enableCodeCoverage YES clean test
*/
const fs = require('fs')
const { argv } = require('yargs')
const { execSync } = require('child_process')
const { log } = console
const archivePath = argv['archive-path']
const prefix = argv.prefix
const report = { coverage: {} }
getFileList(archivePath).forEach(file => {
const coverageInfo = getCoverageInfo(file)
const coverageJson = convertCoverage(coverageInfo)
const repoFilePath = file.replace(prefix, '')
report.coverage[repoFilePath] = coverageJson
})
const tmp = archivePath.split('/').pop().split('.xcresult')[0]
fs.writeFileSync(`./coverage-report-${tmp}.json`, JSON.stringify(report))
log('done.')
function getFileList(archivePath) {
const fileListCmd = 'xcrun xccov view --file-list --archive'
const fileListStr = execSync(`${fileListCmd} ${archivePath}`, { stdio: [process.stdout] }).toString()
return fileListStr.split('\n').filter(i => i !== '')
}
function getCoverageInfo(filePath) {
return execSync(`xcrun xccov view --archive ${archivePath} --file ${filePath}`, { stdio: [process.stdout] }).toString()
}
function convertCoverage(coverageInfo) {
const coverageInfoArr = coverageInfo.split('\n')
const obj = {}
coverageInfoArr.forEach(line => {
const [lineNum, lineInfo] = line.split(':')
if (lineNum && Number.isInteger(Number(lineNum))) {
const lineHits = lineInfo.trimStart().split(' ')[0].trim()
if (lineHits === '*') {
obj[String(lineNum.trim())] = null
} else {
obj[String(lineNum.trim())] = lineHits
}
}
})
return obj
}