Skip to content

Commit

Permalink
Handle relative paths in lcov files
Browse files Browse the repository at this point in the history
  • Loading branch information
ggilder committed Dec 16, 2024
1 parent 891d65a commit 7a07930
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 13 deletions.
6 changes: 3 additions & 3 deletions __tests__/fixtures/lcov.info
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
TN:
SF:src/utils/general.ts
SF:./src/utils/general.ts
FN:2,getFileNameFirstItemFromPath
FNF:1
FNH:0
Expand Down Expand Up @@ -29,7 +29,7 @@ BRF:16
BRH:0
end_of_record
TN:
SF:src/utils/github.ts
SF:./src/utils/github.ts
FN:10,(anonymous_13)
FN:17,(anonymous_14)
FN:27,(anonymous_15)
Expand Down Expand Up @@ -95,7 +95,7 @@ BRF:15
BRH:0
end_of_record
TN:
SF:src/utils/lcov.ts
SF:./src/utils/lcov.ts
FN:5,parseLCov
FN:14,filterCoverageByFile
FN:15,(anonymous_17)
Expand Down
2 changes: 1 addition & 1 deletion __tests__/utils/general.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {

test('filterCoverageByFile', async function () {
const path = getFixturePath('lcov.info')
const parsedLcov = await parseLCov(path)
const parsedLcov = await parseLCov(path, '')
const output = filterCoverageByFile(parsedLcov)
expect(output).toMatchSnapshot()
})
Expand Down
4 changes: 2 additions & 2 deletions __tests__/utils/lcov.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import {getFixturePath} from '../fixtures/util'

test('should parse lCov file', async function () {
const path = getFixturePath('lcov.info')
const output = await parseLCov(path)
const output = await parseLCov(path, process.cwd())
expect(output).toMatchSnapshot()
})

test('should throw err if lCov file path is not given', async function () {
await expect(parseLCov('')).rejects.toThrow('No LCov path provided')
await expect(parseLCov('', '')).rejects.toThrow('No LCov path provided')
})
11 changes: 8 additions & 3 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export async function play(): Promise<void> {
var parsedCov = await parseGoCoverage(COVERAGE_FILE_PATH, 'go.mod')
} else {
// lcov default
var parsedCov = await parseLCov(COVERAGE_FILE_PATH)
var parsedCov = await parseLCov(COVERAGE_FILE_PATH, workspacePath)
}
// Sum up lines.found for each entry in parsedCov
const totalLines = parsedCov.reduce(
Expand Down
15 changes: 13 additions & 2 deletions src/utils/lcov.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
import * as NodeUtil from 'util'
import * as fs from 'fs'
import * as path from 'path'
import {CoverageParsed} from './general'
import parser from 'lcov-parse'

export async function parseLCov(lcovPath: string): Promise<CoverageParsed> {
export async function parseLCov(
lcovPath: string,
workspacePath: string
): Promise<CoverageParsed> {
if (!lcovPath) {
throw Error('No LCov path provided')
}

const parserPromise = NodeUtil.promisify(parser)
const fileRaw = fs.readFileSync(lcovPath, 'utf8')
return parserPromise(fileRaw) as CoverageParsed
const parsed = (await parserPromise(fileRaw)) as CoverageParsed

for (const entry of parsed) {
entry.file = path.relative(workspacePath, entry.file)
}

return parsed
}

0 comments on commit 7a07930

Please sign in to comment.