From 9cd61b99f1f5bc229a34298ad2ae3b1cd89603d1 Mon Sep 17 00:00:00 2001 From: LexManos Date: Thu, 12 Nov 2026 13:11:00 -0800 Subject: [PATCH] Add junit tests for fmlloader Topological Sort Co-authored-by: ZZZank <47418975+ZZZank@users.noreply.github.com> --- fmlloader-tests/build.gradle | 14 ++ .../fml/loading/test/TopologicalSortTest.java | 172 ++++++++++++++++++ fmlloader/build.gradle | 9 - gradle/libs.versions.toml | 8 + settings.gradle | 1 + 5 files changed, 195 insertions(+), 9 deletions(-) create mode 100644 fmlloader-tests/build.gradle create mode 100644 fmlloader-tests/src/test/java/net/minecraftforge/fml/loading/test/TopologicalSortTest.java diff --git a/fmlloader-tests/build.gradle b/fmlloader-tests/build.gradle new file mode 100644 index 0000000000..bd41b1c573 --- /dev/null +++ b/fmlloader-tests/build.gradle @@ -0,0 +1,14 @@ +plugins { + id 'net.minecraftforge.forge.build.convention' + id 'jvm-test-suite' +} + +gradleutils.displayName = 'FMLLoader-Tests' +description = 'JUnit tests for FMLLoader, not meant for distribution' + +dependencies { + testImplementation(project(':fmlloader')) + testImplementation(libs.guava) + testImplementation(libs.junit.api) + testRuntimeOnly(libs.bundles.junit.runtime) +} \ No newline at end of file diff --git a/fmlloader-tests/src/test/java/net/minecraftforge/fml/loading/test/TopologicalSortTest.java b/fmlloader-tests/src/test/java/net/minecraftforge/fml/loading/test/TopologicalSortTest.java new file mode 100644 index 0000000000..68a8c12258 --- /dev/null +++ b/fmlloader-tests/src/test/java/net/minecraftforge/fml/loading/test/TopologicalSortTest.java @@ -0,0 +1,172 @@ +/* + * Copyright (c) Forge Development LLC and contributors + * SPDX-License-Identifier: LGPL-2.1-only + */ + +package net.minecraftforge.fml.loading.test; + +import com.google.common.graph.Graph; +import com.google.common.graph.GraphBuilder; +import com.google.common.graph.MutableGraph; + +import net.minecraftforge.fml.loading.toposort.CyclePresentException; +import net.minecraftforge.fml.loading.toposort.StronglyConnectedComponentDetector; +import net.minecraftforge.fml.loading.toposort.TopologicalSort; + +import java.util.Comparator; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +/** + * Tests for {@link TopologicalSort} and {@link StronglyConnectedComponentDetector} + */ +class TopologicalSortTest { + @Test + void resultProperlySorted() { + MutableGraph graph = GraphBuilder.directed().build(); + graph.putEdge("a", "b"); + graph.putEdge("a", "c"); + graph.putEdge("b", "c"); + var sorted = TopologicalSort.topologicalSort(graph, Comparator.naturalOrder()); + assertEquals(List.of("a", "b", "c"), sorted); + } + + @Test + void throwsOnCycle() { + MutableGraph graph = GraphBuilder.directed().build(); + graph.putEdge("a", "b"); + graph.putEdge("b", "a"); + + var exception = Assertions.assertThrows(CyclePresentException.class, () -> TopologicalSort.topologicalSort(graph, Comparator.naturalOrder())); + assertEquals(Set.of( + Set.of("a", "b") + ), exception.getCycles()); + } + + @Test + void simpleCycle() { + MutableGraph graph = GraphBuilder.directed().build(); + // a -> b -> c -> a + graph.putEdge("a", "b"); + graph.putEdge("b", "c"); + graph.putEdge("c", "a"); + + var cycles = cyclesOf(graph); + assertEquals(Set.of( + Set.of("a", "b", "c") + ), cycles); + } + + @Test + void noCycle() { + MutableGraph graph = GraphBuilder.directed().build(); + graph.putEdge("a", "b"); + graph.putEdge("a", "c"); + graph.putEdge("b", "d"); + graph.putEdge("c", "d"); + graph.putEdge("d", "e"); + + var cycles = cyclesOf(graph); + assertTrue(cycles.isEmpty()); + } + + /** + * Regression test: the previous implementation lowered the lowlink of a node from every already-visited + * successor, including successors whose component was already completed. That merged nodes into the wrong + * component, so a member of the real cycle ({@code c}) was missing from the reported cycle. + */ + @Test + void reportsAllMembersOfCycle() { + MutableGraph graph = GraphBuilder.directed().build(); + graph.addNode("a"); + graph.addNode("b"); + graph.addNode("c"); + graph.putEdge("a", "b"); + graph.putEdge("a", "c"); + graph.putEdge("b", "a"); + graph.putEdge("c", "b"); + + var cycles = cyclesOf(graph); + assertEquals(Set.of( + Set.of("a", "b", "c") + ), cycles); + } + + /** + * Regression test: the previous implementation invented a cycle out of {@code c -> d -> a}, which is a plain + * path, because it compared against the DFS depth instead of a discovery counter and ignored whether a + * successor was still on the stack. + */ + @Test + void doesNotInventCycles() { + MutableGraph graph = GraphBuilder.directed().build(); + graph.addNode("a"); + graph.addNode("b"); + graph.addNode("c"); + graph.addNode("d"); + graph.putEdge("a", "b"); + graph.putEdge("b", "a"); + graph.putEdge("c", "d"); + graph.putEdge("d", "a"); + + var cycles = cyclesOf(graph); + assertEquals(Set.of( + Set.of("a", "b") + ), cycles); + } + + /** + * Reproduces the shape of the mod graphs that reported bogus cycles in the wild: one real three-node cycle, + * with nodes that merely depend on members of the cycle and a node on a plain path into it. + * + *

Every reported component must be the real cycle itself; nodes such as {@code dependent_a} only depend on + * the cycle and must not be reported. + * + * @see related GitHub issue + */ + @Test + void reportsOnlyRealCycles() { + MutableGraph graph = GraphBuilder.directed().build(); + // Mirrors the insertion order of ModSorter: every mod is added as a node first, edges are added afterwards. + for (String mod : List.of("upstream", "cycle_a", "cycle_b", "dependent_a", "cycle_c", "dependent_b")) { + graph.addNode(mod); + } + // a -> b -> a + graph.putEdge("cycle_a", "cycle_b"); + graph.putEdge("cycle_b", "cycle_a"); + // a -> c -> b -> a + graph.putEdge("cycle_a", "cycle_c"); + graph.putEdge("cycle_c", "cycle_b"); + // dA -> b -> a - Valid + graph.putEdge("dependent_a", "cycle_b"); + // dB -> b -> a - Valid + graph.putEdge("dependent_b", "cycle_b"); + // u -> a -> b -> a - Not valid, but u is not part of the cycle so shouldn't be reported + graph.putEdge("upstream", "cycle_a"); + + var cycles = cyclesOf(graph); + assertEquals(Set.of( + Set.of("cycle_a", "cycle_b", "cycle_c") + ), cycles); + + var exception = Assertions.assertThrows(CyclePresentException.class, () -> TopologicalSort.topologicalSort(graph, Comparator.naturalOrder())); + assertEquals(Set.of( + Set.of("cycle_a", "cycle_b", "cycle_c") + ), exception.getCycles()); + } + + private static Set> cyclesOf(Graph graph) { + var detector = new StronglyConnectedComponentDetector<>(graph); + var connected = detector.getComponents(); + var cycles = new HashSet<>(connected); + cycles.removeIf(component -> component.size() < 2); + return cycles; + } +} + diff --git a/fmlloader/build.gradle b/fmlloader/build.gradle index d0499933d0..003af3e4d2 100644 --- a/fmlloader/build.gradle +++ b/fmlloader/build.gradle @@ -1,8 +1,6 @@ import net.minecraftforge.forgedev.legacy.tasks.JarJarMetadataOptions -import org.gradle.api.plugins.jvm.JvmTestSuite plugins { - id 'jvm-test-suite' id 'net.minecraftforge.forge.build.convention' alias libs.plugins.apt } @@ -38,13 +36,6 @@ dependencies { annotationProcessor(libs.bundles.log4j){ transitive = false } } -testing.suites.named('test', JvmTestSuite) { - dependencies { - compileOnly libs.jetbrains.annotations - runtimeOnly libs.bootstrap - } -} - convention.manifest('net/minecraftforge/fml/loading/', [ 'Automatic-Module-Name': 'net.minecraftforge.fmlloader', 'Forge-Module-Layer' : 'boot' diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 4ee9610224..9b0fa33c37 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -24,6 +24,7 @@ jarjar = "0.4.2" log4j = "2.26.0" lwjgl = "3.4.3" gradle = "9.7.1" # Gradle we bundle with the MDK +junit = "6.1.3" [libraries] # TODO [Forge][Buildscript We should not be needing to manually add transitive dependencies @@ -106,6 +107,12 @@ lwjgl-opengl = { module = "org.lwjgl:lwjgl-opengl", version.ref = "lwjgl" } lwjgl-stb = { module = "org.lwjgl:lwjgl-stb", version.ref = "lwjgl" } lwjgl-tinyfd = { module = "org.lwjgl:lwjgl-tinyfd", version.ref = "lwjgl" } + +junit-api = { module = "org.junit.jupiter:junit-jupiter-api", version.ref = "junit" } +junit-engine = { module = "org.junit.jupiter:junit-jupiter-engine", version.ref = "junit" } +junit-platform = { module = "org.junit.platform:junit-platform-launcher", version.ref = "junit" } + + [bundles] # Google's InMemory File System. Used by ForgeDev Tests for now, but could be useful for a lot of things. jimfs = ["guava", "failureaccess"] @@ -123,3 +130,4 @@ night-config = ["night-config-toml", "night-config-core"] jarjar = ["jarjar-fs", "jarjar-selector", "jarjar-meta"] log4j = ["log4j-api", "log4j-core"] lwjgl = ["lwjgl", "lwjgl-sdl", "lwjgl-opengl", "lwjgl-stb", "lwjgl-tinyfd"] +junit-runtime = ["junit-engine", "junit-platform"] \ No newline at end of file diff --git a/settings.gradle b/settings.gradle index f06b78d79e..5bd1de487b 100644 --- a/settings.gradle +++ b/settings.gradle @@ -18,6 +18,7 @@ plugins { rootProject.name = 'forge' include 'fmlloader' +include 'fmlloader-tests' include 'fmlcore' include 'mclanguage' include 'javafmllanguage'