Skip to content

Commit d794620

Browse files
committed
Fix Mix suite with async_run and await_run
1 parent 9cfa855 commit d794620

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

lib/ex_unit/lib/ex_unit.ex

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,31 @@ defmodule ExUnit do
355355
ExUnit.Runner.run(options, nil)
356356
end
357357

358+
@doc """
359+
Starts tests asynchronously while test cases are still loading.
360+
361+
It returns a task that must be given to `await_run/0` when a result
362+
is desired.
363+
"""
364+
@doc since: "1.12.0"
365+
@spec async_run() :: Task.t()
366+
def async_run() do
367+
Task.async(fn ->
368+
options = persist_defaults(configuration())
369+
ExUnit.Runner.run(options, nil)
370+
end)
371+
end
372+
373+
@doc """
374+
Awaits for a test suite that has been started with `async_run/0`.
375+
"""
376+
@doc since: "1.12.0"
377+
@spec await_run(Task.t()) :: suite_result()
378+
def await_run(task) do
379+
ExUnit.Server.modules_loaded()
380+
Task.await(task, :infinity)
381+
end
382+
358383
@doc """
359384
Sets a callback to be executed after the completion of a test suite.
360385

lib/ex_unit/test/ex_unit_test.exs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ defmodule ExUnitTest do
2727
ExUnit.Server.modules_loaded()
2828

2929
assert capture_io(fn ->
30-
assert ExUnit.run() == %{failures: 0, skipped: 0, total: 0, excluded: 0}
30+
assert ExUnit.async_run() |> ExUnit.await_run() ==
31+
%{failures: 0, skipped: 0, total: 0, excluded: 0}
3132
end) =~ "\n0 failures\n"
3233
end
3334

lib/mix/lib/mix/compilers/test.ex

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,15 @@ defmodule Mix.Compilers.Test do
3434
if test_files == [] do
3535
:noop
3636
else
37-
task = Task.async(ExUnit, :run, [])
37+
task = ExUnit.async_run()
3838

3939
try do
4040
case Kernel.ParallelCompiler.require(test_files, parallel_require_callbacks) do
4141
{:ok, _, _} -> :ok
4242
{:error, _, _} -> exit({:shutdown, 1})
4343
end
4444

45-
ExUnit.Server.modules_loaded()
46-
%{failures: failures} = results = Task.await(task, :infinity)
45+
%{failures: failures} = results = ExUnit.await_run(task)
4746

4847
if failures == 0 do
4948
agent_write_manifest(stale_manifest_pid)

0 commit comments

Comments
 (0)