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

feature: Add Code Coverage Report #90

Merged
merged 4 commits into from
Mar 1, 2024
Merged
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
58 changes: 58 additions & 0 deletions .github/actions/run-tests/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
name: Run Tests
description: Runs Unit and Integration Test. Tests will be executed on a Databroker instance.

inputs:
upload-test-reports:
description: "Uploads the resulting test reports if enabled"
required: false
default: 'false'
upload-code-coverage-reports:
description: "Executes Code Coverage Generation and uploads the resulting reports if enabled"
required: false
default: 'false'
databroker-version:
description: "Databroker Version"
required: false
default: 'master'

runs:
using: "composite"
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: "Run Docker Container of Databroker in detached mode"
run: docker run --pull=always --rm --publish 55556:55556/tcp --detach --name databroker ghcr.io/eclipse/kuksa.val/databroker:${{ inputs.databroker-version }} --port 55556 --insecure
shell: bash

- name: Run 'test' with Gradle Wrapper
run: ./gradlew test -Dkotest.tags="!CustomDatabroker"
shell: bash

- name: Upload Test Reports
if: ${{ inputs.upload-test-reports == 'true' }}
uses: actions/upload-artifact@v3
with:
name: test-reports
path: ${{ github.workspace }}/**/reports/tests/*/
if-no-files-found: error
retention-days: 14

- name: Create Code Coverage Reports
if: ${{ inputs.upload-code-coverage-reports == 'true' }}
run: ./gradlew jacocoRootReport
shell: bash

- name: Upload Code Coverage Report
if: ${{ inputs.upload-code-coverage-reports == 'true' }}
uses: actions/upload-artifact@v3
with:
name: code-coverage
path: ${{ github.workspace }}/build/reports/jacoco/jacocoRootReport/html/*
if-no-files-found: error
retention-days: 14

- name: "Stop Docker Container of Databroker"
if: always()
run: docker stop databroker
shell: bash
6 changes: 6 additions & 0 deletions .github/workflows/build-main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ jobs:
if-no-files-found: error
retention-days: 14

- name: Execute Tests
uses: ./.github/actions/run-tests
with:
upload-test-reports: true
upload-code-coverage-reports: true

- name: Archive changelog
uses: actions/upload-artifact@v3
with:
Expand Down
21 changes: 5 additions & 16 deletions .github/workflows/build-pull-request.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ jobs:
- name: Checkout code
uses: actions/checkout@v4

- name: "Run Docker Container of Databroker in detached mode"
run: docker run --pull=always --rm --publish 55556:55556/tcp --detach --name databroker ghcr.io/eclipse/kuksa.val/databroker:master --port 55556 --insecure

- name: Setup Project
uses: ./.github/actions/setup-project

Expand All @@ -23,24 +20,16 @@ jobs:
run: ./gradlew ktlintCheck detekt

- name: Upload Detekt Reports
if: always()
uses: actions/upload-artifact@v3
with:
name: detekt-reports
path: ${{ github.workspace }}/build/reports/detekt
if-no-files-found: error
retention-days: 14

- name: Run 'test' with Gradle Wrapper
run: ./gradlew test -Dkotest.tags="!CustomDatabroker"

- name: Upload Test Reports
uses: actions/upload-artifact@v3
- name: Execute Tests
uses: ./.github/actions/run-tests
with:
name: test-reports
path: ${{ github.workspace }}/**/reports/tests/*/
if-no-files-found: error
retention-days: 14

- name: "Stop Docker Container of Databroker"
if: always()
run: docker stop databroker
upload-test-reports: true
upload-code-coverage-reports: true
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ class DataBrokerConnectorFactory {
createInsecureManagedChannel(connectionInfo)
}

val jsonWebToken = loadJsonWebToken(context, connectionInfo)
var jsonWebToken: JsonWebToken? = null
if (connectionInfo.isAuthenticationEnabled) {
jsonWebToken = loadJsonWebToken(context, connectionInfo.jwtUriPath)
}

return DataBrokerConnector(managedChannel, jsonWebToken).apply {
timeoutConfig = this@DataBrokerConnectorFactory.timeoutConfig
Expand Down Expand Up @@ -86,10 +89,8 @@ class DataBrokerConnectorFactory {
}

@Throws(IOException::class)
private fun loadJsonWebToken(context: Context, connectionInfo: ConnectionInfo): JsonWebToken? {
val isAuthenticationDisabled = !connectionInfo.isAuthenticationEnabled
val jwtUriPath = connectionInfo.jwtUriPath
if (isAuthenticationDisabled || jwtUriPath.isNullOrEmpty()) {
private fun loadJsonWebToken(context: Context, jwtUriPath: String?): JsonWebToken? {
if (jwtUriPath.isNullOrEmpty()) {
return null
}

Expand Down
81 changes: 81 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
*
*/

