-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
639 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package carpetextra; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class TestUtils { | ||
|
||
public static final Logger LOGGER = LoggerFactory.getLogger("CarpetExtraTest"); | ||
|
||
} |
80 changes: 80 additions & 0 deletions
80
src/gametest/java/carpetextra/testbootstrap/TestBootstraper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package carpetextra.testbootstrap; | ||
|
||
import java.io.IOException; | ||
import java.io.UncheckedIOException; | ||
import java.lang.reflect.Field; | ||
import java.util.HashMap; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
import java.util.stream.Stream; | ||
|
||
import com.google.common.reflect.ClassPath; | ||
import com.google.common.reflect.ClassPath.ClassInfo; | ||
|
||
import carpet.CarpetServer; | ||
import carpetextra.TestUtils; | ||
import net.minecraft.test.TestFunctions; | ||
|
||
public class TestBootstraper { | ||
static final String TEST_BASE_PACKAGE = "carpetextra.tests"; | ||
static { | ||
registerExtensionForCommands(); | ||
registerAllClasses(); | ||
} | ||
|
||
static Stream<ClassInfo> allTestClasses() { | ||
try { | ||
return ClassPath.from(TestBootstraper.class.getClassLoader()) | ||
.getAllClasses() | ||
.stream() | ||
.filter(clazz -> clazz.getPackageName().startsWith(TEST_BASE_PACKAGE) && clazz.isTopLevel()); | ||
} catch (IOException e) { | ||
throw new UncheckedIOException("Exception while listing Carpet Extra gametests", e); | ||
} | ||
} | ||
|
||
private static void registerAllClasses() { | ||
AtomicInteger count = new AtomicInteger(); | ||
allTestClasses() | ||
.map(cls -> { | ||
try { | ||
// cls.load() doesn't initialize it, making it not return annotations to the framework | ||
return Class.forName(cls.getName()); | ||
} catch (ClassNotFoundException e) { | ||
throw new IllegalStateException("Couldn't initialize test class we know is there", e); | ||
} | ||
}) | ||
.peek(__ -> count.setPlain(count.getPlain() + 1)) // technically shouldn't be used for this, but good enough | ||
.forEach(TestBootstraper::register); | ||
TestUtils.LOGGER.info("Registered " + count + " gametest classes for Carpet Extra"); | ||
} | ||
|
||
static void register(Class<?> test) { | ||
// HACK: add our modid to gametest api first, or it'll crash | ||
@SuppressWarnings("unchecked") | ||
class HackHolder { | ||
static final HashMap<Class<?>, String> IDS; | ||
static { | ||
HashMap<Class<?>, String> ids; | ||
try { | ||
Field f = Class.forName("net.fabricmc.fabric.impl.gametest.FabricGameTestModInitializer").getDeclaredField("GAME_TEST_IDS"); | ||
f.setAccessible(true); | ||
ids = (HashMap<Class<?>, String>)f.get(null); | ||
} catch (ReflectiveOperationException e) { | ||
TestUtils.LOGGER.warn("Hack to register gametest into fabric gametest failed, expect a crash!"); | ||
ids = null; | ||
} | ||
IDS = ids; | ||
} | ||
} | ||
|
||
if (HackHolder.IDS != null) { | ||
HackHolder.IDS.put(test, "carpet-extra"); | ||
} | ||
|
||
TestFunctions.register(test); | ||
} | ||
|
||
static void registerExtensionForCommands() { | ||
CarpetServer.manageExtension(new TestsClassesCommandExtension()); | ||
} | ||
} |
110 changes: 110 additions & 0 deletions
110
src/gametest/java/carpetextra/testbootstrap/TestsClassesCommandExtension.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package carpetextra.testbootstrap; | ||
|
||
import static carpetextra.testbootstrap.TestBootstraper.TEST_BASE_PACKAGE; | ||
import static com.mojang.brigadier.arguments.StringArgumentType.getString; | ||
import static com.mojang.brigadier.arguments.StringArgumentType.word; | ||
import static net.minecraft.command.CommandSource.suggestMatching; | ||
import static net.minecraft.server.command.CommandManager.argument; | ||
import static net.minecraft.server.command.CommandManager.literal; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.Collection; | ||
import java.util.stream.Stream; | ||
|
||
import com.mojang.brigadier.CommandDispatcher; | ||
import com.mojang.brigadier.builder.LiteralArgumentBuilder; | ||
import com.mojang.brigadier.context.CommandContext; | ||
|
||
import carpet.CarpetExtension; | ||
import carpet.utils.Messenger; | ||
import carpetextra.TestUtils; | ||
import net.minecraft.command.CommandRegistryAccess; | ||
import net.minecraft.data.DataWriter; | ||
import net.minecraft.data.dev.NbtProvider; | ||
import net.minecraft.server.command.ServerCommandSource; | ||
import net.minecraft.test.TestFunctions; | ||
import net.minecraft.util.WorldSavePath; | ||
|
||
// to add a command to register test classes at runtime | ||
public class TestsClassesCommandExtension implements CarpetExtension { | ||
@Override | ||
public void registerCommands(CommandDispatcher<ServerCommandSource> dispatcher, CommandRegistryAccess commandBuildContext) { | ||
registerTestClassCommand(dispatcher); | ||
} | ||
|
||
public static void registerTestClassCommand(CommandDispatcher<ServerCommandSource> dispatcher) { | ||
LiteralArgumentBuilder<ServerCommandSource> command = literal("testhelper") | ||
.then(literal("class") | ||
.then(argument("class", word()) | ||
.suggests((c, b) -> suggestMatching(TestBootstraper.allTestClasses() | ||
.<String>mapMulti((clazz, stream) -> { | ||
if (!TestFunctions.testClassExists(clazz.getSimpleName())) | ||
stream.accept(clazz.getName()); | ||
}) | ||
.map(name -> name.substring(TEST_BASE_PACKAGE.length() + 1)) | ||
, b)) | ||
.executes(c -> { | ||
String className = getString(c, "class"); | ||
try { | ||
Class<?> testClass = Class.forName(TEST_BASE_PACKAGE + '.' + className); | ||
if (TestFunctions.testClassExists(testClass.getSimpleName())) { | ||
c.getSource().sendError(Messenger.c("r Reloading classes is not supported")); | ||
return 0; | ||
} | ||
TestBootstraper.register(testClass); | ||
c.getSource().sendMessage(Messenger.c(" Added test class " + className)); | ||
return 1; | ||
} catch (ClassNotFoundException e) { | ||
c.getSource().sendError(Messenger.c("r Failed to load class " + className)); | ||
return 0; | ||
} | ||
})) | ||
) | ||
.then(literal("convertnbt") | ||
.then(argument("structure", word()) | ||
.suggests((c, b) -> suggestMatching(listStructures(c), b)) | ||
.executes(c -> { | ||
String fileName = getString(c, "structure"); | ||
Path from = structuresPath(c).resolve(fileName + ".nbt"); | ||
Path to = Path.of("../src/gametest/resources/data/carpet-extra/structures"); | ||
NbtProvider.convertNbtToSnbt(DataWriter.UNCACHED, from, fileName, to); | ||
c.getSource().sendMessage(Messenger.c(" Converted and moved structure " + fileName)); | ||
return 0; | ||
}) | ||
) | ||
); | ||
dispatcher.register(command); | ||
} | ||
|
||
private static Path structuresPath(CommandContext<ServerCommandSource> ctx) { | ||
return ctx.getSource().getServer().getSavePath(WorldSavePath.GENERATED).resolve("minecraft/structures"); | ||
} | ||
|
||
private static Collection<String> listStructures(CommandContext<ServerCommandSource> ctx) { | ||
try (Stream<Path> paths = Files.list(structuresPath(ctx))) { | ||
return paths | ||
.map(p -> p.getFileName().toString()) | ||
.filter(p -> p.endsWith(".nbt")) | ||
.map(p -> p.substring(0, p.length() - 4)) | ||
.toList(); | ||
} catch (IOException e) { | ||
TestUtils.LOGGER.info("Failed to list structures", e); | ||
// do nothing? | ||
return Stream.<String>empty().toList(); | ||
} | ||
} | ||
|
||
// private static void reloadTestClass(Class<?> cls) { | ||
// String fabricBatchId = cls.getSimpleName().toLowerCase(); | ||
// TestFunctions.getTestFunctions().removeIf(fn -> fn.templatePath().startsWith(fabricBatchId)); | ||
// // we'd need to remove before and after batch handlers too | ||
// TestFunctions.register(cls); | ||
// } | ||
|
||
@Override | ||
public String version() { | ||
return "testcommand-adder"; | ||
} | ||
} |
Oops, something went wrong.