Skip to content

Commit dc699bc

Browse files
committed
Finish dry run implementation
1 parent 99131b0 commit dc699bc

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
@@ -770,38 +770,35 @@ defmodule Mix.Tasks.TestTest do
770770
end
771771
end
772772

773-
# TODO: Get to pass after deciding on implementation
774-
@tag :skip
775773
describe "--dry-run" do
776-
test "prints which tests would run without executing them" do
774+
test "works with --stale" do
777775
in_fixture("test_stale", fn ->
778-
File.write!("test/dry_run_test.exs", """
776+
File.write!("test/dry_run_test_stale.exs", """
779777
defmodule DryRunTest do
780778
use ExUnit.Case
781779
782-
test "passing test" do
780+
test "new test" do
783781
assert true
784782
end
785-
786-
test "failing test" do
787-
assert false
788-
end
789783
end
790784
""")
791785

792-
assert {output, 0} = mix_code(["test", "--dry-run", "--stale"])
793-
assert output =~ "DRY RUN"
794-
assert output =~ "test/dry_run_test.exs"
795-
refute output =~ "Finished in"
786+
output = mix(["test", "--dry-run", "--stale"])
787+
788+
assert output =~ "Test dry run:"
789+
assert output =~ "test/dry_run_test_stale.exs:4"
790+
assert output =~ "0 tests, 0 failures (dry run)"
796791
end)
797792
end
798793

799-
test "prints message when no tests would run" do
800-
in_fixture("test_stale", fn ->
801-
assert {output, 0} = mix_code(["test", "--dry-run", "non_existent_test.exs"])
802-
assert output =~ "DRY RUN"
803-
assert output =~ "No tests would run"
804-
refute output =~ "Finished in"
794+
test "works with --failed" do
795+
in_fixture("test_failed", fn ->
796+
_initial_run = mix(["test"])
797+
output = mix(["test", "--dry-run", "--failed"])
798+
799+
assert output =~ "Test dry run:"
800+
assert output =~ "test/passing_and_failing_test_failed.exs:5"
801+
assert output =~ "0 tests, 0 failures (dry run)"
805802
end)
806803
end
807804
end

0 commit comments

Comments
 (0)