From 837b3afbeb6dcc1609c7bd2798da74dc0fa1e251 Mon Sep 17 00:00:00 2001 From: Jason Perkins Date: Wed, 31 Oct 2012 19:34:04 -0400 Subject: [PATCH] Improve stack traces from test failures --- tests/testfx.lua | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/tests/testfx.lua b/tests/testfx.lua index ce281240..5c3279d2 100644 --- a/tests/testfx.lua +++ b/tests/testfx.lua @@ -75,7 +75,9 @@ arg[i] = "{" .. table.concat(arg[i], ", ") .. "}" end end - error(string.format(format, unpack(arg)), 3) + + local msg = string.format(format, unpack(arg)) + error(debug.traceback(msg, 3), 3) end @@ -202,8 +204,25 @@ local _OS_host = _OS local function error_handler(err) - local msg = debug.traceback(err, 2) - msg = msg:sub(1, msg:find("[C]", 1, true) - 3) + local msg = err + + -- if the error doesn't include a stack trace, add one + if not msg:find("stack traceback:", 1, true) then + msg = debug.traceback(err, 2) + end + + -- trim of the trailing context of the originating xpcall + local i = msg:find("[C]: in function 'xpcall'", 1, true) + if i then + msg = msg:sub(1, i - 3) + end + + -- if the resulting stack trace is only one level deep, ignore it + local n = select(2, msg:gsub('\n', '\n')) + if n == 2 then + msg = msg:sub(1, msg:find('\n', 1, true) - 1) + end + return msg end @@ -269,7 +288,7 @@ local tok, terr = test_teardown(suitetests, testfunc) ok = ok and tok - err = err or tok + err = err or terr if (not ok) then test.print(string.format("%s.%s: %s", suitename, testname, err))