Skip to content

Commit

Permalink
More work
Browse files Browse the repository at this point in the history
  • Loading branch information
modmuss50 committed Jan 1, 2025
1 parent 490b18d commit 9563571
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,6 @@ public interface GameTestSettings {
*/
Property<Boolean> getCreateSourceSet();

/**
* Contains a boolean indicating whether a run configuration should be created for the tests.
*/
Property<Boolean> getCreateRunConfigurations();

/**
* Contains a string property representing the mod ID associated with the tests.
*
Expand All @@ -70,9 +65,18 @@ public interface GameTestSettings {
/**
* Contains a boolean property indicating whether the eula has been accepted. By enabling this you agree to the Minecraft EULA located at <a href="https://aka.ms/MinecraftEULA">https://aka.ms/MinecraftEULA</a>.
*
* <p>This only works when {@link #getCreateRunConfigurations()} is enabled.
* <p>This only works when {@link #getEnableClientGameTests()} is enabled.
*
* <p>Default: false
*/
Property<Boolean> getEula();

/**
* Contains a boolean property indicating whether the run directories should be cleared before running the tests.
*
* <p>This only works when {@link #getEnableClientGameTests()} is enabled.
*
* <p>Default: true
*/
Property<Boolean> getClearRunDirectory();
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.gradle.api.Action;
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.TaskAction;
import org.gradle.api.tasks.TaskContainer;
Expand All @@ -42,6 +43,8 @@
import net.fabricmc.loom.api.fabricapi.GameTestSettings;
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;

public abstract class FabricApiTesting extends FabricApiAbstractSourceSet {
@Inject
Expand All @@ -62,41 +65,59 @@ void configureTests(Action<GameTestSettings> action) {

GameTestSettings settings = getProject().getObjects().newInstance(GameTestSettings.class);
settings.getCreateSourceSet().convention(false);
settings.getCreateRunConfigurations().convention(true);
settings.getEnableGameTests().convention(true);
settings.getEnableClientGameTests().convention(true);
settings.getEula().convention(false);
settings.getClearRunDirectory().convention(true);

action.execute(settings);

if (settings.getCreateSourceSet().get()) {
configureSourceSet(settings.getModId(), true);
}

if (settings.getCreateRunConfigurations().get()) {
Consumer<RunConfigSettings> configureBase = run -> {
if (settings.getCreateSourceSet().get()) {
run.source(getSourceSetName());
}

run.runDir("build/gametest");
};
Consumer<RunConfigSettings> configureBase = run -> {
if (settings.getCreateSourceSet().get()) {
run.source(getSourceSetName());
}
};

extension.getRunConfigs().create("gameTest", run -> {
if (settings.getEnableGameTests().get()) {
RunConfigSettings gameTest = extension.getRunConfigs().create("gameTest", run -> {
run.inherit(extension.getRunConfigs().getByName("server"));
run.property("fabric-api.gametest");
run.runDir("build/run/gameTest");
configureBase.accept(run);
});

RunConfigSettings runConfigSettings = extension.getRunConfigs().create("clientGameTest", run -> {
tasks.named("test", task -> task.dependsOn(LoomTasks.getRunConfigTaskName(gameTest)));
}

if (settings.getEnableClientGameTests().get()) {
RunConfigSettings clientGameTest = extension.getRunConfigs().create("clientGameTest", run -> {
run.inherit(extension.getRunConfigs().getByName("client"));
run.property("fabric.client.gametest");
run.runDir("build/run/clientGameTest");
configureBase.accept(run);
});

if (settings.getClearRunDirectory().get()) {
var deleteGameTestRunDir = tasks.register("deleteGameTestRunDir", Delete.class, task -> {
task.setGroup(Constants.TaskGroup.FABRIC);
task.delete(clientGameTest.getRunDir());
});

tasks.named(LoomTasks.getRunConfigTaskName(clientGameTest), task -> task.dependsOn(deleteGameTestRunDir));
}

if (settings.getEula().get()) {
var acceptEula = tasks.register("acceptGameTestEula", AcceptEulaTask.class, task -> {
task.getEulaFile().set(getProject().file(runConfigSettings.getRunDir() + "/eula.txt"));
task.getEulaFile().set(getProject().file(clientGameTest.getRunDir() + "/eula.txt"));

if (settings.getClearRunDirectory().get()) {
// Ensure that the eula is accepted after the run directory is cleared
task.dependsOn(tasks.named("deleteGameTestRunDir"));
}
});

tasks.named("configureLaunch", task -> task.dependsOn(acceptEula));
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/fabricmc/loom/task/LoomTasks.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ private void registerIDETasks() {
});
}

private static String getRunConfigTaskName(RunConfigSettings config) {
public static String getRunConfigTaskName(RunConfigSettings config) {
String configName = config.getName();
return "run" + configName.substring(0, 1).toUpperCase() + configName.substring(1);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ class DataGenerationTest extends Specification implements GradleProjectTestTrait
''' + DEPENDENCIES
when:
def result = gradle.run(task: "runClientGameTest")
def eula = new File(gradle.projectDir, "build/gametest/eula.txt")
def eula = new File(gradle.projectDir, "build/run/clientGameTest/eula.txt")
then:
result.task(":runClientGameTest").outcome == SUCCESS
Expand Down

0 comments on commit 9563571

Please sign in to comment.