|
| 1 | +package com.cleanroommc.modularui.test; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.io.PrintWriter; |
| 5 | +import java.nio.file.FileSystems; |
| 6 | +import java.nio.file.Path; |
| 7 | + |
| 8 | +import net.minecraft.server.MinecraftServer; |
| 9 | +import net.minecraft.util.ChatComponentText; |
| 10 | + |
| 11 | +import org.apache.commons.io.output.CloseShieldOutputStream; |
| 12 | +import org.junit.platform.engine.discovery.DiscoverySelectors; |
| 13 | +import org.junit.platform.launcher.Launcher; |
| 14 | +import org.junit.platform.launcher.LauncherDiscoveryRequest; |
| 15 | +import org.junit.platform.launcher.LauncherSession; |
| 16 | +import org.junit.platform.launcher.TestPlan; |
| 17 | +import org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder; |
| 18 | +import org.junit.platform.launcher.core.LauncherFactory; |
| 19 | +import org.junit.platform.launcher.listeners.SummaryGeneratingListener; |
| 20 | +import org.junit.platform.launcher.listeners.TestExecutionSummary; |
| 21 | +import org.junit.platform.reporting.legacy.xml.LegacyXmlReportGeneratingListener; |
| 22 | + |
| 23 | +import cpw.mods.fml.common.FMLCommonHandler; |
| 24 | +import cpw.mods.fml.common.Mod; |
| 25 | +import cpw.mods.fml.common.event.FMLServerStartedEvent; |
| 26 | + |
| 27 | +@Mod(modid = "mui-tests", name = "MUI Dev Tests", version = "1.0", dependencies = "required-after:modularui2") |
| 28 | +public class MUITestMod { |
| 29 | + |
| 30 | + @Mod.EventHandler |
| 31 | + public void onServerStarted(FMLServerStartedEvent event) { |
| 32 | + MinecraftServer.getServer() |
| 33 | + .addChatMessage(new ChatComponentText("Running MUI unit tests...")); |
| 34 | + runTests(); |
| 35 | + MinecraftServer.getServer() |
| 36 | + .addChatMessage(new ChatComponentText("Running MUI unit tests finished")); |
| 37 | + } |
| 38 | + |
| 39 | + private void runTests() { |
| 40 | + // https://junit.org/junit5/docs/current/user-guide/#launcher-api |
| 41 | + System.setProperty("junit.platform.reporting.open.xml.enabled", "false"); |
| 42 | + final Path testsXmlOutDir = FileSystems.getDefault() |
| 43 | + .getPath("./junit-out/") |
| 44 | + .toAbsolutePath(); |
| 45 | + final File testsXmlOutDirFile = testsXmlOutDir.toFile(); |
| 46 | + testsXmlOutDirFile.mkdirs(); |
| 47 | + { |
| 48 | + File[] fileList = testsXmlOutDirFile.listFiles(); |
| 49 | + if (fileList != null) { |
| 50 | + for (File child : fileList) { |
| 51 | + if (child.isFile() && child.getName() |
| 52 | + .endsWith(".xml")) { |
| 53 | + child.delete(); |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + final LauncherDiscoveryRequest discovery = LauncherDiscoveryRequestBuilder.request() |
| 59 | + .selectors(DiscoverySelectors.selectPackage("com.cleanroommc.modularui.test")) |
| 60 | + .build(); |
| 61 | + final SummaryGeneratingListener summaryGenerator = new SummaryGeneratingListener(); |
| 62 | + final TestExecutionSummary summary; |
| 63 | + try (PrintWriter stderrWriter = new PrintWriter(new CloseShieldOutputStream(System.err), true)) { |
| 64 | + final LegacyXmlReportGeneratingListener xmlGenerator = new LegacyXmlReportGeneratingListener( |
| 65 | + testsXmlOutDir, |
| 66 | + stderrWriter); |
| 67 | + try (LauncherSession session = LauncherFactory.openSession()) { |
| 68 | + final Launcher launcher = session.getLauncher(); |
| 69 | + final TestPlan plan = launcher.discover(discovery); |
| 70 | + launcher.registerTestExecutionListeners(summaryGenerator, xmlGenerator); |
| 71 | + launcher.execute(plan); |
| 72 | + } |
| 73 | + summary = summaryGenerator.getSummary(); |
| 74 | + |
| 75 | + summary.printFailuresTo(stderrWriter, 32); |
| 76 | + summary.printTo(stderrWriter); |
| 77 | + stderrWriter.flush(); |
| 78 | + } |
| 79 | + // Throw an exception if running via `runServer` |
| 80 | + if (summary.getTotalFailureCount() > 0 && FMLCommonHandler.instance() |
| 81 | + .getSide() |
| 82 | + .isServer()) { |
| 83 | + throw new RuntimeException("Some of the unit tests failed to execute, check the log for details"); |
| 84 | + } |
| 85 | + } |
| 86 | +} |
0 commit comments