From 31fea289f01802f998b04115aa5cb07469321895 Mon Sep 17 00:00:00 2001 From: yogyagamage <47789154+yogyagamage@users.noreply.github.com> Date: Wed, 11 Sep 2024 21:51:14 -0400 Subject: [PATCH] Add github action to run tests and get coverages --- .github/workflows/test.yml | 47 ++++++++++++++++++++++++++++++++++++++ config/coverage.py | 47 ++++++++++++++++++++++++++++++++++++++ pom.xml | 6 ++++- 3 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/test.yml create mode 100644 config/coverage.py diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000000..ca446c8e39 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,47 @@ +name: Test + +on: + push: + pull_request: + +env: + JAVA_DIST: 'zulu' + JAVA_VERSION: 22 + +defaults: + run: + shell: bash + +jobs: + test: + name: Compile and Test + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-java@v4 + with: + distribution: ${{ env.JAVA_DIST }} + java-version: ${{ env.JAVA_VERSION }} + cache: 'maven' + - name: Build and Test + run: > + xvfb-run + mvn -B verify -Djavafx.platform=linux + jacoco:report + -Pcoverage + - 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=13.43 + if (( $(echo "$coverage <= $threshold" | bc -l) )); then + echo "Coverage has not improved." + exit 1 + else + echo "New coverage: $coverage%" + fi diff --git a/config/coverage.py b/config/coverage.py new file mode 100644 index 0000000000..7b86c7eaad --- /dev/null +++ b/config/coverage.py @@ -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) diff --git a/pom.xml b/pom.xml index 02b4dc2764..daccc13a6e 100644 --- a/pom.xml +++ b/pom.xml @@ -353,7 +353,7 @@ - --enable-preview + @{surefireArgLine} --enable-preview plain true @@ -439,6 +439,10 @@ prepare-agent + + true + surefireArgLine + report