Skip to content

Commit

Permalink
Include the test module in the tested mod when running tests through …
Browse files Browse the repository at this point in the history
…IntelliJ (#25)

We include the `test` source set by default in the Gradle output
directories and should do the same for IJ.
  • Loading branch information
shartte authored Jun 15, 2024
1 parent 5f4bb80 commit 7a5aaee
Showing 1 changed file with 33 additions and 23 deletions.
56 changes: 33 additions & 23 deletions src/main/java/net/neoforged/moddevgradle/internal/RunUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import org.gradle.api.tasks.Internal;
import org.gradle.api.tasks.Nested;
import org.gradle.api.tasks.SourceSet;
import org.gradle.api.tasks.SourceSetContainer;
import org.gradle.process.CommandLineArgumentProvider;
import org.intellij.lang.annotations.Language;
import org.jetbrains.annotations.Nullable;
Expand All @@ -41,6 +40,7 @@
import java.util.Optional;
import java.util.Properties;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.stream.Collectors;

final class RunUtils {
Expand Down Expand Up @@ -186,30 +186,26 @@ public enum RunArgFile {
public static String getArgFileParameter(RegularFile argFile) {
return "@" + argFile.getAsFile().getAbsolutePath();
}

public static ModFoldersProvider getGradleModFoldersProvider(Project project, Provider<Set<ModModel>> modsProvider, boolean includeUnitTests) {
var modFoldersProvider = project.getObjects().newInstance(ModFoldersProvider.class);
modFoldersProvider.getModFolders().set(getModFoldersForGradle(project, modsProvider, includeUnitTests));
return modFoldersProvider;
}

public static ModFoldersProvider getIdeaModFoldersProvider(Project project, @Nullable File outputDirectory, Provider<Set<ModModel>> modsProvider, boolean includeUnitTests) {
var modFoldersProvider = project.getObjects().newInstance(ModFoldersProvider.class);
Provider<Map<String, ModFolder>> folders;
if (outputDirectory != null) {
modFoldersProvider.getModFolders().set(modsProvider.map(mods -> mods.stream()
.collect(Collectors.toMap(ModModel::getName, mod -> {
var modFolder = project.getObjects().newInstance(ModFolder.class);
modFolder.getFolders().from(InternalModelHelper.getModConfiguration(mod));
for (var sourceSet : mod.getModSourceSets().get()) {
// TODO: this is probably broken in multiproject builds
var sourceSetDir = outputDirectory.toPath().resolve(getIdeaOutName(sourceSet));
modFolder.getFolders().from(sourceSetDir.resolve("classes"));
modFolder.getFolders().from(sourceSetDir.resolve("resources"));
}
return modFolder;
}))));
folders = buildModFolders(project, modsProvider, includeUnitTests, (sourceSet, output) -> {
var sourceSetDir = outputDirectory.toPath().resolve(getIdeaOutName(sourceSet));
output.from(sourceSetDir.resolve("classes"), sourceSetDir.resolve("resources"));
});
} else {
modFoldersProvider.getModFolders().set(getModFoldersForGradle(project, modsProvider, includeUnitTests));
folders = getModFoldersForGradle(project, modsProvider, includeUnitTests);
}

var modFoldersProvider = project.getObjects().newInstance(ModFoldersProvider.class);
modFoldersProvider.getModFolders().set(folders);
return modFoldersProvider;
}

Expand All @@ -218,25 +214,39 @@ private static String getIdeaOutName(final SourceSet sourceSet) {
}

private static Provider<Map<String, ModFolder>> getModFoldersForGradle(Project project, Provider<Set<ModModel>> modsProvider, boolean includeUnitTests) {
var extension = ExtensionUtils.findExtension(project, "neoForge", NeoForgeExtension.class);
var unitTestedMod = extension.getUnitTest().getTestedMod()
return buildModFolders(project, modsProvider, includeUnitTests, (sourceSet, output) -> {
output.from(sourceSet.getOutput());
});
}

private static Provider<Map<String, ModFolder>> buildModFolders(Project project, Provider<Set<ModModel>> modsProvider, boolean includeUnitTests, BiConsumer<SourceSet, ConfigurableFileCollection> outputFolderResolver) {
var extension = ExtensionUtils.findExtension(project, NeoForgeExtension.NAME, NeoForgeExtension.class);
var testedModProvider = extension.getUnitTest().getTestedMod()
.filter(m -> includeUnitTests)
.map(Optional::of)
.orElse(Optional.empty());

return modsProvider.zip(unitTestedMod, (mods, testedMod) -> mods.stream()
return modsProvider.zip(testedModProvider, ((mods, testedMod) -> mods.stream()
.collect(Collectors.toMap(ModModel::getName, mod -> {
var modFolder = project.getObjects().newInstance(ModFolder.class);
modFolder.getFolders().from(InternalModelHelper.getModConfiguration(mod));
for (var sourceSet : mod.getModSourceSets().get()) {
modFolder.getFolders().from(sourceSet.getOutput());

var sourceSets = mod.getModSourceSets().get();

for (var sourceSet : sourceSets) {
outputFolderResolver.accept(sourceSet, modFolder.getFolders());
}

// Add the test source set to the mod under test and if unit tests are enabled
if (testedMod.isPresent() && testedMod.get() == mod) {
var sourceSets = ExtensionUtils.findExtension(project, "sourceSets", SourceSetContainer.class);
modFolder.getFolders().from(sourceSets.getByName("test").getOutput());
var testSourceSet = ExtensionUtils.getSourceSets(project).findByName(SourceSet.TEST_SOURCE_SET_NAME);
if (testSourceSet != null && !sourceSets.contains(testSourceSet)) {
outputFolderResolver.accept(testSourceSet, modFolder.getFolders());
}
}

return modFolder;
})));
}))));
}

// TODO: Loom has unit tests for this... Probably a good idea!
Expand Down

0 comments on commit 7a5aaee

Please sign in to comment.