Skip to content

Commit

Permalink
Added two stubs for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
shartte committed May 25, 2024
1 parent edf62fd commit 292f770
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 1 deletion.
8 changes: 8 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
Expand Down Expand Up @@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ public class ModDevPlugin implements Plugin<Project> {

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);
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}


}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}


}
}

0 comments on commit 292f770

Please sign in to comment.