Skip to content

Commit

Permalink
test(FabricMC#894): complete test bench and provide necessary test mo…
Browse files Browse the repository at this point in the history
…ds resources
  • Loading branch information
DavidCWQ committed Mar 3, 2024
1 parent 85bda08 commit 2680bbc
Show file tree
Hide file tree
Showing 7 changed files with 187 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,10 @@ boolean addParent(ModCandidate parent) {
return true;
}

public void testAddParent(ModCandidate parent) {
addParent(parent);
}

public int getMinNestLevel() {
return minNestLevel;
}
Expand Down
117 changes: 88 additions & 29 deletions src/test/java/net/fabricmc/test/LogProvidedModsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,29 @@
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import net.fabricmc.loader.impl.discovery.ModCandidate;
import net.fabricmc.loader.impl.metadata.DependencyOverrides;
import net.fabricmc.loader.impl.metadata.LoaderModMetadata;
import net.fabricmc.loader.impl.metadata.ModMetadataParser;
import net.fabricmc.loader.impl.metadata.ParseMetadataException;
import net.fabricmc.loader.impl.metadata.VersionOverrides;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

Expand All @@ -38,18 +51,31 @@

public class LogProvidedModsTest {
private LogHandler logHandler;
private static Path metadataPath;
@BeforeAll
public static void setupPaths() {
Path testLocation = new File(System.getProperty("user.dir")).toPath()
.resolve("src")
.resolve("test")
.resolve("resources")
.resolve("testing");
metadataPath = testLocation.resolve("metadata");
}

/*
* Calls dumpModsHavingProvider using reflection, so we don't need to make the class
* and method public.
*/
private static LoaderModMetadata parseMetadata(Path path) throws ParseMetadataException, IOException {
try (InputStream is = Files.newInputStream(path)) {
return ModMetadataParser.parseMetadata(is, "null", Collections.emptyList(),
new VersionOverrides(),
new DependencyOverrides(Paths.get("null")), false);
}
}
private static void dumpModsHavingProvider(List<ModCandidate> LoadedMods) {
try {
Method method = FabricLoaderImpl.class.getDeclaredMethod("dumpModsHavingProvider", List.class);
method.setAccessible(true);
method.invoke(FabricLoaderImpl.INSTANCE, LoadedMods);
} catch (NoSuchMethodException | IllegalArgumentException | IllegalAccessException
| InvocationTargetException e) {
| InvocationTargetException e) {
throw new RuntimeException(e);
}
}
Expand All @@ -59,39 +85,72 @@ private static void dumpModsHavingProvider(List<ModCandidate> LoadedMods) {
*/
@BeforeEach
public void setUp() {
logHandler = mock();
// Create a mock of the class that contains function log
logHandler = mock(LogHandler.class);
Mockito.when(logHandler.shouldLog(Mockito.any(), Mockito.any())).thenReturn(true);
Mockito.doNothing().when(logHandler).log(Mockito.anyLong(), Mockito.any(), Mockito.any(), Mockito.any(),
Mockito.any(), Mockito.anyBoolean(), Mockito.anyBoolean());
Mockito.doNothing().when(logHandler).log(Mockito.anyLong(), Mockito.any(), Mockito.any(),
Mockito.any(), Mockito.any(), Mockito.anyBoolean(), Mockito.anyBoolean());
Log.init(logHandler);
}

/*
* Test that the log handler is called with the correct message when there are
* non-fabric mods found.
* nested mods with providers found.
*/
@Test
public void testLogNonFabricMods() {
ModCandidate testMode1 = ModCandidate.createTestData(null, null,
-1, null, false, Collections.emptyList());
ModCandidate testMode2 = ModCandidate.createTestData(null, null,
-1, null, false, Collections.emptyList());
ModCandidate testMode3 = ModCandidate.createTestData(null, null,
-1, null, false, Collections.emptyList());


List<ModCandidate> LoadedMods = new ArrayList<>();
LoadedMods.add(testMode1);
LoadedMods.add(testMode2);
LoadedMods.add(testMode3);
@DisplayName("Log nested mods that have providers")
public void testModsHavingProvider() throws ParseMetadataException, IOException {

LoaderModMetadata metadata1 = parseMetadata(metadataPath.resolve("fabric.test.mod1.json"));
LoaderModMetadata metadata2 = parseMetadata(metadataPath.resolve("fabric.test.mod2.json"));
LoaderModMetadata metadata3 = parseMetadata(metadataPath.resolve("fabric.test.mod3.json"));
LoaderModMetadata metadata4 = parseMetadata(metadataPath.resolve("fabric.test.mod4.json"));
LoaderModMetadata metadata5 = parseMetadata(metadataPath.resolve("fabric.test.mod5.json"));

// Create nested testing mods
List<ModCandidate> LoadedMods;

{
ModCandidate testMode1 = ModCandidate.createTestData(null, null,
-1, metadata1, false, Collections.emptyList());
Collection<ModCandidate> nestedMod2 = new ArrayList<>();
nestedMod2.add(testMode1);
ModCandidate testMode2 = ModCandidate.createTestData(null, null,
-1, metadata2, false, nestedMod2);
for (ModCandidate child : testMode2.getNestedMods()) {
child.testAddParent(testMode2);
}
ModCandidate testMode3 = ModCandidate.createTestData(null, null,
-1, metadata3, false, Collections.emptyList());
Collection<ModCandidate> nestedMod4 = new ArrayList<>();
nestedMod4.add(testMode2);
nestedMod4.add(testMode3);
ModCandidate testMode4 = ModCandidate.createTestData(null, null,
-1, metadata4, false, nestedMod4);
for (ModCandidate child : testMode4.getNestedMods()) {
child.testAddParent(testMode4);
}
ModCandidate testMode5 = ModCandidate.createTestData(null, null,
-1, metadata5, false, Collections.emptyList());

LoadedMods = new ArrayList<>();
LoadedMods.add(testMode1);
LoadedMods.add(testMode2);
LoadedMods.add(testMode3);
LoadedMods.add(testMode4);
LoadedMods.add(testMode5);
}

dumpModsHavingProvider(LoadedMods);

String expectedLog = "Found 2 loaded mods that have providers:"
+ System.lineSeparator() + "\t- mod1 0.1.0 (in mod x 2.2.7)"
+ System.lineSeparator() + "\t- mod3 9.1.1 (in mod y 1.1.1)";
String expectedLog = "Found 3 loaded mods that have providers:"
+ System.lineSeparator() + "\t- mod1 0.1.0 (in mod4 1.1.1)"
+ System.lineSeparator() + "\t- mod2 2.2.7 (in mod4 1.1.1)"
+ System.lineSeparator() + "\t- mod3 9.1.1 (in mod4 1.1.1)";

Mockito.verify(logHandler, Mockito.times(1)).log(Mockito.anyLong(), Mockito.any(), Mockito.any(),
eq(expectedLog), Mockito.any(), Mockito.anyBoolean(), Mockito.anyBoolean());
}
Mockito.verify(logHandler, Mockito.times(1)).log(
Mockito.anyLong(), Mockito.any(), Mockito.any(),
eq(expectedLog),
Mockito.any(), Mockito.anyBoolean(), Mockito.anyBoolean());
}
}
19 changes: 19 additions & 0 deletions src/test/resources/testing/metadata/fabric.test.mod1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"schemaVersion": 1,
"id": "mod1",
"name": "Test Mod1",
"version": "0.1.0",
"entrypoints": {
"preLaunch": [
"net.fabricmc.test.TestMod"
],
"main": [
"net.fabricmc.test.TestMod"
],
"test:testing": [
"net.fabricmc.test.EntrypointTest::instanceEntry",
"net.fabricmc.test.EntrypointTest::staticEntry",
"net.fabricmc.test.EntrypointTest::FIELD_ENTRY"
]
}
}
19 changes: 19 additions & 0 deletions src/test/resources/testing/metadata/fabric.test.mod2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"schemaVersion": 1,
"id": "mod2",
"name": "Test Mod2",
"version": "2.2.7",
"entrypoints": {
"preLaunch": [
"net.fabricmc.test.TestMod"
],
"main": [
"net.fabricmc.test.TestMod"
],
"test:testing": [
"net.fabricmc.test.EntrypointTest::instanceEntry",
"net.fabricmc.test.EntrypointTest::staticEntry",
"net.fabricmc.test.EntrypointTest::FIELD_ENTRY"
]
}
}
19 changes: 19 additions & 0 deletions src/test/resources/testing/metadata/fabric.test.mod3.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"schemaVersion": 1,
"id": "mod3",
"name": "Test Mod3",
"version": "9.1.1",
"entrypoints": {
"preLaunch": [
"net.fabricmc.test.TestMod"
],
"main": [
"net.fabricmc.test.TestMod"
],
"test:testing": [
"net.fabricmc.test.EntrypointTest::instanceEntry",
"net.fabricmc.test.EntrypointTest::staticEntry",
"net.fabricmc.test.EntrypointTest::FIELD_ENTRY"
]
}
}
19 changes: 19 additions & 0 deletions src/test/resources/testing/metadata/fabric.test.mod4.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"schemaVersion": 1,
"id": "mod4",
"name": "Test Mod4",
"version": "1.1.1",
"entrypoints": {
"preLaunch": [
"net.fabricmc.test.TestMod"
],
"main": [
"net.fabricmc.test.TestMod"
],
"test:testing": [
"net.fabricmc.test.EntrypointTest::instanceEntry",
"net.fabricmc.test.EntrypointTest::staticEntry",
"net.fabricmc.test.EntrypointTest::FIELD_ENTRY"
]
}
}
19 changes: 19 additions & 0 deletions src/test/resources/testing/metadata/fabric.test.mod5.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"schemaVersion": 1,
"id": "mod5",
"name": "Test Mod5",
"version": "0.0.0",
"entrypoints": {
"preLaunch": [
"net.fabricmc.test.TestMod"
],
"main": [
"net.fabricmc.test.TestMod"
],
"test:testing": [
"net.fabricmc.test.EntrypointTest::instanceEntry",
"net.fabricmc.test.EntrypointTest::staticEntry",
"net.fabricmc.test.EntrypointTest::FIELD_ENTRY"
]
}
}

0 comments on commit 2680bbc

Please sign in to comment.