Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update test coverage summary to produce a markdown-ish table #14384

Merged
merged 1 commit into from
Apr 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions lib/mix/lib/mix/tasks/test.coverage.ex
Original file line number Diff line number Diff line change
Expand Up @@ -359,11 +359,18 @@ defmodule Mix.Tasks.Test.Coverage do

defp print_summary(results, totals, opts) when is_list(opts) do
threshold = get_threshold(opts)
Mix.shell().info("Percentage | Module")
Mix.shell().info("-----------|--------------------------")
results |> Enum.sort() |> Enum.each(&display(&1, threshold))
Mix.shell().info("-----------|--------------------------")
display({totals, "Total"}, threshold)

results =
results |> Enum.sort() |> Enum.map(fn {coverage, module} -> {coverage, inspect(module)} end)

name_max_length = results |> Enum.map(&String.length(elem(&1, 1))) |> Enum.max() |> max(10)
name_separator = String.duplicate("-", name_max_length)

Mix.shell().info("| Percentage | #{String.pad_trailing("Module", name_max_length)} |")
Mix.shell().info("|------------|-#{name_separator}-|")
Enum.each(results, &display(&1, threshold, name_max_length))
Mix.shell().info("|------------|-#{name_separator}-|")
display({totals, "Total"}, threshold, name_max_length)
Mix.shell().info("")
end

Expand All @@ -374,14 +381,16 @@ defmodule Mix.Tasks.Test.Coverage do
Mix.shell().info("")
end

defp display({percentage, name}, threshold) do
defp display({percentage, name}, threshold, pad_length) do
Mix.shell().info([
"| ",
color(percentage, threshold),
format_number(percentage, 9),
"%",
:reset,
" | ",
format_name(name)
String.pad_trailing(name, pad_length),
" |"
])
end

Expand All @@ -395,9 +404,6 @@ defmodule Mix.Tasks.Test.Coverage do

defp format_number(number, length), do: :io_lib.format("~#{length}.2f", [number])

defp format_name(name) when is_binary(name), do: name
defp format_name(mod) when is_atom(mod), do: inspect(mod)

defp get_threshold(true), do: @default_threshold
defp get_threshold(opts), do: Keyword.get(opts, :threshold, @default_threshold)
end
52 changes: 26 additions & 26 deletions lib/mix/test/mix/tasks/test_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,12 @@ defmodule Mix.Tasks.TestTest do
assert output =~ """
Generating cover results ...

Percentage | Module
-----------|--------------------------
100.00% | Bar.Protocol
100.00% | Bar.Protocol.BitString
-----------|--------------------------
100.00% | Total
| Percentage | Module |
|------------|------------------------|
| 100.00% | Bar.Protocol |
| 100.00% | Bar.Protocol.BitString |
|------------|------------------------|
| 100.00% | Total |
"""

assert output =~ "1 test, 0 failures"
Expand All @@ -164,11 +164,11 @@ defmodule Mix.Tasks.TestTest do
assert output =~ """
Generating cover results ...

Percentage | Module
-----------|--------------------------
100.00% | Foo
-----------|--------------------------
100.00% | Total
| Percentage | Module |
|------------|------------|
| 100.00% | Foo |
|------------|------------|
| 100.00% | Total |
"""

# We skip a test in bar to force coverage below the default threshold
Expand All @@ -195,15 +195,15 @@ defmodule Mix.Tasks.TestTest do
Importing cover results: apps/bar/cover/default.coverdata
Importing cover results: apps/foo/cover/default.coverdata

Percentage | Module
-----------|--------------------------
100.00% | Bar
100.00% | Bar.Ignore
100.00% | Bar.Protocol
100.00% | Bar.Protocol.BitString
100.00% | Foo
-----------|--------------------------
100.00% | Total
| Percentage | Module |
|------------|------------------------|
| 100.00% | Bar |
| 100.00% | Bar.Ignore |
| 100.00% | Bar.Protocol |
| 100.00% | Bar.Protocol.BitString |
| 100.00% | Foo |
|------------|------------------------|
| 100.00% | Total |
"""
end)
end
Expand Down Expand Up @@ -366,12 +366,12 @@ defmodule Mix.Tasks.TestTest do
Importing cover results: cover/1.coverdata
Importing cover results: cover/2.coverdata

Percentage | Module
-----------|--------------------------
100.00% | A
100.00% | B
-----------|--------------------------
100.00% | Total
| Percentage | Module |
|------------|------------|
| 100.00% | A |
| 100.00% | B |
|------------|------------|
| 100.00% | Total |

Generated HTML coverage results in \"cover\" directory
"""
Expand Down
Loading