From bf3dfcb34bbcb87c8764ad3d82db817298af7853 Mon Sep 17 00:00:00 2001 From: Jon Meredith Date: Mon, 3 May 2010 11:37:14 -0600 Subject: Taught eunit/coverage to split the src module coverage stats from any test code. --- src/rebar_eunit.erl | 51 ++++++++++++++++++++++++++++++++------------------- src/rebar_utils.erl | 4 ++++ 2 files changed, 36 insertions(+), 19 deletions(-) (limited to 'src') diff --git a/src/rebar_eunit.erl b/src/rebar_eunit.erl index aca68fe..c4740a7 100644 --- a/src/rebar_eunit.erl +++ b/src/rebar_eunit.erl @@ -83,10 +83,11 @@ eunit(Config, _File) -> BeamFiles = [N || N <- rebar_utils:beams(?EUNIT_DIR), string:str(N, "_tests.beam") =:= 0], Modules = [rebar_utils:beam_to_mod(?EUNIT_DIR, N) || N <- BeamFiles], - + SrcModules = [rebar_utils:erl_to_mod(M) || M <- SrcErls], + cover_init(Config, BeamFiles), EunitResult = perform_eunit(Config, Modules), - perform_cover(Config, Modules), + perform_cover(Config, Modules, SrcModules), case EunitResult of ok -> @@ -182,17 +183,18 @@ is_quickcheck_avail() -> IsAvail end. -perform_cover(Config, BeamFiles) -> - perform_cover(rebar_config:get(Config, cover_enabled, false), Config, BeamFiles). +perform_cover(Config, BeamFiles, SrcModules) -> + perform_cover(rebar_config:get(Config, cover_enabled, false), + Config, BeamFiles, SrcModules). -perform_cover(false, _Config, _BeamFiles) -> +perform_cover(false, _Config, _BeamFiles, _SrcModules) -> ok; -perform_cover(true, Config, BeamFiles) -> - cover_analyze(Config, BeamFiles). +perform_cover(true, Config, BeamFiles, SrcModules) -> + cover_analyze(Config, BeamFiles, SrcModules). -cover_analyze(_Config, []) -> +cover_analyze(_Config, [], _SrcModules) -> ok; -cover_analyze(_Config, Modules) -> +cover_analyze(_Config, Modules, SrcModules) -> Suite = list_to_atom(rebar_config:get_global(suite, "")), FilteredModules = [M || M <- Modules, M =/= Suite], @@ -200,7 +202,7 @@ cover_analyze(_Config, Modules) -> Coverage = [cover_analyze_mod(M) || M <- FilteredModules], %% Write index of coverage info - cover_write_index(lists:sort(Coverage)), + cover_write_index(lists:sort(Coverage), SrcModules), %% Write coverage details for each file [{ok, _} = cover:analyze_to_file(M, cover_file(M), [html]) || {M, _, _} <- Coverage], @@ -243,7 +245,19 @@ cover_analyze_mod(Module) -> {0,0} end. -cover_write_index(Coverage) -> +cover_write_index(Coverage, SrcModules) -> + {ok, F} = file:open(filename:join([?EUNIT_DIR, "index.html"]), [write]), + ok = file:write(F, "Coverage Summary\n"), + IsSrcCoverage = fun({Mod,_C,_N}) -> lists:member(Mod, SrcModules) end, + {SrcCoverage, TestCoverage} = lists:partition(IsSrcCoverage, Coverage), + cover_write_index_section(F, "Source", SrcCoverage), + cover_write_index_section(F, "Test", TestCoverage), + ok = file:write(F, ""), + file:close(F). + +cover_write_index_section(_F, _SectionName, []) -> + ok; +cover_write_index_section(F, SectionName, Coverage) -> %% Calculate total coverage % {Covered, NotCovered} = lists:foldl(fun({_Mod, C, N}, {CAcc, NAcc}) -> {CAcc + C, NAcc + N} @@ -251,20 +265,19 @@ cover_write_index(Coverage) -> TotalCoverage = percentage(Covered, NotCovered), %% Write the report - {ok, F} = file:open(filename:join([?EUNIT_DIR, "index.html"]), [write]), - ok = file:write(F, "Coverage Summary\n" - "

Coverage Summary

\n"), - ok = file:write(F, ?FMT("

Total: ~w%

\n", [TotalCoverage])), + ok = file:write(F, ?FMT("

~s Summary

\n", [SectionName])), + ok = file:write(F, ?FMT("

Total: ~s

\n", [TotalCoverage])), ok = file:write(F, "\n"), - [ok = file:write(F, ?FMT("\n", + [ok = file:write(F, ?FMT("\n", [Module, Module, percentage(Cov, NotCov)])) || {Module, Cov, NotCov} <- Coverage], - ok = file:write(F, "
ModuleCoverage %
~s~w%
~s~s
"), - file:close(F). + ok = file:write(F, "\n"). cover_file(Module) -> filename:join([?EUNIT_DIR, atom_to_list(Module) ++ ".COVER.html"]). +percentage(0, 0) -> + "not executed"; percentage(Cov, NotCov) -> - trunc((Cov / (Cov + NotCov)) * 100). + integer_to_list(trunc((Cov / (Cov + NotCov)) * 100)) ++ "%". diff --git a/src/rebar_utils.erl b/src/rebar_utils.erl index e7d22fa..1aecd2e 100644 --- a/src/rebar_utils.erl +++ b/src/rebar_utils.erl @@ -36,6 +36,7 @@ now_str/0, ensure_dir/1, beam_to_mod/2, beams/1, + erl_to_mod/1, abort/2, escript_foldl/3]). @@ -153,6 +154,9 @@ beam_to_mod(Dir, Filename) -> [Dir | Rest] = filename:split(Filename), list_to_atom(filename:basename(string:join(Rest, "."), ".beam")). +erl_to_mod(Filename) -> + list_to_atom(filename:rootname(filename:basename(Filename))). + beams(Dir) -> filelib:fold_files(Dir, ".*\.beam\$", true, fun(F, Acc) -> [F | Acc] end, []). -- cgit v1.1