Skip to content

Commit

Permalink
Gradle: Determine test output directory based on the task
Browse files Browse the repository at this point in the history
  • Loading branch information
roguexz committed Sep 26, 2020
1 parent b069233 commit ec7612c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.StringJoiner;

import org.gradle.api.Project;
import org.gradle.api.file.FileCollection;
Expand All @@ -14,6 +16,7 @@
import org.gradle.api.plugins.JavaPluginConvention;
import org.gradle.api.provider.Provider;
import org.gradle.api.tasks.SourceSet;
import org.gradle.api.tasks.SourceSetContainer;
import org.gradle.api.tasks.testing.Test;
import org.gradle.jvm.tasks.Jar;

Expand Down Expand Up @@ -55,6 +58,23 @@ void beforeTest(Test task) {
final Path serializedModel = QuarkusGradleUtils.serializeAppModel(appModel, task);
props.put(BootstrapConstants.SERIALIZED_APP_MODEL, serializedModel.toString());

// Identify the folder containing the sources associated with this test task
StringJoiner joiner = new StringJoiner(",");
getSourceSets().stream()
.filter(sourceSet -> Objects.equals(
task.getTestClassesDirs().getAsPath(),
sourceSet.getOutput().getClassesDirs().getAsPath()))
.flatMap(sourceSet -> sourceSet.getOutput().getClassesDirs().getFiles().stream())
.filter(File::exists)
.distinct()
.forEach(testSrcDir -> {
String mapping = String.format("%s:%s",
project.relativePath(testSrcDir),
project.relativePath(outputDirectory()));
joiner.add(mapping);
});
task.environment("TEST_TO_MAIN_MAPPINGS", joiner.toString());

final String nativeRunner = task.getProject().getBuildDir().toPath().resolve(finalName() + "-runner")
.toAbsolutePath()
.toString();
Expand Down Expand Up @@ -96,8 +116,8 @@ public Path appJarOrClasses() {

public File outputDirectory() {
if (outputDirectory == null) {
outputDirectory = getLastFile(project.getConvention().getPlugin(JavaPluginConvention.class)
.getSourceSets().getByName(SourceSet.MAIN_SOURCE_SET_NAME).getOutput().getClassesDirs());
outputDirectory = getLastFile(getSourceSets().getByName(SourceSet.MAIN_SOURCE_SET_NAME).getOutput()
.getClassesDirs());
}
return outputDirectory;
}
Expand All @@ -108,8 +128,8 @@ public void setOutputDirectory(String outputDirectory) {

public File outputConfigDirectory() {
if (outputConfigDirectory == null) {
outputConfigDirectory = project.getConvention().getPlugin(JavaPluginConvention.class)
.getSourceSets().getByName(SourceSet.MAIN_SOURCE_SET_NAME).getOutput().getResourcesDir();
outputConfigDirectory = getSourceSets().getByName(SourceSet.MAIN_SOURCE_SET_NAME).getOutput()
.getResourcesDir();
}
return outputConfigDirectory;
}
Expand All @@ -120,8 +140,8 @@ public void setOutputConfigDirectory(String outputConfigDirectory) {

public File sourceDir() {
if (sourceDir == null) {
sourceDir = getLastFile(project.getConvention().getPlugin(JavaPluginConvention.class)
.getSourceSets().getByName(SourceSet.MAIN_SOURCE_SET_NAME).getAllJava().getSourceDirectories());
sourceDir = getLastFile(getSourceSets().getByName(SourceSet.MAIN_SOURCE_SET_NAME).getAllJava()
.getSourceDirectories());
}
return sourceDir;
}
Expand Down Expand Up @@ -153,8 +173,7 @@ public void setFinalName(String finalName) {
}

public Set<File> resourcesDir() {
return project.getConvention().getPlugin(JavaPluginConvention.class)
.getSourceSets().getByName(SourceSet.MAIN_SOURCE_SET_NAME).getResources().getSrcDirs();
return getSourceSets().getByName(SourceSet.MAIN_SOURCE_SET_NAME).getResources().getSrcDirs();
}

public AppArtifact getAppArtifact() {
Expand Down Expand Up @@ -199,4 +218,13 @@ private File getLastFile(FileCollection fileCollection) {
return result;
}

/**
* Convenience method to get the source sets associated with the current project.
*
* @return the source sets associated with the current project.
*/
private SourceSetContainer getSourceSets() {
return project.getConvention().getPlugin(JavaPluginConvention.class).getSourceSets();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public final class PathTestHelper {
File.separator + "classes");
//endregion

String mappings = System.getenv("ADDITIONAL_TEST_TO_MAIN_MAPPINGS");
String mappings = System.getenv("TEST_TO_MAIN_MAPPINGS");
if (mappings != null) {
Stream.of(mappings.split(","))
.filter(s -> !s.isEmpty())
Expand Down

0 comments on commit ec7612c

Please sign in to comment.