import com.android.build.api.dsl.LibraryExtension
import com.android.build.gradle.internal.dsl.BaseAppModuleExtension
import org.eclipse.kuksa.version.VERSION_FILE_DEFAULT_NAME
import org.eclipse.kuksa.version.VERSION_FILE_DEFAULT_PATH_KEY
import java.nio.file.FileVisitResult
Expand All @@ -38,6 +40,7 @@ plugins {
detekt
version
kotlin("jvm")
jacoco
}

subprojects {
Expand Down Expand Up @@ -102,3 +105,81 @@ tasks.register("mergeDashFiles") {
}
}
}

// region jacoco coverage report

subprojects {
apply {
plugin("jacoco")
}

if (plugins.hasPlugin("com.android.library")) {
configure<LibraryExtension> {
@Suppress("UnstableApiUsage")
testOptions {
buildTypes {
named("debug") {
enableUnitTestCoverage = true
enableAndroidTestCoverage = true
}
}
}
}
}

if (plugins.hasPlugin("com.android.application")) {
configure<BaseAppModuleExtension> {
@Suppress("UnstableApiUsage")
testOptions {
buildTypes {
named("debug") {
enableUnitTestCoverage = true
enableAndroidTestCoverage = true
}
}
}
}
}
}

tasks.create("jacocoRootReport", JacocoReport::class.java) {
group = "report"

reports {
html.required.set(true)
xml.required.set(false)
csv.required.set(false)
}

val excludes = listOf(
"**/buildSrc/**",
"**/app/**",
"**/samples/**",
"**/vssprocessor/plugin/**", // code coverage not supported for Gradle Plugins / Gradle TestKit tests
"**/build/**/org/eclipse/kuksa/vss/**", // generated code
"**/test*/**/*.*",
)

val sourcesKotlin = subprojects.map { it.layout.projectDirectory.dir("src/main/kotlin") }
val sourcesJava = subprojects.map { it.layout.projectDirectory.dir("src/main/java") }
Chrylo marked this conversation as resolved.
Show resolved Hide resolved

val classes = fileTree(rootDir) {
include(
"**/build/classes/kotlin/**/*.class", // kotlin-jvm modules
"**/build/tmp/kotlin-classes/debug/**/*.class", // android modules (application, libraries)
)
exclude(excludes)
}.toSet()

val executionPaths = fileTree(rootDir) {
include("**/*.exec", "**/*.ec")
exclude(excludes)
}.toSet()

additionalSourceDirs.setFrom(sourcesKotlin, sourcesJava)
sourceDirectories.setFrom(sourcesKotlin, sourcesJava)
classDirectories.setFrom(classes)
executionData.setFrom(executionPaths)
}

// endregion jacoco
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ class DataBrokerConnectionTest : BehaviorSpec({

// this test closes the connection, the connection can't be used afterward anymore
`when`("A DisconnectListener is registered successfully") {
val disconnectListener = mockk<DisconnectListener>()
val disconnectListener = mockk<DisconnectListener>(relaxed = true)
val disconnectListeners = dataBrokerConnection.disconnectListeners
disconnectListeners.register(disconnectListener)

Expand Down
Loading