From 42e66efe025b81764340dbd4f7fc6b9e66c13be5 Mon Sep 17 00:00:00 2001 From: Adam <897017+aSemy@users.noreply.github.com> Date: Sat, 27 Aug 2022 11:34:01 +0200 Subject: [PATCH] filter out projects without a build file #222 --- .../functional/cases/MultiProjectTests.kt | 126 +++++++++++++++++- .../kover/appliers/KoverMergedApplier.kt | 11 +- 2 files changed, 129 insertions(+), 8 deletions(-) diff --git a/src/functionalTest/kotlin/kotlinx/kover/test/functional/cases/MultiProjectTests.kt b/src/functionalTest/kotlin/kotlinx/kover/test/functional/cases/MultiProjectTests.kt index 7b1279a9..7e38b496 100644 --- a/src/functionalTest/kotlin/kotlinx/kover/test/functional/cases/MultiProjectTests.kt +++ b/src/functionalTest/kotlin/kotlinx/kover/test/functional/cases/MultiProjectTests.kt @@ -1,11 +1,13 @@ package kotlinx.kover.test.functional.cases import kotlinx.kover.test.functional.cases.utils.* -import kotlinx.kover.test.functional.cases.utils.defaultMergedXmlReport import kotlinx.kover.test.functional.core.* -import kotlinx.kover.test.functional.core.BaseGradleScriptTest -import org.gradle.testkit.runner.* -import kotlin.test.* +import org.gradle.testkit.runner.GradleRunner +import org.gradle.testkit.runner.TaskOutcome +import java.nio.file.Files +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertTrue internal class MultiProjectTests : BaseGradleScriptTest() { private val subprojectName = "common" @@ -190,4 +192,120 @@ internal class MultiProjectTests : BaseGradleScriptTest() { } } } + + @Test + fun testNestedProjectInsideEmptyProject() { + + val projectDir = Files.createTempDirectory("nested-project").toFile() + + projectDir.resolve("settings.gradle.kts").apply { + //language=kts + writeText( + """ +rootProject.name = "nested-project" + +include(":subprojects:alpha-project") + """.trimIndent() + ) + } + + projectDir.resolve("build.gradle.kts").apply { + //language=kts + writeText( + """ +plugins { + base + id("org.jetbrains.kotlinx.kover") +} + +repositories { mavenCentral() } + +kover { + isDisabled.set(false) +} + +koverMerged { + enable() +} + """.trimIndent() + ) + } + + + projectDir.resolve("subprojects/alpha-project/build.gradle.kts").apply { + parentFile.mkdirs() + //language=kts + writeText( + """ +plugins { + kotlin("jvm") version embeddedKotlinVersion + id("org.jetbrains.kotlinx.kover") +} + +repositories { mavenCentral() } + +dependencies { + testImplementation(kotlin("test")) +} + +kover { + isDisabled.set(false) +} + """.trimIndent() + ) + } + + projectDir.resolve("subprojects/alpha-project/src/test/kotlin/MyTest.kt").apply { + parentFile.mkdirs() + //language=kotlin + writeText( + """ +import kotlin.test.* + +class MyTest { + @Test + fun foo() { + assertEquals("123", 123.toString()) + } +} + """.trimIndent() + ) + } + + val gradleRunner = GradleRunner.create() + .withProjectDir(projectDir) + .withPluginClasspath() + + gradleRunner + .withArguments(":tasks", "--stacktrace", "--info") + .build().also { result -> + assertTrue(result.output.contains("koverMergedReport")) + assertEquals( + TaskOutcome.SUCCESS, + result.task(":tasks")?.outcome, + result.output + ) + } + + gradleRunner + .withArguments("check", "--stacktrace", "--info") + .build().also { result -> + assertEquals( + TaskOutcome.SUCCESS, + result.task(":subprojects:alpha-project:test")?.outcome, + result.output + ) + } + + gradleRunner + .withArguments(":koverMergedReport", "--stacktrace", "--info") + .build().also { result -> + assertTrue(result.output.contains("koverMergedReport")) + assertEquals( + TaskOutcome.SUCCESS, + result.task(":koverMergedReport")?.outcome, + result.output + ) + } + } } diff --git a/src/main/kotlin/kotlinx/kover/appliers/KoverMergedApplier.kt b/src/main/kotlin/kotlinx/kover/appliers/KoverMergedApplier.kt index 6c1da980..ba9f5364 100644 --- a/src/main/kotlin/kotlinx/kover/appliers/KoverMergedApplier.kt +++ b/src/main/kotlin/kotlinx/kover/appliers/KoverMergedApplier.kt @@ -178,13 +178,16 @@ private inline fun Project.mergedFilesProvider( private fun filterProjects(filters: KoverProjectsFilter, allProjects: Iterable): List { + + val actualProjects = allProjects.filter { it.buildFile.exists() } + if (filters.excludes.isEmpty()) { - return allProjects.toList() + return actualProjects.toList() } - val projectsByPath = allProjects.associateBy { p -> p.path }.toMutableMap() - val pathsByName = allProjects.associate { it.name to mutableListOf() } - allProjects.forEach { pathsByName.getValue(it.name) += it.path } + val projectsByPath = actualProjects.associateBy { p -> p.path }.toMutableMap() + val pathsByName = actualProjects.associate { it.name to mutableListOf() } + actualProjects.forEach { pathsByName.getValue(it.name) += it.path } val excludedPaths = filters.excludes.map { if (it.startsWith(':')) {