Skip to content

Commit

Permalink
Merge pull request #166 from kaleido-io/coverage
Browse files Browse the repository at this point in the history
Add task type for checking coverage
  • Loading branch information
dwertent authored Sep 16, 2024
2 parents c8d7a03 + a214cf7 commit 44fff74
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 31 deletions.
43 changes: 43 additions & 0 deletions buildSrc/src/main/groovy/GoCheckCoverage.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import org.gradle.api.DefaultTask
import org.gradle.api.GradleException
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.InputFile
import org.gradle.api.tasks.TaskAction

class GoCheckCoverage extends DefaultTask {

@InputFile
File coverageFile

@Input
BigDecimal target

@Input
BigDecimal maxGap

void coverageFile(Object f) {
coverageFile = project.file(f)
}

@TaskAction
void exec() {
String coverageOutput = ("go tool cover -func=${coverageFile}").execute().text
String totalCoverage = coverageOutput.readLines().find { line ->
line.contains('total:')
}?.split()?.last()?.replace('%', '')
println "Coverage is ${totalCoverage}%"
if (totalCoverage && totalCoverage.toFloat() < target) {
throw new GradleException(
"ERROR: Coverage is below ${target}% (current coverage: ${totalCoverage}%)"
)
} else if (totalCoverage.toFloat() - target > maxGap) {
throw new GradleException(
"ERROR: The target coverage ${target}% is below the current coverage: ${totalCoverage}% by " +
"more than ${maxGap}%, please update the target value in ./core/go/build.gradle file"
)
} else {
println "Coverage is above ${target}%, current coverage: ${totalCoverage}%"
}
}

}
20 changes: 4 additions & 16 deletions core/go/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,6 @@ task buildCoverageTxt(type: Exec, dependsOn: [
args 'tool', 'covdata', 'textfmt'
args '-i', "${projectDir}/coverage"
args '-o', "${projectDir}/coverage/coverage_unfiltered.txt"

}

task aggregateCoverage(type: Copy, dependsOn: buildCoverageTxt) {
Expand All @@ -427,21 +426,10 @@ task aggregateCoverage(type: Copy, dependsOn: buildCoverageTxt) {
filter(LineContains, negate: true, contains: getProject().ext.coverageExcludedPackages, matchAny: true)
}

task checkCoverage(dependsOn: [aggregateCoverage]) {
inputs.files('coverage/coverage.txt')

doLast {
def coverageOutput = ("go tool cover -func=${projectDir}/coverage/coverage.txt").execute().text
def totalCoverage = coverageOutput.readLines().find { it.contains('total:') }?.split()?.last()?.replace('%', '')
println "Coverage is ${totalCoverage}%"
if (totalCoverage && totalCoverage.toFloat() < targetCoverage) {
throw new GradleException("ERROR: Coverage is below ${targetCoverage}% (current coverage: ${totalCoverage}%)")
} else if ( totalCoverage.toFloat() - targetCoverage > maxCoverageBarGap) {
throw new GradleException("ERROR: The target coverage ${targetCoverage}% is below the current coverage: ${totalCoverage}% for more than ${maxCoverageBarGap}%, please update the targetCoverage value in ./core/go/build.gradle file")
} else {
println "Coverage is above ${targetCoverage}%, current coverage: ${totalCoverage}%"
}
}
task checkCoverage(type: GoCheckCoverage, dependsOn: [aggregateCoverage]) {
coverageFile('coverage/coverage.txt')
target = targetCoverage
maxGap = maxCoverageBarGap
}

task test {
Expand Down
19 changes: 4 additions & 15 deletions toolkit/go/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -177,21 +177,10 @@ task aggregateCoverage(type: Copy, dependsOn: tasks.buildCoverageTxt) {
filter(LineContains, negate: true, contains: getProject().ext.coverageExcludedPackages, matchAny: true)
}

task checkCoverage(dependsOn: [tasks.aggregateCoverage]) {
inputs.files('coverage/coverage.txt')

doLast {
def coverageOutput = ("go tool cover -func=${projectDir}/coverage/coverage.txt").execute().text
def totalCoverage = coverageOutput.readLines().find { it.contains('total:') }?.split()?.last()?.replace('%', '')
println "Coverage is ${totalCoverage}%"
if (totalCoverage && totalCoverage.toFloat() < targetCoverage) {
throw new GradleException("ERROR: Coverage is below ${targetCoverage}% (current coverage: ${totalCoverage}%)")
} else if ( totalCoverage.toFloat() - targetCoverage > maxCoverageBarGap) {
throw new GradleException("ERROR: The target coverage ${targetCoverage}% is below the current coverage: ${totalCoverage}% for more than ${maxCoverageBarGap}%, please update the targetCoverage value in ./core/go/build.gradle file")
} else {
println "Coverage is above ${targetCoverage}%, current coverage: ${totalCoverage}%"
}
}
task checkCoverage(type: GoCheckCoverage, dependsOn: [tasks.aggregateCoverage]) {
coverageFile('coverage/coverage.txt')
target = targetCoverage
maxGap = maxCoverageBarGap
}

task buildStarterLibrary(type:Exec, dependsOn:[tasks.goGet]) {
Expand Down

0 comments on commit 44fff74

Please sign in to comment.