Skip to content

Commit a7b2d6c

Browse files
authored
Fix --warnings-as-errors not catching misnamed test file warnings (#15194)
1 parent 4064905 commit a7b2d6c

2 files changed

Lines changed: 48 additions & 8 deletions

File tree

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

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -712,14 +712,7 @@ defmodule Mix.Tasks.Test do
712712
cond do
713713
warnings_as_errors? and (warnings? or helper_warned? or warn_files != []) and
714714
failures == 0 ->
715-
message =
716-
"\nERROR! Test suite aborted after successful execution due to warnings while using the --warnings-as-errors option"
717-
718-
IO.puts(:stderr, IO.ANSI.format([:red, message]))
719-
720-
System.at_exit(fn _ ->
721-
exit({:shutdown, 1})
722-
end)
715+
abort_due_to_warnings()
723716

724717
failures > 0 and opts[:raise] ->
725718
raise_with_shell(shell, "\"mix test\" failed")
@@ -746,6 +739,9 @@ defmodule Mix.Tasks.Test do
746739

747740
{:noop, _} ->
748741
cond do
742+
warnings_as_errors? and warn_files != [] ->
743+
abort_due_to_warnings()
744+
749745
opts[:stale] ->
750746
Mix.shell().info("No stale tests")
751747

@@ -788,6 +784,17 @@ defmodule Mix.Tasks.Test do
788784
{files, directly_included}
789785
end
790786

787+
defp abort_due_to_warnings() do
788+
message =
789+
"\nERROR! Test suite aborted after successful execution due to warnings while using the --warnings-as-errors option"
790+
791+
IO.puts(:stderr, IO.ANSI.format([:red, message]))
792+
793+
System.at_exit(fn _ ->
794+
exit({:shutdown, 1})
795+
end)
796+
end
797+
791798
defp raise_with_shell(shell, message) do
792799
Mix.shell(shell)
793800
Mix.raise(message)

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,39 @@ defmodule Mix.Tasks.TestTest do
594594
end)
595595
end
596596

597+
test "fail with exit status 1 if misnamed test files with --warnings-as-errors (no tests to run)" do
598+
in_tmp("test_warn_as_errors_noop", fn ->
599+
File.write!("mix.exs", """
600+
defmodule TestWarnAsErrorsNoop.MixProject do
601+
use Mix.Project
602+
603+
def project do
604+
[
605+
app: :test_warn_as_errors_noop,
606+
version: "0.0.1",
607+
test_load_filters: [~r/.*_tests\.exs/]
608+
]
609+
end
610+
end
611+
""")
612+
613+
File.mkdir!("test")
614+
File.write!("test/test_helper.exs", "ExUnit.start()")
615+
616+
File.touch!("test/a_missing.exs")
617+
618+
msg =
619+
"Test suite aborted after successful execution due to warnings while using the --warnings-as-errors option"
620+
621+
{output, exit_status} = mix_code(["test", "--warnings-as-errors"])
622+
623+
assert output =~ "the following files do not match"
624+
assert output =~ "test/a_missing.exs"
625+
assert output =~ msg
626+
assert exit_status == 1
627+
end)
628+
end
629+
597630
test "fail with exit status 1 if warning in test_helper.exs" do
598631
in_fixture("test_stale", fn ->
599632
File.write!("test/test_helper.exs", """

0 commit comments

Comments
 (0)