Skip to content

Commit db70fd5

Browse files
author
José Valim
committed
Also support on_exit on setup_all
1 parent 513ed0b commit db70fd5

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

lib/ex_unit/lib/ex_unit/runner.ex

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,8 @@ defmodule ExUnit.Runner do
142142

143143
{case_pid, case_ref} =
144144
spawn_monitor(fn ->
145+
ExUnit.OnExitHandler.register(self)
146+
145147
case exec_case_setup(test_case) do
146148
{:ok, test_case, context} ->
147149
Enum.each(tests, &run_test(config, &1, context))
@@ -154,12 +156,16 @@ defmodule ExUnit.Runner do
154156
end
155157
end)
156158

157-
receive do
158-
{^case_pid, :case_finished, test_case, tests} ->
159-
{test_case, tests}
160-
{:DOWN, ^case_ref, :process, ^case_pid, error} ->
161-
{%{test_case | state: {:failed, {{:EXIT, case_pid}, error, []}}}, []}
162-
end
159+
{test_case, pending} =
160+
receive do
161+
{^case_pid, :case_finished, test_case, tests} ->
162+
{test_case, tests}
163+
{:DOWN, ^case_ref, :process, ^case_pid, error} ->
164+
test_case = %{test_case | state: {:failed, {{:EXIT, case_pid}, error, []}}}
165+
{test_case, []}
166+
end
167+
168+
{exec_on_exit(test_case, case_pid), pending}
163169
end
164170

165171
defp exec_case_setup(%ExUnit.TestCase{name: case_name} = test_case) do
@@ -249,13 +255,13 @@ defmodule ExUnit.Runner do
249255
%{test | state: state}
250256
end
251257

252-
defp exec_on_exit(test, test_pid) do
253-
case ExUnit.OnExitHandler.run(test_pid) do
258+
defp exec_on_exit(test_or_case, pid) do
259+
case ExUnit.OnExitHandler.run(pid) do
254260
:ok ->
255-
test
261+
test_or_case
256262
{kind, reason, stack} ->
257-
state = test.state || {:failed, {kind, reason, prune_stacktrace(stack)}}
258-
%{test | state: state}
263+
state = test_or_case.state || {:failed, {kind, reason, prune_stacktrace(stack)}}
264+
%{test_or_case | state: state}
259265
end
260266
end
261267

lib/ex_unit/test/ex_unit/callbacks_test.exs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,12 @@ defmodule ExUnit.CallbacksTest do
184184
end
185185
end
186186

187+
setup_all do
188+
on_exit fn ->
189+
IO.puts "on_exit setup_all run"
190+
end
191+
end
192+
187193
test "ok" do
188194
on_exit fn ->
189195
IO.puts "simple on_exit run"
@@ -213,6 +219,7 @@ defmodule ExUnit.CallbacksTest do
213219
simple on_exit run
214220
on_exit 1 overrides -> run
215221
on_exit setup run
222+
on_exit setup_all run
216223
"""
217224

218225
refute output =~ "not run"

0 commit comments

Comments
 (0)