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) return require(module)
end end
-- similar to clean_require above; forces clean load of scripts directly -- forces clean load of scripts directly or indirectly included from the test
-- included from a test file. note that this does *not* force indirectly loaded -- file. we use our own scripts table instead of the one in dfhack.internal so
-- scripts (that is, scripts that are reqscript()'d by the scripts that are -- we don't affect the state scripts that are used outside the test harness.
-- reqscript()'d by the test file) to be reloaded. local test_scripts = {is_test_scripts=true}
local test_envvars = {}
local function clean_reqscript(name) local function clean_reqscript(name)
dfhack.internal.scripts[dfhack.findScript(name)] = nil return dfhack.script_environment(name, true, test_envvars, test_scripts)
return reqscript(name)
end end
test_envvars.require = clean_require
test_envvars.reqscript = clean_reqscript
local function ensure_title_screen() local function ensure_title_screen()
if df.viewscreen_titlest:is_instance(dfhack.gui.getCurViewscreen()) then 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) json.encode_file(status, STATUS_FILE)
end 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) local function finish_tests(done_command)
dfhack.internal.IN_TEST = false dfhack.internal.IN_TEST = false
invalidate_scripts()
if done_command and #done_command > 0 then if done_command and #done_command > 0 then
dfhack.run_command(done_command) dfhack.run_command(done_command)
end end

@ -562,7 +562,6 @@ function Script:get_flags()
end end
internal.scripts = internal.scripts or {} internal.scripts = internal.scripts or {}
local scripts = internal.scripts
local hack_path = dfhack.getHackPath() local hack_path = dfhack.getHackPath()
@ -583,6 +582,7 @@ local valid_script_flags = {
module_strict = {required = false}, module_strict = {required = false},
alias = {required = false}, alias = {required = false},
alias_count = {required = false}, alias_count = {required = false},
scripts = {required = false},
} }
function dfhack.run_script(name,...) function dfhack.run_script(name,...)
@ -602,13 +602,18 @@ function dfhack.reqscript(name)
end end
reqscript = dfhack.reqscript 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) local path = dfhack.findScript(name)
if not scripts[path] or scripts[path]:needs_update() then if not scripts[path] or scripts[path]:needs_update() then
local _, env = dfhack.run_script_with_env(nil, name, { local _, env = dfhack.run_script_with_env(
module=true, envVars,
module_strict=(strict and true or false) -- ensure that this key is present if 'strict' is nil name,
}) {
scripts=scripts,
module=true,
module_strict=(strict and true or false) -- ensure that this key is present if 'strict' is nil
})
return env return env
else else
if strict and not scripts[path]:get_flags().module then 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) error('Could not find script ' .. name)
end end
local scripts = flags.scripts or internal.scripts
if scripts[file] == nil then if scripts[file] == nil then
scripts[file] = Script(file) scripts[file] = Script(file)
end end