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

Auto add standard kotlin source dirs if present #1347

Merged
merged 1 commit into from
Sep 2, 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 @@ -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
Loading