Skip to content

Add mix test --dry-run flag #14499

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions lib/ex_unit/lib/ex_unit/cli_formatter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ defmodule ExUnit.CLIFormatter do
IO.puts("")

config = %{
dry_run: opts[:dry_run],
trace: opts[:trace],
colors: colors(opts),
width: get_terminal_width(),
Expand Down Expand Up @@ -154,7 +155,16 @@ defmodule ExUnit.CLIFormatter do
{:noreply, config}
end

def handle_cast({:module_finished, %ExUnit.TestModule{state: nil}}, config) do
def handle_cast({:module_finished, %ExUnit.TestModule{state: nil} = module}, config) do
if config.dry_run do
IO.puts("Test dry run:")
file_path = Path.relative_to_cwd(module.file)

Enum.each(module.tests, fn test ->
IO.puts("#{file_path}:#{test.tags.line}")
end)
end

{:noreply, config}
end

Expand Down Expand Up @@ -356,7 +366,11 @@ defmodule ExUnit.CLIFormatter do
)
|> if_true(
config.excluded_counter > 0,
&(&1 <> " (#{config.excluded_counter} excluded)")
&(&1 <> ", (#{config.excluded_counter} excluded)")
)
|> if_true(
config.dry_run == true,
&(&1 <> " (dry run)")
)

cond do
Expand Down
7 changes: 6 additions & 1 deletion lib/ex_unit/lib/ex_unit/runner.ex
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ defmodule ExUnit.Runner do
seed: opts[:seed],
stats_pid: stats_pid,
timeout: opts[:timeout],
trace: opts[:trace]
trace: opts[:trace],
dry_run: opts[:dry_run]
}
end

Expand Down Expand Up @@ -306,6 +307,10 @@ defmodule ExUnit.Runner do
{test_module, [], []}
end

defp run_module_tests(%{dry_run: true}, test_module, _async?, tests) do
{test_module, [], tests}
end

defp run_module_tests(config, test_module, async?, tests) do
Process.put(@current_key, test_module)
%ExUnit.TestModule{name: module, tags: tags, parameters: params} = test_module
Expand Down
10 changes: 8 additions & 2 deletions lib/mix/lib/mix/tasks/test.ex
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ defmodule Mix.Tasks.Test do

* `--cover` - runs coverage tool. See "Coverage" section below

* `--dry-run` *(since v1.19.0)* - prints which tests would be run based on current options,
but does not actually run any tests. This combines with all other options
like `--stale`, `--only`, `--exclude`, and so on.

* `--exclude` - excludes tests that match the filter. This option may be given
several times to apply different filters, such as `--exclude ci --exclude slow`

Expand Down Expand Up @@ -494,7 +498,8 @@ defmodule Mix.Tasks.Test do
warnings_as_errors: :boolean,
profile_require: :string,
exit_status: :integer,
repeat_until_failure: :integer
repeat_until_failure: :integer,
dry_run: :boolean
]

@cover [output: "cover", tool: Mix.Tasks.Test.Coverage]
Expand Down Expand Up @@ -847,7 +852,8 @@ defmodule Mix.Tasks.Test do
:only_test_ids,
:test_location_relative_path,
:exit_status,
:repeat_until_failure
:repeat_until_failure,
:dry_run
]

@doc false
Expand Down
33 changes: 33 additions & 0 deletions lib/mix/test/mix/tasks/test_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,39 @@ defmodule Mix.Tasks.TestTest do
end
end

describe "--dry-run" do
test "works with --stale" do
in_fixture("test_stale", fn ->
File.write!("test/dry_run_test_stale.exs", """
defmodule DryRunTest do
use ExUnit.Case

test "new test" do
assert true
end
end
""")

output = mix(["test", "--dry-run", "--stale"])

assert output =~ "Test dry run:"
assert output =~ "test/dry_run_test_stale.exs:4"
assert output =~ "0 tests, 0 failures (dry run)"
end)
end

test "works with --failed" do
in_fixture("test_failed", fn ->
_initial_run = mix(["test"])
output = mix(["test", "--dry-run", "--failed"])

assert output =~ "Test dry run:"
assert output =~ "test/passing_and_failing_test_failed.exs:5"
assert output =~ "0 tests, 0 failures (dry run)"
end)
end
end

defp receive_until_match(port, expected, acc) do
receive do
{^port, {:data, output}} ->
Expand Down