Skip to content

Commit a229ee0

Browse files
committed
Check Godot output in tests
1 parent c51f1d1 commit a229ee0

File tree

6 files changed

+65
-9
lines changed

6 files changed

+65
-9
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ repos:
1414
- id: mixed-line-ending
1515
exclude: (^tests/_lib_vendors|^(tests|examples)/lib) # Ignore 3rd party stuff
1616
- id: trailing-whitespace
17-
exclude: (^tests/_lib_vendors|^(tests|examples)/lib) # Ignore 3rd party stuff
17+
exclude: (expected.output|^tests/_lib_vendors|^(tests|examples)/lib) # Ignore 3rd party stuff
1818
- repo: local
1919
hooks:
2020
- id: git_actions_pin

tests/0-gdscript/expected.output

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Godot Engine .* - https://godotengine.org
2+
3+
Hello, World !

tests/0-gdscript/main.gd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
extends Node
22

33
func _ready():
4+
print("Hello, World !")
45
# Exit godot
56
self.get_tree().quit()
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
My GDExtension entry point call
2+
Godot Engine .* - https://godotengine.org
3+
My GDExtension initialize
4+
5+
Hello, World !
6+
My GDExtension deinitialize

tests/1-gdextension/main.gd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
extends Node
22

33
func _ready():
4+
print("Hello, World !")
45
# Exit godot
56
self.get_tree().quit()

tests/run.py

Lines changed: 53 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#! /usr/bin/env python
22

33
import os
4+
import re
45
import sys
56
import platform
67
import importlib.util
@@ -150,27 +151,71 @@ def run_test(
150151
print(" ".join(cmd), flush=True)
151152
res = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
152153
total_output = b""
154+
subprocess_done = False
153155
while True:
154156
buff: bytes = res.stdout.read1()
155157
total_output += buff
156158
os.write(sys.stdout.fileno(), buff)
159+
if subprocess_done:
160+
break
157161
try:
158162
res.wait(timeout=0.1)
159-
buff: bytes = res.stdout.read1()
160-
total_output += buff
161-
os.write(sys.stdout.fileno(), buff)
162-
break
163+
# Subprocess is done, but we still have one more stdin/stderr pump to do
164+
subprocess_done = True
163165
except subprocess.TimeoutExpired:
164166
# Subprocess is still running
165167
pass
166168
if res.returncode != 0:
167169
raise SystemExit(f"{RED}{test_name}: Non-zero return code: {res.returncode}{NO_COLOR}")
168-
for line in total_output.splitlines():
169-
lower_line = line.lower()
170-
if b"error" in lower_line or b"warning" in lower_line:
170+
171+
try:
172+
expected_output = (test_workdir / "expected.output").read_text()
173+
174+
except FileNotFoundError:
175+
for line in total_output.splitlines():
176+
lower_line = line.lower()
177+
if b"error" in lower_line or b"warning" in lower_line:
178+
raise SystemExit(
179+
f"{RED}{test_name}: stdout/stderr contains logs with error and/or warning ({line!r}){NO_COLOR}"
180+
)
181+
182+
else:
183+
expected_lines = expected_output.splitlines()
184+
actual_lines = total_output.splitlines()
185+
msg = []
186+
mismatch = False
187+
for i in range(0, max(len(expected_lines), len(actual_lines))):
188+
try:
189+
expected_line = expected_lines[i]
190+
except IndexError:
191+
expected_line = None
192+
try:
193+
actual_line = actual_lines[i].decode()
194+
except IndexError:
195+
actual_line = None
196+
197+
assert expected_line is not None or actual_line is not None
198+
if expected_line is not None and actual_line is None:
199+
msg.append(f"{RED}- {expected_line}{NO_COLOR}")
200+
mismatch = True
201+
elif expected_line is None and actual_line is not None:
202+
msg.append(f"{GREEN}+ {actual_line}{NO_COLOR}")
203+
mismatch = True
204+
else:
205+
assert expected_line is not None
206+
assert actual_line is not None
207+
if not re.match(f"^{expected_line}$", actual_line):
208+
msg.append(f"{RED}- {expected_line}{NO_COLOR}")
209+
msg.append(f"{GREEN}+ {actual_line}{NO_COLOR}")
210+
mismatch = True
211+
else:
212+
msg.append(f"~ {actual_line}")
213+
214+
if mismatch:
171215
raise SystemExit(
172-
f"{RED}{test_name}: stdout/stderr contains logs with error and/or warning ({line!r}){NO_COLOR}"
216+
f"{RED}{test_name}: unexpected stdout/stderr:{NO_COLOR}\n\n " + "\n ".join(msg)
173217
)
218+
174219
print(f"{GREEN}{test_name}: All good \\o/{NO_COLOR}", flush=True)
175220

176221

0 commit comments

Comments
 (0)