Skip to content

Commit 66b4ab1

Browse files
agundyjosevalim
authored andcommitted
Mix.Tasks.Test.Coverage warns on failure (#11457)
Adds an explicit warning when exiting with an error code because of a failed test coverage threshold. Example: ``` -----------|-------------------------- 62.35% | Total Coverage test failed, threshold not met: Coverage: 62.35% Threshold: 100.00% ```
1 parent 01a4889 commit 66b4ab1

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,7 @@ defmodule Mix.Tasks.Test.Coverage do
299299
print_summary(module_results, totals, summary_opts)
300300

301301
if totals < get_threshold(summary_opts) do
302+
print_failed_threshold(totals, get_threshold(summary_opts))
302303
System.at_exit(fn _ -> exit({:shutdown, 3}) end)
303304
end
304305

@@ -353,6 +354,13 @@ defmodule Mix.Tasks.Test.Coverage do
353354
Mix.shell().info("")
354355
end
355356

357+
defp print_failed_threshold(totals, threshold) do
358+
Mix.shell().info("Coverage test failed, threshold not met:\n")
359+
Mix.shell().info(" Coverage: #{format_number(totals, 6)}%")
360+
Mix.shell().info(" Threshold: #{format_number(threshold, 6)}%")
361+
Mix.shell().info("")
362+
end
363+
356364
defp display({percentage, name}, threshold) do
357365
Mix.shell().info([
358366
color(percentage, threshold),
@@ -369,6 +377,9 @@ defmodule Mix.Tasks.Test.Coverage do
369377
defp color(percentage, threshold) when percentage >= threshold, do: :green
370378
defp color(_, _), do: :red
371379

380+
defp format_number(number, length) when is_integer(number),
381+
do: format_number(number / 1, length)
382+
372383
defp format_number(number, length), do: :io_lib.format("~#{length}.2f", [number])
373384

374385
defp format_name(name) when is_binary(name), do: name

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,14 @@ defmodule Mix.Tasks.TestTest do
168168

169169
# We skip a test in bar to force coverage below the default threshold
170170
# which should result in an exit status of 1.
171-
assert {_, code} = mix_code(["test", "--cover", "--exclude", "maybe_skip"])
171+
assert {output, code} = mix_code(["test", "--cover", "--exclude", "maybe_skip"])
172+
173+
assert output =~ """
174+
Coverage test failed, threshold not met:
175+
176+
Coverage: 0.00%
177+
Threshold: 90.00%
178+
"""
172179

173180
unless windows?() do
174181
assert code == 3

0 commit comments

Comments
 (0)