Skip to content

Commit ecac839

Browse files
committed
Load async and module tags in runner, closes #13639
1 parent c69938b commit ecac839

File tree

3 files changed

+19
-18
lines changed

3 files changed

+19
-18
lines changed

lib/ex_unit/lib/ex_unit/case.ex

+1-3
Original file line numberDiff line numberDiff line change
@@ -545,22 +545,20 @@ defmodule ExUnit.Case do
545545
end
546546

547547
@doc false
548-
defmacro __before_compile__(%{module: module} = env) do
548+
defmacro __before_compile__(%{module: module}) do
549549
tests =
550550
module
551551
|> Module.get_attribute(:ex_unit_tests)
552552
|> Enum.reverse()
553553
|> Macro.escape()
554554

555555
moduletag = Module.get_attribute(module, :moduletag)
556-
{async?, _parameterize} = Module.get_attribute(module, :ex_unit_module)
557556

558557
tags =
559558
moduletag
560559
|> normalize_tags()
561560
|> validate_tags()
562561
|> Map.new()
563-
|> Map.merge(%{module: module, case: env.module, async: async?})
564562

565563
quote do
566564
def __ex_unit__ do

lib/ex_unit/lib/ex_unit/runner.ex

+16-14
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ defmodule ExUnit.Runner do
107107

108108
# Slots are available, start with async modules
109109
async_modules = ExUnit.Server.take_async_modules(available) ->
110-
running = spawn_modules(config, async_modules, running)
110+
running = spawn_modules(config, async_modules, true, running)
111111
modules_to_restore = maybe_store_modules(modules_to_restore, :async, async_modules)
112112
async_loop(config, running, true, modules_to_restore)
113113

@@ -125,7 +125,7 @@ defmodule ExUnit.Runner do
125125

126126
# Run all sync modules directly
127127
for pair <- sync_modules do
128-
running = spawn_modules(config, [pair], %{})
128+
running = spawn_modules(config, [pair], false, %{})
129129
running != %{} and wait_until_available(config, running)
130130
end
131131

@@ -157,16 +157,16 @@ defmodule ExUnit.Runner do
157157
end
158158
end
159159

160-
defp spawn_modules(_config, [], running) do
160+
defp spawn_modules(_config, [], _async, running) do
161161
running
162162
end
163163

164-
defp spawn_modules(config, [{module, params} | modules], running) do
164+
defp spawn_modules(config, [{module, params} | modules], async?, running) do
165165
if max_failures_reached?(config) do
166166
running
167167
else
168-
{pid, ref} = spawn_monitor(fn -> run_module(config, module, params) end)
169-
spawn_modules(config, modules, Map.put(running, ref, pid))
168+
{pid, ref} = spawn_monitor(fn -> run_module(config, module, async?, params) end)
169+
spawn_modules(config, modules, async?, Map.put(running, ref, pid))
170170
end
171171
end
172172

@@ -221,20 +221,20 @@ defmodule ExUnit.Runner do
221221

222222
## Running modules
223223

224-
defp run_module(config, module, params) do
224+
defp run_module(config, module, async?, params) do
225225
test_module = %{module.__ex_unit__() | parameters: params}
226226
EM.module_started(config.manager, test_module)
227227

228228
# Prepare tests, selecting which ones should be run or skipped
229-
{to_run_tests, excluded_and_skipped_tests} = prepare_tests(config, test_module.tests)
229+
{to_run_tests, excluded_and_skipped_tests} = prepare_tests(config, async?, test_module.tests)
230230

231231
for excluded_or_skipped_test <- excluded_and_skipped_tests do
232232
EM.test_started(config.manager, excluded_or_skipped_test)
233233
EM.test_finished(config.manager, excluded_or_skipped_test)
234234
end
235235

236236
{test_module, invalid_tests, finished_tests} =
237-
run_module_tests(config, test_module, to_run_tests)
237+
run_module_tests(config, test_module, async?, to_run_tests)
238238

239239
pending_tests =
240240
case process_max_failures(config, test_module) do
@@ -261,7 +261,7 @@ defmodule ExUnit.Runner do
261261
end
262262
end
263263

264-
defp prepare_tests(config, tests) do
264+
defp prepare_tests(config, async?, tests) do
265265
tests = shuffle(config, tests)
266266
include = config.include
267267
exclude = config.exclude
@@ -270,7 +270,7 @@ defmodule ExUnit.Runner do
270270
{to_run, to_skip} =
271271
for test <- tests, include_test?(test_ids, test), reduce: {[], []} do
272272
{to_run, to_skip} ->
273-
tags = Map.merge(test.tags, %{test: test.name, module: test.module})
273+
tags = Map.merge(test.tags, %{test: test.name, module: test.module, async: async?})
274274

275275
case ExUnit.Filters.eval(include, exclude, tags, tests) do
276276
:ok -> {[%{test | tags: tags} | to_run], to_skip}
@@ -285,12 +285,12 @@ defmodule ExUnit.Runner do
285285
test_ids == nil or MapSet.member?(test_ids, {test.module, test.name})
286286
end
287287

288-
defp run_module_tests(_config, test_module, []) do
288+
defp run_module_tests(_config, test_module, _async?, []) do
289289
{test_module, [], []}
290290
end
291291

292-
defp run_module_tests(config, test_module, tests) do
293-
{module_pid, module_ref} = run_setup_all(test_module, self())
292+
defp run_module_tests(config, test_module, async?, tests) do
293+
{module_pid, module_ref} = run_setup_all(test_module, async?, self())
294294

295295
{test_module, invalid_tests, finished_tests} =
296296
receive do
@@ -324,12 +324,14 @@ defmodule ExUnit.Runner do
324324

325325
defp run_setup_all(
326326
%ExUnit.TestModule{name: module, tags: tags, parameters: params} = test_module,
327+
async?,
327328
parent_pid
328329
) do
329330
Process.put(@current_key, test_module)
330331

331332
spawn_monitor(fn ->
332333
ExUnit.OnExitHandler.register(self())
334+
tags = tags |> Map.merge(params) |> Map.merge(%{module: module, async: async?})
333335

334336
result =
335337
try do

lib/ex_unit/test/ex_unit/case_test.exs

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ defmodule ExUnit.CaseTest do
2929
test "tags", context do
3030
line = __ENV__.line - 1
3131
assert context[:module] == __MODULE__
32-
assert context[:case] == __MODULE__
3332
assert context[:test] == __ENV__.function |> elem(0)
3433
assert context[:line] == line
3534
assert context[:async] == true
@@ -44,6 +43,8 @@ defmodule ExUnit.CaseTest do
4443

4544
# tags are passed to setup_all
4645
setup_all context do
46+
assert context.async
47+
assert context.module == __MODULE__
4748
%{moduletag_from_setup_all: context[:moduletag]}
4849
end
4950

0 commit comments

Comments
 (0)