make save_dir configurable, use load-save to load

develop
myk002 2021-04-19 20:16:58 -07:00
parent f8baba2cfd
commit eaf0722cdf
No known key found for this signature in database
GPG Key ID: 8A39CA0FA0C16E78
1 changed files with 39 additions and 24 deletions

@ -27,14 +27,26 @@ Options:
-d, --test_dir specifies which directory to look in for tests. defaults to -d, --test_dir specifies which directory to look in for tests. defaults to
the "hack/scripts/test" folder in your DF installation. the "hack/scripts/test" folder in your DF installation.
-m, --modes only run tests in the given comma separated list of modes. -m, --modes only run tests in the given comma separated list of modes.
valid modes are 'none' (test can be run on any screen) and see the next section for a list of valid modes. if not
'title' (test must be run on the DF title screen). if not specified, the tests are not filtered by modes.
specified, no modes are filtered.
-r, --resume skip tests that have already been run. remove the -r, --resume skip tests that have already been run. remove the
test_status.json file to reset the record. test_status.json file to reset the record.
-s, --save_dir the save folder to load for "fortress" mode tests. this
save is only loaded if a fort is not already loaded when
a "fortress" mode test is run. if not specified, defaults to
'region1'.
-t, --tests only run tests that match one of the comma separated list of -t, --tests only run tests that match one of the comma separated list of
patterns. if not specified, no tests are filtered. patterns. if not specified, no tests are filtered.
Modes:
none the test can be run on any screen
title the test must be run on the DF title screen. note that if the game
has a map loaded, "title" mode tests cannot be run
fortress the test must be run while a map is loaded. if the game is
currently on the title screen, the save specified by the save_dir
parameter will be loaded.
Examples: Examples:
test runs all tests test runs all tests
@ -145,7 +157,7 @@ end
-- Requires that a fortress game is already loaded or is ready to be loaded via -- Requires that a fortress game is already loaded or is ready to be loaded via
-- the "Continue Playing" option in the title screen. Otherwise the function -- the "Continue Playing" option in the title screen. Otherwise the function
-- will time out and/or exit with error. -- will time out and/or exit with error.
local function ensure_fortress() local function ensure_fortress(config)
local focus_string = dfhack.gui.getCurFocus(true) local focus_string = dfhack.gui.getCurFocus(true)
for screen_timeout = 1,10 do for screen_timeout = 1,10 do
if is_fortress(focus_string) then if is_fortress(focus_string) then
@ -155,21 +167,17 @@ local function ensure_fortress()
return return
end end
local scr = dfhack.gui.getCurViewscreen() local scr = dfhack.gui.getCurViewscreen()
if focus_string == 'title' then if focus_string == 'title' or
scr:feed_key(df.interface_key.SELECT) focus_string == 'dfhack/lua/load_screen' then
scr:feed_key(df.interface_key.SELECT) -- qerror()'s on falure
elseif focus_string == 'dfhack/lua/load_screen' or dfhack.run_script('load-save', config.save_dir)
focus_string == 'dfhack/lua' then
scr:feed_key(df.interface_key.SELECT)
elseif focus_string == 'new_region' or
focus_string == 'adopt_region' then
qerror('Please ensure a fortress save exists in region1/')
elseif focus_string ~= 'loadgame' then elseif focus_string ~= 'loadgame' then
-- if we're not actively loading a game, assume we're in -- if we're not actively loading a game, hope we're in
-- a loaded fortress, but in some subscreen -- a screen where hitting ESC will get us to the game map
-- or the title screen
scr:feed_key(df.interface_key.LEAVESCREEN) scr:feed_key(df.interface_key.LEAVESCREEN)
end end
-- wait for active screen to change -- wait for current screen to change
local prev_focus_string = focus_string local prev_focus_string = focus_string
for frame_timeout = 1,100 do for frame_timeout = 1,100 do
delay(10) delay(10)
@ -207,6 +215,10 @@ local function load_test_config(config_file)
config.test_dir = dfhack.getHackPath() .. 'scripts/test' config.test_dir = dfhack.getHackPath() .. 'scripts/test'
end end
if not config.save_dir then
config.save_dir = 'region1'
end
return config return config
end end
@ -470,13 +482,13 @@ local function filter_tests(tests, config)
return status return status
end end
local function run_tests(tests, status, counts) local function run_tests(tests, status, counts, config)
print(('Running %d tests'):format(#tests)) print(('Running %d tests'):format(#tests))
for _, test in pairs(tests) do for _, test in pairs(tests) do
status[test.full_name] = TestStatus.FAILED status[test.full_name] = TestStatus.FAILED
if MODES[test.config.mode].failed then goto skip end if MODES[test.config.mode].failed then goto skip end
if not MODES[test.config.mode].detect() then if not MODES[test.config.mode].detect() then
local ok, err = pcall(MODES[test.config.mode].navigate) local ok, err = pcall(MODES[test.config.mode].navigate, config)
if not ok then if not ok then
MODES[test.config.mode].failed = true MODES[test.config.mode].failed = true
dfhack.printerr(tostring(err)) dfhack.printerr(tostring(err))
@ -511,17 +523,19 @@ local function run_tests(tests, status, counts)
end end
local function main(args) local function main(args)
local help, resume, test_dir, mode_filter, test_filter = local help, resume, test_dir, mode_filter, save_dir, test_filter =
false, false, nil, {}, {} false, false, nil, {}, nil, {}
local other_args = utils.processArgsGetopt(args, { local other_args = utils.processArgsGetopt(args, {
{'h', 'help', handler=function() help = true end}, {'h', 'help', handler=function() help = true end},
{'d', 'test_dir', hasArg=true, {'d', 'test_dir', hasArg=true,
handler=function(arg) test_dir = arg end}, handler=function(arg) test_dir = arg end},
{'m', 'modes', hasArg=true, {'m', 'modes', hasArg=true,
handler=function(arg) mode_filter = arg:split(',') end}, handler=function(arg) mode_filter = arg:split(',') end},
{'r', 'resume', handler=function() resume = true end}, {'r', 'resume', handler=function() resume = true end},
{'s', 'save_dir', hasArg=true,
handler=function(arg) save_dir = arg end},
{'t', 'tests', hasArg=true, {'t', 'tests', hasArg=true,
handler=function(arg) test_filter = arg:split(',') end}, handler=function(arg) test_filter = arg:split(',') end},
}) })
if help then print(help_text) return end if help then print(help_text) return end
@ -532,6 +546,7 @@ local function main(args)
-- override config with any params specified on the commandline -- override config with any params specified on the commandline
if test_dir then config.test_dir = test_dir end if test_dir then config.test_dir = test_dir end
if resume then config.resume = true end if resume then config.resume = true end
if save_dir then config.save_dir = save_dir end
if #mode_filter > 0 then config.modes = mode_filter end if #mode_filter > 0 then config.modes = mode_filter end
if #test_filter > 0 then config.tests = test_filter end if #test_filter > 0 then config.tests = test_filter end
if #done_command > 0 then config.done_command = done_command end if #done_command > 0 then config.done_command = done_command end
@ -555,7 +570,7 @@ local function main(args)
script.start(function() script.start(function()
dfhack.call_with_finalizer(1, true, dfhack.call_with_finalizer(1, true,
finish_tests, config.done_command, finish_tests, config.done_command,
run_tests, tests, status, counts) run_tests, tests, status, counts, config)
end) end)
end end