From 12315fc5dd7880fc9b3a0c2c47daebcfefe83a52 Mon Sep 17 00:00:00 2001 From: myk002 Date: Tue, 6 Apr 2021 11:33:49 -0700 Subject: [PATCH] wrap dfhack.run_script for tests so dfhack.internal.scripts isn't polluted with IN_TEST envs --- ci/test.lua | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/ci/test.lua b/ci/test.lua index 2209f58b8..5c12463f2 100644 --- a/ci/test.lua +++ b/ci/test.lua @@ -82,11 +82,23 @@ local function clean_require(module) return require(module) end --- forces clean load of scripts directly or indirectly included from the test --- file. we use our own scripts table instead of the one in dfhack.internal so --- we don't affect the state scripts that are used outside the test harness. +-- clean_run_script and clean_reqscript force a clean load of scripts directly +-- or indirectly included from the test file. we use our own scripts table +-- instead of the one in dfhack.internal so we don't affect the state scripts +-- that are used outside the test harness. local test_scripts = {} local test_envvars = {} + +-- clean_run_script is accessed via the dfhack table, not directly from the env. +-- therefore we use this function in wrap_test() below and not in test_envvars. +local function clean_run_script(name, ...) + return dfhack.run_script_with_env( + test_envvars, + name, + {scripts=test_scripts}, + ...) +end + local function clean_reqscript(name) local path = dfhack.findScript(name) if test_scripts[path] then return test_scripts[path].env end @@ -266,16 +278,20 @@ local function sort_tests(tests) end) end -local function detect_printerr(func) - local saved_printerr = dfhack.printerr +local function wrap_test(func) + local saved_printerr, saved_run_script = dfhack.printerr, dfhack.run_script local printerr_called = false dfhack.printerr = function(msg) if msg == nil then return end saved_printerr(msg) printerr_called = true end + dfhack.run_script = clean_run_script return dfhack.with_finalize( - function() dfhack.printerr = saved_printerr end, + function() + dfhack.printerr = saved_printerr + dfhack.run_script = saved_run_script + end, function() local ok, err = pcall(func) if printerr_called then @@ -294,7 +310,7 @@ local function run_test(test, status, counts) test.private.checks_ok = 0 counts.tests = counts.tests + 1 dfhack.internal.IN_TEST = true - local ok, err = detect_printerr(test.func) + local ok, err = wrap_test(test.func) dfhack.internal.IN_TEST = false local passed = false if not ok then