Improvements to test and IDE runs (#1252)

* Improves to test and IDE runs

* Use correct sourceset
This commit is contained in:
modmuss
2025-01-22 22:05:55 +00:00
committed by GitHub
parent 3ee1372feb
commit 362fc98c47
4 changed files with 42 additions and 7 deletions

View File

@@ -79,4 +79,14 @@ public interface GameTestSettings {
* <p>Default: true
*/
Property<Boolean> getClearRunDirectory();
/**
* Contains a string property representing the username to use for the client side game tests.
*
* <p>This only works when {@link #getEnableClientGameTests()} is enabled.
*
* <p>Default: Player0
*/
@Optional
Property<String> getUsername();
}

View File

@@ -46,7 +46,7 @@ abstract class FabricApiAbstractSourceSet {
protected abstract String getSourceSetName();
protected void configureSourceSet(Property<String> modId, boolean isClient) {
protected SourceSet configureSourceSet(Property<String> modId, boolean isClient) {
final LoomGradleExtension extension = LoomGradleExtension.get(getProject());
final SourceSet mainSourceSet = SourceSetHelper.getMainSourceSet(getProject());
@@ -54,18 +54,18 @@ abstract class FabricApiAbstractSourceSet {
SourceSetContainer sourceSets = SourceSetHelper.getSourceSets(getProject());
// Create the new datagen sourceset, depend on the main or client sourceset.
SourceSet dataGenSourceSet = sourceSets.create(getSourceSetName(), sourceSet -> {
dependsOn(sourceSet, mainSourceSet);
// Create the new sourceset, depend on the main or client sourceset.
SourceSet sourceSet = sourceSets.create(getSourceSetName(), ss -> {
dependsOn(ss, mainSourceSet);
if (isClientAndSplit) {
dependsOn(sourceSet, SourceSetHelper.getSourceSetByName(MinecraftSourceSets.Split.CLIENT_ONLY_SOURCE_SET_NAME, getProject()));
dependsOn(ss, SourceSetHelper.getSourceSetByName(MinecraftSourceSets.Split.CLIENT_ONLY_SOURCE_SET_NAME, getProject()));
}
});
modId.convention(getProject().provider(() -> {
try {
final FabricModJson fabricModJson = FabricModJsonFactory.createFromSourceSetsNullable(getProject(), dataGenSourceSet);
final FabricModJson fabricModJson = FabricModJsonFactory.createFromSourceSetsNullable(getProject(), sourceSet);
if (fabricModJson == null) {
throw new RuntimeException("Could not find a fabric.mod.json file in the data source set or a value for DataGenerationSettings.getModId()");
@@ -83,6 +83,8 @@ abstract class FabricApiAbstractSourceSet {
});
extension.createRemapConfigurations(sourceSets.getByName(getSourceSetName()));
return sourceSet;
}
private static void extendsFrom(Project project, String name, String extendsFrom) {

View File

@@ -24,6 +24,7 @@
package net.fabricmc.loom.configuration.fabricapi;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
@@ -36,6 +37,7 @@ import org.gradle.api.Project;
import org.gradle.api.file.RegularFileProperty;
import org.gradle.api.tasks.Delete;
import org.gradle.api.tasks.OutputFile;
import org.gradle.api.tasks.SourceSet;
import org.gradle.api.tasks.TaskAction;
import org.gradle.api.tasks.TaskContainer;
@@ -45,6 +47,7 @@ import net.fabricmc.loom.configuration.ide.RunConfigSettings;
import net.fabricmc.loom.task.AbstractLoomTask;
import net.fabricmc.loom.task.LoomTasks;
import net.fabricmc.loom.util.Constants;
import net.fabricmc.loom.util.gradle.SourceSetHelper;
public abstract class FabricApiTesting extends FabricApiAbstractSourceSet {
@Inject
@@ -69,11 +72,16 @@ public abstract class FabricApiTesting extends FabricApiAbstractSourceSet {
settings.getEnableClientGameTests().convention(true);
settings.getEula().convention(false);
settings.getClearRunDirectory().convention(true);
settings.getUsername().convention("Player0");
action.execute(settings);
final SourceSet testSourceSet;
if (settings.getCreateSourceSet().get()) {
configureSourceSet(settings.getModId(), true);
testSourceSet = configureSourceSet(settings.getModId(), true);
} else {
testSourceSet = SourceSetHelper.getMainSourceSet(getProject());
}
Consumer<RunConfigSettings> configureBase = run -> {
@@ -94,10 +102,23 @@ public abstract class FabricApiTesting extends FabricApiAbstractSourceSet {
}
if (settings.getEnableClientGameTests().get()) {
// Not ideal as there may be multiple resources directories, if this isnt correct the mod will need to override this.
final File resourcesDir = testSourceSet.getResources().getFiles().stream().findAny().orElse(null);
RunConfigSettings clientGameTest = extension.getRunConfigs().create("clientGameTest", run -> {
run.inherit(extension.getRunConfigs().getByName("client"));
run.property("fabric.client.gametest");
if (resourcesDir != null) {
run.property("fabric.client.gametest.testModResourcesPath", resourcesDir.getAbsolutePath());
}
run.runDir("build/run/clientGameTest");
if (settings.getUsername().isPresent()) {
run.programArgs("--username", settings.getUsername().get());
}
configureBase.accept(run);
});

View File

@@ -110,6 +110,8 @@ public abstract class IdeaSyncTask extends AbstractLoomTask {
irc.getExcludedLibraryPaths().set(excludedLibraryPaths);
irc.getLaunchFile().set(runConfigFile);
configs.add(irc);
settings.makeRunDir();
}
return configs;