Skip to content

Commit

Permalink
Improvements to test and IDE runs (#1252)
Browse files Browse the repository at this point in the history
* Improves to test and IDE runs

* Use correct sourceset
  • Loading branch information
modmuss50 authored Jan 22, 2025
1 parent 3ee1372 commit 362fc98
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,26 +46,26 @@ 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());

final boolean isClientAndSplit = extension.areEnvironmentSourceSetsSplit() && isClient;

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()");
Expand All @@ -83,6 +83,8 @@ protected void configureSourceSet(Property<String> modId, boolean isClient) {
});

extension.createRemapConfigurations(sourceSets.getByName(getSourceSetName()));

return sourceSet;
}

private static void extendsFrom(Project project, String name, String extendsFrom) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -36,6 +37,7 @@
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;

Expand All @@ -45,6 +47,7 @@
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
Expand All @@ -69,11 +72,16 @@ void configureTests(Action<GameTestSettings> action) {
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 -> {
Expand All @@ -94,10 +102,23 @@ void configureTests(Action<GameTestSettings> action) {
}

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);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ private List<IntelijRunConfig> getRunConfigs() throws IOException {
irc.getExcludedLibraryPaths().set(excludedLibraryPaths);
irc.getLaunchFile().set(runConfigFile);
configs.add(irc);

settings.makeRunDir();
}

return configs;
Expand Down

0 comments on commit 362fc98

Please sign in to comment.