From 292f770e96e6d381349930c9f93e08938ff5301b Mon Sep 17 00:00:00 2001 From: Sebastian Hartte Date: Sat, 25 May 2024 19:11:32 +0200 Subject: [PATCH] Added two stubs for tests --- build.gradle | 8 +++ .../moddevgradle/internal/ModDevPlugin.java | 4 +- .../functional/GroovyScriptTest.java | 63 ++++++++++++++++++ .../functional/KotlinScriptTest.java | 65 +++++++++++++++++++ 4 files changed, 139 insertions(+), 1 deletion(-) create mode 100644 src/test/java/net/neoforged/moddevgradle/functional/GroovyScriptTest.java create mode 100644 src/test/java/net/neoforged/moddevgradle/functional/KotlinScriptTest.java diff --git a/build.gradle b/build.gradle index 78494aab..57de7504 100644 --- a/build.gradle +++ b/build.gradle @@ -65,6 +65,8 @@ dependencies { testImplementation(enforcedPlatform("org.junit:junit-bom:5.10.2")) testImplementation 'org.junit.jupiter:junit-jupiter-api' testImplementation 'org.junit.jupiter:junit-jupiter-params' + testImplementation 'org.assertj:assertj-core:3.25.1' + testImplementation gradleTestKit() testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine' testRuntimeOnly 'org.junit.platform:junit-platform-launcher' } @@ -119,3 +121,9 @@ gradlePlugin { test { useJUnitPlatform() } + +// The java-gradle-plugin ignores the java8 source-set we added to the main source sets classpath +// For testkit tests to work, this needs to be present +tasks.withType(PluginUnderTestMetadata).configureEach { + it.pluginClasspath.from(sourceSets.java8.output.classesDirs) +} diff --git a/src/main/java/net/neoforged/moddevgradle/internal/ModDevPlugin.java b/src/main/java/net/neoforged/moddevgradle/internal/ModDevPlugin.java index dc48a475..093b74cb 100644 --- a/src/main/java/net/neoforged/moddevgradle/internal/ModDevPlugin.java +++ b/src/main/java/net/neoforged/moddevgradle/internal/ModDevPlugin.java @@ -57,6 +57,8 @@ public class ModDevPlugin implements Plugin { private static final String JAR_JAR_GROUP = "jarjar"; + private static final String TASK_GROUP = "mod development"; + @Override public void apply(Project project) { project.getPlugins().apply(JavaLibraryPlugin.class); @@ -344,7 +346,7 @@ public void apply(Project project) { idePostSyncTask.configure(task -> task.dependsOn(prepareRunTask)); tasks.register(InternalModelHelper.nameOfRun(run, "run", ""), RunGameTask.class, task -> { - task.setGroup("neoforge moddev"); + task.setGroup(TASK_GROUP); // Launch with the Java version used in the project var toolchainService = ExtensionUtils.findExtension(project, "javaToolchains", JavaToolchainService.class); diff --git a/src/test/java/net/neoforged/moddevgradle/functional/GroovyScriptTest.java b/src/test/java/net/neoforged/moddevgradle/functional/GroovyScriptTest.java new file mode 100644 index 00000000..c27a1666 --- /dev/null +++ b/src/test/java/net/neoforged/moddevgradle/functional/GroovyScriptTest.java @@ -0,0 +1,63 @@ +package net.neoforged.moddevgradle.functional; + +import org.gradle.testkit.runner.BuildResult; +import org.gradle.testkit.runner.GradleRunner; +import org.gradle.testkit.runner.TaskOutcome; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class GroovyScriptTest { + @TempDir + File testProjectDir; + private File settingsFile; + private File buildFile; + + @BeforeEach + public void setup() { + settingsFile = new File(testProjectDir, "settings.gradle"); + buildFile = new File(testProjectDir, "build.gradle"); + } + + @Test + public void testApplyInEmptyProject() throws IOException { + writeFile(settingsFile, "rootProject.name = 'hello-world'"); + String buildFileContent = """ + plugins { + id "net.neoforged.moddev" + } + """; + writeFile(buildFile, buildFileContent); + + BuildResult result = GradleRunner.create() + .withPluginClasspath() + .withProjectDir(testProjectDir) + .withArguments("tasks", "--all") + .build(); + + assertThat(result.getOutput()).contains("createMinecraftArtifacts"); + assertEquals(TaskOutcome.SUCCESS, result.task(":tasks").getOutcome()); + } + + private void writeFile(File destination, String content) throws IOException { + BufferedWriter output = null; + try { + output = new BufferedWriter(new FileWriter(destination)); + output.write(content); + } finally { + if (output != null) { + output.close(); + } + } + + + } +} diff --git a/src/test/java/net/neoforged/moddevgradle/functional/KotlinScriptTest.java b/src/test/java/net/neoforged/moddevgradle/functional/KotlinScriptTest.java new file mode 100644 index 00000000..d6106fac --- /dev/null +++ b/src/test/java/net/neoforged/moddevgradle/functional/KotlinScriptTest.java @@ -0,0 +1,65 @@ +package net.neoforged.moddevgradle.functional; + +import org.gradle.testkit.runner.BuildResult; +import org.gradle.testkit.runner.GradleRunner; +import org.gradle.testkit.runner.TaskOutcome; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class KotlinScriptTest { + @TempDir + File testProjectDir; + private File settingsFile; + private File buildFile; + + @BeforeEach + public void setup() { + settingsFile = new File(testProjectDir, "settings.gradle.kts"); + buildFile = new File(testProjectDir, "build.gradle.kts"); + } + + @Test + public void testApplyInEmptyProject() throws IOException { + writeFile(settingsFile, """ + rootProject.name = "hello-world"; + """); + String buildFileContent = """ + plugins { + id("net.neoforged.moddev") + } + """; + writeFile(buildFile, buildFileContent); + + BuildResult result = GradleRunner.create() + .withPluginClasspath() + .withProjectDir(testProjectDir) + .withArguments("tasks", "--all") + .build(); + + assertThat(result.getOutput()).contains("createMinecraftArtifacts"); + assertEquals(TaskOutcome.SUCCESS, result.task(":tasks").getOutcome()); + } + + private void writeFile(File destination, String content) throws IOException { + BufferedWriter output = null; + try { + output = new BufferedWriter(new FileWriter(destination)); + output.write(content); + } finally { + if (output != null) { + output.close(); + } + } + + + } +}