Skip to content

Commit b9387d3

Browse files
authored
Have mix test fail if warnings and --warnings-as-errors (#14756)
1 parent 95265ba commit b9387d3

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
@@ -35,7 +35,7 @@ Although we expect the vast majority of programs to remain compatible over time,
3535

3636
* 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.
3737

38-
* 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.
38+
* 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.
3939

4040
* 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.
4141

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

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

675675
try do
676-
Enum.each(test_paths, &require_test_helper(shell, &1))
676+
helper_warned? = Enum.any?(test_paths, &require_test_helper(shell, &1))
677677
# test_opts always wins because those are given via args
678678
ExUnit.configure(ex_unit_opts |> merge_helper_opts() |> Keyword.merge(test_opts))
679-
CT.require_and_run(matched_test_files, test_paths, test_elixirc_options, opts)
679+
680+
{CT.require_and_run(matched_test_files, test_paths, test_elixirc_options, opts),
681+
helper_warned?}
680682
catch
681683
kind, reason ->
682684
# Also mark the whole suite as failed
683685
file = get_manifest_path(opts)
684686
ExUnit.Filters.fail_all!(file)
685687
:erlang.raise(kind, reason, __STACKTRACE__)
686688
else
687-
{:ok, %{excluded: excluded, failures: failures, warnings?: warnings?, total: total}} ->
689+
{{:ok, %{excluded: excluded, failures: failures, warnings?: warnings?, total: total}},
690+
helper_warned?} ->
688691
Mix.shell(shell)
689692
cover && cover.()
690693

691694
cond do
692-
warnings_as_errors? and warnings? and failures == 0 ->
695+
warnings_as_errors? and (warnings? or helper_warned?) and failures == 0 ->
693696
message =
694697
"\nERROR! Test suite aborted after successful execution due to warnings while using the --warnings-as-errors option"
695698

@@ -722,7 +725,7 @@ defmodule Mix.Tasks.Test do
722725
:ok
723726
end
724727

725-
:noop ->
728+
{:noop, _} ->
726729
cond do
727730
opts[:stale] ->
728731
Mix.shell().info("No stale tests")
@@ -1032,7 +1035,8 @@ defmodule Mix.Tasks.Test do
10321035
file = Path.join(dir, "test_helper.exs")
10331036

10341037
if File.exists?(file) do
1035-
Code.require_file(file)
1038+
{_result, warnings} = Code.with_diagnostics([log: true], fn -> Code.require_file(file) end)
1039+
warnings != []
10361040
else
10371041
raise_with_shell(
10381042
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)