Skip to content

Commit

Permalink
add await in pre-run script, post-run scrpt, and should_skip_script. (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
LowFire authored Nov 29, 2024
1 parent b88fcdf commit 817e65c
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 6 deletions.
10 changes: 5 additions & 5 deletions addons/gut/gut.gd
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ func _validate_hook_script(path):
func _run_hook_script(inst):
if(inst != null):
inst.gut = self
inst.run()
await inst.run()
return inst

# ------------------------------------------------------------------------------
Expand Down Expand Up @@ -448,7 +448,7 @@ func _end_run():
_log_end_run()
_is_running = false

_run_hook_script(get_post_run_script_instance())
await _run_hook_script(get_post_run_script_instance())
_export_results()
end_run.emit()

Expand Down Expand Up @@ -664,7 +664,7 @@ func _should_skip_script(test_script, collected_script):
var should_skip = false

if(skip_value == null):
skip_value = test_script.should_skip_script()
skip_value = await test_script.should_skip_script()
else:
_lgr.deprecated('Using the skip_script var has been deprecated. Implement the new should_skip_script() method in your test instead.')

Expand Down Expand Up @@ -699,7 +699,7 @@ func _test_the_scripts(indexes=[]):
_lgr.error('Something went wrong and the run was aborted.')
return

_run_hook_script(get_pre_run_script_instance())
await _run_hook_script(get_pre_run_script_instance())
if(_pre_run_script_instance!= null and _pre_run_script_instance.should_abort()):
_lgr.error('pre-run abort')
end_run.emit()
Expand Down Expand Up @@ -739,7 +739,7 @@ func _test_the_scripts(indexes=[]):
# ----
# SHORTCIRCUIT
# skip_script logic
if(_should_skip_script(test_script, coll_script)):
if(await _should_skip_script(test_script, coll_script)):
continue
# ----

Expand Down
19 changes: 18 additions & 1 deletion test/integration/test_gut_skip_scripts.gd
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ var _src_should_skip_script_method_ret_string = """
func should_skip_script():
return 'skip me'
"""
var _scr_awaiting_should_skip_script = """
func should_skip_script():
print("Awaiting before skipping.")
await wait_seconds(1)
print("Now skip.")
return true
"""

var _gut = null

Expand Down Expand Up @@ -66,6 +73,17 @@ func test_when_skip_scrpt_var_is_true_the_script_is_skipped():
assert_eq(smry.tests, 0, 'no tests should be ran')
assert_eq(smry.risky, 1, 'Should be marked as risky due to skip')

func test_awaiting_before_should_skip_script():
var s = DynamicGutTest.new()
s.add_source(_scr_awaiting_should_skip_script)
s.run_test_in_gut(_gut)
await wait_for_signal(_gut.end_run, 3, "Should take exactly 1 second")
var summery = GutUtils.Summary.new()
var totals = summery.get_totals(_gut)

assert_eq(totals.tests, 0, 'no tests should be ran')
assert_eq(totals.risky, 1, 'Should be marked as risky due to skip')


# --------------
# skip method
Expand Down Expand Up @@ -112,4 +130,3 @@ func test_using_should_skip_script_method_is_not_deprecated():
var smry = s.run_test_in_gut(_gut)

assert_eq(smry.deprecated, 0, 'nothing is deprecated')

15 changes: 15 additions & 0 deletions test/resources/awaiting_post_run_script.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
extends 'res://addons/gut/hook_script.gd'

var awaited = false

func run():
print('!! --- post-run script awaiting --- !!')

var awaiter = GutUtils.Awaiter.new()
gut.add_child(awaiter)
awaiter.wait_seconds(1)
await awaiter.timeout
awaited = true
awaiter.queue_free()

print('!! --- post-run script has successfully awaited --- !!')
15 changes: 15 additions & 0 deletions test/resources/awaiting_pre_run_script.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
extends 'res://addons/gut/hook_script.gd'

var awaited = false

func run():
print('!! --- pre-run script awaiting --- !!')

var awaiter = GutUtils.Awaiter.new()
gut.add_child(awaiter)
awaiter.wait_seconds(1)
await awaiter.timeout
awaited = true
awaiter.queue_free()

print('!! --- pre-run script has successfully awaited --- !!')
16 changes: 16 additions & 0 deletions test/unit/test_gut.gd
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,22 @@ class TestEverythingElse:
gr.test_gut.test_scripts()
assert_eq(gr.test_gut.get_test_count(), 0, 'test should not be run')
assert_errored(gr.test_gut, -1)

func test_awaiting_in_the_pre_hook_script():
var pre_run_script = load("res://test/resources/awaiting_pre_run_script.gd")
gr.test_gut.pre_run_script = "res://test/resources/awaiting_pre_run_script.gd"
gr.test_gut.add_script(SAMPLES_DIR + 'test_sample_all_passed.gd')
gr.test_gut.test_scripts()
await wait_for_signal(gr.test_gut.start_run, 3, "It should take exactly 1 second.")
assert_true(gr.test_gut.get_pre_run_script_instance().awaited, "Pre-run script awaited.")

func test_awaiting_in_the_post_hook_script():
var pre_run_script = load("res://test/resources/awaiting_post_run_script.gd")
gr.test_gut.post_run_script = "res://test/resources/awaiting_post_run_script.gd"
gr.test_gut.add_script(SAMPLES_DIR + 'test_sample_all_passed.gd')
gr.test_gut.test_scripts()
await wait_for_signal(gr.test_gut.end_run, 3, "It should take exactly 1 second.")
assert_true(gr.test_gut.get_post_run_script_instance().awaited, "Post-run script awaited.")

# ------------------------------
# Parameterized Test Tests
Expand Down

0 comments on commit 817e65c

Please sign in to comment.