From c8abecd08268b5ce26317484b5e5009af0137118 Mon Sep 17 00:00:00 2001 From: myk002 Date: Sat, 3 Apr 2021 15:40:40 -0700 Subject: [PATCH] detect dfhack.printerr usage at the test level not the expect level. this allows us to properly nest expect calls inside of expect.printerr_match() as originally intended. --- ci/test.lua | 54 +++++++++++++++++++++++++++++------------------------ 1 file changed, 30 insertions(+), 24 deletions(-) diff --git a/ci/test.lua b/ci/test.lua index 596b2c716..80b275643 100644 --- a/ci/test.lua +++ b/ci/test.lua @@ -141,30 +141,14 @@ local function load_test_config(config_file) return config end -local function run_expect_func(func, ...) - local args = {...} - local saved_printerr = dfhack.printerr - local printerr_called = false - dfhack.printerr = function(msg) printerr_called = true end - return dfhack.with_finalize( - function() dfhack.printerr = saved_printerr end, - function() - local ret = {func(table.unpack(args))} - if printerr_called then - return {false, - "dfhack.printerr was called outside of" .. - " expect.printerr_match(). please wrap your test" .. - " with expect.printerr_match()."} - end - return ret - end - ) -end - +-- we have to save and use the original dfhack.printerr here so our test harness +-- output doesn't trigger its own dfhack.printerr usage detection (see +-- detect_printerr below) +local orig_printerr = dfhack.printerr local function wrap_expect(func, private) return function(...) private.checks = private.checks + 1 - local ret = run_expect_func(func, ...) + local ret = {func(...)} local ok = table.remove(ret, 1) if ok then private.checks_ok = private.checks_ok + 1 @@ -177,9 +161,9 @@ local function wrap_expect(func, private) end end msg = msg:sub(3) -- strip leading ': ' - dfhack.printerr('Check failed! ' .. (msg or '(no message)')) + orig_printerr('Check failed! ' .. (msg or '(no message)')) local info = debug.getinfo(2) - dfhack.printerr((' at %s:%d'):format(info.short_src, info.currentline)) + orig_printerr((' at %s:%d'):format(info.short_src, info.currentline)) print('') end end @@ -282,12 +266,34 @@ local function sort_tests(tests) end) end +local function detect_printerr(func) + local saved_printerr = dfhack.printerr + local printerr_called = false + dfhack.printerr = function(msg) + saved_printerr(msg) + printerr_called = true + end + return dfhack.with_finalize( + function() dfhack.printerr = saved_printerr end, + function() + local ok, err = pcall(func) + if printerr_called then + return false, + "dfhack.printerr was called outside of" .. + " expect.printerr_match(). please wrap your test" .. + " with expect.printerr_match()." + end + return ok, err + end + ) +end + local function run_test(test, status, counts) test.private.checks = 0 test.private.checks_ok = 0 counts.tests = counts.tests + 1 dfhack.internal.IN_TEST = true - local ok, err = pcall(test.func) + local ok, err = detect_printerr(test.func) dfhack.internal.IN_TEST = false local passed = false if not ok then