Skip to content

Commit 012ccf9

Browse files
author
José Valim
committed
Ensure shell is reverted after mix test run
1 parent e465d2f commit 012ccf9

File tree

3 files changed

+30
-12
lines changed

3 files changed

+30
-12
lines changed

lib/mix/lib/mix/cli.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ defmodule Mix.CLI do
8585
# We only rescue exceptions in the Mix namespace, all
8686
# others pass through and will explode on the users face
8787
exception ->
88-
if Map.get(exception, :mix) && not Mix.debug?() do
88+
if Map.get(exception, :mix, false) and not Mix.debug?() do
8989
mod = exception.__struct__ |> Module.split() |> Enum.at(0, "Mix")
9090
Mix.shell().error("** (#{mod}) #{Exception.message(exception)}")
9191
exit({:shutdown, 1})

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

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -421,8 +421,10 @@ defmodule Mix.Tasks.Test do
421421
{:error, {:already_loaded, :ex_unit}} -> :ok
422422
end
423423

424-
# Then configure ExUnit again so that command line options
425-
# override test_helper.exs
424+
# The test helper may change the Mix.shell(), so let's make sure to revert it later
425+
shell = Mix.shell()
426+
427+
# Configure ExUnit now and then again so the task options override test_helper.exs
426428
{ex_unit_opts, allowed_files} = process_ex_unit_opts(opts)
427429
ExUnit.configure(ex_unit_opts)
428430

@@ -442,20 +444,21 @@ defmodule Mix.Tasks.Test do
442444

443445
display_warn_test_pattern(test_files, test_pattern, matched_test_files, warn_test_pattern)
444446

445-
case CT.require_and_run(matched_test_files, test_paths, opts) do
447+
results = CT.require_and_run(matched_test_files, test_paths, opts)
448+
Mix.shell(shell)
449+
450+
case results do
446451
{:ok, %{excluded: excluded, failures: failures, total: total}} ->
447452
cover && cover.()
448453

449-
option_only_present? = Keyword.has_key?(opts, :only)
450-
451454
cond do
452455
failures > 0 and opts[:raise] ->
453456
Mix.raise("\"mix test\" failed")
454457

455458
failures > 0 ->
456459
System.at_exit(fn _ -> exit({:shutdown, 1}) end)
457460

458-
excluded == total and option_only_present? ->
461+
excluded == total and Keyword.has_key?(opts, :only) ->
459462
message = "The --only option was given to \"mix test\" but no test was executed"
460463
raise_or_error_at_exit(message, opts)
461464

@@ -541,9 +544,15 @@ defmodule Mix.Tasks.Test do
541544
end
542545

543546
defp merge_helper_opts(opts) do
547+
# The only options that are additive from app env are the excludes
544548
merge_opts(opts, :exclude)
545549
end
546550

551+
defp merge_opts(opts, key) do
552+
value = List.wrap(Application.get_env(:ex_unit, key, []))
553+
Keyword.update(opts, key, value, &Enum.uniq(&1 ++ value))
554+
end
555+
547556
defp default_opts(opts) do
548557
# Set autorun to false because Mix
549558
# automatically runs the test suite for us.
@@ -643,11 +652,6 @@ defmodule Mix.Tasks.Test do
643652
end
644653
end
645654

646-
defp merge_opts(opts, key) do
647-
value = List.wrap(Application.get_env(:ex_unit, key, []))
648-
Keyword.update(opts, key, value, &Enum.uniq(&1 ++ value))
649-
end
650-
651655
defp require_test_helper(dir) do
652656
file = Path.join(dir, "test_helper.exs")
653657

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,20 @@ defmodule Mix.Tasks.TestTest do
277277
end)
278278
end
279279

280+
test "raises when no test runs even with Mix.shell() change" do
281+
in_fixture("test_stale", fn ->
282+
File.write!("test/test_helper.exs", """
283+
Mix.shell(Mix.Shell.Process)
284+
ExUnit.start()
285+
""")
286+
287+
assert_run_output(
288+
["--only", "unknown"],
289+
"The --only option was given to \"mix test\" but no test was executed"
290+
)
291+
end)
292+
end
293+
280294
test "raises an exception if line numbers are given with multiple files" do
281295
in_fixture("test_stale", fn ->
282296
assert_run_output(

0 commit comments

Comments
 (0)