Skip to content

Commit

Permalink
Fix utils.strip_luatest_trace
Browse files Browse the repository at this point in the history
 * Strip uselss `...` and `eval` lines.
 * Scan the stack trace backwards so as to skip `[C]`, `...`, and `eval`
   lines that were called by luatest internal files, not vice versa.
 * Don't strip lines related to luatest test files (from luatest/test),
   even though they match the luatest internal file pattern, because
   we do want to see stack traces when we test luatest.

Needed for #396
  • Loading branch information
locker committed Oct 25, 2024
1 parent 8254673 commit 186f595
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
- Fix reporting of an assertion failure in `Server:exec()` in case verbose
error serialization is enabled in Tarantool (gh-376).
- Added `assert_items_exclude`.
- Strip useless `...` lines from error trace.

## 1.0.1

Expand Down
34 changes: 29 additions & 5 deletions luatest/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -61,23 +61,47 @@ end

-- Check if line of stack trace comes from inside luatest.
local function is_luatest_internal_line(s)
return s:find('[/\\]luatest[/\\]') or s:find('bin[/\\]luatest')
if s:find('bin[/\\]luatest') then
return true
end
if s:find('[/\\]luatest[/\\]') then
-- Do not strip lines originating from luatest test files because
-- we want to see stack traces when we test luatest.
return not s:find('[/\\]luatest[/\\]test[/\\]')
end
return false
end

function utils.strip_luatest_trace(trace)
local lines = trace:split('\n')
local result = {lines[1]} -- always keep 1st line

-- Scan the stack trace backwards (from caller to callee) and strip all
-- frames related to luatest, as well as `[C]:`, `...`, `eval:` frames
-- called by them because they don't change context.
local result = {}
local keep = true
for i = 2, table.maxn(lines) do
for i = table.maxn(lines), 2, -1 do
local line = lines[i]
-- `[C]:` lines don't change context
if not line:find('^%s+%[C%]:') then
if not (line:find('^%s+%[C%]:') or
line:find('^%s+%.%.%.$') or
line:find('^%s+eval:')) then
keep = not is_luatest_internal_line(line)
end
if keep then
table.insert(result, line)
end
end

-- Always keep the 1st line because it's the header ('stack traceback:').
table.insert(result, lines[1])

-- Since we scanned the stack trace backwards, we need to reverse
-- the result.
for i = 1, math.floor(#result / 2) do
local v = result[#result - i + 1]
result[#result - i + 1] = result[i]
result[i] = v
end
return table.concat(result, '\n')
end

Expand Down
37 changes: 37 additions & 0 deletions test/luaunit/utility_test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -847,6 +847,43 @@ function g.test_stripStackTrace()
[[stack traceback:
luaunit2/example_with_luaunit.lua:124: in function 'test1_withFailure']]
)

t.assert_equals(subject([[stack traceback:
/tmp/luatest/luatest/utils.lua:55: in function 'luatest_error'
/tmp/luatest/luatest/assertions.lua:66: in function 'failure'
/tmp/luatest/luatest/assertions.lua:71: in function 'fail_fmt'
/tmp/luatest/luatest/assertions.lua:341: in function 'assert_le'
/tmp/luatest/test/example_test.lua:37: in function 'check'
/tmp/luatest/test/example_test.lua:40: in function </tmp/luatest/test/example_test.lua:34>
[C]: in function 'xpcall'
eval:9: in main chunk
[C]: at 0x5da8d110ced6
/tmp/luatest/luatest/server.lua:745: in function 'exec'
/tmp/luatest/test/example_test.lua:34: in function 'test.test_fail_server'
/tmp/luatest/luatest/runner.lua:472: in function </tmp/luatest/luatest/runner.lua:471>
[C]: in function 'xpcall'
/tmp/luatest/luatest/runner.lua:471: in function 'super'
/tmp/luatest/luatest/capturing.lua:106: in function 'protected_call'
/tmp/luatest/luatest/runner.lua:559: in function 'super'
/tmp/luatest/luatest/hooks.lua:290: in function 'invoke_test_function'
/tmp/luatest/luatest/runner.lua:554: in function 'super'
...
[C]: in function 'xpcall'
/tmp/luatest/luatest/utils.lua:39: in function 'run_tests'
/tmp/luatest/luatest/runner.lua:381: in function </tmp/luatest/luatest/runner.lua:366>
[C]: in function 'xpcall'
/tmp/luatest/luatest/capturing.lua:74: in function </tmp/luatest/luatest/capturing.lua:72>
[C]: in function 'xpcall'
/tmp/luatest/luatest/runner.lua:55: in function 'fn'
.../vlad/src/tarantool/luatest/luatest/sandboxed_runner.lua:14: in function 'run'
/tmp/luatest/luatest/cli_entrypoint.lua:4: in function </tmp/luatest/luatest/cli_entrypoint.lua:3>
/tmp/luatest/bin/luatest:5: in main chunk]]
),
[[stack traceback:
/tmp/luatest/test/example_test.lua:37: in function 'check'
/tmp/luatest/test/example_test.lua:40: in function </tmp/luatest/test/example_test.lua:34>
/tmp/luatest/test/example_test.lua:34: in function 'test.test_fail_server']]
)
end

function g.test_eps_value()
Expand Down

0 comments on commit 186f595

Please sign in to comment.