Commits

Andrew Thompson  committed 98f31cd

Add optional eunit coverage report to terminal

Add a coverage report similar to the one output to index.html except
that it is output to the terminal if the new rebar.conf option
'cover_print_enabled' is set to true.

  • Participants
  • Parent commits f4d1ff4

Comments (0)

Files changed (2)

File rebar.config.sample

 %% Whether to enable coverage reporting. Default is `false'
 {cover_enabled, false}.
 
+%% Whether to print coverage report to console. Default is `false'
+{cover_print_enabled, false}.
+
 %% == Dialyzer ==
 
 %% Options for running the dialyzer, right now only `plt' is supported

File src/rebar_eunit.erl

 
 cover_analyze(_Config, [], _SrcModules) ->
     ok;
-cover_analyze(_Config, Modules, SrcModules) ->
+cover_analyze(Config, Modules, SrcModules) ->
     Suite = list_to_atom(rebar_config:get_global(suite, "")),
     FilteredModules = [M || M <- Modules, M =/= Suite],
 
     [{ok, _} = cover:analyze_to_file(M, cover_file(M), [html]) || {M, _, _} <- Coverage],
 
     Index = filename:join([rebar_utils:get_cwd(), ?EUNIT_DIR, "index.html"]),
-    ?CONSOLE("Cover analysis: ~s\n", [Index]).
+    ?CONSOLE("Cover analysis: ~s\n", [Index]),
+
+    %% Print coverage report, if configured
+    case rebar_config:get(Config, cover_print_enabled, false) of
+        true ->
+            cover_print_coverage(lists:sort(Coverage));
+        false ->
+            ok
+    end.
 
 cover_init(false, _BeamFiles) ->
     ok;
         {Module, Cov, NotCov} <- Coverage],
     ok = file:write(F, "</table>\n").
 
+cover_print_coverage(Coverage) ->
+    {Covered, NotCovered} = lists:foldl(fun({_Mod, C, N}, {CAcc, NAcc}) ->
+                                                {CAcc + C, NAcc + N}
+                                        end, {0, 0}, Coverage),
+    TotalCoverage = percentage(Covered, NotCovered),
+
+    %% Determine the longest module name for right-padding
+    Width = lists:foldl(fun({Mod, _, _}, Acc) ->
+                case length(atom_to_list(Mod)) of
+                    N when N > Acc ->
+                        N;
+                    _ ->
+                        Acc
+                end
+        end, 0, Coverage) * -1,
+
+    %% Print the output the console
+    ?CONSOLE("~nCode Coverage:~n", []),
+    [?CONSOLE("~*s : ~3s~n",
+            [Width, Mod, percentage(C, N)]) || {Mod, C, N} <- Coverage],
+    ?CONSOLE("~n~*s : ~s~n", [Width, "Total", TotalCoverage]).
+
 cover_file(Module) ->
     filename:join([?EUNIT_DIR, atom_to_list(Module) ++ ".COVER.html"]).