From 63a4a88f075be4029f6502e9f48e6e35b2dd8281 Mon Sep 17 00:00:00 2001 From: Henry Coles Date: Fri, 3 Mar 2023 09:17:34 +0000 Subject: [PATCH] Auto add kotlin source dirs when present The jetbrains kotlin plugin doesn't seem to use the source dirs configured in the maven model. Many projects will therefore not have them properly configured. This hack ensures pitest picks them up if they are present. --- .../maven/MojoToReportOptionsConverter.java | 14 ++++++++++ .../MojoToReportOptionsConverterTest.java | 28 +++++++++++++++++++ .../java/org/pitest/maven/PitMojoTest.java | 1 + 3 files changed, 43 insertions(+) diff --git a/pitest-maven/src/main/java/org/pitest/maven/MojoToReportOptionsConverter.java b/pitest-maven/src/main/java/org/pitest/maven/MojoToReportOptionsConverter.java index 258810e25..83480365b 100644 --- a/pitest-maven/src/main/java/org/pitest/maven/MojoToReportOptionsConverter.java +++ b/pitest-maven/src/main/java/org/pitest/maven/MojoToReportOptionsConverter.java @@ -30,6 +30,7 @@ import java.io.File; import java.nio.file.FileSystems; +import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayList; @@ -44,6 +45,7 @@ import java.util.function.Function; import java.util.function.Predicate; import java.util.stream.Collectors; +import java.util.stream.Stream; public class MojoToReportOptionsConverter { @@ -135,6 +137,7 @@ private ReportOptions parseReportOptions(final List classPath) { final List sourceRoots = new ArrayList<>(); sourceRoots.addAll(this.mojo.getProject().getCompileSourceRoots()); sourceRoots.addAll(this.mojo.getProject().getTestCompileSourceRoots()); + sourceRoots.addAll(kotlinIfPresent()); data.setSourceDirs(stringsToPaths(sourceRoots)); @@ -170,6 +173,17 @@ private ReportOptions parseReportOptions(final List classPath) { return data; } + // Many maven projects will not have the kotlin sources dirs configured within the maven + // model. To work around this, the horrible hack below adds them if they are present + private Collection kotlinIfPresent() { + Path test = Paths.get(this.mojo.getProject().getBasedir().getAbsolutePath(),"src","test","kotlin" ); + Path main = Paths.get(this.mojo.getProject().getBasedir().getAbsolutePath(),"src","main","kotlin" ); + return Stream.of(test, main) + .filter(Files::exists) + .map(p -> p.toFile().getAbsolutePath()) + .collect(Collectors.toList()); + } + private void configureVerbosity(ReportOptions data) { if (this.mojo.isVerbose()) { data.setVerbosity(Verbosity.VERBOSE); diff --git a/pitest-maven/src/test/java/org/pitest/maven/MojoToReportOptionsConverterTest.java b/pitest-maven/src/test/java/org/pitest/maven/MojoToReportOptionsConverterTest.java index 0be58d7e7..98b3d332a 100644 --- a/pitest-maven/src/test/java/org/pitest/maven/MojoToReportOptionsConverterTest.java +++ b/pitest-maven/src/test/java/org/pitest/maven/MojoToReportOptionsConverterTest.java @@ -20,13 +20,17 @@ import org.apache.maven.model.Model; import org.apache.maven.model.Plugin; import org.codehaus.plexus.util.xml.Xpp3Dom; +import org.junit.rules.TemporaryFolder; import org.mockito.Mockito; import org.pitest.mutationtest.config.ConfigOption; import org.pitest.mutationtest.config.ReportOptions; import org.pitest.util.Unchecked; import java.io.File; +import java.io.IOException; import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayList; import java.util.Arrays; @@ -66,6 +70,7 @@ public void setUp() throws Exception { Build build = new Build(); build.setOutputDirectory(""); when(this.project.getBuild()).thenReturn(build); + when(this.project.getBasedir()).thenReturn(new File("BASEDIR")); } public void testsParsesReportDir() { @@ -444,6 +449,29 @@ public void testEvaluatesArgLineProperties() { assertThat(actual.getArgLine()).isEqualTo("fooValue barValue"); } + public void testAutoAddsKotlinSourceDirsWhenPresent() throws IOException { + // we're stuck in junit 3 land but can + // use junit 4's temporary folder rule programatically + TemporaryFolder t = new TemporaryFolder(); + try { + t.create(); + File base = t.getRoot(); + when(project.getBasedir()).thenReturn(base); + + Path main = base.toPath().resolve("src").resolve("main").resolve("kotlin"); + Path test = base.toPath().resolve("src").resolve("test").resolve("kotlin"); + Files.createDirectories(main); + Files.createDirectories(test); + + ReportOptions actual = parseConfig(""); + assertThat(actual.getSourcePaths()).contains(main); + } finally { + t.delete(); + } + + } + + private ReportOptions parseConfig(final String xml) { try { final String pom = createPomWithConfiguration(xml); diff --git a/pitest-maven/src/test/java/org/pitest/maven/PitMojoTest.java b/pitest-maven/src/test/java/org/pitest/maven/PitMojoTest.java index e36718d8c..8759f6137 100644 --- a/pitest-maven/src/test/java/org/pitest/maven/PitMojoTest.java +++ b/pitest-maven/src/test/java/org/pitest/maven/PitMojoTest.java @@ -34,6 +34,7 @@ public void setUp() throws Exception { super.setUp(); when(this.project.getExecutionProject()).thenReturn(executionProject); + when(this.project.getBasedir()).thenReturn(new File("BASEDIR")); when(this.executionProject.getBasedir()).thenReturn(new File("BASEDIR")); }