| 1 | package net.minecraft.gametest.framework;
|
| 2 |
|
| 3 | import com.mojang.logging.LogUtils;
|
| 4 | import java.io.File;
|
| 5 | import java.io.IOException;
|
| 6 | import java.nio.file.Files;
|
| 7 | import java.nio.file.Path;
|
| 8 | import java.nio.file.Paths;
|
| 9 | import java.util.Optional;
|
| 10 | import java.util.function.Consumer;
|
| 11 | import java.util.stream.Stream;
|
| 12 | import joptsimple.OptionParser;
|
| 13 | import joptsimple.OptionSet;
|
| 14 | import joptsimple.OptionSpec;
|
| 15 | import net.minecraft.SuppressForbidden;
|
| 16 | import net.minecraft.server.Bootstrap;
|
| 17 | import net.minecraft.server.MinecraftServer;
|
| 18 | import net.minecraft.server.packs.repository.PackRepository;
|
| 19 | import net.minecraft.server.packs.repository.ServerPacksSource;
|
| 20 | import net.minecraft.util.Util;
|
| 21 | import net.minecraft.world.level.storage.LevelStorageSource;
|
| 22 | import org.apache.commons.io.FileUtils;
|
| 23 | import org.slf4j.Logger;
|
| 24 |
|
| 25 | public class GameTestMainUtil {
|
| 26 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 27 | private static final String DEFAULT_UNIVERSE_DIR = "gametestserver";
|
| 28 | private static final String LEVEL_NAME = "gametestworld";
|
| 29 | private static final OptionParser parser = new OptionParser();
|
| 30 | private static final OptionSpec<String> universe = parser.accepts(
|
| 31 | "universe", "The path to where the test server world will be created. Any existing folder will be replaced."
|
| 32 | )
|
| 33 | .withRequiredArg()
|
| 34 | .defaultsTo("gametestserver");
|
| 35 | private static final OptionSpec<File> report = parser.accepts("report", "Exports results in a junit-like XML report at the given path.")
|
| 36 | .withRequiredArg()
|
| 37 | .ofType(File.class);
|
| 38 | private static final OptionSpec<String> tests = parser.accepts("tests", "Which test(s) to run (namespaced ID selector using wildcards). Empty means run all.")
|
| 39 | .withRequiredArg();
|
| 40 | private static final OptionSpec<Boolean> verify = parser.accepts(
|
| 41 | "verify", "Runs the tests specified with `test` or `testNamespace` 100 times for each 90 degree rotation step"
|
| 42 | )
|
| 43 | .withRequiredArg()
|
| 44 | .ofType(Boolean.class)
|
| 45 | .defaultsTo(false);
|
| 46 | private static final OptionSpec<Integer> repeatCount = parser.accepts("repeatCount", "Runs each of the specified tests this many times")
|
| 47 | .withRequiredArg()
|
| 48 | .ofType(Integer.class)
|
| 49 | .defaultsTo(1);
|
| 50 | private static final OptionSpec<String> packs = parser.accepts("packs", "A folder of datapacks to include in the world").withRequiredArg();
|
| 51 | private static final OptionSpec<Void> help = parser.accepts("help").forHelp();
|
| 52 |
|
| 53 | @SuppressForbidden(reason = "Using System.err due to no bootstrap")
|
| 54 | public static void runGameTestServer(final String[] args, final Consumer<String> onUniverseCreated) throws Exception {
|
| 55 | parser.allowsUnrecognizedOptions();
|
| 56 | OptionSet options = parser.parse(args);
|
| 57 | if (options.has(help)) {
|
| 58 | parser.printHelpOn(System.err);
|
| 59 | } else {
|
| 60 | if (options.valueOf(verify) && !options.has(tests)) {
|
| 61 | LOGGER.error("Please specify a test selection to run the verify option. For example: --verify --tests example:test_something_*");
|
| 62 | System.exit(-1);
|
| 63 | }
|
| 64 |
|
| 65 | if (options.valueOf(verify) && options.has(repeatCount)) {
|
| 66 | LOGGER.info("Flag --verify is true, the --repeatCount value will be ignored");
|
| 67 | }
|
| 68 |
|
| 69 | LOGGER.info("Running GameTestMain with cwd '{}', universe path '{}'", System.getProperty("user.dir"), options.valueOf(universe));
|
| 70 | if (options.has(report)) {
|
| 71 | GlobalTestReporter.replaceWith(new JUnitLikeTestReporter(report.value(options)));
|
| 72 | }
|
| 73 |
|
| 74 | Bootstrap.bootStrap();
|
| 75 | Util.startTimerHackThread();
|
| 76 | String universePath = options.valueOf(universe);
|
| 77 | createOrResetDir(universePath);
|
| 78 | onUniverseCreated.accept(universePath);
|
| 79 | if (options.has(packs)) {
|
| 80 | String packFolder = options.valueOf(packs);
|
| 81 | copyPacks(universePath, packFolder);
|
| 82 | }
|
| 83 |
|
| 84 | LevelStorageSource.LevelStorageAccess levelStorageSource = LevelStorageSource.createDefault(Paths.get(universePath)).createAccess("gametestworld");
|
| 85 | PackRepository packRepository = ServerPacksSource.createPackRepository(levelStorageSource);
|
| 86 | MinecraftServer.spin(
|
| 87 | thread -> GameTestServer.create(
|
| 88 | thread, levelStorageSource, packRepository, optionalFromOption(options, tests), options.valueOf(verify), options.valueOf(repeatCount)
|
| 89 | )
|
| 90 | );
|
| 91 | }
|
| 92 | }
|
| 93 |
|
| 94 | private static Optional<String> optionalFromOption(final OptionSet options, final OptionSpec<String> option) {
|
| 95 | return options.has(option) ? Optional.of(options.valueOf(option)) : Optional.empty();
|
| 96 | }
|
| 97 |
|
| 98 | private static void createOrResetDir(final String universePath) throws IOException {
|
| 99 | Path universeDir = Paths.get(universePath);
|
| 100 | if (Files.exists(universeDir)) {
|
| 101 | FileUtils.deleteDirectory(universeDir.toFile());
|
| 102 | }
|
| 103 |
|
| 104 | Files.createDirectories(universeDir);
|
| 105 | }
|
| 106 |
|
| 107 | private static void copyPacks(final String serverPath, final String packSourcePath) throws IOException {
|
| 108 | Path worldPackFolder = Paths.get(serverPath).resolve("gametestworld").resolve("datapacks");
|
| 109 | if (!Files.exists(worldPackFolder)) {
|
| 110 | Files.createDirectories(worldPackFolder);
|
| 111 | }
|
| 112 |
|
| 113 | Path sourceFolder = Paths.get(packSourcePath);
|
| 114 | if (Files.exists(sourceFolder)) {
|
| 115 | try (Stream<Path> list = Files.list(sourceFolder)) {
|
| 116 | for (Path path : list.toList()) {
|
| 117 | Path destination = worldPackFolder.resolve(path.getFileName());
|
| 118 | if (Files.isDirectory(path)) {
|
| 119 | if (Files.isRegularFile(path.resolve("pack.mcmeta"))) {
|
| 120 | FileUtils.copyDirectory(path.toFile(), destination.toFile());
|
| 121 | LOGGER.info("Included folder pack {}", path.getFileName());
|
| 122 | }
|
| 123 | } else if (path.toString().endsWith(".zip")) {
|
| 124 | Files.copy(path, destination);
|
| 125 | LOGGER.info("Included zip pack {}", path.getFileName());
|
| 126 | }
|
| 127 | }
|
| 128 | }
|
| 129 | }
|
| 130 | }
|
| 131 | }
|
| 132 |
|