@@ -209,8 +209,12 @@ defmodule Mix.Tasks.Test do
209
209
Note that in trace mode test timeouts will be ignored as timeout is set to `:infinity`
210
210
211
211
* `--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
214
218
`--failed` flag.
215
219
216
220
This option only applies to test files. To treat warnings as errors during compilation and
@@ -617,6 +621,9 @@ defmodule Mix.Tasks.Test do
617
621
{ ex_unit_opts , allowed_files } = process_ex_unit_opts ( opts )
618
622
ExUnit . configure ( ex_unit_opts )
619
623
624
+ warnings_as_errors? = Keyword . get ( opts , :warnings_as_errors , false )
625
+ exit_status = Keyword . fetch! ( ex_unit_opts , :exit_status )
626
+
620
627
# Prepare and extract all files to require and run
621
628
test_paths = project [ :test_paths ] || default_test_paths ( )
622
629
test_pattern = project [ :test_pattern ] || "*.{ex,exs}"
@@ -664,17 +671,32 @@ defmodule Mix.Tasks.Test do
664
671
ExUnit.Filters . fail_all! ( file )
665
672
:erlang . raise ( kind , reason , __STACKTRACE__ )
666
673
else
667
- { :ok , % { excluded: excluded , failures: failures , total: total } } ->
674
+ { :ok , % { excluded: excluded , failures: failures , warnings?: warnings? , total: total } } ->
668
675
Mix . shell ( shell )
669
676
cover && cover . ( )
670
677
671
678
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
+
672
689
failures > 0 and opts [ :raise ] ->
673
690
raise_with_shell ( shell , "\" mix test\" failed" )
674
691
692
+ warnings_as_errors? and warnings? and failures > 0 ->
693
+ System . at_exit ( fn _ ->
694
+ exit ( { :shutdown , exit_status + 1 } )
695
+ end )
696
+
675
697
failures > 0 ->
676
698
System . at_exit ( fn _ ->
677
- exit ( { :shutdown , Keyword . fetch! ( ex_unit_opts , : exit_status) } )
699
+ exit ( { :shutdown , exit_status } )
678
700
end )
679
701
680
702
excluded == total and Keyword . has_key? ( opts , :only ) ->
0 commit comments