Skip to content

Commit

Permalink
Added support of parallel tests execution
Browse files Browse the repository at this point in the history
Fixes #113
  • Loading branch information
shanshin committed Jan 20, 2022
1 parent 2cc1e93 commit 66c5778
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
36 changes: 32 additions & 4 deletions src/main/kotlin/kotlinx/kover/KoverPlugin.kt
Expand Up @@ -102,7 +102,7 @@ class KoverPlugin : Plugin<Project> {
}

tasks.withType(Test::class.java).configureEach { t ->
t.configTest(providers, agents)
t.configTestTask(providers, agents)
}
}

Expand Down Expand Up @@ -238,7 +238,7 @@ class KoverPlugin : Plugin<Project> {
return extension
}

private fun Test.configTest(
private fun Test.configTestTask(
providers: AllProviders,
agents: Map<CoverageEngine, CoverageAgent>
) {
Expand All @@ -263,7 +263,8 @@ class KoverPlugin : Plugin<Project> {
)
)

doLast(IntellijErrorLogChecker(taskExtension))
doFirst(BinaryReportCleanupAction(providers.koverExtension, taskExtension))
doLast(IntellijErrorLogCopyAction(taskExtension))
}

private fun Project.checkAlreadyApplied() {
Expand All @@ -278,7 +279,34 @@ class KoverPlugin : Plugin<Project> {
}
}

private class IntellijErrorLogChecker(private val taskExtension: KoverTaskExtension) : Action<Task> {
/*
To support parallel tests, both Coverage Engines work in append to data file mode.
For this reason, before starting the tests, it is necessary to clear the file from the results of previous runs.
*/
private class BinaryReportCleanupAction(
private val koverExtensionProvider: Provider<KoverExtension>,
private val taskExtension: KoverTaskExtension
) : Action<Task> {
override fun execute(task: Task) {
val koverExtension = koverExtensionProvider.get()
val file = taskExtension.binaryReportFile.get()

// always delete previous data file
file.delete()

if (!taskExtension.isDisabled
&& !koverExtension.isDisabled
&& !koverExtension.disabledProjects.contains(task.project.name)
&& koverExtension.coverageEngine.get() == CoverageEngine.INTELLIJ
) {
// IntelliJ engine expected empty file for parallel test execution.
// Since it is impossible to know in advance whether the tests will be run in parallel, we always create an empty file.
file.createNewFile()
}
}
}

private class IntellijErrorLogCopyAction(private val taskExtension: KoverTaskExtension) : Action<Task> {
override fun execute(task: Task) {
task.project.copyIntellijErrorLog(
task.project.layout.buildDirectory.get().file("kover/errors/${task.name}.log").asFile,
Expand Down
Expand Up @@ -21,7 +21,7 @@ internal fun Project.createIntellijAgent(koverExtension: KoverExtension): Covera
private class IntellijAgent(private val config: Configuration): CoverageAgent {
private val trackingPerTest = false // a flag to enable tracking per test coverage
private val calculateForUnloadedClasses = false // a flag to calculate coverage for unloaded classes
private val appendToDataFile = false // a flag to use data file as initial coverage
private val appendToDataFile = true // a flag to use data file as initial coverage
private val samplingMode = false //a flag to run coverage in sampling mode or in tracing mode otherwise

override val engine: CoverageEngine = CoverageEngine.INTELLIJ
Expand Down
Expand Up @@ -37,7 +37,7 @@ private class JacocoAgent(private val config: Configuration, private val project

return listOfNotNull(
"destfile=${binary.canonicalPath}",
"append=false", // Kover don't support parallel execution of one task
"append=true",
"inclnolocationclasses=false",
"dumponexit=true",
"output=file",
Expand Down

0 comments on commit 66c5778

Please sign in to comment.