Skip to content

Commit

Permalink
Fixed adding data to an already existing binary report file
Browse files Browse the repository at this point in the history
If the build cache is not used and the build directory is not cleared before the test run, then the coverage data from previous test runs are merged with the data from the recent run.
In this case, the report may include coverage from those tests that were removed in the latest version of the code.

To solve this, it is necessary to delete the binary report file before each run of the test task.

Fixes #489
  • Loading branch information
shanshin committed Oct 13, 2023
1 parent ca040f1 commit d677079
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
package kotlinx.kover.gradle.plugin.test.functional.cases

import kotlinx.kover.gradle.plugin.commons.CoverageToolVendor
import kotlinx.kover.gradle.plugin.test.functional.framework.configurator.*
import kotlinx.kover.gradle.plugin.test.functional.framework.starter.*

Expand All @@ -29,55 +30,80 @@ internal class ReportsCachingTests {
addProjectWithKover {
sourcesFrom("simple")
}
reportAndCheck("SUCCESS", true)
reportAndCheck("SUCCESS", cached = true)

run("clean", "--build-cache") {
checkDefaultBinReport(false)
checkDefaultReports(false)
}
reportAndCheck("FROM-CACHE", true)
reportAndCheck("FROM-CACHE", cached = true)
}

@SlicedGeneratedTest(allTools = true)
fun BuildConfigurator.testOuOfDateOnSources() {
@GeneratedTest(tool = CoverageToolVendor.KOVER)
fun BuildConfigurator.testKoverOutOfDateOnSources() {
useLocalCache()

addProjectWithKover {
sourcesFrom("simple")
}
reportAndCheck("SUCCESS", true)
reportAndCheck("SUCCESS", cached = true)

edit("src/main/kotlin/Sources.kt") {
"$it\n class Additional"
}
// tasks must be restarted after the source code is edited
reportAndCheck("SUCCESS", true)
reportAndCheck("SUCCESS", cached = true)

edit("src/test/kotlin/TestClass.kt") {
"$it\n class AdditionalTest"
}

// tasks must be restarted after tests are edited
reportAndCheck("SUCCESS", true)
// test task must be restarted after test class is edited, but reports is up-to-date because no sources changed
reportAndCheck("SUCCESS", "UP-TO-DATE",true)
}

@SlicedGeneratedTest(allTools = true)
@GeneratedTest(tool = CoverageToolVendor.JACOCO)
fun BuildConfigurator.testJaCoCoOutOfDateOnSources() {
useLocalCache()

addProjectWithKover {
sourcesFrom("simple")
}
reportAndCheck("SUCCESS", cached = true)

edit("src/main/kotlin/Sources.kt") {
"$it\n class Additional"
}
// tasks must be restarted after the source code is edited
reportAndCheck("SUCCESS", cached = true)

edit("src/test/kotlin/TestClass.kt") {
"$it\n class AdditionalTest"
}

// test task must be restarted after test class is edited,
// reports is not up-to-date because JaCoCo .exec binary report contains instrumentation time
reportAndCheck("SUCCESS", "SUCCESS",true)
}

@GeneratedTest(tool = CoverageToolVendor.KOVER)
fun BuildConfigurator.testProjectReportCaching() {
useLocalCache()

addProjectWithKover {
sourcesFrom("simple")
}
reportAndCheck("SUCCESS", true)
reportAndCheck("SUCCESS", cached = true)
run("clean", "--build-cache") {
checkDefaultBinReport(false)
checkDefaultReports(false)
}
reportAndCheck("FROM-CACHE", true)
reportAndCheck("FROM-CACHE", cached = true)
}


private fun BuildConfigurator.reportAndCheck(outcome: String, cached: Boolean = false) {

private fun BuildConfigurator.reportAndCheck(testOutcome: String, reportsOutcome: String = testOutcome, cached: Boolean = false) {
val args = if (cached) {
arrayOf("koverXmlReport", "koverHtmlReport", "--build-cache")
} else {
Expand All @@ -86,9 +112,9 @@ internal class ReportsCachingTests {
run(*args) {
checkDefaultBinReport()
checkDefaultReports()
checkOutcome("test", outcome)
checkOutcome("koverXmlReport", outcome)
checkOutcome("koverHtmlReport", outcome)
checkOutcome("test", testOutcome)
checkOutcome("koverXmlReport", reportsOutcome)
checkOutcome("koverHtmlReport", reportsOutcome)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ internal class JvmTestTaskConfigurator(
}
testTask.dependsOn(data.findAgentJarTask)

testTask.doFirst {
// delete report so that when the data is re-measured, it is not appended to an already existing file
// see https://github.com/Kotlin/kotlinx-kover/issues/489
binReportProvider.get().asFile.delete()
}

// Always excludes android classes, see https://github.com/Kotlin/kotlinx-kover/issues/89
val excluded = data.excludedClasses + listOf("android.*", "com.android.*")

Expand Down

0 comments on commit d677079

Please sign in to comment.