load scripts into different namespace for testing

develop
myk002 2021-03-29 11:26:28 -07:00
parent 8ccacd94e0
commit e4cab1b1c6
No known key found for this signature in database
GPG Key ID: 8A39CA0FA0C16E78
2 changed files with 20 additions and 22 deletions

@ -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

@ -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