Skip to content

Commit

Permalink
ci: Add test coverage badge and action (#6)
Browse files Browse the repository at this point in the history
---------
Signed-off-by: starry-shivam <[email protected]>
  • Loading branch information
starry-shivam authored Jun 18, 2024
1 parent abaa94e commit 6036864
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 3 deletions.
34 changes: 34 additions & 0 deletions .github/workflows/coverage-badge.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Create coverage badge

on:
push:
branches: [ "main" ]

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: Set up JDK
uses: actions/setup-java@v3
with:
java-version: '21'
distribution: 'temurin'

- name: Generate coverage output
run: |
echo "COVERAGE=$(${{github.workspace}}/gradlew -q printLineCoverage)" >> $GITHUB_ENV
- name: Update dynamic badge gist
uses: schneegans/[email protected]
with:
auth: ${{secrets.GIST_SECRET}}
gistID: 040646868c4c6473297b0d4e1546bf21
filename: KtScheduler_coverage_badge.json
label: coverage
message: ${{env.COVERAGE}}%
valColorRange: ${{env.COVERAGE}}
minColorRange: 0
maxColorRange: 100
26 changes: 26 additions & 0 deletions .github/workflows/test_coverage.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: coverage

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: set up JDK 21
uses: actions/setup-java@v3
with:
java-version: '21'
distribution: 'temurin'
cache: gradle

- name: Grant execute permission for gradlew
run: chmod +x gradlew
- name: Run tests with gradle
run: ./gradlew koverVerify
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@

<p align="center">
<img alt="GitHub" src="https://img.shields.io/github/license/Pool-Of-Tears/Myne">
<img src="https://img.shields.io/endpoint?url=https://gist.githubusercontent.com/starry-shivam/040646868c4c6473297b0d4e1546bf21/raw/fb1f5b7d6822d0d4fcbd7d2133bed9acf652c49e/KtScheduler_coverage_badge.json" alt="coverage badge">
<img alt="GitHub code size in bytes" src="https://img.shields.io/github/languages/code-size/starry-shivam/KtScheduler">
<a href="https://github.com/starry-shivam/KtScheduler/actions/workflows/tests.yml"><img src="https://github.com/starry-shivam/KtScheduler/actions/workflows/tests.yml/badge.svg" alt="tests"></a>
<a href="https://github.com/starry-shivam/KtScheduler/releases"><img src="https://img.shields.io/github/v/tag/starry-shivam/KtScheduler" alt="version"></a>
<a href="https://www.repostatus.org/#active"><img src="https://www.repostatus.org/badges/latest/active.svg" alt="Project Status: Active – The project has reached a stable, usable state and is being actively developed." /></a>

</p>

**KtScheduler** is a lightweight task/job scheduling library for Kotlin, powered by Kotlin coroutines! The design of
Expand Down Expand Up @@ -52,7 +54,8 @@ val job = Job(
jobId = "OneTimeJob", // Must be unique for each job
function = { println("OneTime Job executed at ${ZonedDateTime.now(timeZone)}") },
trigger = OneTimeTrigger(ZonedDateTime.now(timeZone).plusSeconds(5)),
// Next runtime of the job; when creating the job for the first time, it will be used as the initial runtime.
// Next runtime of the job; when creating the job for the first time,
// it will be used as the initial runtime.
nextRunTime = ZonedDateTime.now(timeZone).plusSeconds(5),
// Coroutine dispatcher in which the job should be executed.
dispatcher = Dispatchers.Default
Expand Down Expand Up @@ -168,7 +171,7 @@ the issue you want to contribute to before starting to work on it.
### Supporting ❤️

If you found this library helpful, you can support me by giving a small tip
via [GitHub Sponsors](https://github.com/sponsors/starry-shivam) and joining the list of stargazers 🌟
via [GitHub Sponsors](https://github.com/sponsors/starry-shivam) and/or joing the list of [stargazers](https://github.com/starry-shivam/KtScheduler/stargazers) by leaving a star! 🌟

------

Expand Down
40 changes: 39 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import javax.xml.parsers.DocumentBuilderFactory

plugins {
kotlin("jvm") version "2.0.0"
id("org.jetbrains.kotlinx.kover") version "0.8.1"
Expand All @@ -24,7 +26,8 @@ dependencies {

kover {
reports {
verify { rule { minBound(70) } }
// Require 95% minimum test coverage.
verify { rule { minBound(95) } }
}
}

Expand All @@ -39,4 +42,39 @@ publishing {
}
}

// Print line coverage percentage to console so we can generate badge in CI.
tasks.register("printLineCoverage") {
group = "verification"
dependsOn("koverXmlReport")
doLast {
val report = file("$buildDir/reports/kover/report.xml")

val doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(report)
val rootNode = doc.firstChild
var childNode = rootNode.firstChild

var coveragePercent = 0.0

while (childNode != null) {
if (childNode.nodeName == "counter") {
val typeAttr = childNode.attributes.getNamedItem("type")
if (typeAttr.textContent == "LINE") {
val missedAttr = childNode.attributes.getNamedItem("missed")
val coveredAttr = childNode.attributes.getNamedItem("covered")

val missed = missedAttr.textContent.toLong()
val covered = coveredAttr.textContent.toLong()

coveragePercent = (covered * 100.0) / (missed + covered)

break
}
}
childNode = childNode.nextSibling
}

println("%.1f".format(coveragePercent))
}
}


0 comments on commit 6036864

Please sign in to comment.