Skip to content

Commit 49aed74

Browse files
committed
Add mix test --dry-run flag
1 parent cc9a5e7 commit 49aed74

File tree

4 files changed

+63
-5
lines changed

4 files changed

+63
-5
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: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ defmodule ExUnit.Runner do
8888
seed: opts[:seed],
8989
stats_pid: stats_pid,
9090
timeout: opts[:timeout],
91-
trace: opts[:trace]
91+
trace: opts[:trace],
92+
dry_run: opts[:dry_run]
9293
}
9394
end
9495

@@ -306,6 +307,10 @@ defmodule ExUnit.Runner do
306307
{test_module, [], []}
307308
end
308309

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

lib/mix/lib/mix/tasks/test.ex

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,10 @@ defmodule Mix.Tasks.Test do
121121
122122
* `--cover` - runs coverage tool. See "Coverage" section below
123123
124+
* `--dry-run` *(since v1.19.0)* - prints which tests would be run based on current options,
125+
but does not actually run any tests. This combines with all other options
126+
like `--stale`, `--only`, `--exclude`, and so on.
127+
124128
* `--exclude` - excludes tests that match the filter. This option may be given
125129
several times to apply different filters, such as `--exclude ci --exclude slow`
126130
@@ -494,7 +498,8 @@ defmodule Mix.Tasks.Test do
494498
warnings_as_errors: :boolean,
495499
profile_require: :string,
496500
exit_status: :integer,
497-
repeat_until_failure: :integer
501+
repeat_until_failure: :integer,
502+
dry_run: :boolean
498503
]
499504

500505
@cover [output: "cover", tool: Mix.Tasks.Test.Coverage]
@@ -847,7 +852,8 @@ defmodule Mix.Tasks.Test do
847852
:only_test_ids,
848853
:test_location_relative_path,
849854
:exit_status,
850-
:repeat_until_failure
855+
:repeat_until_failure,
856+
:dry_run
851857
]
852858

853859
@doc false

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,39 @@ defmodule Mix.Tasks.TestTest do
716716
end
717717
end
718718

719+
describe "--dry-run" do
720+
test "works with --stale" do
721+
in_fixture("test_stale", fn ->
722+
File.write!("test/dry_run_test_stale.exs", """
723+
defmodule DryRunTest do
724+
use ExUnit.Case
725+
726+
test "new test" do
727+
assert true
728+
end
729+
end
730+
""")
731+
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)"
737+
end)
738+
end
739+
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)"
748+
end)
749+
end
750+
end
751+
719752
defp receive_until_match(port, expected, acc) do
720753
receive do
721754
{^port, {:data, output}} ->

0 commit comments

Comments
 (0)