From c7b9fa4513f794c9456bde0f78970d9e5e3da360 Mon Sep 17 00:00:00 2001 From: Henry Coles Date: Mon, 2 Sep 2024 09:36:41 +0100 Subject: [PATCH] Auto add standard kotlin source dirs if present src/main/kotlin and src/test/kotlin are now auto added to the a maven project by the mojo if present. This is necessary as, for maven builds, there directories are often added at runtime by the build helper or kotlin plugins. If they pitest goal is run directly (as is common) these plugins will not fire and the config will be missing causing confusing failures. This replaces an earlier hack where there directories were aded at a lower level. --- .../sub-module-1/pom.xml | 18 ------------ .../sub-module-2/pom.xml | 18 ------------ .../sub-module-3/pom.xml | 18 ------------ .../org/pitest/maven/AbstractPitMojo.java | 28 +++++++++++++++++++ .../maven/MojoToReportOptionsConverter.java | 14 ---------- .../MojoToReportOptionsConverterTest.java | 22 --------------- .../java/org/pitest/maven/PitMojoTest.java | 2 +- 7 files changed, 29 insertions(+), 91 deletions(-) diff --git a/pitest-maven-verification/src/test/resources/pit-kotlin-multi-module/sub-module-1/pom.xml b/pitest-maven-verification/src/test/resources/pit-kotlin-multi-module/sub-module-1/pom.xml index ecc723376..46fd628a0 100644 --- a/pitest-maven-verification/src/test/resources/pit-kotlin-multi-module/sub-module-1/pom.xml +++ b/pitest-maven-verification/src/test/resources/pit-kotlin-multi-module/sub-module-1/pom.xml @@ -39,24 +39,6 @@ org.jetbrains.kotlin kotlin-maven-plugin - - org.codehaus.mojo - build-helper-maven-plugin - 3.2.0 - - - generate-sources - - add-source - - - - src/main/kotlin - - - - - diff --git a/pitest-maven-verification/src/test/resources/pit-kotlin-multi-module/sub-module-2/pom.xml b/pitest-maven-verification/src/test/resources/pit-kotlin-multi-module/sub-module-2/pom.xml index 6e18d815c..00d24c96c 100644 --- a/pitest-maven-verification/src/test/resources/pit-kotlin-multi-module/sub-module-2/pom.xml +++ b/pitest-maven-verification/src/test/resources/pit-kotlin-multi-module/sub-module-2/pom.xml @@ -39,24 +39,6 @@ org.jetbrains.kotlin kotlin-maven-plugin - - org.codehaus.mojo - build-helper-maven-plugin - 3.2.0 - - - generate-sources - - add-source - - - - src/main/kotlin - - - - - diff --git a/pitest-maven-verification/src/test/resources/pit-kotlin-multi-module/sub-module-3/pom.xml b/pitest-maven-verification/src/test/resources/pit-kotlin-multi-module/sub-module-3/pom.xml index 135d604e2..e0bfad6d6 100644 --- a/pitest-maven-verification/src/test/resources/pit-kotlin-multi-module/sub-module-3/pom.xml +++ b/pitest-maven-verification/src/test/resources/pit-kotlin-multi-module/sub-module-3/pom.xml @@ -39,24 +39,6 @@ org.jetbrains.kotlin kotlin-maven-plugin - - org.codehaus.mojo - build-helper-maven-plugin - 3.2.0 - - - generate-sources - - add-source - - - - src/main/kotlin - - - - - diff --git a/pitest-maven/src/main/java/org/pitest/maven/AbstractPitMojo.java b/pitest-maven/src/main/java/org/pitest/maven/AbstractPitMojo.java index 979e0eedd..514c3198b 100644 --- a/pitest-maven/src/main/java/org/pitest/maven/AbstractPitMojo.java +++ b/pitest-maven/src/main/java/org/pitest/maven/AbstractPitMojo.java @@ -353,6 +353,12 @@ public class AbstractPitMojo extends AbstractMojo { @Parameter(property = "pit.outputEncoding", defaultValue = "${project.reporting.outputEncoding}") private String outputEncoding; + @Parameter(property = "pit.additionalSources", defaultValue = "src/main/kotlin") + private List additionalSources; + + @Parameter(property = "pit.additionalTestSources", defaultValue = "src/test/kotlin") + private List additionalTestSources; + /** * The base directory of a multi-module project. Defaults to the execution * directory @@ -433,6 +439,7 @@ public final void execute() throws MojoExecutionException, MojoFailureException { switchLogging(); + augmentConfig(); RunDecision shouldRun = shouldRun(); if (shouldRun.shouldRun()) { @@ -469,6 +476,27 @@ public final void execute() throws MojoExecutionException, } } + /** + * Maven kotlin projects often add the kotlin sources at runtime via the build helper or kotlin p;lugins. + * Unfortunately, pitest is often has its maven goal called directly so this + * config isn't visible to it. We therefore add them in at runtime ourselves if present. + */ + private void augmentConfig() { + for (File source : emptyWithoutNulls(additionalSources)) { + if (source.exists() && ! this.project.getCompileSourceRoots().contains(source.getAbsolutePath())) { + this.getLog().info("Adding source root " + source); + this.project.addCompileSourceRoot(source.getAbsolutePath()); + } + } + + for (File source : emptyWithoutNulls(additionalTestSources)) { + if (source.exists() && ! this.project.getTestCompileSourceRoots().contains(source.getAbsolutePath())) { + this.getLog().info("Adding test root " + source); + this.project.addTestCompileSourceRoot(source.getAbsolutePath()); + } + } + } + private void switchLogging() { if (this.useSlf4j) { SLF4JBridgeHandler.removeHandlersForRootLogger(); 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 ae9d9894f..7ea345b49 100644 --- a/pitest-maven/src/main/java/org/pitest/maven/MojoToReportOptionsConverter.java +++ b/pitest-maven/src/main/java/org/pitest/maven/MojoToReportOptionsConverter.java @@ -34,7 +34,6 @@ 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; @@ -50,7 +49,6 @@ import java.util.function.Function; import java.util.function.Predicate; import java.util.stream.Collectors; -import java.util.stream.Stream; import static org.pitest.functional.Streams.asStream; @@ -220,7 +218,6 @@ 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)); @@ -256,17 +253,6 @@ 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 3f4d518d1..7edcf0798 100644 --- a/pitest-maven/src/test/java/org/pitest/maven/MojoToReportOptionsConverterTest.java +++ b/pitest-maven/src/test/java/org/pitest/maven/MojoToReportOptionsConverterTest.java @@ -470,28 +470,6 @@ public void testEvaluatesNormalPropertiesInArgLines() { 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 e187f79bd..afa965ec8 100644 --- a/pitest-maven/src/test/java/org/pitest/maven/PitMojoTest.java +++ b/pitest-maven/src/test/java/org/pitest/maven/PitMojoTest.java @@ -38,7 +38,7 @@ public void setUp() throws Exception { when(this.executionProject.getBasedir()).thenReturn(new File("BASEDIR")); } - public void testRunsAMutationReportWhenMutationCoverageGoalTrigered() + public void testRunsAMutationReportWhenMutationCoverageGoalTriggered() throws Exception { this.testee = createPITMojo(createPomWithConfiguration("")); final Build build = new Build();