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 kotlin source dirs when present #1162

Merged
merged 1 commit into from
Mar 3, 2023
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 @@ -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