Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/tests/compatibility_test/run_compatibility_test.py
23449 views
1
#!/usr/bin/env python3
2
from __future__ import annotations
3
4
import itertools
5
import json
6
import os
7
import pathlib
8
import subprocess
9
import urllib.request
10
from typing import Any
11
12
PROJECT_PATH = pathlib.Path(__file__).parent.resolve().joinpath("godot")
13
CLASS_METHODS_FILE = PROJECT_PATH.joinpath("class_methods.txt")
14
BUILTIN_METHODS_FILE = PROJECT_PATH.joinpath("builtin_methods.txt")
15
UTILITY_FUNCTIONS_FILE = PROJECT_PATH.joinpath("utility_functions.txt")
16
17
18
def download_gdextension_api(reftag: str) -> dict[str, Any]:
19
with urllib.request.urlopen(
20
f"https://raw.githubusercontent.com/godotengine/godot-cpp/godot-{reftag}/gdextension/extension_api.json"
21
) as f:
22
gdextension_api_json: dict[str, Any] = json.load(f)
23
return gdextension_api_json
24
25
26
def remove_test_data_files():
27
for test_data in [CLASS_METHODS_FILE, BUILTIN_METHODS_FILE, UTILITY_FUNCTIONS_FILE]:
28
if os.path.isfile(test_data):
29
os.remove(test_data)
30
31
32
def generate_test_data_files(reftag: str):
33
"""
34
Parses methods specified in given Godot version into a form readable by the compatibility checker GDExtension.
35
"""
36
gdextension_reference_json = download_gdextension_api(reftag)
37
38
with open(CLASS_METHODS_FILE, "w") as classes_file:
39
classes_file.writelines([
40
f"{klass['name']} {func['name']} {func['hash']}\n"
41
for (klass, func) in itertools.chain(
42
(
43
(klass, method)
44
for klass in gdextension_reference_json["classes"]
45
for method in klass.get("methods", [])
46
if not method.get("is_virtual")
47
),
48
)
49
])
50
51
variant_types: dict[str, int] | None = None
52
for global_enum in gdextension_reference_json["global_enums"]:
53
if global_enum.get("name") != "Variant.Type":
54
continue
55
variant_types = {
56
variant_type.get("name").removeprefix("TYPE_").lower().replace("_", ""): variant_type.get("value")
57
for variant_type in global_enum.get("values")
58
}
59
60
if not variant_types:
61
return
62
63
with open(BUILTIN_METHODS_FILE, "w") as f:
64
f.writelines([
65
f"{variant_types[klass['name'].lower()]} {func['name']} {func['hash']}\n"
66
for (klass, func) in itertools.chain(
67
(
68
(klass, method)
69
for klass in gdextension_reference_json["builtin_classes"]
70
for method in klass.get("methods", [])
71
),
72
)
73
])
74
75
with open(UTILITY_FUNCTIONS_FILE, "w") as f:
76
f.writelines([f"{func['name']} {func['hash']}\n" for func in gdextension_reference_json["utility_functions"]])
77
78
79
def has_compatibility_test_failed(errors: str) -> bool:
80
"""
81
Checks if provided errors are related to the compatibility test.
82
83
Makes sure that test won't fail on unrelated account (for example editor misconfiguration).
84
"""
85
compatibility_errors = [
86
"Error loading extension",
87
"Failed to load interface method",
88
'Parameter "mb" is null.',
89
'Parameter "bfi" is null.',
90
"Method bind not found:",
91
"Utility function not found:",
92
"has changed and no compatibility fallback has been provided",
93
"Failed to open file `builtin_methods.txt`",
94
"Failed to open file `class_methods.txt`",
95
"Failed to open file `utility_functions.txt`",
96
"Failed to open file `platform_methods.txt`",
97
"Outcome = FAILURE",
98
]
99
100
return any(compatibility_error in errors for compatibility_error in compatibility_errors)
101
102
103
def process_compatibility_test(proc: subprocess.Popen[bytes], timeout: int = 5) -> str | None:
104
"""
105
Returns the stderr output as a string, if any.
106
107
Terminates test if nothing has been written to stdout/stderr for specified time.
108
"""
109
errors = bytearray()
110
111
while True:
112
try:
113
_out, err = proc.communicate(timeout=timeout)
114
if err:
115
errors.extend(err)
116
except subprocess.TimeoutExpired:
117
proc.kill()
118
_out, err = proc.communicate()
119
if err:
120
errors.extend(err)
121
break
122
123
return errors.decode("utf-8") if errors else None
124
125
126
def compatibility_check(godot4_bin: str) -> bool:
127
"""
128
Checks if methods specified for previous Godot versions can be properly loaded with
129
the latest Godot4 binary.
130
"""
131
# A bit crude albeit working solution – use stderr to check for compatibility-related errors.
132
proc = subprocess.Popen(
133
[godot4_bin, "--headless", "-e", "--path", PROJECT_PATH],
134
stdout=subprocess.PIPE,
135
stderr=subprocess.PIPE,
136
)
137
138
if (errors := process_compatibility_test(proc)) and has_compatibility_test_failed(errors):
139
print(f"Compatibility test failed. Errors:\n {errors}")
140
return False
141
return True
142
143
144
if __name__ == "__main__":
145
godot4_bin = os.environ["GODOT4_BIN"]
146
reftags = os.environ["REFTAGS"].split(",")
147
is_success = True
148
for reftag in reftags:
149
generate_test_data_files(reftag)
150
if not compatibility_check(godot4_bin):
151
print(f"Compatibility test against Godot{reftag} failed")
152
is_success = False
153
remove_test_data_files()
154
155
if not is_success:
156
exit(1)
157
158