Skip to content

Commit

Permalink
Merge pull request #1162 from hcoles/feature/use_standard_kotlin_sour…
Browse files Browse the repository at this point in the history
…ce_dirs

Auto add kotlin source dirs when present
  • Loading branch information
hcoles authored Mar 3, 2023
2 parents 8ad249d + 63a4a88 commit 6fd04a7
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {

Expand Down Expand Up @@ -135,6 +137,7 @@ 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 @@ -170,6 +173,17 @@ 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 @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}

Expand Down

0 comments on commit 6fd04a7

Please sign in to comment.