Skip to content

Commit

Permalink
Various fixes and default IJ junit run configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
shartte committed Jun 2, 2024
1 parent 1b7af25 commit 7ed8494
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 25 deletions.
5 changes: 2 additions & 3 deletions src/main/java/net/neoforged/moddevgradle/dsl/UnitTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import org.gradle.api.Project;
import org.gradle.api.file.DirectoryProperty;
import org.gradle.api.provider.Property;
import org.jetbrains.annotations.Nullable;

import javax.inject.Inject;

Expand All @@ -18,7 +17,7 @@ public abstract class UnitTest {
public UnitTest(Project project) {
this.project = project;

getGameDirectory().convention(project.getLayout().getBuildDirectory().dir("fmljunitrun"));
getGameDirectory().convention(project.getLayout().getBuildDirectory().dir("minecraft-junit"));
}

/**
Expand All @@ -29,7 +28,7 @@ public void enable() {
}

/**
* The mod that is currently being tested, so that the unit test classes and resources can be added to it.
* The mod that will be loaded in JUnit tests.

This comment has been minimized.

Copy link
@Technici4n

Technici4n Jun 3, 2024

Member

It's more complicated. We add test classes and resources to it. You removed that info :P

*/
public abstract Property<ModModel> getTestedMod();

Expand Down
75 changes: 53 additions & 22 deletions src/main/java/net/neoforged/moddevgradle/internal/ModDevPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import org.jetbrains.annotations.Nullable;
import org.jetbrains.gradle.ext.Application;
import org.jetbrains.gradle.ext.IdeaExtPlugin;
import org.jetbrains.gradle.ext.JUnit;
import org.jetbrains.gradle.ext.ModuleRef;
import org.jetbrains.gradle.ext.ProjectSettings;
import org.jetbrains.gradle.ext.RunConfigurationContainer;
Expand All @@ -60,6 +61,7 @@
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

public class ModDevPlugin implements Plugin<Project> {
Expand Down Expand Up @@ -95,35 +97,44 @@ public void apply(Project project) {
});

repositories.addLast(repositories.maven(repo -> {
repo.setName("NeoForged Releases");
repo.setUrl(URI.create("https://maven.neoforged.net/releases/"));
}));
repositories.addLast(repositories.maven(repo -> {
repo.setName("Mojang Minecraft Libraries");
repo.setUrl(URI.create("https://libraries.minecraft.net/"));
repo.metadataSources(sources -> sources.artifact());
repo.metadataSources(sources -> sources.mavenPom());
// TODO: Filter known groups that they ship and dont just run everything against it
}));
addTemporaryRepositories(repositories);

project.getDependencies().attributesSchema(attributesSchema -> {
attributesSchema.attribute(ATTRIBUTE_DISTRIBUTION).getDisambiguationRules().add(DistributionDisambiguation.class);
attributesSchema.attribute(ATTRIBUTE_OPERATING_SYSTEM).getDisambiguationRules().add(OperatingSystemDisambiguation.class);
});

var createManifest = tasks.register("createArtifactManifest", CreateArtifactManifestTask.class, task -> {
configureArtifactManifestTask(task, extension);
task.getManifestFile().set(modDevBuildDir.map(dir -> dir.file("nfrt_artifact_manifest.properties")));
});

var neoFormRuntimeConfig = configurations.create("neoFormRuntime", files -> {
files.setCanBeConsumed(false);
files.setCanBeResolved(true);
files.defaultDependencies(spec -> {
spec.addLater(extension.getNeoFormRuntime().getVersion().map(version -> dependencyFactory.create("net.neoforged:neoform-runtime:" + version).attributes(attributes -> {
var neoFormRuntimeConfig = configurations.create("neoFormRuntime", spec -> {
spec.setDescription("The NeoFormRuntime CLI tool");
spec.setCanBeConsumed(false);
spec.setCanBeResolved(true);
spec.defaultDependencies(dependencies -> {
dependencies.addLater(extension.getNeoFormRuntime().getVersion().map(version -> dependencyFactory.create("net.neoforged:neoform-runtime:" + version).attributes(attributes -> {
attributes.attribute(Bundling.BUNDLING_ATTRIBUTE, project.getObjects().named(Bundling.class, Bundling.SHADOWED));
})));
});
});

// Create an access transformer configuration
var accessTransformers = configurations.create("accessTransformers", files -> {
files.setCanBeConsumed(false);
files.setCanBeResolved(true);
files.defaultDependencies(dependencies -> {
var accessTransformers = configurations.create("accessTransformers", spec -> {
spec.setDescription("AccessTransformers to widen visibility of Minecraft classes/fields/methods");
spec.setCanBeConsumed(false);
spec.setCanBeResolved(true);
spec.defaultDependencies(dependencies -> {
dependencies.addLater(
extension.getAccessTransformers()
.map(project::files)
Expand All @@ -136,6 +147,7 @@ public void apply(Project project) {
var parchment = extension.getParchment();
configureParchmentRepository(project, parchment);
var parchmentData = configurations.create("parchmentData", spec -> {
spec.setDescription("Data used to add parameter names and javadoc to Minecraft sources");
spec.setCanBeResolved(true);
spec.setCanBeConsumed(false);
spec.setTransitive(false); // Expect a single result
Expand Down Expand Up @@ -181,6 +193,7 @@ public void apply(Project project) {
}

var localRuntime = configurations.create("neoForgeGeneratedArtifacts", config -> {
config.setDescription("Minecraft artifacts that were generated locally by NFRT");
config.setCanBeResolved(false);
config.setCanBeConsumed(false);
config.withDependencies(dependencies -> {
Expand All @@ -192,11 +205,6 @@ public void apply(Project project) {
});
});

project.getDependencies().attributesSchema(attributesSchema -> {
attributesSchema.attribute(ATTRIBUTE_DISTRIBUTION).getDisambiguationRules().add(DistributionDisambiguation.class);
attributesSchema.attribute(ATTRIBUTE_OPERATING_SYSTEM).getDisambiguationRules().add(OperatingSystemDisambiguation.class);
});

configurations.named("compileOnly").configure(configuration -> {
configuration.withDependencies(dependencies -> {
dependencies.addLater(minecraftClassesArtifact.map(dependencyFactory::create));
Expand Down Expand Up @@ -318,7 +326,17 @@ public void apply(Project project) {

setupJarJar(project);

configureTesting = () -> setupTesting(project, modDevBuildDir, userDevConfigOnly, neoForgeModDevModules, downloadAssets, ideSyncTask, createArtifacts, neoForgeModDevLibrariesDependency, minecraftClassesArtifact);
configureTesting = () -> setupTesting(
project,
modDevBuildDir,
userDevConfigOnly,
neoForgeModDevModules,
downloadAssets,
ideSyncTask,
createArtifacts,
neoForgeModDevLibrariesDependency,
minecraftClassesArtifact
);

configureIntelliJModel(project, ideSyncTask, extension, prepareRunTasks);

Expand Down Expand Up @@ -477,7 +495,6 @@ private void setupTesting(Project project,
var unitTest = extension.getUnitTest();

var tasks = project.getTasks();
var layout = project.getLayout();
var configurations = project.getConfigurations();
var dependencyFactory = project.getDependencyFactory();

Expand Down Expand Up @@ -547,19 +564,33 @@ private void setupTesting(Project project,

// The FML JUnit plugin uses this system property to read a
// file containing the program arguments needed to launch
task.systemProperty("fml.junit.argsfile", RunUtils.escapeJvmArg(fmlJunitArgsFile.get().getAsFile().getAbsolutePath()));
task.systemProperty("fml.junit.argsfile", fmlJunitArgsFile.get().getAsFile().getAbsolutePath());
task.jvmArgs(RunUtils.escapeJvmArg(RunUtils.getArgFileParameter(testVmArgsFile.get())));

var modFoldersProvider = RunUtils.getGradleModFoldersProvider(project, project.provider(extension::getMods), true);
task.getJvmArgumentProviders().add(modFoldersProvider);
});

var test = tasks.named("test", Test.class);
// This is quite stupid... Test doesn't have a property for game directory, so we need to afterEvaluate it.
// Test tasks don't have a provider-based property for working directory, so we need to afterEvaluate it.
project.afterEvaluate(p -> {
test.configure(t -> {
t.setWorkingDir(unitTest.getGameDirectory());
tasks.withType(Test.class).configureEach(task -> {

This comment has been minimized.

Copy link
@Technici4n

Technici4n Jun 3, 2024

Member

I doubt that we want to configure other random test tasks?

task.setWorkingDir(unitTest.getGameDirectory());
});

// Configure IntelliJ default JUnit parameters, which is used when the user configures IJ to run tests natively
var intelliJRunConfigurations = getIntelliJRunConfigurations(p);
if (intelliJRunConfigurations != null) {
var outputDirectory = RunUtils.getIntellijOutputDirectory(p);
intelliJRunConfigurations.defaults(JUnit.class, jUnitDefaults -> {
jUnitDefaults.setVmParameters(
RunUtils.escapeJvmArg("-Dfml.junit.argsfile=" + fmlJunitArgsFile.get().getAsFile().getAbsolutePath())
+ " "
+ RunUtils.escapeJvmArg(RunUtils.getArgFileParameter(testVmArgsFile.get()))
+ " "
+ RunUtils.escapeJvmArg(RunUtils.getIdeaModFoldersProvider(p, outputDirectory, unitTest.getTestedMod().map(Set::of), true).getArgument())
);
});
}
});
}

Expand Down

0 comments on commit 7ed8494

Please sign in to comment.