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.
This commit is contained in:
Andrew Thompson 2010-10-06 21:05:13 +02:00
parent d5c408c2e4
commit f2244b26be
2 changed files with 35 additions and 2 deletions

View file

@ -71,6 +71,9 @@
%% 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

View file

@ -214,7 +214,7 @@ perform_cover(true, Config, BeamFiles, SrcModules) ->
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],
@ -228,7 +228,15 @@ cover_analyze(_Config, Modules, SrcModules) ->
[{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;
@ -326,6 +334,28 @@ cover_write_index_section(F, SectionName, Coverage) ->
{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"]).