diff --git a/.github/actions/sonar/action.yml b/.github/actions/sonar/action.yml index c75a7b316..4e7ac7de0 100644 --- a/.github/actions/sonar/action.yml +++ b/.github/actions/sonar/action.yml @@ -11,7 +11,10 @@ inputs: description: Sonar host URL required: true coverage-file-name: - description: Name of the generic coverage file + description: Name of the LCOV coverage file + required: true + coverage-file-name-android: + description: Name of the Kover coverage file required: true branch: description: Branch @@ -41,6 +44,7 @@ runs: echo "sonar.pullrequest.base=${{ inputs.base-branch }}" >> sonar-project.properties echo "sonar.scm.revision=${{ inputs.pull-request-sha }}" >> sonar-project.properties echo "sonar.javascript.lcov.reportPaths=${{ inputs.coverage-file-name }}" >> sonar-project.properties + echo "sonar.coverage.jacoco.xmlReportPaths=${{ inputs.coverage-file-name-android }}" >> sonar-project.properties - name: Run Sonar shell: bash run: | diff --git a/.github/workflows/unit-tests-and-code-quality.yml b/.github/workflows/unit-tests-and-code-quality.yml index 931b52385..c978aade9 100644 --- a/.github/workflows/unit-tests-and-code-quality.yml +++ b/.github/workflows/unit-tests-and-code-quality.yml @@ -23,6 +23,14 @@ jobs: name: coverage-file path: coverage + - name: Run Android unit tests and generate coverage + run: bundle exec fastlane android run_unit_tests_coverage + + - uses: actions/upload-artifact@master + with: + name: coverage-file-android + path: package/sdk/android/build/reports/kover + sonarcloud: needs: - run-unit-tests @@ -36,6 +44,10 @@ jobs: with: name: coverage-file path: coverage + - uses: actions/download-artifact@master + with: + name: coverage-file-android + path: kover - name: SonarCloud Scan uses: ./.github/actions/sonar with: @@ -47,3 +59,4 @@ jobs: base-branch: ${{ github.base_ref }} pull-request-sha: ${{ github.event.pull_request.head.sha }} coverage-file-name: ./coverage/lcov.info + coverage-file-name-android: ./kover/reportsRelease.xml diff --git a/fastlane/AndroidFastFile b/fastlane/AndroidFastFile index 940e69fd3..591905238 100644 --- a/fastlane/AndroidFastFile +++ b/fastlane/AndroidFastFile @@ -84,6 +84,9 @@ platform :android do save_browserstack_id(browserstack_id: ENV['BROWSERSTACK_APP_ID']) end + lane :run_unit_tests_coverage do + gradle(task: "clean koverXmlReportRelease", project_dir: 'packages/example/android/') + end desc 'Store the Browserstack ID into a file' private_lane :save_browserstack_id do |options| diff --git a/packages/sdk/android/build.gradle b/packages/sdk/android/build.gradle index 9df62c3ed..1f7b1ef54 100644 --- a/packages/sdk/android/build.gradle +++ b/packages/sdk/android/build.gradle @@ -1,7 +1,7 @@ buildscript { // Buildscript is evaluated before everything else so we can't use getExtOrDefault def kotlin_version = rootProject.ext.has('kotlinVersion') ? rootProject.ext.get('kotlinVersion') : project.properties['ReactNative_kotlinVersion'] - + def kover_version = '0.7.6' repositories { google() mavenCentral() @@ -13,12 +13,14 @@ buildscript { // noinspection DifferentKotlinGradleVersion classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" classpath "org.jetbrains.kotlin:kotlin-serialization:$kotlin_version" + classpath "org.jetbrains.kotlinx:kover-gradle-plugin:$kover_version" } } apply plugin: 'com.android.library' apply plugin: 'kotlin-android' apply plugin: 'kotlinx-serialization' +apply from: "./config/coverage/kover.gradle" def getExtOrDefault(name) { return rootProject.ext.has(name) ? rootProject.ext.get(name) : project.properties['ReactNative_' + name] @@ -37,6 +39,8 @@ android { minSdkVersion 21 targetSdkVersion getExtOrIntegerDefault('targetSdkVersion') + testInstrumentationRunnerArguments runnerBuilder: 'de.mannodermaus.junit5.AndroidJUnit5Builder' + testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } buildTypes { @@ -58,6 +62,13 @@ android { kotlinOptions { freeCompilerArgs += '-Xjvm-default=all' } + testOptions { + execution = 'ANDROIDX_TEST_ORCHESTRATOR' + + unitTests.all { + useJUnitPlatform() + } + } } repositories { @@ -139,4 +150,10 @@ dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.3" implementation 'io.primer:android:2.27.0' + + testImplementation 'io.mockk:mockk:1.13.10' + testImplementation 'org.junit.jupiter:junit-jupiter-api:5.10.2' + testImplementation 'org.junit.jupiter:junit-jupiter-api:5.10.2' + testImplementation 'org.junit.jupiter:junit-jupiter-params:5.10.2' + testImplementation 'de.mannodermaus.junit5:android-test-runner:1.3.0' } diff --git a/packages/sdk/android/src/test/java/com/primerioreactnative/utils/ErrorHelperTest.kt b/packages/sdk/android/src/test/java/com/primerioreactnative/utils/ErrorHelperTest.kt new file mode 100644 index 000000000..99259f7a1 --- /dev/null +++ b/packages/sdk/android/src/test/java/com/primerioreactnative/utils/ErrorHelperTest.kt @@ -0,0 +1,24 @@ +package com.primerioreactnative.utils + +import com.primerioreactnative.datamodels.ErrorTypeRN +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Test + +internal class ErrorHelperTest { + + // Create a mock ErrorTypeRN instance + private val errorType = ErrorTypeRN.InvalidVaultedPaymentMethodId + + @Test + fun `errorTo should create PrimerErrorRN with correct properties`() { + // Mock message + val errorMessage = "This is an error message" + + // Call the infix function + val primerError = errorType errorTo errorMessage + + // Verify the errorId and message are set correctly in the PrimerErrorRN object + assertEquals(errorType.errorId, primerError.errorId) + assertEquals(errorMessage, primerError.description) + } +}