@@ -209,8 +209,12 @@ defmodule Mix.Tasks.Test do
209209 Note that in trace mode test timeouts will be ignored as timeout is set to `:infinity`
210210
211211 * `--warnings-as-errors` *(since v1.12.0)* - treats compilation warnings (from loading the
212- test suite) as errors and returns a non-zero exit status if the test suite would otherwise
213- pass. Note that failures reported by `--warnings-as-errors` cannot be retried with the
212+ test suite) as errors and returns an exit status of 1 if the test suite would otherwise
213+ pass. If the test suite fails and also include warnings as errors, the exit
214+ status returned will be the value of the `--exit-status` option, which
215+ defaults to 2, plus one. Therefore in the default case, this will be exit status 3.
216+
217+ Note that failures reported by `--warnings-as-errors` cannot be retried with the
214218 `--failed` flag.
215219
216220 This option only applies to test files. To treat warnings as errors during compilation and
@@ -617,6 +621,9 @@ defmodule Mix.Tasks.Test do
617621 { ex_unit_opts , allowed_files } = process_ex_unit_opts ( opts )
618622 ExUnit . configure ( ex_unit_opts )
619623
624+ warnings_as_errors? = Keyword . get ( opts , :warnings_as_errors , false )
625+ exit_status = Keyword . fetch! ( ex_unit_opts , :exit_status )
626+
620627 # Prepare and extract all files to require and run
621628 test_paths = project [ :test_paths ] || default_test_paths ( )
622629 test_pattern = project [ :test_pattern ] || "*.{ex,exs}"
@@ -664,17 +671,32 @@ defmodule Mix.Tasks.Test do
664671 ExUnit.Filters . fail_all! ( file )
665672 :erlang . raise ( kind , reason , __STACKTRACE__ )
666673 else
667- { :ok , % { excluded: excluded , failures: failures , total: total } } ->
674+ { :ok , % { excluded: excluded , failures: failures , warnings?: warnings? , total: total } } ->
668675 Mix . shell ( shell )
669676 cover && cover . ( )
670677
671678 cond do
679+ warnings_as_errors? and warnings? and failures == 0 ->
680+ message =
681+ "\n ERROR! Test suite aborted after successful execution due to warnings while using the --warnings-as-errors option"
682+
683+ IO . puts ( :stderr , IO.ANSI . format ( [ :red , message ] ) )
684+
685+ System . at_exit ( fn _ ->
686+ exit ( { :shutdown , 1 } )
687+ end )
688+
672689 failures > 0 and opts [ :raise ] ->
673690 raise_with_shell ( shell , "\" mix test\" failed" )
674691
692+ warnings_as_errors? and warnings? and failures > 0 ->
693+ System . at_exit ( fn _ ->
694+ exit ( { :shutdown , exit_status + 1 } )
695+ end )
696+
675697 failures > 0 ->
676698 System . at_exit ( fn _ ->
677- exit ( { :shutdown , Keyword . fetch! ( ex_unit_opts , : exit_status) } )
699+ exit ( { :shutdown , exit_status } )
678700 end )
679701
680702 excluded == total and Keyword . has_key? ( opts , :only ) ->
0 commit comments