From e31ec0fbb383e50e4925d519faf3f4e70c895e0c Mon Sep 17 00:00:00 2001 From: Junha Date: Sun, 9 Jul 2023 03:29:07 +0000 Subject: [PATCH] chore: add test coverage add into gradle, git actions (with Jacoco, CodeCov) --- .github/workflows/gradle-build-test-lint.yml | 38 ++++++++++--- .gitignore | 2 + build.gradle.kts | 59 ++++++++++++++++++++ 3 files changed, 91 insertions(+), 8 deletions(-) diff --git a/.github/workflows/gradle-build-test-lint.yml b/.github/workflows/gradle-build-test-lint.yml index 2b78db9c..2687e9ec 100644 --- a/.github/workflows/gradle-build-test-lint.yml +++ b/.github/workflows/gradle-build-test-lint.yml @@ -1,9 +1,6 @@ name: Gradlew build-test-ktlint -on: - pull_request: - push: - branches: [main] +on: [push] jobs: build: @@ -16,16 +13,41 @@ jobs: with: java-version: '17' distribution: 'temurin' - - name: Validate Gradle wrapper uses: gradle/wrapper-validation-action@ccb4328a959376b642e027874838f60f8e596de3 - - name: Build And Test with Gradle + - name: Build uses: gradle/gradle-build-action@749f47bda3e44aa060e82d7b3ef7e40d953bd629 with: - arguments: build -x ktlintMainSourceSetCheck -x ktlintTestSourceSetCheck -x ktlintKotlinScriptCheck + arguments: bootJar - name: Ktlint check uses: gradle/gradle-build-action@749f47bda3e44aa060e82d7b3ef7e40d953bd629 + if: always() + with: + arguments: ktlintCheck + + - name: Test + uses: gradle/gradle-build-action@749f47bda3e44aa060e82d7b3ef7e40d953bd629 + if: always() + with: + arguments: test -x jacocoTestReport + + - name: Jacoco Coverage Report + uses: gradle/gradle-build-action@749f47bda3e44aa060e82d7b3ef7e40d953bd629 + if: always() + with: + arguments: jacocoTestReport -x test -x jacocoTestCoverageVerification + + - name: Upload coverage to Codecov + uses: codecov/codecov-action@v3 + if: always() + with: + token: ${{ secrets.CODECOV_TOKEN }} + file: ./build/reports/jacoco/test/jacocoTestReport.xml + + - name: Jacoco Coverage Verification + uses: gradle/gradle-build-action@749f47bda3e44aa060e82d7b3ef7e40d953bd629 + if: always() with: - arguments: ktlintCheck \ No newline at end of file + arguments: jacocoTestCoverageVerification \ No newline at end of file diff --git a/.gitignore b/.gitignore index c2065bc2..b3161238 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ +jacocoRepost/ + HELP.md .gradle build/ diff --git a/build.gradle.kts b/build.gradle.kts index 5df30708..6461064e 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -9,6 +9,7 @@ plugins { kotlin("plugin.allopen") version "1.6.21" kotlin("plugin.noarg") version "1.6.21" id("org.jlleitschuh.gradle.ktlint") version "11.5.0" + id("jacoco") } group = "com.example" @@ -42,6 +43,7 @@ tasks.withType { tasks.withType { useJUnitPlatform() + finalizedBy(tasks.jacocoTestReport) } allOpen { @@ -64,3 +66,60 @@ val installLocalGitHook = tasks.register("installLocalGitHook") { tasks.build { dependsOn(installLocalGitHook) } + +subprojects { + apply(plugin = "jacoco") + + jacoco { + toolVersion = "0.8.10" + } +} + +tasks.jacocoTestReport { + + dependsOn(tasks.test) + reports { + html.isEnabled = true + csv.isEnabled = false + xml.isEnabled = true + html.destination = File("${rootProject.rootDir}/jacocoRepost") + } + + finalizedBy(tasks.jacocoTestCoverageVerification) +} + +tasks.jacocoTestCoverageVerification { + + var Qdomains = mutableListOf() + + for (qPattern in 'A'..'Z') { + Qdomains.add("*.Q$qPattern*") + } + + violationRules { + rule { + element = "CLASS" + + limit { + counter = "BRANCH" + value = "COVEREDRATIO" + // minimum = "0.8".toBigDecimal() + } + + limit { + counter = "LINE" + value = "COVEREDRATIO" + // maximum = "0.8".toBigDecimal() + } + + // 빈 줄을 제외한 코드의 라인수를 최대 200라인으로 제한합니다. + limit { + counter = "LINE" + value = "TOTALCOUNT" + maximum = "200".toBigDecimal() + } + + excludes = Qdomains + } + } +}