diff --git a/.github/actions/project-setup/action.yml b/.github/actions/project-setup/action.yml new file mode 100644 index 000000000..18bceda33 --- /dev/null +++ b/.github/actions/project-setup/action.yml @@ -0,0 +1,30 @@ +runs: + using: composite + steps: + - name: Checkout (full history) + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Setup Java + uses: actions/setup-java@v4 + with: + java-version: '17' + distribution: 'temurin' + + - name: Setup Android SDK + uses: android-actions/setup-android@v3 + with: + cmdline-tools-version: '10406996' + log-accepted-android-sdk-licenses: false + + - name: Cache Gradle + uses: actions/cache@v4 + with: + path: | + ~/.gradle/caches + ~/.gradle/wrapper + key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties', '**/gradle/libs.versions.toml') }} + restore-keys: | + ${{ runner.os }}-gradle- + diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f3409c2a1..1ba3736d0 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -4,26 +4,22 @@ on: pull_request: branches: "**" +env: + DIFF_COVERAGE_THRESHOLD: '80' + jobs: build: runs-on: ubuntu-24.04 steps: - - uses: actions/checkout@v4 - - name: "[Setup] Java" - uses: actions/setup-java@v4 - with: - java-version: '17' - distribution: 'temurin' - - name: "[Setup] Android" - uses: android-actions/setup-android@v3 - with: - cmdline-tools-version: 10406996 - log-accepted-android-sdk-licenses: false - - name: "[Test] Linting" + - name: "[Checkout] Repo" + uses: actions/checkout@v4 + - name: "[Setup] Project" + uses: ./.github/actions/project-setup + - name: "[Code Formatting] Spotless" working-directory: OneSignalSDK run: | ./gradlew spotlessCheck --console=plain - - name: "[Test] Detekt" + - name: "[Static Code Analysis] Detekt" working-directory: OneSignalSDK run: | ./gradlew detekt --console=plain @@ -31,6 +27,34 @@ jobs: working-directory: OneSignalSDK run: | ./gradlew testReleaseUnitTest --console=plain --continue + - name: "[Coverage] Generate JaCoCo merged XML" + working-directory: OneSignalSDK + run: | + ./gradlew jacocoTestReportAll jacocoMergedReport --console=plain --continue + - name: "[Setup] Python" + uses: actions/setup-python@v5 + with: + python-version: '3.x' + - name: "[Diff Coverage] Install diff-cover" + run: | + python -m pip install --upgrade pip diff-cover + - name: "[Diff Coverage] Check and HTML report" + working-directory: OneSignalSDK + run: | + REPORT=build/reports/jacoco/merged/jacocoMergedReport.xml + test -f "$REPORT" || { echo "Merged JaCoCo report not found at $REPORT" >&2; exit 1; } + python -m diff_cover.diff_cover_tool "$REPORT" \ + --compare-branch=origin/main \ + --fail-under=$DIFF_COVERAGE_THRESHOLD + python -m diff_cover.diff_cover_tool "$REPORT" \ + --compare-branch=origin/main \ + --html-report diff_coverage.html || true + - name: Upload diff coverage HTML + if: always() + uses: actions/upload-artifact@v4 + with: + name: diff-coverage-report + path: OneSignalSDK/diff_coverage.html - name: Unit tests results if: failure() uses: actions/upload-artifact@v4 diff --git a/OneSignalSDK/build.gradle b/OneSignalSDK/build.gradle index ec1adeee7..33ce53b6c 100644 --- a/OneSignalSDK/build.gradle +++ b/OneSignalSDK/build.gradle @@ -107,3 +107,6 @@ gradle.projectsEvaluated { description = 'Creates/updates Detekt baselines for all modules.' } } + +// Apply JaCoCo configuration from separate file +apply from: 'jacoco.gradle' diff --git a/OneSignalSDK/jacoco.gradle b/OneSignalSDK/jacoco.gradle new file mode 100644 index 000000000..c7b551de2 --- /dev/null +++ b/OneSignalSDK/jacoco.gradle @@ -0,0 +1,301 @@ +// JaCoCo Test Coverage Configuration +// This file contains all JaCoCo-related configuration for test coverage reporting + +subprojects { + // Apply JaCoCo to all modules with tests + plugins.withId("com.android.library") { + apply plugin: 'jacoco' + + jacoco { + toolVersion = "0.8.11" + } + + android { + buildTypes { + debug { + testCoverageEnabled = true + } + } + } + + def coverageExcludes = [ + // Android generated + '**/R.class', + '**/R$*.class', + '**/BuildConfig.*', + '**/Manifest*.*', + 'android/**/*.*', + + // Test files (just in case) + '**/*Test*.*', + '**/*Mock*.*', + '**/test/**/*.*', + '**/androidTest/**/*.*', + + // View binding & injection + '**/*$ViewInjector*.*', + '**/*$ViewBinder*.*', + '**/*Binding.*', + + // Kotlin lambdas & synthetic + '**/Lambda$*.class', + '**/Lambda.class', + '**/*Lambda.class', + '**/*Lambda*.class', + '**/*$inlined$*.class', + + // Dagger/Hilt generated + '**/*_MembersInjector.class', + '**/Dagger*.*', + '**/*_Factory.*', + '**/*_Provide*Factory.*', + '**/*Module.*', + '**/*Module_*.*', + '**/*Component.*', + '**/*Component$*.*', + '**/*Subcomponent*.*', + '**/Hilt_*.*', + '**/*_HiltModules*.*', + + // Data classes & enums (often no logic to test) + '**/*$Companion.class', + + // Sealed classes + '**/*$WhenMappings.class' + ] + + tasks.register('jacocoTestReport', JacocoReport) { + dependsOn 'testDebugUnitTest' + + reports { + xml.required = true + html.required = true + csv.required = false + } + + def javaClasses = fileTree(dir: "$buildDir/intermediates/javac/debug", excludes: coverageExcludes) + def kotlinClasses = fileTree(dir: "$buildDir/tmp/kotlin-classes/debug", excludes: coverageExcludes) + + classDirectories.from = files([javaClasses, kotlinClasses]) + + sourceDirectories.from = files([ + "$projectDir/src/main/java", + "$projectDir/src/main/kotlin" + ]) + + executionData.from = fileTree(dir: buildDir, includes: [ + 'outputs/unit_test_code_coverage/debugUnitTest/testDebugUnitTest.exec', + 'jacoco/testDebugUnitTest.exec' + ]) + } + + tasks.register('jacocoTestCoverageVerification', JacocoCoverageVerification) { + dependsOn 'jacocoTestReport' + + def javaClasses = fileTree(dir: "$buildDir/intermediates/javac/debug", excludes: coverageExcludes) + def kotlinClasses = fileTree(dir: "$buildDir/tmp/kotlin-classes/debug", excludes: coverageExcludes) + + classDirectories.from = files([javaClasses, kotlinClasses]) + + sourceDirectories.from = files([ + "$projectDir/src/main/java", + "$projectDir/src/main/kotlin" + ]) + + executionData.from = fileTree(dir: buildDir, includes: [ + 'outputs/unit_test_code_coverage/debugUnitTest/testDebugUnitTest.exec', + 'jacoco/testDebugUnitTest.exec' + ]) + + violationRules { + rule { + // Start with baseline - we'll gradually increase these thresholds + enabled = false // Disabled for now - just generate reports, don't enforce + element = 'CLASS' + + limit { + counter = 'LINE' + value = 'COVEREDRATIO' + minimum = 0.00 // Baseline: 0% - increase this as coverage improves + } + + limit { + counter = 'BRANCH' + value = 'COVEREDRATIO' + minimum = 0.00 // Baseline: 0% - increase this as coverage improves + } + } + } + } + } +} + +// Task to run coverage on all modules +tasks.register('jacocoTestReportAll') { + group = 'verification' + description = 'Generate JaCoCo test coverage reports for all modules' + + subprojects.each { subproject -> + subproject.plugins.withId('com.android.library') { + dependsOn "${subproject.path}:jacocoTestReport" + } + } +} + +// Task to verify coverage on all modules +tasks.register('jacocoTestCoverageVerificationAll') { + group = 'verification' + description = 'Verify JaCoCo test coverage is above threshold for all modules' + + subprojects.each { subproject -> + subproject.plugins.withId('com.android.library') { + dependsOn "${subproject.path}:jacocoTestCoverageVerification" + } + } +} + +// Merged XML report across all Android library modules for CI tools (e.g., diff-cover) +tasks.register('jacocoMergedReport', JacocoReport) { + group = 'verification' + description = 'Generate a single merged JaCoCo XML report across all modules' + + dependsOn 'jacocoTestReportAll' + + reports { + xml.required = true + html.required = false + csv.required = false + xml.outputLocation = file("${buildDir}/reports/jacoco/merged/jacocoMergedReport.xml") + } + + def allClassDirs = [] + def allSourceDirs = [] + def allExecData = [] + + subprojects.each { subproject -> + subproject.plugins.withId('com.android.library') { + // Class and source directories + def javaClasses = subproject.fileTree(dir: "${subproject.buildDir}/intermediates/javac/debug", excludes: [ + '**/R.class','**/R$*.class','**/BuildConfig.*','**/Manifest*.*','android/**/*.*', + '**/*Test*.*','**/*Mock*.*','**/test/**/*.*','**/androidTest/**/*.*', + '**/*$ViewInjector*.*','**/*$ViewBinder*.*','**/*Binding.*', + '**/Lambda$*.class','**/Lambda.class','**/*Lambda.class','**/*Lambda*.class','**/*$inlined$*.class', + '**/*_MembersInjector.class','**/Dagger*.*','**/*_Factory.*','**/*_Provide*Factory.*','**/*Module.*','**/*Module_*.*','**/*Component.*','**/*Component$*.*','**/*Subcomponent*.*','**/Hilt_*.*','**/*_HiltModules*.*', + '**/*$Companion.class','**/*$WhenMappings.class' + ]) + def kotlinClasses = subproject.fileTree(dir: "${subproject.buildDir}/tmp/kotlin-classes/debug", excludes: javaClasses.excludes) + allClassDirs << javaClasses + allClassDirs << kotlinClasses + + allSourceDirs << subproject.file("${subproject.projectDir}/src/main/java") + allSourceDirs << subproject.file("${subproject.projectDir}/src/main/kotlin") + + // Exec data from unit tests + allExecData << subproject.fileTree(dir: subproject.buildDir, includes: [ + 'outputs/unit_test_code_coverage/debugUnitTest/testDebugUnitTest.exec', + 'jacoco/testDebugUnitTest.exec' + ]) + } + } + + classDirectories.from = files(allClassDirs) + sourceDirectories.from = files(allSourceDirs) + executionData.from = files(allExecData) +} + +// Task to print coverage summary for CI/CD +tasks.register('jacocoTestReportSummary') { + group = 'verification' + description = 'Print JaCoCo test coverage summary for all modules (useful for CI/CD)' + + dependsOn 'jacocoTestReportAll' + + doLast { + def totalInstructions = 0 + def coveredInstructions = 0 + def totalBranches = 0 + def coveredBranches = 0 + def totalLines = 0 + def coveredLines = 0 + + println("\n" + "=".multiply(80)) + println("JaCoCo Test Coverage Summary") + println("=".multiply(80)) + + subprojects.each { subproject -> + subproject.plugins.withId('com.android.library') { + def reportFile = file("${subproject.buildDir}/reports/jacoco/jacocoTestReport/jacocoTestReport.xml") + + if (reportFile.exists()) { + def parser = new XmlSlurper() + parser.setFeature("http://apache.org/xml/features/disallow-doctype-decl", false) + parser.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false) + def report = parser.parse(reportFile) + def moduleName = subproject.name + + def instructions = report.counter.find { it.@type == 'INSTRUCTION' } + def branches = report.counter.find { it.@type == 'BRANCH' } + def lines = report.counter.find { it.@type == 'LINE' } + + if (instructions) { + def missed = instructions.@missed.toInteger() + def covered = instructions.@covered.toInteger() + def total = missed + covered + def percentage = total > 0 ? (covered * 100.0 / total) : 0 + + totalInstructions += total + coveredInstructions += covered + + println("\nModule: ${moduleName}") + println(" Instructions: ${covered}/${total} (${String.format('%.2f', percentage)}%)") + } + + if (branches) { + def missed = branches.@missed.toInteger() + def covered = branches.@covered.toInteger() + def total = missed + covered + def percentage = total > 0 ? (covered * 100.0 / total) : 0 + + totalBranches += total + coveredBranches += covered + + println(" Branches: ${covered}/${total} (${String.format('%.2f', percentage)}%)") + } + + if (lines) { + def missed = lines.@missed.toInteger() + def covered = lines.@covered.toInteger() + def total = missed + covered + def percentage = total > 0 ? (covered * 100.0 / total) : 0 + + totalLines += total + coveredLines += covered + + println(" Lines: ${covered}/${total} (${String.format('%.2f', percentage)}%)") + } + + println(" Report: file://${reportFile.parentFile}/html/index.html") + } + } + } + + if (totalInstructions > 0) { + def overallInstructionPercent = (coveredInstructions * 100.0 / totalInstructions) + def overallBranchPercent = totalBranches > 0 ? (coveredBranches * 100.0 / totalBranches) : 0 + def overallLinePercent = totalLines > 0 ? (coveredLines * 100.0 / totalLines) : 0 + + println("\n" + "=".multiply(80)) + println("Overall Coverage") + println("=".multiply(80)) + println("Instructions: ${coveredInstructions}/${totalInstructions} (${String.format('%.2f', overallInstructionPercent)}%)") + println("Branches: ${coveredBranches}/${totalBranches} (${String.format('%.2f', overallBranchPercent)}%)") + println("Lines: ${coveredLines}/${totalLines} (${String.format('%.2f', overallLinePercent)}%)") + println("=".multiply(80) + "\n") + + // CI-friendly output + println("::set-output name=coverage::${String.format('%.2f', overallLinePercent)}") + println("COVERAGE_PERCENTAGE=${String.format('%.2f', overallLinePercent)}") + } + } +} +