From 88c05240629e5313e63315056fe99e1443845f48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonatan=20M=C3=A4nnchen?= Date: Tue, 1 Apr 2025 15:01:59 +0200 Subject: [PATCH] Update test coverage summary to produce a markdown-ish table Prep for Coverage Reporting in tests --- lib/mix/lib/mix/tasks/test.coverage.ex | 26 ++++++++----- lib/mix/test/mix/tasks/test_test.exs | 52 +++++++++++++------------- 2 files changed, 42 insertions(+), 36 deletions(-) diff --git a/lib/mix/lib/mix/tasks/test.coverage.ex b/lib/mix/lib/mix/tasks/test.coverage.ex index 30466491bc2..143a6bc0dff 100644 --- a/lib/mix/lib/mix/tasks/test.coverage.ex +++ b/lib/mix/lib/mix/tasks/test.coverage.ex @@ -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 @@ -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 @@ -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 diff --git a/lib/mix/test/mix/tasks/test_test.exs b/lib/mix/test/mix/tasks/test_test.exs index af8bd1e6171..a4700639679 100644 --- a/lib/mix/test/mix/tasks/test_test.exs +++ b/lib/mix/test/mix/tasks/test_test.exs @@ -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" @@ -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 @@ -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 @@ -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 """