Skip to content

Commit 56e03ed

Browse files
committed
Finish dry run implementation
1 parent a2d19b2 commit 56e03ed

File tree

3 files changed

+44
-40
lines changed

3 files changed

+44
-40
lines changed

lib/ex_unit/lib/ex_unit/cli_formatter.ex

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ defmodule ExUnit.CLIFormatter do
1818
IO.puts("")
1919

2020
config = %{
21+
dry_run: opts[:dry_run],
2122
trace: opts[:trace],
2223
colors: colors(opts),
2324
width: get_terminal_width(),
@@ -154,7 +155,16 @@ defmodule ExUnit.CLIFormatter do
154155
{:noreply, config}
155156
end
156157

157-
def handle_cast({:module_finished, %ExUnit.TestModule{state: nil}}, config) do
158+
def handle_cast({:module_finished, %ExUnit.TestModule{state: nil} = module}, config) do
159+
if config.dry_run do
160+
IO.puts("Test dry run:")
161+
file_path = Path.relative_to_cwd(module.file)
162+
163+
Enum.each(module.tests, fn test ->
164+
IO.puts("#{file_path}:#{test.tags.line}")
165+
end)
166+
end
167+
158168
{:noreply, config}
159169
end
160170

@@ -356,7 +366,11 @@ defmodule ExUnit.CLIFormatter do
356366
)
357367
|> if_true(
358368
config.excluded_counter > 0,
359-
&(&1 <> " (#{config.excluded_counter} excluded)")
369+
&(&1 <> ", (#{config.excluded_counter} excluded)")
370+
)
371+
|> if_true(
372+
config.dry_run == true,
373+
&(&1 <> " (dry run)")
360374
)
361375

362376
cond do

lib/ex_unit/lib/ex_unit/runner.ex

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -44,27 +44,12 @@ defmodule ExUnit.Runner do
4444
config = configure(opts, manager, self(), stats_pid)
4545
:erlang.system_flag(:backtrace_depth, Keyword.fetch!(opts, :stacktrace_depth))
4646

47-
modules_to_restore =
48-
if Keyword.fetch!(opts, :repeat_until_failure) > 0, do: {[], []}, else: nil
49-
50-
modules_to_restore =
51-
if not opts[:dry_run] do
52-
do_run(config, modules_to_restore, opts, load_us)
53-
else
54-
nil
55-
end
56-
57-
stats = ExUnit.RunnerStats.stats(stats_pid)
58-
EM.stop(config.manager)
59-
after_suite_callbacks = Application.fetch_env!(:ex_unit, :after_suite)
60-
Enum.each(after_suite_callbacks, fn callback -> callback.(stats) end)
61-
{stats, modules_to_restore}
62-
end
63-
64-
defp do_run(config, modules_to_restore, opts, load_us) do
6547
start_time = System.monotonic_time()
6648
EM.suite_started(config.manager, opts)
6749

50+
modules_to_restore =
51+
if Keyword.fetch!(opts, :repeat_until_failure) > 0, do: {[], []}, else: nil
52+
6853
{async_stop_time, modules_to_restore} = async_loop(config, %{}, false, modules_to_restore)
6954
stop_time = System.monotonic_time()
7055

@@ -80,7 +65,11 @@ defmodule ExUnit.Runner do
8065
times_us = %{async: async_us, load: load_us, run: run_us}
8166
EM.suite_finished(config.manager, times_us)
8267

83-
modules_to_restore
68+
stats = ExUnit.RunnerStats.stats(stats_pid)
69+
EM.stop(config.manager)
70+
after_suite_callbacks = Application.fetch_env!(:ex_unit, :after_suite)
71+
Enum.each(after_suite_callbacks, fn callback -> callback.(stats) end)
72+
{stats, modules_to_restore}
8473
end
8574

8675
defp configure(opts, manager, runner_pid, stats_pid) do
@@ -318,6 +307,10 @@ defmodule ExUnit.Runner do
318307
{test_module, [], []}
319308
end
320309

310+
defp run_module_tests(%{dry_run: true}, test_module, _async?, tests) do
311+
{test_module, [], tests}
312+
end
313+
321314
defp run_module_tests(config, test_module, async?, tests) do
322315
Process.put(@current_key, test_module)
323316
%ExUnit.TestModule{name: module, tags: tags, parameters: params} = test_module

lib/mix/test/mix/tasks/test_test.exs

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -716,38 +716,35 @@ defmodule Mix.Tasks.TestTest do
716716
end
717717
end
718718

719-
# TODO: Get to pass after deciding on implementation
720-
@tag :skip
721719
describe "--dry-run" do
722-
test "prints which tests would run without executing them" do
720+
test "works with --stale" do
723721
in_fixture("test_stale", fn ->
724-
File.write!("test/dry_run_test.exs", """
722+
File.write!("test/dry_run_test_stale.exs", """
725723
defmodule DryRunTest do
726724
use ExUnit.Case
727725
728-
test "passing test" do
726+
test "new test" do
729727
assert true
730728
end
731-
732-
test "failing test" do
733-
assert false
734-
end
735729
end
736730
""")
737731

738-
assert {output, 0} = mix_code(["test", "--dry-run", "--stale"])
739-
assert output =~ "DRY RUN"
740-
assert output =~ "test/dry_run_test.exs"
741-
refute output =~ "Finished in"
732+
output = mix(["test", "--dry-run", "--stale"])
733+
734+
assert output =~ "Test dry run:"
735+
assert output =~ "test/dry_run_test_stale.exs:4"
736+
assert output =~ "0 tests, 0 failures (dry run)"
742737
end)
743738
end
744739

745-
test "prints message when no tests would run" do
746-
in_fixture("test_stale", fn ->
747-
assert {output, 0} = mix_code(["test", "--dry-run", "non_existent_test.exs"])
748-
assert output =~ "DRY RUN"
749-
assert output =~ "No tests would run"
750-
refute output =~ "Finished in"
740+
test "works with --failed" do
741+
in_fixture("test_failed", fn ->
742+
_initial_run = mix(["test"])
743+
output = mix(["test", "--dry-run", "--failed"])
744+
745+
assert output =~ "Test dry run:"
746+
assert output =~ "test/passing_and_failing_test_failed.exs:5"
747+
assert output =~ "0 tests, 0 failures (dry run)"
751748
end)
752749
end
753750
end

0 commit comments

Comments
 (0)