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

Include the test module in the tested mod when running tests through IntelliJ #25

Merged
merged 2 commits into from
Jun 15, 2024
Merged
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
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
Loading