Path: blob/master/tests/compatibility_test/run_compatibility_test.py
23449 views
#!/usr/bin/env python31from __future__ import annotations23import itertools4import json5import os6import pathlib7import subprocess8import urllib.request9from typing import Any1011PROJECT_PATH = pathlib.Path(__file__).parent.resolve().joinpath("godot")12CLASS_METHODS_FILE = PROJECT_PATH.joinpath("class_methods.txt")13BUILTIN_METHODS_FILE = PROJECT_PATH.joinpath("builtin_methods.txt")14UTILITY_FUNCTIONS_FILE = PROJECT_PATH.joinpath("utility_functions.txt")151617def download_gdextension_api(reftag: str) -> dict[str, Any]:18with urllib.request.urlopen(19f"https://raw.githubusercontent.com/godotengine/godot-cpp/godot-{reftag}/gdextension/extension_api.json"20) as f:21gdextension_api_json: dict[str, Any] = json.load(f)22return gdextension_api_json232425def remove_test_data_files():26for test_data in [CLASS_METHODS_FILE, BUILTIN_METHODS_FILE, UTILITY_FUNCTIONS_FILE]:27if os.path.isfile(test_data):28os.remove(test_data)293031def generate_test_data_files(reftag: str):32"""33Parses methods specified in given Godot version into a form readable by the compatibility checker GDExtension.34"""35gdextension_reference_json = download_gdextension_api(reftag)3637with open(CLASS_METHODS_FILE, "w") as classes_file:38classes_file.writelines([39f"{klass['name']} {func['name']} {func['hash']}\n"40for (klass, func) in itertools.chain(41(42(klass, method)43for klass in gdextension_reference_json["classes"]44for method in klass.get("methods", [])45if not method.get("is_virtual")46),47)48])4950variant_types: dict[str, int] | None = None51for global_enum in gdextension_reference_json["global_enums"]:52if global_enum.get("name") != "Variant.Type":53continue54variant_types = {55variant_type.get("name").removeprefix("TYPE_").lower().replace("_", ""): variant_type.get("value")56for variant_type in global_enum.get("values")57}5859if not variant_types:60return6162with open(BUILTIN_METHODS_FILE, "w") as f:63f.writelines([64f"{variant_types[klass['name'].lower()]} {func['name']} {func['hash']}\n"65for (klass, func) in itertools.chain(66(67(klass, method)68for klass in gdextension_reference_json["builtin_classes"]69for method in klass.get("methods", [])70),71)72])7374with open(UTILITY_FUNCTIONS_FILE, "w") as f:75f.writelines([f"{func['name']} {func['hash']}\n" for func in gdextension_reference_json["utility_functions"]])767778def has_compatibility_test_failed(errors: str) -> bool:79"""80Checks if provided errors are related to the compatibility test.8182Makes sure that test won't fail on unrelated account (for example editor misconfiguration).83"""84compatibility_errors = [85"Error loading extension",86"Failed to load interface method",87'Parameter "mb" is null.',88'Parameter "bfi" is null.',89"Method bind not found:",90"Utility function not found:",91"has changed and no compatibility fallback has been provided",92"Failed to open file `builtin_methods.txt`",93"Failed to open file `class_methods.txt`",94"Failed to open file `utility_functions.txt`",95"Failed to open file `platform_methods.txt`",96"Outcome = FAILURE",97]9899return any(compatibility_error in errors for compatibility_error in compatibility_errors)100101102def process_compatibility_test(proc: subprocess.Popen[bytes], timeout: int = 5) -> str | None:103"""104Returns the stderr output as a string, if any.105106Terminates test if nothing has been written to stdout/stderr for specified time.107"""108errors = bytearray()109110while True:111try:112_out, err = proc.communicate(timeout=timeout)113if err:114errors.extend(err)115except subprocess.TimeoutExpired:116proc.kill()117_out, err = proc.communicate()118if err:119errors.extend(err)120break121122return errors.decode("utf-8") if errors else None123124125def compatibility_check(godot4_bin: str) -> bool:126"""127Checks if methods specified for previous Godot versions can be properly loaded with128the latest Godot4 binary.129"""130# A bit crude albeit working solution – use stderr to check for compatibility-related errors.131proc = subprocess.Popen(132[godot4_bin, "--headless", "-e", "--path", PROJECT_PATH],133stdout=subprocess.PIPE,134stderr=subprocess.PIPE,135)136137if (errors := process_compatibility_test(proc)) and has_compatibility_test_failed(errors):138print(f"Compatibility test failed. Errors:\n {errors}")139return False140return True141142143if __name__ == "__main__":144godot4_bin = os.environ["GODOT4_BIN"]145reftags = os.environ["REFTAGS"].split(",")146is_success = True147for reftag in reftags:148generate_test_data_files(reftag)149if not compatibility_check(godot4_bin):150print(f"Compatibility test against Godot{reftag} failed")151is_success = False152remove_test_data_files()153154if not is_success:155exit(1)156157158