From ceb163551bef45ac76e8e80d7fb7e51de4dcf446 Mon Sep 17 00:00:00 2001 From: Alicia Bendz Date: Sat, 26 Oct 2024 20:17:10 -0400 Subject: [PATCH 1/5] feat: Add Jacoco coverage and report generation --- .github/workflows/pr_unit_test.yaml | 14 ++++++++ app/build.gradle.kts | 55 +++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) diff --git a/.github/workflows/pr_unit_test.yaml b/.github/workflows/pr_unit_test.yaml index 7e391063..86636fd4 100644 --- a/.github/workflows/pr_unit_test.yaml +++ b/.github/workflows/pr_unit_test.yaml @@ -26,3 +26,17 @@ jobs: - name: Run tests run: ./gradlew test + + - name: Generate coverage report + run: ./gradlew jacocoCoreDebugCodeCoverage + + - name: Add coverage to PR + id: jacoco + uses: madrapps/jacoco-report@v1.7.1 + with: + paths: | + ${{ github.workspace }}/**/build/reports/jacoco/jacocoCoreDebugCodeCoverage/jacocoCoreDebugCodeCoverage.xml + token: ${{ secrets.GITHUB_TOKEN }} + min-coverage-overall: 40 + min-coverage-changed-files: 60 + update-comment: true diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 6506b89b..1b3c9fd5 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -1,4 +1,5 @@ import java.io.FileInputStream +import java.util.Locale import java.util.Properties val keystorePropertiesFile = rootProject.file("keystore.properties") @@ -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") @@ -63,6 +69,8 @@ android { buildTypes { getByName("debug") { applicationIdSuffix = ".debug" + enableUnitTestCoverage = true + enableAndroidTestCoverage = true } getByName("release") { isMinifyEnabled = true @@ -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("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 { @@ -155,3 +203,10 @@ tasks.register("moveFromi18n") { tasks.named("preBuild").configure { dependsOn(tasks.named("moveFromi18n")) } + +tasks.withType(Test::class) { + configure { + isIncludeNoLocationClasses = true + excludes = listOf("jdk.internal.*") + } +} From c33436624841c8a89b58ea52485614add5b909bd Mon Sep 17 00:00:00 2001 From: Alicia Bendz Date: Mon, 28 Oct 2024 17:38:37 -0400 Subject: [PATCH 2/5] Split code coverage into a separate PR workflow --- .github/workflows/pr_code_coverage.yaml | 36 +++++++++++++++++++++++++ .github/workflows/pr_unit_test.yaml | 14 ---------- 2 files changed, 36 insertions(+), 14 deletions(-) create mode 100644 .github/workflows/pr_code_coverage.yaml diff --git a/.github/workflows/pr_code_coverage.yaml b/.github/workflows/pr_code_coverage.yaml new file mode 100644 index 00000000..0d6a0944 --- /dev/null +++ b/.github/workflows/pr_code_coverage.yaml @@ -0,0 +1,36 @@ +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 + + 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/jacoco-report@v1.7.1 + with: + paths: | + ${{ github.workspace }}/**/build/reports/jacoco/jacocoCoreDebugCodeCoverage/jacocoCoreDebugCodeCoverage.xml + token: ${{ secrets.GITHUB_TOKEN }} + min-coverage-overall: 40 + min-coverage-changed-files: 60 + update-comment: true diff --git a/.github/workflows/pr_unit_test.yaml b/.github/workflows/pr_unit_test.yaml index 86636fd4..7e391063 100644 --- a/.github/workflows/pr_unit_test.yaml +++ b/.github/workflows/pr_unit_test.yaml @@ -26,17 +26,3 @@ jobs: - name: Run tests run: ./gradlew test - - - name: Generate coverage report - run: ./gradlew jacocoCoreDebugCodeCoverage - - - name: Add coverage to PR - id: jacoco - uses: madrapps/jacoco-report@v1.7.1 - with: - paths: | - ${{ github.workspace }}/**/build/reports/jacoco/jacocoCoreDebugCodeCoverage/jacocoCoreDebugCodeCoverage.xml - token: ${{ secrets.GITHUB_TOKEN }} - min-coverage-overall: 40 - min-coverage-changed-files: 60 - update-comment: true From 668f006531fcfb97830c56a61d867f7db098b2b0 Mon Sep 17 00:00:00 2001 From: Alicia Bendz Date: Mon, 28 Oct 2024 17:44:10 -0400 Subject: [PATCH 3/5] Add missing parameter for Jacoco report on PR --- .github/workflows/pr_code_coverage.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/pr_code_coverage.yaml b/.github/workflows/pr_code_coverage.yaml index 0d6a0944..e03b8614 100644 --- a/.github/workflows/pr_code_coverage.yaml +++ b/.github/workflows/pr_code_coverage.yaml @@ -33,4 +33,5 @@ jobs: token: ${{ secrets.GITHUB_TOKEN }} min-coverage-overall: 40 min-coverage-changed-files: 60 + title: Code Coverage update-comment: true From 0b527b2a039513b9d518e7fecd552d7920a6cfe1 Mon Sep 17 00:00:00 2001 From: Alicia Bendz Date: Mon, 28 Oct 2024 17:49:58 -0400 Subject: [PATCH 4/5] Add permissions to github job --- .github/workflows/pr_code_coverage.yaml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/pr_code_coverage.yaml b/.github/workflows/pr_code_coverage.yaml index e03b8614..2c53f2f0 100644 --- a/.github/workflows/pr_code_coverage.yaml +++ b/.github/workflows/pr_code_coverage.yaml @@ -9,6 +9,8 @@ jobs: code-coverage: runs-on: ubuntu-latest name: Generate code coverage report + permissions: + pull-requests: write steps: - name: Checkout repository From e73eac16dfc351fee99605aa0d555615fe6fecad Mon Sep 17 00:00:00 2001 From: Alicia Bendz Date: Mon, 28 Oct 2024 18:12:03 -0400 Subject: [PATCH 5/5] Add debug mode to code coverage PR workflow to see failure logs --- .github/workflows/pr_code_coverage.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/pr_code_coverage.yaml b/.github/workflows/pr_code_coverage.yaml index 2c53f2f0..3d69d723 100644 --- a/.github/workflows/pr_code_coverage.yaml +++ b/.github/workflows/pr_code_coverage.yaml @@ -37,3 +37,4 @@ jobs: min-coverage-changed-files: 60 title: Code Coverage update-comment: true + debug-mode: true