Skip to content

new parsing & name #171

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: 'Python Coverage Reporter Build'
name: 'Get Cover Build'
on: ['pull_request']

jobs:
Expand Down
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
# Python Cov: Python Coverage Reporter GitHub Action
# Get Cover: The Esential Coverage Reporter GitHub Action

> 🐍 parse and publish coverage xml to a PR, enforce coverage rate on new & modified files
> ☂️ parse and publish coverage xml to a PR, enforce coverage rate on new & modified files

## Usage

Create a new workflow `.yml` file in the `.github/workflows/` directory.

You can create a coverage report using `$ pytest --cov-report xml:path/to/coverage.xml`
You can create a coverage report using python:
- pytest `$ pytest --cov-report xml:path/to/coverage.xml`
- coverage `$ coverage xml path/to/coverage.xml`

### Minimal Configuration
```yml
Expand All @@ -20,7 +22,7 @@ jobs:
coverage:
runs-on: ubuntu-latest
steps:
- name: Python Cov
- name: Get Cover
uses: orgoro/coverage@v2
with:
coverageFile: path/to/coverage.xml
Expand Down
4 changes: 2 additions & 2 deletions action.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: 'Python Cov'
description: 'Publish python 🐍 coverage report to a PR & enforce coverage on new & modified files'
name: 'Get Cover'
description: 'Publish coverage report to a PR & enforce coverage on new & modified files'
author: 'orgoro'
inputs:
coverageFile:
Expand Down
9 changes: 9 additions & 0 deletions coverage-v2.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<sources>
<source>src</source>
</sources>
<coverage version="6.0" timestamp="1633536330812" line-rate="0.94" lines-covered="940" lines-valid="1000" branches-covered="0" branches-valid="0" branch-rate="0" complexity="0">
<class branch-rate="0" complexity="0" filename="client.ts" line-rate="0.9" name="client.ts">
<class branch-rate="0" complexity="0" filename="compareCommits.ts" line-rate="0.88" name="compareCommits.ts">
<class branch-rate="0" complexity="0" filename="coverage.ts" line-rate="0.99" name="coverage.ts">
<class name="main.ts" filename="main.ts" complexity="0" line-rate="0" branch-rate="0">
<class branch-rate="0" complexity="0" filename="messagePr.ts" line-rate="0.88" name="messagePr.ts">
39 changes: 25 additions & 14 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.

23 changes: 19 additions & 4 deletions src/coverage.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
import {parseFilesCoverage, parseSource} from './coverage'
import {parseFilesCoverage, parseSource, parseAverageCoverage} from './coverage'
import fs from 'fs'
import {parse} from 'path/posix'

const coverageFilePathV1 = './coverage.xml'
const coverageFilePathV2 = './coverage-v2.xml'

describe('tests', () => {
it.each([coverageFilePathV1, coverageFilePathV2])('parses average coverage', coverageFilePath => {
const report = fs.readFileSync(coverageFilePath, 'utf8')
const parsed = parseAverageCoverage(report, 0.8)
console.log(parsed)
expect(parsed).toBeDefined()
const {ratio, covered, total, pass, threshold} = parsed
expect(total).toBe(1000)
expect(covered).toBe(940)
expect(ratio).toBe(0.94)
expect(threshold).toBe(0.8)
expect(pass).toBeTruthy()
})
it('parses coverage as expected when float', () => {
const report = fs.readFileSync('./coverage.xml', 'utf8')
const report = fs.readFileSync(coverageFilePathV1, 'utf8')
const parsed = parseFilesCoverage(report, 'src', ['src/coverage.ts'], 0.8)
expect(parsed).toBeDefined()
expect(parsed![0]).toBeDefined()
Expand All @@ -13,7 +28,7 @@ describe('tests', () => {
})

it('parses coverage as expected when zero', () => {
const report = fs.readFileSync('./coverage.xml', 'utf8')
const report = fs.readFileSync(coverageFilePathV1, 'utf8')
const parsed = parseFilesCoverage(report, 'src', ['src/main.ts'], 0.01)
expect(parsed).toBeDefined()
expect(parsed![0]).toBeDefined()
Expand All @@ -22,7 +37,7 @@ describe('tests', () => {
})

it('parses source', () => {
const report = fs.readFileSync('./coverage.xml', 'utf8')
const report = fs.readFileSync(coverageFilePathV1, 'utf8')
const parsed = parseSource(report)
expect(parsed).toBe('src')
})
Expand Down
36 changes: 23 additions & 13 deletions src/coverage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,20 +64,30 @@ export function parseSource(report: string): string {
return 'unknown'
}
}
function setFailed(): AverageCoverage {
core.setFailed('❌ could not parse total coverage - make sure xml report is valid')
return {ratio: -1, covered: -1, threshold: -1, total: -1, pass: false}
}

function parseAverageCoverage(report: string, threshold: number): AverageCoverage {
const regex = new RegExp(
`.*<coverage.*lines-valid="(?<total>[\\d\\.]+)".*lines-covered="(?<covered>[\\d\\.]+)".*line-rate="(?<ratio>[\\d\\.]+)"`
)
const match = report.match(regex)
export function parseAverageCoverage(report: string, threshold: number): AverageCoverage {
const lineRegex = new RegExp(`.*<coverage.*>`)
const totalRegex = new RegExp(`.*lines-valid="(?<total>[\\d\\.]+)".*`)
const coveredRegex = new RegExp(`.*lines-covered="(?<covered>[\\d\\.]+)".*`)
const ratioRegex = new RegExp(`.*line-rate="(?<ratio>[\\d\\.]+).*"`)

if (match?.groups) {
const ratio = parseFloat(match.groups['ratio'])
const covered = parseFloat(match.groups['covered'])
const total = parseFloat(match.groups['total'])
return {ratio, covered, threshold, total, pass: ratio > threshold}
} else {
core.setFailed('❌ could not parse total coverage - make sure xml report is valid')
return {ratio: -1, covered: -1, threshold: -1, total: -1, pass: false}
const match = report.match(lineRegex)
let result = null
if (match?.length === 1) {
const totalMatch = match[0].match(totalRegex)
const coveredMatch = match[0].match(coveredRegex)
const ratioMatch = match[0].match(ratioRegex)

if (totalMatch?.groups && coveredMatch?.groups && ratioMatch?.groups) {
const total = parseFloat(totalMatch.groups['total'])
const covered = parseFloat(coveredMatch.groups['covered'])
const ratio = parseFloat(ratioMatch.groups['ratio'])
result = {ratio, covered, threshold, total, pass: ratio > threshold}
}
}
return result ?? setFailed()
}
4 changes: 2 additions & 2 deletions src/scorePr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ export function scorePr(filesCover: FilesCoverage): boolean {
core.info('No covered modified files in this PR ')
}
const sha = context.payload.pull_request?.head.sha.slice(0, 7)
const action = '[action](https://github.com/marketplace/actions/python-cov)'
message = message.concat(`\n\n\n> **updated for commit: \`${sha}\` by ${action}🐍**`)
const action = '[action](https://github.com/marketplace/actions/get-cover)'
message = message.concat(`\n\n\n> **updated for commit: \`${sha}\` by ${action}🛡**`)
message = `\n> current status: ${passOverall ? '✅' : '❌'}`.concat(message)
publishMessage(context.issue.number, message)
core.endGroup()
Expand Down