Skip to content
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

feat: Add Jacoco coverage and report generation #227

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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
40 changes: 40 additions & 0 deletions .github/workflows/pr_code_coverage.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: pr_code_coverage
on:
pull_request:
branches:
- main
types: [opened, reopened, synchronize]

jobs:
code-coverage:
runs-on: ubuntu-latest
name: Generate code coverage report
permissions:
pull-requests: write

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Setup JDK environment
uses: actions/setup-java@v4
with:
distribution: "zulu"
java-version: 17
cache: 'gradle'

- name: Generate coverage report
run: ./gradlew jacocoCoreDebugCodeCoverage

- name: Add coverage to PR
id: jacoco
uses: madrapps/[email protected]
with:
paths: |
${{ github.workspace }}/**/build/reports/jacoco/jacocoCoreDebugCodeCoverage/jacocoCoreDebugCodeCoverage.xml
token: ${{ secrets.GITHUB_TOKEN }}
min-coverage-overall: 40
min-coverage-changed-files: 60
title: Code Coverage
update-comment: true
debug-mode: true
55 changes: 55 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import java.io.FileInputStream
import java.util.Locale
import java.util.Properties

val keystorePropertiesFile = rootProject.file("keystore.properties")
Expand All @@ -16,6 +17,11 @@ plugins {
id("io.gitlab.arturbosch.detekt")
id("com.google.devtools.ksp") version "2.0.0-1.0.22" apply true
id("de.mannodermaus.android-junit5") version "1.11.2.0"
id("jacoco")
}

jacoco {
toolVersion = "0.8.12"
}

val kotlinVersion by extra("2.0.0")
Expand Down Expand Up @@ -63,6 +69,8 @@ android {
buildTypes {
getByName("debug") {
applicationIdSuffix = ".debug"
enableUnitTestCoverage = true
enableAndroidTestCoverage = true
}
getByName("release") {
isMinifyEnabled = true
Expand Down Expand Up @@ -99,6 +107,46 @@ android {
}

namespace = "be.scri"

applicationVariants.all {
val variantName = this.name.replaceFirstChar { if (it.isLowerCase()) it.titlecase(Locale.getDefault()) else it.toString() }
val unitTests = "test${variantName}UnitTest"
val androidTests = "connected${variantName}AndroidTest"

val exclusions = listOf(
// data binding
"**/R.class",
"**/R\$*.class",
"**/BuildConfig.*",
"**/Manifest*.*",
"**/*Test*.*"
)

tasks.register<JacocoReport>("jacoco${variantName}CodeCoverage") {
dependsOn(listOf(unitTests, androidTests))
group = "Reporting"
description = "Generate Jacoco coverage reports for the $variantName build"
reports {
xml.required.set(true)
html.required.set(true)
}
// Set source directories to the main source directory
sourceDirectories.setFrom(layout.projectDirectory.dir("src/main"))
// Set class directories to compiled Java and Kotlin classes, excluding specified exclusions
classDirectories.setFrom(files(
fileTree(layout.buildDirectory.dir("intermediates/javac/")) {
exclude(exclusions)
},
fileTree(layout.buildDirectory.dir("tmp/kotlin-classes/")) {
exclude(exclusions)
}
))
// Collect execution data from .exec and .ec files generated during test execution
executionData.setFrom(files(
fileTree(layout.buildDirectory) { include(listOf("**/*.exec", "**/*.ec")) }
))
}
}
}

dependencies {
Expand Down Expand Up @@ -155,3 +203,10 @@ tasks.register<Copy>("moveFromi18n") {
tasks.named("preBuild").configure {
dependsOn(tasks.named("moveFromi18n"))
}

tasks.withType(Test::class) {
configure<JacocoTaskExtension> {
isIncludeNoLocationClasses = true
excludes = listOf("jdk.internal.*")
}
}
Loading