Ensure that test stack frames in the test file are printed

Previously, only frames in the file that called `expect.*()` were printed.
This change allows calling `expect.*()` from functions called by the files
under test. See dfhack/scripts#385 for an example with `expect.fail()`.
develop
lethosor 2022-05-27 01:03:40 -04:00
parent 888c531774
commit 71d003c77d
No known key found for this signature in database
GPG Key ID: 76A269552F4F58C1
1 changed files with 6 additions and 6 deletions

@ -240,7 +240,7 @@ end
-- output doesn't trigger its own dfhack.printerr usage detection (see -- output doesn't trigger its own dfhack.printerr usage detection (see
-- detect_printerr below) -- detect_printerr below)
local orig_printerr = dfhack.printerr local orig_printerr = dfhack.printerr
local function wrap_expect(func, private) local function wrap_expect(func, private, path)
return function(...) return function(...)
private.checks = private.checks + 1 private.checks = private.checks + 1
local ret = {func(...)} local ret = {func(...)}
@ -269,7 +269,7 @@ local function wrap_expect(func, private)
end end
-- Skip any frames corresponding to C calls, or Lua functions defined in another file -- Skip any frames corresponding to C calls, or Lua functions defined in another file
-- these could include pcall(), with_finalize(), etc. -- these could include pcall(), with_finalize(), etc.
if info.what == 'Lua' and info.short_src == caller_src then if info.what == 'Lua' and (info.short_src == caller_src or info.short_src == path) then
orig_printerr((' at %s:%d'):format(info.short_src, info.currentline)) orig_printerr((' at %s:%d'):format(info.short_src, info.currentline))
end end
frame = frame + 1 frame = frame + 1
@ -278,7 +278,7 @@ local function wrap_expect(func, private)
end end
end end
local function build_test_env() local function build_test_env(path)
local env = { local env = {
test = utils.OrderedTable(), test = utils.OrderedTable(),
-- config values can be overridden in the test file to define -- config values can be overridden in the test file to define
@ -309,7 +309,7 @@ local function build_test_env()
checks_ok = 0, checks_ok = 0,
} }
for name, func in pairs(expect) do for name, func in pairs(expect) do
env.expect[name] = wrap_expect(func, private) env.expect[name] = wrap_expect(func, private, path)
end end
setmetatable(env, {__index = _G}) setmetatable(env, {__index = _G})
return env, private return env, private
@ -345,9 +345,9 @@ local function finish_tests(done_command)
end end
local function load_tests(file, tests) local function load_tests(file, tests)
local short_filename = file:sub((file:find('test') or -4)+5, -1) local short_filename = file:sub((file:find('test') or -4) + 5, -1)
print('Loading file: ' .. short_filename) print('Loading file: ' .. short_filename)
local env, env_private = build_test_env() local env, env_private = build_test_env(file)
local code, err = loadfile(file, 't', env) local code, err = loadfile(file, 't', env)
if not code then if not code then
dfhack.printerr('Failed to load file: ' .. tostring(err)) dfhack.printerr('Failed to load file: ' .. tostring(err))