From 035c9eec044dd0893fe9d81468c7564008f34f60 Mon Sep 17 00:00:00 2001 From: myk002 Date: Mon, 22 Mar 2021 13:29:47 -0700 Subject: [PATCH 1/6] force reloading of scripts under test and invalidate scripts once tests are complete. this ensures that the IN_TEST flag is respected. --- ci/test.lua | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/ci/test.lua b/ci/test.lua index 91f39310b..114b125f0 100644 --- a/ci/test.lua +++ b/ci/test.lua @@ -82,6 +82,14 @@ local function clean_require(module) return require(module) end +-- similar to clean_require above; forces clean load of scripts directly +-- included from a test file. note that this does *not* force transitively +-- loaded scripts to be reloaded. +local function clean_reqscript(name) + dfhack.internal.scripts[dfhack.findScript(name)] = nil + return reqscript(name) +end + local function ensure_title_screen() if df.viewscreen_titlest:is_instance(dfhack.gui.getCurViewscreen()) then return @@ -129,6 +137,7 @@ local function build_test_env() expect = {}, delay = delay, require = clean_require, + reqscript = clean_reqscript, } local private = { checks = 0, @@ -182,8 +191,18 @@ local function save_test_status(status) json.encode_file(status, STATUS_FILE) end +-- causes scripts to be reloaded the next time they are reqscript()'d. this +-- allows scripts that change their behavior based on the value of +-- dfhack.internal.IN_TEST to return to non-test behavior after tests are run. +local function invalidate_scripts() + for k,_ in pairs(dfhack.internal.scripts) do + dfhack.internal.scripts[k] = nil + end +end + local function finish_tests(done_command) dfhack.internal.IN_TEST = false + invalidate_scripts() if done_command and #done_command > 0 then dfhack.run_command(done_command) end From 8ccacd94e09c5149a17f286c336e857d3159b71e Mon Sep 17 00:00:00 2001 From: myk002 Date: Wed, 24 Mar 2021 21:49:53 -0700 Subject: [PATCH 2/6] clarify clean_reqscript() function comment --- ci/test.lua | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/ci/test.lua b/ci/test.lua index 114b125f0..93ccfa349 100644 --- a/ci/test.lua +++ b/ci/test.lua @@ -83,8 +83,9 @@ local function clean_require(module) end -- similar to clean_require above; forces clean load of scripts directly --- included from a test file. note that this does *not* force transitively --- loaded scripts to be reloaded. +-- included from a test file. note that this does *not* force indirectly loaded +-- scripts (that is, scripts that are reqscript()'d by the scripts that are +-- reqscript()'d by the test file) to be reloaded. local function clean_reqscript(name) dfhack.internal.scripts[dfhack.findScript(name)] = nil return reqscript(name) From e4cab1b1c60243c30b489a645e4995b40e53c7b8 Mon Sep 17 00:00:00 2001 From: myk002 Date: Mon, 29 Mar 2021 11:26:28 -0700 Subject: [PATCH 3/6] load scripts into different namespace for testing --- ci/test.lua | 24 ++++++++---------------- library/lua/dfhack.lua | 18 ++++++++++++------ 2 files changed, 20 insertions(+), 22 deletions(-) diff --git a/ci/test.lua b/ci/test.lua index 93ccfa349..0fb6ac49c 100644 --- a/ci/test.lua +++ b/ci/test.lua @@ -82,14 +82,16 @@ local function clean_require(module) return require(module) end --- similar to clean_require above; forces clean load of scripts directly --- included from a test file. note that this does *not* force indirectly loaded --- scripts (that is, scripts that are reqscript()'d by the scripts that are --- reqscript()'d by the test file) to be reloaded. +-- 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. +local test_scripts = {is_test_scripts=true} +local test_envvars = {} local function clean_reqscript(name) - dfhack.internal.scripts[dfhack.findScript(name)] = nil - return reqscript(name) + return dfhack.script_environment(name, true, test_envvars, test_scripts) end +test_envvars.require = clean_require +test_envvars.reqscript = clean_reqscript local function ensure_title_screen() if df.viewscreen_titlest:is_instance(dfhack.gui.getCurViewscreen()) then @@ -192,18 +194,8 @@ local function save_test_status(status) json.encode_file(status, STATUS_FILE) end --- causes scripts to be reloaded the next time they are reqscript()'d. this --- allows scripts that change their behavior based on the value of --- dfhack.internal.IN_TEST to return to non-test behavior after tests are run. -local function invalidate_scripts() - for k,_ in pairs(dfhack.internal.scripts) do - dfhack.internal.scripts[k] = nil - end -end - local function finish_tests(done_command) dfhack.internal.IN_TEST = false - invalidate_scripts() if done_command and #done_command > 0 then dfhack.run_command(done_command) end diff --git a/library/lua/dfhack.lua b/library/lua/dfhack.lua index 26a23db55..f3956d174 100644 --- a/library/lua/dfhack.lua +++ b/library/lua/dfhack.lua @@ -562,7 +562,6 @@ function Script:get_flags() end internal.scripts = internal.scripts or {} -local scripts = internal.scripts local hack_path = dfhack.getHackPath() @@ -583,6 +582,7 @@ local valid_script_flags = { module_strict = {required = false}, alias = {required = false}, alias_count = {required = false}, + scripts = {required = false}, } function dfhack.run_script(name,...) @@ -602,13 +602,18 @@ function dfhack.reqscript(name) end reqscript = dfhack.reqscript -function dfhack.script_environment(name, strict) +function dfhack.script_environment(name, strict, envVars, scripts) + scripts = scripts or internal.scripts local path = dfhack.findScript(name) if not scripts[path] or scripts[path]:needs_update() then - local _, env = dfhack.run_script_with_env(nil, name, { - module=true, - module_strict=(strict and true or false) -- ensure that this key is present if 'strict' is nil - }) + local _, env = dfhack.run_script_with_env( + envVars, + name, + { + scripts=scripts, + module=true, + module_strict=(strict and true or false) -- ensure that this key is present if 'strict' is nil + }) return env else if strict and not scripts[path]:get_flags().module then @@ -625,6 +630,7 @@ function dfhack.run_script_with_env(envVars, name, flags, ...) error('Could not find script ' .. name) end + local scripts = flags.scripts or internal.scripts if scripts[file] == nil then scripts[file] = Script(file) end From 54a1e8d98a9fd0607ce3b93832dd75642ebc82a7 Mon Sep 17 00:00:00 2001 From: myk002 Date: Mon, 29 Mar 2021 12:23:53 -0700 Subject: [PATCH 4/6] move script_environment changes to test.lua --- ci/test.lua | 12 +++++++++++- library/lua/dfhack.lua | 8 +++----- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/ci/test.lua b/ci/test.lua index 0fb6ac49c..bdd7be8d8 100644 --- a/ci/test.lua +++ b/ci/test.lua @@ -88,7 +88,17 @@ end local test_scripts = {is_test_scripts=true} local test_envvars = {} local function clean_reqscript(name) - return dfhack.script_environment(name, true, test_envvars, test_scripts) + local path = dfhack.findScript(name) + if test_scripts[path] then return test_scripts[path].env end + local _, env = dfhack.run_script_with_env( + test_envvars, + name, + { + scripts=test_scripts, + module=true, + module_strict=true + }) + return env end test_envvars.require = clean_require test_envvars.reqscript = clean_reqscript diff --git a/library/lua/dfhack.lua b/library/lua/dfhack.lua index f3956d174..b19dd3877 100644 --- a/library/lua/dfhack.lua +++ b/library/lua/dfhack.lua @@ -602,15 +602,13 @@ function dfhack.reqscript(name) end reqscript = dfhack.reqscript -function dfhack.script_environment(name, strict, envVars, scripts) - scripts = scripts or internal.scripts +function dfhack.script_environment(name, strict) + local scripts = internal.scripts local path = dfhack.findScript(name) if not scripts[path] or scripts[path]:needs_update() then - local _, env = dfhack.run_script_with_env( - envVars, + local _, env = dfhack.run_script_with_env(nil, name, { - scripts=scripts, module=true, module_strict=(strict and true or false) -- ensure that this key is present if 'strict' is nil }) From d71a816b203f04f2fb8d32de3d296d83ae2d1f9e Mon Sep 17 00:00:00 2001 From: myk002 Date: Mon, 29 Mar 2021 12:28:19 -0700 Subject: [PATCH 5/6] remove debug marker --- ci/test.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ci/test.lua b/ci/test.lua index bdd7be8d8..35e93fd12 100644 --- a/ci/test.lua +++ b/ci/test.lua @@ -85,7 +85,7 @@ 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. -local test_scripts = {is_test_scripts=true} +local test_scripts = {} local test_envvars = {} local function clean_reqscript(name) local path = dfhack.findScript(name) From 4e844f39ae3410e90d36cfd11f452f220c353624 Mon Sep 17 00:00:00 2001 From: myk002 Date: Mon, 29 Mar 2021 12:29:15 -0700 Subject: [PATCH 6/6] undo formatting change in script_environment --- library/lua/dfhack.lua | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/library/lua/dfhack.lua b/library/lua/dfhack.lua index b19dd3877..d1e664af7 100644 --- a/library/lua/dfhack.lua +++ b/library/lua/dfhack.lua @@ -606,12 +606,10 @@ function dfhack.script_environment(name, strict) local scripts = internal.scripts local path = dfhack.findScript(name) if not scripts[path] or scripts[path]:needs_update() then - local _, env = dfhack.run_script_with_env(nil, - name, - { - module=true, - module_strict=(strict and true or false) -- ensure that this key is present if 'strict' is nil - }) + local _, env = dfhack.run_script_with_env(nil, name, { + module=true, + module_strict=(strict and true or false) -- ensure that this key is present if 'strict' is nil + }) return env else if strict and not scripts[path]:get_flags().module then