Skip to content

Commit 2a2765c

Browse files
authored
Update test coverage summary to produce a markdown-ish table (#14384)
This allows the table to embedded into places that support Markdown rendering.
1 parent 624ae0c commit 2a2765c

File tree

2 files changed

+42
-36
lines changed

2 files changed

+42
-36
lines changed

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

+16-10
Original file line numberDiff line numberDiff line change
@@ -359,11 +359,18 @@ defmodule Mix.Tasks.Test.Coverage do
359359

360360
defp print_summary(results, totals, opts) when is_list(opts) do
361361
threshold = get_threshold(opts)
362-
Mix.shell().info("Percentage | Module")
363-
Mix.shell().info("-----------|--------------------------")
364-
results |> Enum.sort() |> Enum.each(&display(&1, threshold))
365-
Mix.shell().info("-----------|--------------------------")
366-
display({totals, "Total"}, threshold)
362+
363+
results =
364+
results |> Enum.sort() |> Enum.map(fn {coverage, module} -> {coverage, inspect(module)} end)
365+
366+
name_max_length = results |> Enum.map(&String.length(elem(&1, 1))) |> Enum.max() |> max(10)
367+
name_separator = String.duplicate("-", name_max_length)
368+
369+
Mix.shell().info("| Percentage | #{String.pad_trailing("Module", name_max_length)} |")
370+
Mix.shell().info("|------------|-#{name_separator}-|")
371+
Enum.each(results, &display(&1, threshold, name_max_length))
372+
Mix.shell().info("|------------|-#{name_separator}-|")
373+
display({totals, "Total"}, threshold, name_max_length)
367374
Mix.shell().info("")
368375
end
369376

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

377-
defp display({percentage, name}, threshold) do
384+
defp display({percentage, name}, threshold, pad_length) do
378385
Mix.shell().info([
386+
"| ",
379387
color(percentage, threshold),
380388
format_number(percentage, 9),
381389
"%",
382390
:reset,
383391
" | ",
384-
format_name(name)
392+
String.pad_trailing(name, pad_length),
393+
" |"
385394
])
386395
end
387396

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

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

398-
defp format_name(name) when is_binary(name), do: name
399-
defp format_name(mod) when is_atom(mod), do: inspect(mod)
400-
401407
defp get_threshold(true), do: @default_threshold
402408
defp get_threshold(opts), do: Keyword.get(opts, :threshold, @default_threshold)
403409
end

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

+26-26
Original file line numberDiff line numberDiff line change
@@ -150,12 +150,12 @@ defmodule Mix.Tasks.TestTest do
150150
assert output =~ """
151151
Generating cover results ...
152152
153-
Percentage | Module
154-
-----------|--------------------------
155-
100.00% | Bar.Protocol
156-
100.00% | Bar.Protocol.BitString
157-
-----------|--------------------------
158-
100.00% | Total
153+
| Percentage | Module |
154+
|------------|------------------------|
155+
| 100.00% | Bar.Protocol |
156+
| 100.00% | Bar.Protocol.BitString |
157+
|------------|------------------------|
158+
| 100.00% | Total |
159159
"""
160160

161161
assert output =~ "1 test, 0 failures"
@@ -164,11 +164,11 @@ defmodule Mix.Tasks.TestTest do
164164
assert output =~ """
165165
Generating cover results ...
166166
167-
Percentage | Module
168-
-----------|--------------------------
169-
100.00% | Foo
170-
-----------|--------------------------
171-
100.00% | Total
167+
| Percentage | Module |
168+
|------------|------------|
169+
| 100.00% | Foo |
170+
|------------|------------|
171+
| 100.00% | Total |
172172
"""
173173

174174
# We skip a test in bar to force coverage below the default threshold
@@ -195,15 +195,15 @@ defmodule Mix.Tasks.TestTest do
195195
Importing cover results: apps/bar/cover/default.coverdata
196196
Importing cover results: apps/foo/cover/default.coverdata
197197
198-
Percentage | Module
199-
-----------|--------------------------
200-
100.00% | Bar
201-
100.00% | Bar.Ignore
202-
100.00% | Bar.Protocol
203-
100.00% | Bar.Protocol.BitString
204-
100.00% | Foo
205-
-----------|--------------------------
206-
100.00% | Total
198+
| Percentage | Module |
199+
|------------|------------------------|
200+
| 100.00% | Bar |
201+
| 100.00% | Bar.Ignore |
202+
| 100.00% | Bar.Protocol |
203+
| 100.00% | Bar.Protocol.BitString |
204+
| 100.00% | Foo |
205+
|------------|------------------------|
206+
| 100.00% | Total |
207207
"""
208208
end)
209209
end
@@ -366,12 +366,12 @@ defmodule Mix.Tasks.TestTest do
366366
Importing cover results: cover/1.coverdata
367367
Importing cover results: cover/2.coverdata
368368
369-
Percentage | Module
370-
-----------|--------------------------
371-
100.00% | A
372-
100.00% | B
373-
-----------|--------------------------
374-
100.00% | Total
369+
| Percentage | Module |
370+
|------------|------------|
371+
| 100.00% | A |
372+
| 100.00% | B |
373+
|------------|------------|
374+
| 100.00% | Total |
375375
376376
Generated HTML coverage results in \"cover\" directory
377377
"""

0 commit comments

Comments
 (0)