Skip to content

Commit

Permalink
Merge pull request #1347 from hcoles/feature/auto_configure_kotlin
Browse files Browse the repository at this point in the history
Auto add standard kotlin source dirs if present
  • Loading branch information
hcoles authored Sep 2, 2024
2 parents 5b8e2c1 + c7b9fa4 commit 8714b60
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,24 +39,6 @@
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>src/main/kotlin</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,24 +39,6 @@
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>src/main/kotlin</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,24 +39,6 @@
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>src/main/kotlin</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

Expand Down
28 changes: 28 additions & 0 deletions pitest-maven/src/main/java/org/pitest/maven/AbstractPitMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<File> additionalSources;

@Parameter(property = "pit.additionalTestSources", defaultValue = "src/test/kotlin")
private List<File> additionalTestSources;

/**
* The base directory of a multi-module project. Defaults to the execution
* directory
Expand Down Expand Up @@ -433,6 +439,7 @@ public final void execute() throws MojoExecutionException,
MojoFailureException {

switchLogging();
augmentConfig();
RunDecision shouldRun = shouldRun();

if (shouldRun.shouldRun()) {
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -220,7 +218,6 @@ private ReportOptions parseReportOptions(final List<String> classPath) {
final List<String> sourceRoots = new ArrayList<>();
sourceRoots.addAll(this.mojo.getProject().getCompileSourceRoots());
sourceRoots.addAll(this.mojo.getProject().getTestCompileSourceRoots());
sourceRoots.addAll(kotlinIfPresent());

data.setSourceDirs(stringsToPaths(sourceRoots));

Expand Down Expand Up @@ -256,17 +253,6 @@ private ReportOptions parseReportOptions(final List<String> 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<String> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 8714b60

Please sign in to comment.