diff --git a/.github/workflows/unit-and-instrumented-tests-action.yml b/.github/workflows/unit-and-instrumented-tests-action.yml index a1fafa6af..6907c2cf4 100644 --- a/.github/workflows/unit-and-instrumented-tests-action.yml +++ b/.github/workflows/unit-and-instrumented-tests-action.yml @@ -30,6 +30,7 @@ jobs: with: name: unit-test-api-level-$API_MIN path: ./Branch-SDK/build/ + instrumented-test-api-level-min: name: instrumented-test-api-level-$API_MIN runs-on: macos-latest @@ -66,6 +67,44 @@ jobs: with: name: instrumented-test-api-level-$API_MIN path: ./Branch-SDK/build/ + + jacoco-test-coverage-api-level-min: + name: jacoco-test-coverage-api-level-$API_MIN + if: ${{ always() && contains(join(needs.*.result, ','), 'success') }} + needs: [unit-test-api-level-min, instrumented-test-api-level-min] + runs-on: macos-latest + steps: + - name: Checkout + uses: actions/checkout@v3 + # create an emulator with google apis, runs on java 8 + - name: Create Android emulator + run: | + echo "y" | $ANDROID_HOME/tools/bin/sdkmanager --licenses + echo "y" | $ANDROID_HOME/tools/bin/sdkmanager --install "system-images;android-"$API_MIN";google_apis;x86_64" + echo "no" | $ANDROID_HOME/tools/bin/avdmanager --verbose create avd --force --name test --package "system-images;android-"$API_MIN";google_apis;x86_64" + # boots and waits for the emulator to be ready + - name: Launch Emulator + run: | + echo "Starting emulator and waiting for boot to complete." + nohup $ANDROID_HOME/tools/emulator -avd test -no-audio -no-boot-anim -camera-back none -camera-front none -qemu -m 2048 2>&1 & + $ANDROID_HOME/platform-tools/adb wait-for-device shell 'while [[ -z $(getprop sys.boot_completed) ]]; do echo "waiting..."; sleep 1; done; input keyevent 82' + echo "Emulator has finished booting" + # repo's gradle is configured to run on java 17 + - name: Setup java 17 for gradle + uses: actions/setup-java@v3 + with: + distribution: ${{ env.JAVA_DISTRIBUTION }} + java-version: ${{ env.JAVA_VERSION }} + - name: Run Coverage + run: | + ./gradlew :Branch-SDK:jacocoTestReport --info + - name: Upload Coverage Test Report + if: success() + uses: codecov/codecov-action@v3 + with: + file: ./Branch-SDK/build/reports/jacoco/jacocoTestReport/jacocoTestReport.xml + fail_ci_if_error: true + unit-test-api-level-current: name: unit-test-api-level-$API_CURRENT runs-on: macos-latest diff --git a/Branch-SDK/build.gradle.kts b/Branch-SDK/build.gradle.kts index d17951c32..a2550f8ac 100644 --- a/Branch-SDK/build.gradle.kts +++ b/Branch-SDK/build.gradle.kts @@ -6,8 +6,12 @@ plugins { `maven-publish` signing id("org.gradle.test-retry") version "1.5.3" + id("jacoco") } val coroutinesVersion = "1.6.4" +jacoco { + toolVersion = "0.8.10" +} dependencies { implementation(fileTree(mapOf("dir" to "libs", "include" to "*.jar"))) @@ -92,6 +96,7 @@ android { debug { enableUnitTestCoverage = true + enableAndroidTestCoverage = true buildConfigField("long", "VERSION_CODE", VERSION_CODE) buildConfigField("String", "VERSION_NAME", VERSION_NAME.wrapInQuotes()) } @@ -274,5 +279,30 @@ tasks { retry { maxRetries.set(3) } + configure { + isIncludeNoLocationClasses = true + excludes = listOf("jdk.internal.*") + } } } + +tasks.create("jacocoTestReport") { + group = "Reporting" + description = "Generate Jacoco code coverage reports after running tests." + dependsOn("testDebugUnitTest","createDebugCoverageReport") + reports { + xml.required.set(true) + html.required.set(true) + } + sourceDirectories.setFrom("${project.projectDir}/src/main/java") + classDirectories.setFrom("${project.buildDir}/intermediates/javac/debug/classes") + executionData.setFrom( + fileTree(project.buildDir) { + include( + "jacoco/testDebugUnitTest.exec", + "outputs/unit_test_code_coverage/debugUnitTest/testDebugUnitTest.exec", + "outputs/code_coverage/debugAndroidTest/connected/**/*.ec", + ) + } + ) +}