Proposal: Print coverage results to console with default coverage reporter

40 views
Skip to first unread message

Łukasz Niemier

unread,
May 9, 2018, 6:22:02 AM5/9/18
to elixir-lang-core
This would greatly reduce people using additional coverage reporters like excoveralls or similar as some tools (at least GitLab CI that I am using) allows to parse test output to fetch general coverage from there.

My proposal is to generate per module and total percentage from default coverage reporter. Example Coverage module that provides such result (additional there is additional option :threshold which colours resulting coverage red/green).

defmodule Cover do
  @moduledoc false

  @threshold 90

  def start(compile_path, opts) do
    Mix.shell().info("Cover compiling modules ...")
    _ = :cover.start()

    case :cover.compile_beam_directory(compile_path |> to_charlist) do
      results when is_list(results) ->
        :ok

      {:error, _} ->
        Mix.raise("Failed to cover compile directory: " <> compile_path)
    end

    output = opts[:output]

    fn ->
      File.mkdir_p!(output)

      Mix.shell().info("\nCover results")

      {:result, ok, _fail} = :cover.analyse(:coverage, :module)

      Mix.shell().info("Percentage | Module")
      Mix.shell().info("-----------|--------------------------")

      total =
        ok
        |> Stream.each(&display(&1, opts))
        |> Stream.each(&html(&1, output))
        |> Enum.reduce({0, 0}, fn {_, {cov, not_cov}}, {tot_cov, tot_not_cov} ->
          {tot_cov + cov, tot_not_cov + not_cov}
        end)

      Mix.shell().info("-----------|--------------------------")

      display({"Total", total}, opts)
    end
  end

  defp colour(percentage, threshold) when percentage > threshold, do: :green
  defp colour(_, _), do: :red

  defp display({name, coverage}, opts) do
    threshold = Keyword.get(opts, :threshold, @threshold)
    percentage = percentage(coverage)

    Mix.shell().info([
      colour(percentage, threshold),
      format(percentage, 9),
      "%",
      :reset,
      " | #{name}"
    ])
  end

  defp html({mod, _}, output) do
    {:ok, _} = :cover.analyse_to_file(mod, '#{output}/#{mod}.html', [:html])
  end

  defp percentage({0, 0}), do: 100
  defp percentage({cov, not_cov}), do: cov / (cov + not_cov) * 100

  defp format(num, len) when is_integer(num) do
    num
    |> Integer.to_string()
    |> String.pad_leading(len)
  end

  defp format(num, len) when is_float(num) do
    num
    |> Float.round(2)
    |> Float.to_string()
    |> String.pad_leading(len)
  end
end

José Valim

unread,
May 9, 2018, 7:25:04 AM5/9/18
to elixir-l...@googlegroups.com
Can you please provide a sample of the output?



José Valim
Founder and 
Director of R&D

--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-core+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/6ca4916a-8b3d-44c5-9df7-ba0467150a5a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Łukasz Niemier

unread,
May 9, 2018, 7:29:39 AM5/9/18
to elixir-lang-core
Very under tested project that I used to test this formatter:

Cover compiling modules ...
..

Finished in 0.1 seconds
2 tests, 0 failures

Randomized with seed 438070

Cover results
Percentage | Module
-----------|--------------------------
      0.0% | Elixir.Cover
      0.0% | Elixir.ImagerWeb.Instrumenter
     50.0% | Elixir.Imager.Application
      0.0% | Elixir.Imager.Convert
      0.0% | Elixir.ImagerWeb.ChannelCase
      0.0% | Elixir.ImagerWeb.UserSocket
      0.0% | Elixir.ImagerWeb.WebhookController
     6.45% | Elixir.ImagerWeb.Router.Helpers
    15.79% | Elixir.ImagerWeb.ErrorView
    45.45% | Elixir.ImagerWeb.ConnCase
     5.26% | Elixir.ImagerWeb.Endpoint
    23.81% | Elixir.Imager.Stats
     5.71% | Elixir.Imager
      0.0% | Elixir.ImagerWeb.Router
      0.0% | Elixir.ImagerWeb
-----------|--------------------------
     7.88% | Total

Screenshot with colours: https://cl.ly/0Y1K192Q220k

--

Łukasz Niemier
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages