diff options
-rw-r--r-- | src/rebar_eunit.erl | 32 | ||||
-rw-r--r-- | test/rebar_eunit_tests.erl | 24 |
2 files changed, 53 insertions, 3 deletions
diff --git a/src/rebar_eunit.erl b/src/rebar_eunit.erl index c4740a7..bd93a48 100644 --- a/src/rebar_eunit.erl +++ b/src/rebar_eunit.erl @@ -238,13 +238,43 @@ cover_init(Config, BeamFiles) -> cover_analyze_mod(Module) -> case cover:analyze(Module, coverage, module) of {ok, {Module, {Covered, NotCovered}}} -> - {Module, Covered, NotCovered}; + %% Modules that include the eunit header get an implicit + %% test/0 fun, which cover considers a runnable line, but + %% eunit:test(TestRepresentation) never calls. Decrement + %% NotCovered in this case. + align_notcovered_count(Module, Covered, NotCovered, + is_eunitized(Module)); {error, Reason} -> ?ERROR("Cover analyze failed for ~p: ~p ~p\n", [Module, Reason, code:which(Module)]), {0,0} end. +is_eunitized(Mod) -> + has_eunit_test_fun(Mod) andalso + has_eunit_header(Mod). + +has_eunit_test_fun(Mod) -> + length([F || {exports, Funs} <- Mod:module_info(), + {F, 0} <- Funs, + F == test]) =/= 0. + +has_eunit_header(Mod) -> + OrigEnv = set_proc_env(), + try has_header(Mod, "include/eunit.hrl") + after restore_proc_env(OrigEnv) + end. + +has_header(Mod, Header) -> + {ok, {_, [{abstract_code, {_, AC}}]}} = beam_lib:chunks(Mod, [abstract_code]), + length([F || {attribute, 1, file, {F, 1}} <- AC, + string:str(F, Header) =/= 0]) =/= 0. + +align_notcovered_count(Module, Covered, NotCovered, false) -> + {Module, Covered, NotCovered}; +align_notcovered_count(Module, Covered, NotCovered, true) -> + {Module, Covered, NotCovered - 1}. + cover_write_index(Coverage, SrcModules) -> {ok, F} = file:open(filename:join([?EUNIT_DIR, "index.html"]), [write]), ok = file:write(F, "<html><head><title>Coverage Summary</title></head>\n"), diff --git a/test/rebar_eunit_tests.erl b/test/rebar_eunit_tests.erl index 4f035fb..2187091 100644 --- a/test/rebar_eunit_tests.erl +++ b/test/rebar_eunit_tests.erl @@ -88,14 +88,25 @@ cover_with_suite_test_() -> {"Only production modules get coverage reports", assert_files_not_in("the temporary eunit directory", [".eunit/myapp_mymod_tests.COVER.html", - ".eunit/mysuite"])}]}. + ".eunit/mysuite.COVER.html"])}]}. expected_cover_generated_files() -> [".eunit/index.html", ".eunit/myapp_app.COVER.html", ".eunit/myapp_mymod.COVER.html", ".eunit/myapp_sup.COVER.html"]. - + +cover_coverage_test_() -> + {"Coverage is accurately calculated", + setup, fun() -> setup_cover_project(), rebar("-v eunit") end, + fun teardown/1, + + [{"Modules that include the EUnit header can still have 100% coverage", + %% cover notices the implicit EUnit test/0 func that never gets + %% called during eunit:test(TestRepresentation), so NotCounted + %% needs to be decremented in this case. + assert_full_coverage("myapp_mymod")}]}. + %% ==================================================================== %% Environment and Setup Tests %% ==================================================================== @@ -227,3 +238,12 @@ assert_files_not_in(Name, [File|T]) -> assert_files_not_in(Name, T)]; assert_files_not_in(_, []) -> []. +assert_full_coverage(Mod) -> + fun() -> + {ok, F} = file:read_file(".eunit/index.html"), + Result = [X || X <- string:tokens(binary_to_list(F), "\n"), + string:str(X, Mod) =/= 0, + string:str(X, "100%") =/= 0], + file:close(F), + ?assert(length(Result) == 1) + end. |