#!/usr/bin/env python3 """Run the companion mod's in-game tests on their own: one server boot on a fresh flat world, `/cobblesurvival selftest run`, wait for the results, stop, scan the log. The tests (mod/src/main/kotlin/dev/cobblesurvival/selftest/) play real situations on the live server with every mod and config of the pack, e.g. the permadeath matrix: Pokémon fainting from a fall, lava, poison, in wild and trainer battles, with and without a Revival Charm. tools/quick_check.py runs them too; this is the faster loop while working on them. Usage: tools/selftest.py [--accept-eula] (run tools/build.py first) """ import argparse import shutil import sys import time from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parent)) from mcserver import SELFTEST_JVM_ARGS, SERVER, Server, eula_accepted, prepare, scan_log # noqa: E402 LEVEL = "selftest" def main(): parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) parser.add_argument("--accept-eula", action="store_true", help="write eula=true for the test server") args = parser.parse_args() if args.accept_eula: (SERVER / "eula.txt").parent.mkdir(parents=True, exist_ok=True) (SERVER / "eula.txt").write_text("eula=true\n") elif not eula_accepted(): sys.exit("build/server/eula.txt does not accept the Minecraft EULA yet; pass --accept-eula if you accept it.") started = time.time() args_file = prepare() shutil.rmtree(SERVER / LEVEL, ignore_errors=True) flat = '{"layers":[{"block":"minecraft:bedrock","height":1},{"block":"minecraft:stone","height":3},' \ '{"block":"minecraft:grass_block","height":1}],"biome":"minecraft:plains"}' props = {"level-name": LEVEL, "level-type": "minecraft:flat", "generator-settings": flat} failures = [] with Server(args_file, props, stdout_name="selftest-stdout.log", jvm_args=SELFTEST_JVM_ARGS) as server: try: print(f"server started in {server.wait_ready():.0f}s") failures += server.run_selftest() server.stop() except Exception as e: # report; the context manager still kills the process failures.append(f"{type(e).__name__}: {e}") failures += [f"log: {line}" for line in scan_log(server.log)] print(f"\nselftest took {time.time() - started:.0f}s") if failures: print("SELFTEST FAILED") print("\n".join(f" - {f}" for f in failures)) sys.exit(1) print("SELFTEST PASSED") if __name__ == "__main__": main()