-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #715 from codecov/feat-xcode
Feat xcode
- Loading branch information
Showing
15 changed files
with
150 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import fs from 'fs/promises' | ||
|
||
import { info, UploadLogger } from '../helpers/logger' | ||
import { | ||
XcodeCoverageFileReport, | ||
XcodeCoverageReport, | ||
} from '../types' | ||
import { isProgramInstalled, runExternalProgram } from "./util" | ||
|
||
export async function generateXcodeCoverageFiles(archivePath: string): Promise<string> { | ||
if (!isProgramInstalled('xcrun')) { | ||
throw new Error('xcrun is not installed, cannot process files') | ||
} | ||
info('Running xcode coversion...') | ||
|
||
const coverage: XcodeCoverageReport = {} | ||
const report = { coverage: coverage } | ||
|
||
getFileList(archivePath).forEach(repoFilePath => { | ||
UploadLogger.verbose(`Converting ${repoFilePath}...`) | ||
const coverageInfo = getCoverageInfo(archivePath, repoFilePath) | ||
const coverageJson = convertCoverage(coverageInfo) | ||
report.coverage[repoFilePath] = coverageJson | ||
}) | ||
|
||
let pathFilename = archivePath.split('/').pop() | ||
if (pathFilename) { | ||
pathFilename = pathFilename.split('.xcresult')[0] | ||
} | ||
const filename = `./coverage-report-${pathFilename}.json` | ||
UploadLogger.verbose(`Writing coverage to ${filename}`) | ||
await fs.writeFile(filename, JSON.stringify(report)) | ||
return filename | ||
} | ||
|
||
function getFileList(archivePath: string): string[] { | ||
const fileList = runExternalProgram('xcrun', ['xccov', 'view', '--file-list', '--archive', archivePath]); | ||
return fileList.split('\n').filter(i => i !== '') | ||
} | ||
|
||
function getCoverageInfo(archivePath: string, filePath: string): string { | ||
return runExternalProgram('xcrun', ['xccov', 'view', '--archive', archivePath, '--file', filePath]) | ||
} | ||
|
||
function convertCoverage(coverageInfo: string): XcodeCoverageFileReport { | ||
const coverageInfoArr = coverageInfo.split('\n') | ||
const obj: XcodeCoverageFileReport = {} | ||
coverageInfoArr.forEach(line => { | ||
const [lineNum, lineInfo] = line.split(':') | ||
if (lineNum && Number.isInteger(Number(lineNum))) { | ||
const lineHits = lineInfo?.trimStart().split(' ')[0]?.trim() | ||
if (typeof lineHits !== 'string') { | ||
return | ||
} | ||
if (lineHits === '*') { | ||
obj[String(lineNum.trim())] = null | ||
} else { | ||
obj[String(lineNum.trim())] = lineHits | ||
} | ||
} | ||
}) | ||
return obj | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import td from 'testdouble' | ||
import childProcess from 'child_process' | ||
import { SPAWNPROCESSBUFFERSIZE } from '../../src/helpers/util' | ||
import { generateXcodeCoverageFiles } from '../../src/helpers/xcode' | ||
|
||
describe('generateXcodeCoverageFiles()', () => { | ||
afterEach(() => { | ||
td.reset() | ||
}) | ||
|
||
it('should find all files in fixtures', async () => { | ||
const spawnSync = td.replace(childProcess, 'spawnSync') | ||
td.when(spawnSync('xcrun')).thenReturn({ | ||
stdout: 'xcrun installed', | ||
error: null | ||
}) | ||
td.when(spawnSync('xcrun', td.matchers.contains('--file-list'), { maxBuffer: SPAWNPROCESSBUFFERSIZE })).thenReturn({ | ||
stdout: '../fixtures/xcode/UI/View1.swift', | ||
error: null | ||
}) | ||
td.when(spawnSync('xcrun', td.matchers.contains('--file'), { maxBuffer: SPAWNPROCESSBUFFERSIZE })).thenReturn({ | ||
stdout: ' 1: *\n 2: 0', | ||
error: null | ||
}) | ||
|
||
expect(await generateXcodeCoverageFiles('../fixtures/xcode/test.xcresult')).toBe('./coverage-report-test.json') | ||
}) | ||
|
||
it('should return an error when xcode is not installed', async () => { | ||
const spawnSync = td.replace(childProcess, 'spawnSync') | ||
td.when(spawnSync('xcrun')).thenReturn({ error: "Command 'xcrun' not found" }) | ||
|
||
await expect(generateXcodeCoverageFiles('')).rejects.toThrowError(/xcrun is not installed/) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters