From 83dc0acb66daadff53f950cf1258e907a6c81acb Mon Sep 17 00:00:00 2001 From: "Sergey.Shanshin" Date: Mon, 4 Nov 2024 15:51:29 +0100 Subject: [PATCH] Fixed warning for empty Kotlin plugin configuration When a configuration is specified in the execution for the `compile` target, but the `sourceDirs` child element is missing, an error occurred. A check has been added to fix the error. Fixes #701 --- .../functional/cases/MavenPluginTests.kt | 8 ++ .../tests/functional/framework/Checker.kt | 10 +- .../tests/kotlin-empty-config/pom.xml | 112 ++++++++++++++++++ .../src/main/kotlin/Main.kt | 11 ++ .../src/test/kotlin/MainKtTest.kt | 10 ++ .../abstracts/AbstractCoverageTaskMojo.kt | 4 +- 6 files changed, 153 insertions(+), 2 deletions(-) create mode 100644 kover-maven-plugin/src/functionalTest/templates/tests/kotlin-empty-config/pom.xml create mode 100644 kover-maven-plugin/src/functionalTest/templates/tests/kotlin-empty-config/src/main/kotlin/Main.kt create mode 100644 kover-maven-plugin/src/functionalTest/templates/tests/kotlin-empty-config/src/test/kotlin/MainKtTest.kt diff --git a/kover-maven-plugin/src/functionalTest/kotlin/kotlinx/kover/maven/plugin/tests/functional/cases/MavenPluginTests.kt b/kover-maven-plugin/src/functionalTest/kotlin/kotlinx/kover/maven/plugin/tests/functional/cases/MavenPluginTests.kt index 3e041795..d6997abe 100644 --- a/kover-maven-plugin/src/functionalTest/kotlin/kotlinx/kover/maven/plugin/tests/functional/cases/MavenPluginTests.kt +++ b/kover-maven-plugin/src/functionalTest/kotlin/kotlinx/kover/maven/plugin/tests/functional/cases/MavenPluginTests.kt @@ -14,6 +14,8 @@ import kotlinx.kover.maven.plugin.tests.functional.framework.CounterAssert.* import kotlinx.kover.maven.plugin.tests.functional.framework.CounterType.LINE import org.junit.jupiter.api.Test import java.io.File +import kotlin.test.assertContains +import kotlin.test.assertFalse import kotlin.test.assertTrue class MavenPluginTests { @@ -264,4 +266,10 @@ Rule violated: assertDefaultHtmlTitle("charset", "UTF-16BE") } + @Test + fun testEmptyKotlinConfig() = runAndCheckTest("kotlin-empty-config", "verify") { + assertBuildIsSuccessful() + assertFalse("java.lang.NullPointerException" in log, "NPE should not be thrown") + } + } diff --git a/kover-maven-plugin/src/functionalTest/kotlin/kotlinx/kover/maven/plugin/tests/functional/framework/Checker.kt b/kover-maven-plugin/src/functionalTest/kotlin/kotlinx/kover/maven/plugin/tests/functional/framework/Checker.kt index 9e1d01e5..baf6c38c 100644 --- a/kover-maven-plugin/src/functionalTest/kotlin/kotlinx/kover/maven/plugin/tests/functional/framework/Checker.kt +++ b/kover-maven-plugin/src/functionalTest/kotlin/kotlinx/kover/maven/plugin/tests/functional/framework/Checker.kt @@ -8,16 +8,24 @@ import java.io.File import java.nio.file.Files private const val EXAMPLES_DIR = "examples" +private const val TESTS_DIR = "src/functionalTest/templates/tests" fun runAndCheckExample(name: String, vararg args: String, checker: CheckerContext.() -> Unit) { val exampleDir = File(EXAMPLES_DIR).resolve(name) if (!exampleDir.exists()) { throw MavenAssertionException("Example '$exampleDir' not found in directory '$EXAMPLES_DIR'") } - exampleDir.runAndCheck(args.toList(), checker) } +fun runAndCheckTest(name: String, vararg args: String, checker: CheckerContext.() -> Unit) { + val testDir = File(TESTS_DIR).resolve(name) + if (!testDir.exists()) { + throw MavenAssertionException("Test '$testDir' not found in directory '$TESTS_DIR'") + } + testDir.runAndCheck(args.toList(), checker) +} + private fun File.runAndCheck(commands: List, checker: CheckerContext.() -> Unit) { val workingDirectory = Files.createTempDirectory("kover-maven-test").toFile() diff --git a/kover-maven-plugin/src/functionalTest/templates/tests/kotlin-empty-config/pom.xml b/kover-maven-plugin/src/functionalTest/templates/tests/kotlin-empty-config/pom.xml new file mode 100644 index 00000000..2e6a7f23 --- /dev/null +++ b/kover-maven-plugin/src/functionalTest/templates/tests/kotlin-empty-config/pom.xml @@ -0,0 +1,112 @@ + + + + + 4.0.0 + + org.example + charset + 1.0-SNAPSHOT + + + UTF-8 + official + 1.8 + 0.8.2 + + + + + mavenCentral + https://repo1.maven.org/maven2/ + + + + + src/main/kotlin + src/test/kotlin + + + org.jetbrains.kotlin + kotlin-maven-plugin + ${kotlin.version} + + + compile-kotlin + compile + + compile + + + + test-compile + test-compile + + test-compile + + + + + + + + + + org.jetbrains.kotlinx + kover-maven-plugin + ${kover.version} + + + instr + + instrumentation + + + + + kover-html + + report-html + + + + + + + + maven-surefire-plugin + 2.22.2 + + + maven-failsafe-plugin + 2.22.2 + + + + + + + + org.jetbrains.kotlin + kotlin-test-junit5 + ${kotlin.version} + test + + + org.junit.jupiter + junit-jupiter + 5.8.2 + test + + + org.jetbrains.kotlin + kotlin-stdlib + ${kotlin.version} + + + + \ No newline at end of file diff --git a/kover-maven-plugin/src/functionalTest/templates/tests/kotlin-empty-config/src/main/kotlin/Main.kt b/kover-maven-plugin/src/functionalTest/templates/tests/kotlin-empty-config/src/main/kotlin/Main.kt new file mode 100644 index 00000000..221f082c --- /dev/null +++ b/kover-maven-plugin/src/functionalTest/templates/tests/kotlin-empty-config/src/main/kotlin/Main.kt @@ -0,0 +1,11 @@ +package kotlinx.kover.maven.plugin.testing + +class Main { + fun used() { + println("used") + } + + fun unused() { + println("unused") + } +} \ No newline at end of file diff --git a/kover-maven-plugin/src/functionalTest/templates/tests/kotlin-empty-config/src/test/kotlin/MainKtTest.kt b/kover-maven-plugin/src/functionalTest/templates/tests/kotlin-empty-config/src/test/kotlin/MainKtTest.kt new file mode 100644 index 00000000..7a1bf0b1 --- /dev/null +++ b/kover-maven-plugin/src/functionalTest/templates/tests/kotlin-empty-config/src/test/kotlin/MainKtTest.kt @@ -0,0 +1,10 @@ +package kotlinx.kover.maven.plugin.testing + +import kotlin.test.Test + +class MainKtTest { + @Test + fun myTest() { + Main().used() + } +} \ No newline at end of file diff --git a/kover-maven-plugin/src/main/kotlin/kotlinx/kover/maven/plugin/mojo/abstracts/AbstractCoverageTaskMojo.kt b/kover-maven-plugin/src/main/kotlin/kotlinx/kover/maven/plugin/mojo/abstracts/AbstractCoverageTaskMojo.kt index 4cfc22f0..4f423c9b 100644 --- a/kover-maven-plugin/src/main/kotlin/kotlinx/kover/maven/plugin/mojo/abstracts/AbstractCoverageTaskMojo.kt +++ b/kover-maven-plugin/src/main/kotlin/kotlinx/kover/maven/plugin/mojo/abstracts/AbstractCoverageTaskMojo.kt @@ -226,7 +226,9 @@ abstract class AbstractCoverageTaskMojo : AbstractKoverMojo() { .filter { "compile" in it.goals } .filter { execution -> execution.configuration != null && execution.configuration is Xpp3Dom } .flatMap { execution -> - (execution.configuration as Xpp3Dom).getChild("sourceDirs").children.map { toAbsoluteFile(it.value) } + val config = execution.configuration as Xpp3Dom + val sourceDirs = config.getChild("sourceDirs") ?: return@flatMap emptyList() + sourceDirs.children.map { toAbsoluteFile(it.value) } } } catch (e: Exception) { // in future versions configuration may be changed