Prevent any further code execution after crash

This commit is contained in:
JeremyStar™ 2024-04-06 13:47:48 +02:00
parent 7b9c84b805
commit ec1699f3c7
Signed by: JeremyStarTM
GPG key ID: E366BAEF67E4704D

View file

@ -19,6 +19,9 @@ var stats_skip: int = 0
var stats_forgot: int = 0 var stats_forgot: int = 0
var stats_unknown: int = 0 var stats_unknown: int = 0
# To stop any still running test
var crashed: bool = false
func _ready() -> void: func _ready() -> void:
await get_tree().process_frame await get_tree().process_frame
@ -48,7 +51,11 @@ func _ready() -> void:
if !DirAccess.dir_exists_absolute(config_test_directory): await lcrash("Testing directory \"" + config_test_directory + "\" does not exist") if !DirAccess.dir_exists_absolute(config_test_directory): await lcrash("Testing directory \"" + config_test_directory + "\" does not exist")
# Run test suites # Run test suites
for suite in DirAccess.get_files_at(config_test_directory): await run_suite(suite.replace(".gd", "")) for suite in DirAccess.get_files_at(config_test_directory):
if crashed: return
await run_suite(suite.replace(".gd", ""))
if crashed: return
# Print test results # Print test results
lresu("") lresu("")
@ -106,6 +113,7 @@ func run_suite(suite: String) -> void:
# Execute before_all() # Execute before_all()
suite_node.lfunc = "before_all" suite_node.lfunc = "before_all"
await suite_node.before_all() await suite_node.before_all()
if crashed: return
# Iterate over all functions # Iterate over all functions
for test_method in suite_node.get_method_list(): for test_method in suite_node.get_method_list():
@ -122,12 +130,15 @@ func run_suite(suite: String) -> void:
# Execute before_each() # Execute before_each()
suite_node.lfunc = "before_each" suite_node.lfunc = "before_each"
await suite_node.before_each() await suite_node.before_each()
if crashed: return
# Execute test function # Execute test function
suite_node.lfunc = test_method["name"].replace("test_", "") suite_node.lfunc = test_method["name"].replace("test_", "")
await suite_node.call(test_method["name"]) await suite_node.call(test_method["name"])
if crashed: return
# Execute after_each() # Execute after_each()
suite_node.lfunc = "after_each" suite_node.lfunc = "after_each"
await suite_node.after_each() await suite_node.after_each()
if crashed: return
# Merge result into result dictionary # Merge result into result dictionary
results.merge({ suite + ":" + test_method["name"].replace("test_", ""): { "status": suite_node.test_status, "message": lsanitize(suite_node.test_message) if suite_node.test_message != "" else "[i]No message has been set.[/i]" } }) results.merge({ suite + ":" + test_method["name"].replace("test_", ""): { "status": suite_node.test_status, "message": lsanitize(suite_node.test_message) if suite_node.test_message != "" else "[i]No message has been set.[/i]" } })
@ -135,6 +146,7 @@ func run_suite(suite: String) -> void:
# Execute after_all() # Execute after_all()
suite_node.lfunc = "after_all" suite_node.lfunc = "after_all"
await suite_node.after_all() await suite_node.after_all()
if crashed: return
# Remove suite node # Remove suite node
get_tree().root.remove_child(suite_node) get_tree().root.remove_child(suite_node)
@ -148,6 +160,7 @@ func linfo(message: String, origin: String = "Bessere Tests") -> void: print_ric
func lwarn(message: String, origin: String = "Bessere Tests") -> void: print_rich("[color=yellow]WARN " + origin + ": " + message + "[/color]") func lwarn(message: String, origin: String = "Bessere Tests") -> void: print_rich("[color=yellow]WARN " + origin + ": " + message + "[/color]")
func lerror(message: String, origin: String = "Bessere Tests") -> void: print_rich("[color=red]ERR! " + origin + ": " + message + "[/color]") func lerror(message: String, origin: String = "Bessere Tests") -> void: print_rich("[color=red]ERR! " + origin + ": " + message + "[/color]")
func lcrash(message: String, origin: String = "Bessere Tests") -> void: func lcrash(message: String, origin: String = "Bessere Tests") -> void:
crashed = true
print_rich("[color=red][b]CRSH " + origin + ": " + message + "[/b][/color]") print_rich("[color=red][b]CRSH " + origin + ": " + message + "[/b][/color]")
await terminate(69) await terminate(69)
@ -167,12 +180,14 @@ func terminate(exit_code: int = 0) -> void:
# Checks # Checks
func check_children() -> void: func check_children() -> void:
if crashed: return
if get_tree().root.get_child_count() != 1: if get_tree().root.get_child_count() != 1:
lwarn("There are still '" + str(get_tree().root.get_child_count()-1) + "' children active in the scene tree. Please make sure to call 'remove_child' on them in your tests.") lwarn("There are still '" + str(get_tree().root.get_child_count()-1) + "' children active in the scene tree. Please make sure to call 'remove_child' on them in your tests.")
lwarn("Here's a tree of /root:") lwarn("Here's a tree of /root:")
get_tree().root.print_tree_pretty() get_tree().root.print_tree_pretty()
func check_orphan_nodes() -> void: func check_orphan_nodes() -> void:
if crashed: return
if config_print_orphan_nodes: if config_print_orphan_nodes:
if Performance.get_monitor(Performance.OBJECT_ORPHAN_NODE_COUNT) != 4: if Performance.get_monitor(Performance.OBJECT_ORPHAN_NODE_COUNT) != 4:
lwarn("There are still '" + str(Performance.get_monitor(Performance.OBJECT_ORPHAN_NODE_COUNT)-4) + "' orphan nodes loaded. Please make sure to call 'queue_free' or 'free' on them in your tests.") lwarn("There are still '" + str(Performance.get_monitor(Performance.OBJECT_ORPHAN_NODE_COUNT)-4) + "' orphan nodes loaded. Please make sure to call 'queue_free' or 'free' on them in your tests.")