aboutsummaryrefslogtreecommitdiff
path: root/src/functionalTest/java/kubatech/test/kubatechTestMod.java
blob: bc68d3f20319c5b3dd0830c7c80008e8e07e63cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package kubatech.test;

import java.io.File;
import java.io.PrintWriter;
import java.nio.file.FileSystems;
import java.nio.file.Path;

import net.minecraft.server.MinecraftServer;
import net.minecraft.util.ChatComponentText;

import org.apache.commons.io.output.CloseShieldOutputStream;
import org.junit.platform.engine.discovery.DiscoverySelectors;
import org.junit.platform.launcher.Launcher;
import org.junit.platform.launcher.LauncherDiscoveryRequest;
import org.junit.platform.launcher.LauncherSession;
import org.junit.platform.launcher.TestPlan;
import org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder;
import org.junit.platform.launcher.core.LauncherFactory;
import org.junit.platform.launcher.listeners.SummaryGeneratingListener;
import org.junit.platform.launcher.listeners.TestExecutionSummary;
import org.junit.platform.reporting.legacy.xml.LegacyXmlReportGeneratingListener;

import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.Mod.EventHandler;
import cpw.mods.fml.common.event.FMLPreInitializationEvent;
import cpw.mods.fml.common.event.FMLServerStartedEvent;
import gregtech.GT_Mod;

@Mod(
    modid = "kubatech-tests",
    name = "KubaTech Dev Tests",
    version = "1.0",
    dependencies = "required-after:kubatech;required-after:gregtech;after:berriespp;")
public class kubatechTestMod {

    @EventHandler
    public void preInit(FMLPreInitializationEvent ev) {
        // Disable GT5u messing with vanilla recipes for unit tests
        GT_Mod.gregtechproxy.mNerfedWoodPlank = false;
        GT_Mod.gregtechproxy.mNerfedVanillaTools = false;
    }

    @EventHandler
    public void onServerStarted(FMLServerStartedEvent startedEv) {
        MinecraftServer.getServer()
            .addChatMessage(new ChatComponentText("Running KT unit tests..."));
        runTests();
        MinecraftServer.getServer()
            .addChatMessage(new ChatComponentText("Running KT unit tests finished"));
    }

    public void runTests() {
        // https://junit.org/junit5/docs/current/user-guide/#launcher-api
        System.setProperty("junit.platform.reporting.open.xml.enabled", "false");
        final Path testsXmlOutDir = FileSystems.getDefault()
            .getPath("./junit-out/")
            .toAbsolutePath();
        final File testsXmlOutDirFile = testsXmlOutDir.toFile();
        testsXmlOutDirFile.mkdirs();
        {
            File[] fileList = testsXmlOutDirFile.listFiles();
            if (fileList != null) {
                for (File child : fileList) {
                    if (child.isFile() && child.getName()
                        .endsWith(".xml")) {
                        child.delete();
                    }
                }
            }
        }
        final LauncherDiscoveryRequest discovery = LauncherDiscoveryRequestBuilder.request()
            .selectors(DiscoverySelectors.selectPackage("kubatech.test"))
            .build();
        final SummaryGeneratingListener summaryGenerator = new SummaryGeneratingListener();
        final TestExecutionSummary summary;
        try (PrintWriter stderrWriter = new PrintWriter(new CloseShieldOutputStream(System.err), true)) {
            final LegacyXmlReportGeneratingListener xmlGenerator = new LegacyXmlReportGeneratingListener(
                testsXmlOutDir,
                stderrWriter);
            try (LauncherSession session = LauncherFactory.openSession()) {
                final Launcher launcher = session.getLauncher();
                final TestPlan plan = launcher.discover(discovery);
                launcher.registerTestExecutionListeners(summaryGenerator, xmlGenerator);
                launcher.execute(plan);
            }
            summary = summaryGenerator.getSummary();

            summary.printFailuresTo(stderrWriter, 32);
            summary.printTo(stderrWriter);
            stderrWriter.flush();
        }
        // Throw an exception if running via `runServer`
        if (summary.getTotalFailureCount() > 0 && FMLCommonHandler.instance()
            .getSide()
            .isServer()) {
            throw new RuntimeException("Some of the unit tests failed to execute, check the log for details");
        }
    }

}