Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed warning for empty Kotlin plugin configuration #702

Merged
merged 1 commit into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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")
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>, checker: CheckerContext.() -> Unit) {
val workingDirectory = Files.createTempDirectory("kover-maven-test").toFile()

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright 2017-2024 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.example</groupId>
<artifactId>charset</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<kotlin.code.style>official</kotlin.code.style>
<kotlin.compiler.jvmTarget>1.8</kotlin.compiler.jvmTarget>
<kover.version>0.8.2</kover.version>
</properties>

<repositories>
<repository>
<id>mavenCentral</id>
<url>https://repo1.maven.org/maven2/</url>
</repository>
</repositories>

<build>
<sourceDirectory>src/main/kotlin</sourceDirectory>
<testSourceDirectory>src/test/kotlin</testSourceDirectory>
<plugins>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile-kotlin</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
<configuration>
<!-- There are no configs -->
</configuration>
</plugin>

<plugin>
<groupId>org.jetbrains.kotlinx</groupId>
<artifactId>kover-maven-plugin</artifactId>
<version>${kover.version}</version>
<executions>
<execution>
<id>instr</id>
<goals>
<goal>instrumentation</goal>
</goals>
</execution>

<execution>
<id>kover-html</id>
<goals>
<goal>report-html</goal>
</goals>
</execution>

</executions>
</plugin>

<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.22.2</version>
</plugin>
</plugins>

</build>

<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-test-junit5</artifactId>
<version>${kotlin.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
<version>${kotlin.version}</version>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package kotlinx.kover.maven.plugin.testing

class Main {
fun used() {
println("used")
}

fun unused() {
println("unused")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package kotlinx.kover.maven.plugin.testing

import kotlin.test.Test

class MainKtTest {
@Test
fun myTest() {
Main().used()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there are <sourceDirs><sourceDirs/>, but no children are specified, will children be a null or an emptyList?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Empty list, it's not mentioned in the docs, but implementation is

    public Xpp3Dom[] getChildren()
    {
        if ( null == childList || childList.isEmpty() )
        {
            return EMPTY_DOM_ARRAY;
        }
        else
        {
            return (Xpp3Dom[]) childList.toArray( new Xpp3Dom[childList.size()] );
        }
    }

}
} catch (e: Exception) {
// in future versions configuration may be changed
Expand Down