Skip to content

Commit

Permalink
Add github action to run tests and get coverages
Browse files Browse the repository at this point in the history
  • Loading branch information
yogyagamage committed Sep 12, 2024
1 parent d68f7a8 commit 7eca521
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
38 changes: 38 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: Java CI with Maven

on:
push:
pull_request:

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- uses: actions/[email protected]
with:
distribution: 'temurin'
java-version: '17'
cache: 'maven'

- name: Build and Test with Maven
timeout-minutes: 15
run: ./mvnw -B verify

- name: Get JaCoCo Coverage
id: coverage
run: |
coverage=$(python3 config/coverage.py target/site/jacoco/jacoco.csv)
echo "COVERAGE=$coverage" >> $GITHUB_ENV
- name: Fail if coverage has not improved.
run: |
coverage=$COVERAGE
threshold=24.46
if (( $(echo "$coverage <= $threshold" | bc -l) )); then
echo "Coverage is not improved."
exit 1
else
echo "New coverage: $coverage%. Coverage is improved!"
fi
47 changes: 47 additions & 0 deletions config/coverage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import csv

def computeCoverage(fileList):
"""Parses one or more jacoco.csv files and computes code coverage percentages."""
missed = 0
covered = 0
missedBranches = 0
coveredBranches = 0

for filename in fileList:
try:
with open(filename, newline='') as csvfile:
jacocoReader = csv.reader(csvfile)
for i, row in enumerate(jacocoReader):
if i > 0: # Skip header
missed += int(row[3])
covered += int(row[4])
missedBranches += int(row[5])
coveredBranches += int(row[6])
except FileNotFoundError:
print(f"File not found: {filename}")
return (0, 0)
except Exception as e:
print(f"Error processing file {filename}: {str(e)}")
return (0, 0)

return (
calculatePercentage(covered, missed),
calculatePercentage(coveredBranches, missedBranches)
)

def calculatePercentage(covered, missed):
"""Calculates the coverage percentage."""
if missed == 0 and covered == 0:
return 1
return covered / (covered + missed)

def main(jacocoCsvFile):
coverage, branchCoverage = computeCoverage([jacocoCsvFile])
"""Return coverage percentage to check against the previous coverage."""
return round(coverage * 100, 2)

if __name__ == "__main__":
import sys
jacocoCsvFile = sys.argv[1]
result = main(jacocoCsvFile)
print(result)

0 comments on commit 7eca521

Please sign in to comment.