Skip to content

Commit 7f45bef

Browse files
committed
Have mix test fail if warnings and --warnings-as-errors (#14756)
1 parent bbcd7f3 commit 7f45bef

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed

lib/elixir/pages/references/compatibility-and-deprecations.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ Although we expect the vast majority of programs to remain compatible over time,
3434

3535
* Bugs: if an API has undesired behavior, a program that depends on the buggy behavior may break if the bug is fixed. We reserve the right to fix such bugs.
3636

37-
* Compiler front-end: improvements may be done to the compiler, introducing new warnings for ambiguous modes and providing more detailed error messages. Those can lead to compilation errors (when running with `--warning-as-errors`) or tooling failures when asserting on specific error messages (although one should avoid such). We reserve the right to do such improvements.
37+
* Compiler front-end: improvements may be done to the compiler, introducing new warnings for ambiguous modes and providing more detailed error messages. Those can lead to compilation errors (when running with `--warnings-as-errors`) or tooling failures when asserting on specific error messages (although one should avoid such). We reserve the right to do such improvements.
3838

3939
* Imports: new functions may be added to the `Kernel` module, which is auto-imported. They may collide with local functions defined in your modules. Collisions can be resolved in a backwards compatible fashion using `import Kernel, except: [...]` with a list of all functions you don't want to be imported from `Kernel`. We reserve the right to do such additions.
4040

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -666,23 +666,26 @@ defmodule Mix.Tasks.Test do
666666
warn_files != [] && warn_misnamed_test_files(warn_files)
667667

668668
try do
669-
Enum.each(test_paths, &require_test_helper(shell, &1))
669+
helper_warned? = Enum.any?(test_paths, &require_test_helper(shell, &1))
670670
# test_opts always wins because those are given via args
671671
ExUnit.configure(ex_unit_opts |> merge_helper_opts() |> Keyword.merge(test_opts))
672-
CT.require_and_run(matched_test_files, test_paths, test_elixirc_options, opts)
672+
673+
{CT.require_and_run(matched_test_files, test_paths, test_elixirc_options, opts),
674+
helper_warned?}
673675
catch
674676
kind, reason ->
675677
# Also mark the whole suite as failed
676678
file = get_manifest_path(opts)
677679
ExUnit.Filters.fail_all!(file)
678680
:erlang.raise(kind, reason, __STACKTRACE__)
679681
else
680-
{:ok, %{excluded: excluded, failures: failures, warnings?: warnings?, total: total}} ->
682+
{{:ok, %{excluded: excluded, failures: failures, warnings?: warnings?, total: total}},
683+
helper_warned?} ->
681684
Mix.shell(shell)
682685
cover && cover.()
683686

684687
cond do
685-
warnings_as_errors? and warnings? and failures == 0 ->
688+
warnings_as_errors? and (warnings? or helper_warned?) and failures == 0 ->
686689
message =
687690
"\nERROR! Test suite aborted after successful execution due to warnings while using the --warnings-as-errors option"
688691

@@ -715,7 +718,7 @@ defmodule Mix.Tasks.Test do
715718
:ok
716719
end
717720

718-
:noop ->
721+
{:noop, _} ->
719722
cond do
720723
opts[:stale] ->
721724
Mix.shell().info("No stale tests")
@@ -1024,7 +1027,8 @@ defmodule Mix.Tasks.Test do
10241027
file = Path.join(dir, "test_helper.exs")
10251028

10261029
if File.exists?(file) do
1027-
Code.require_file(file)
1030+
{_result, warnings} = Code.with_diagnostics([log: true], fn -> Code.require_file(file) end)
1031+
warnings != []
10281032
else
10291033
raise_with_shell(
10301034
shell,

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,25 @@ defmodule Mix.Tasks.TestTest do
593593
assert output =~ "2 failures"
594594
end)
595595
end
596+
597+
test "fail with exit status 1 if warning in test_helper.exs" do
598+
in_fixture("test_stale", fn ->
599+
File.write!("test/test_helper.exs", """
600+
unused_var = 123
601+
602+
ExUnit.start()
603+
""")
604+
605+
msg =
606+
"Test suite aborted after successful execution due to warnings while using the --warnings-as-errors option"
607+
608+
{output, exit_status} = mix_code(["test", "--warnings-as-errors"])
609+
610+
assert output =~ "variable \"unused_var\" is unused"
611+
assert output =~ msg
612+
assert exit_status == 1
613+
end)
614+
end
596615
end
597616

598617
describe "--exit-status" do

0 commit comments

Comments
 (0)