Skip to content

Commit ccb2a74

Browse files
lukaszsamsonjosevalim
authored andcommitted
Fix crash when format_kind_reason is executed with TestModule (#14901)
Closes #14900.
1 parent 92094c9 commit ccb2a74

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

lib/ex_unit/lib/ex_unit/formatter.ex

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,14 @@ defmodule ExUnit.Formatter do
338338
{formatted_reason, wrapped_stack} =
339339
format_exception(test, struct, wrapped_stack, width, formatter, @counter_padding)
340340

341-
formatted_stack = format_stacktrace(wrapped_stack, test.module, test.name, formatter)
341+
formatted_stack =
342+
format_stacktrace(
343+
wrapped_stack,
344+
stacktrace_case(test),
345+
stacktrace_test(test),
346+
formatter
347+
)
348+
342349
{error_info(header, formatter) <> pad(formatted_reason <> formatted_stack), stack}
343350

344351
:error ->
@@ -367,6 +374,14 @@ defmodule ExUnit.Formatter do
367374

368375
defp linked_or_trapped_exit(_kind, _reason), do: :error
369376

377+
defp stacktrace_case(%ExUnit.Test{module: module}), do: module
378+
defp stacktrace_case(%ExUnit.TestModule{name: name}), do: name
379+
defp stacktrace_case(_), do: nil
380+
381+
defp stacktrace_test(%ExUnit.Test{name: name}), do: name
382+
defp stacktrace_test(%ExUnit.TestModule{}), do: nil
383+
defp stacktrace_test(_), do: nil
384+
370385
defp format_exception(test, %ExUnit.AssertionError{} = struct, stack, width, formatter, pad) do
371386
label_padding_size = if has_value?(struct.right), do: 7, else: 6
372387
padding_size = label_padding_size + byte_size(@counter_padding)

lib/ex_unit/test/ex_unit/formatter_test.exs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,21 @@ defmodule ExUnit.FormatterTest do
465465
"""
466466
end
467467

468+
test "formats setup_all exit stacktraces using module context" do
469+
stacktrace = [
470+
{Hello, :setup_all, [], [file: __ENV__.file, line: 123]},
471+
{Hello, :some_fun, [], [file: __ENV__.file, line: 456]}
472+
]
473+
474+
failure = [{{:EXIT, self()}, {:function_clause, stacktrace}, []}]
475+
476+
formatted =
477+
format_test_all_failure(test_module(), failure, 1, 80, &formatter/2)
478+
479+
assert formatted =~ "Hello.setup_all/0"
480+
assert formatted =~ "Hello.some_fun()"
481+
end
482+
468483
test "formats assertions with operators with no limit" do
469484
failure = [{:error, catch_assertion(assert [1, 2, 3] == [4, 5, 6]), []}]
470485

0 commit comments

Comments
 (0)