FabricMC / fabric-loom

Gradle build system plugin used to automate the setup of a minecraft mod development environment.
MIT License
223 stars 194 forks source link

Improve test-classpath configuration for split environment #1060

Open LCLPYT opened 4 months ago

LCLPYT commented 4 months ago

Context

I recently wanted to add unit tests to my mod. In the project, loom is configured with splitEnvironmentSourceSets().

When trying to run the gradle 'test' task, I get the following error:

Minecraft game provider couldn't locate the game! The game may be absent from the class path, lacks some expected files, suffers from jar corruption or is of an unsupported variety/version.

The issue is, that the game common and environment jars are not in the class path. I fixed the issue by manually adding this to my buildscript:

configurations {
    // common
    testCompileClasspath.extendsFrom(minecraftCommonNamedCompile)
    testRuntimeClasspath.extendsFrom(minecraftCommonNamedRuntime)
    // client only
    testCompileClasspath.extendsFrom(minecraftClientOnlyNamedCompile)
    testRuntimeClasspath.extendsFrom(minecraftClientOnlyNamedRuntime)
}

Now, game provider works, but this error occurs instead:

...
Caused by: java.lang.IllegalArgumentException: The specified resource 'fabric-networking-api-v1.client.mixins.json' was invalid or could not be read
    at org.spongepowered.asm.mixin.transformer.MixinConfig.create(MixinConfig.java:1290)
    at org.spongepowered.asm.mixin.transformer.Config.create(Config.java:148)
    ... 41 more

The client mixin files cannot be found (the actual file is different sometimes). I fixed this by adding this to my build.gradle:

configurations {
    testRuntimeClasspath.extendsFrom(modRuntimeClasspathClientMapped)
}

Then, everything is working as expected for my client+server project.

For server-only projects

One of my other mods is configured with serverOnlyMinecraftJar(). When executing the unit tests, the same game provider error occurs. I fixed it with:

configurations {
    testCompileClasspath.extendsFrom(minecraftCommonNamedCompile)
    testRuntimeClasspath.extendsFrom(minecraftCommonNamedRuntime)
}

However, the error with the mixins does not occur.

Issue

This manual classpath configuration is a little confusing for mod developers who want to test their code with unit tests, especially when they don't know Gradle well...

Maybe loom could automatically do this configuration for projects with splitEnvironmentSourceSets() or serverOnlyMinecraftJar()? I am pretty sure that clientOnlyMinecraftJar() needs extra configuration too. At the very least, I think this should be documented somewhere.

Cheers