|
| 1 | +package com.github.skriptdev.skript.api.skript.testing; |
| 2 | + |
| 3 | +import com.github.skriptdev.skript.api.skript.testing.elements.EvtTest.TestContext; |
| 4 | +import com.github.skriptdev.skript.api.utils.Utils; |
| 5 | +import com.github.skriptdev.skript.plugin.Skript; |
| 6 | +import com.hypixel.hytale.server.core.HytaleServer; |
| 7 | +import com.hypixel.hytale.server.core.Message; |
| 8 | +import com.hypixel.hytale.server.core.universe.Universe; |
| 9 | +import com.hypixel.hytale.server.core.universe.world.World; |
| 10 | +import com.hypixel.hytale.server.core.util.MessageUtil; |
| 11 | +import fi.sulku.hytale.TinyMsg; |
| 12 | +import io.github.syst3ms.skriptparser.lang.TriggerMap; |
| 13 | +import io.github.syst3ms.skriptparser.log.LogEntry; |
| 14 | +import io.github.syst3ms.skriptparser.parsing.ScriptLoader; |
| 15 | + |
| 16 | +import java.io.File; |
| 17 | +import java.nio.file.Path; |
| 18 | +import java.util.ArrayList; |
| 19 | +import java.util.Arrays; |
| 20 | +import java.util.Comparator; |
| 21 | +import java.util.List; |
| 22 | +import java.util.concurrent.TimeUnit; |
| 23 | + |
| 24 | +public class TestingThingy { |
| 25 | + |
| 26 | + private static Skript skript; |
| 27 | + |
| 28 | + @SuppressWarnings("DataFlowIssue") |
| 29 | + public static void start(Skript skript) { |
| 30 | + TestingThingy.skript = skript; |
| 31 | + |
| 32 | + Runnable runTestsInWorld = () -> { |
| 33 | + Utils.log("Running tests in world 'default'..."); |
| 34 | + runTests(); |
| 35 | + Utils.log("Finished running tests!"); |
| 36 | + }; |
| 37 | + Runnable loadTestsRunnable = () -> { |
| 38 | + Utils.log("Loading test scripts..."); |
| 39 | + loadTests(); |
| 40 | + Utils.log("Finished loading test scripts!"); |
| 41 | + World world = Universe.get().getWorld("default"); |
| 42 | + if (world.isPaused()) world.setPaused(false); |
| 43 | + |
| 44 | + world.execute(runTestsInWorld); |
| 45 | + }; |
| 46 | + HytaleServer.SCHEDULED_EXECUTOR.schedule(loadTestsRunnable, 2, TimeUnit.SECONDS); |
| 47 | + } |
| 48 | + |
| 49 | + private static void loadTests() { |
| 50 | + Path path = Path.of("/Users/ShaneBee/IdeaProjects/HySkript/HySkript/src/test/skript/tests"); |
| 51 | + loadScripts(path); |
| 52 | + } |
| 53 | + |
| 54 | + private static void runTests() { |
| 55 | + |
| 56 | + TestResults testResults = new TestResults(); |
| 57 | + |
| 58 | + TestContext testContext = new TestContext(testResults); |
| 59 | + TriggerMap.callTriggersByContext(testContext); |
| 60 | + |
| 61 | + if (testResults.isSuccess()) { |
| 62 | + Message message = TinyMsg.parse("<green>All tests passed!"); |
| 63 | + Utils.log(MessageUtil.toAnsiString(message).toAnsi()); |
| 64 | + } else { |
| 65 | + Utils.error("Some tests failed!"); |
| 66 | + } |
| 67 | + |
| 68 | + testResults.printToJsonFile(); |
| 69 | + |
| 70 | + // Figure out how to fail the build on GitHub |
| 71 | + Runnable shutdownServer = () -> HytaleServer.get().shutdownServer(); |
| 72 | + HytaleServer.SCHEDULED_EXECUTOR.schedule(shutdownServer, 5, TimeUnit.SECONDS); |
| 73 | + } |
| 74 | + |
| 75 | + private static void loadScripts(Path directory) { |
| 76 | + File scriptsDirectory = directory.toFile(); |
| 77 | + Utils.log("Loading test directory '" + scriptsDirectory.getAbsolutePath() + "'..."); |
| 78 | + List<String> scriptNames = loadScriptsInDirectory(scriptsDirectory); |
| 79 | + Utils.log("Loaded " + scriptNames.size() + " scripts!"); |
| 80 | + } |
| 81 | + |
| 82 | + private static List<String> loadScriptsInDirectory(File directory) { |
| 83 | + if (directory == null || !directory.isDirectory()) return List.of(); |
| 84 | + |
| 85 | + List<String> loadedScripts = new ArrayList<>(); |
| 86 | + |
| 87 | + File[] files = directory.listFiles(); |
| 88 | + if (files == null) return loadedScripts; |
| 89 | + |
| 90 | + Arrays.sort(files, |
| 91 | + Comparator.comparing(File::isDirectory).reversed() // Directories first |
| 92 | + .thenComparing(File::getName, String.CASE_INSENSITIVE_ORDER)); // Then sort by name alphabetically |
| 93 | + |
| 94 | + for (File file : files) { |
| 95 | + // Skip disabled files and hidden files |
| 96 | + String fileName = file.getName(); |
| 97 | + if (fileName.startsWith("-") || fileName.startsWith(".")) continue; |
| 98 | + if (file.isDirectory()) { |
| 99 | + loadedScripts.addAll(loadScriptsInDirectory(file)); |
| 100 | + } else { |
| 101 | + if (!fileName.endsWith(".sk")) continue; |
| 102 | + Utils.log("Loading script '" + fileName + "'..."); |
| 103 | + List<LogEntry> logEntries = ScriptLoader.loadScript(file.toPath(), false); |
| 104 | + for (LogEntry logEntry : logEntries) { |
| 105 | + Utils.log(null, logEntry); |
| 106 | + } |
| 107 | + loadedScripts.add(fileName.substring(0, fileName.length() - 3)); |
| 108 | + } |
| 109 | + } |
| 110 | + return loadedScripts; |
| 111 | + } |
| 112 | + |
| 113 | +} |
0 commit comments