From 25538e071fa154b49a8f282e84907316b1bf54b6 Mon Sep 17 00:00:00 2001 From: Chris Bernard Date: Thu, 4 Mar 2010 20:53:12 -0500 Subject: Fixed 3 bugs in rebar_eunit. Added EUnit tests to capture them. 1. When running the eunit command with the convention of putting tests in "*_tests" modules, eunit would run those tests twice. This is because: 1) eunit:test/1 will naturally look for foo's tests both in foo, and in foo_tests, and 2) eunit:test/1 was being folded over all project modules. The fix is to filter "*_tests" modules from the list passed to eunit:test/1. 2. When running the eunit command with cover enabled and tests in a 'test' directory, cover would error because it couldn't find the source code for those tests. This is because cover:analyze/3 will only find module source in "." and "../src". This is hard-coded in cover :-(. Since cover shouldn't be calculating code coverage on test code anyway, the fix is to not fold cover:analyze/3 over non-production code. 3. When running the eunit command with cover enabled and a test suite defined, cover would only attempt to calculate coverage on the the test suite itself. This was because only the suite was passed to cover:analyze/3. The fix is to fold cover:analyze/3 over all the production code, filtering out the suite module if it is defined. --- src/rebar_eunit.erl | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/rebar_eunit.erl b/src/rebar_eunit.erl index 38c1001..33a6ba7 100644 --- a/src/rebar_eunit.erl +++ b/src/rebar_eunit.erl @@ -72,12 +72,15 @@ eunit(Config, _File) -> %% and eunit testing. Normally you can just tell cover and/or eunit to %% scan the directory for you, but eunit does a code:purge in conjunction %% with that scan and causes any cover compilation info to be lost. - BeamFiles = rebar_utils:beams(?EUNIT_DIR), + %% Filter out "*_tests" modules so eunit won't doubly run them and + %% so cover only calculates coverage on production code. + 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], cover_init(Config, BeamFiles), EunitResult = perform_eunit(Config, Modules), - perform_cover(Config, BeamFiles), + perform_cover(Config, Modules), case EunitResult of ok -> @@ -176,18 +179,16 @@ perform_cover(Config, BeamFiles) -> perform_cover(false, _Config, _BeamFiles) -> ok; perform_cover(true, Config, BeamFiles) -> - perform_cover(Config, BeamFiles, rebar_config:get_global(suite, undefined)); -perform_cover(Config, BeamFiles, undefined) -> - cover_analyze(Config, BeamFiles); -perform_cover(Config, _BeamFiles, Suite) -> - cover_analyze(Config, [filename:join([?EUNIT_DIR | string:tokens(Suite, ".")]) ++ ".beam"]). + cover_analyze(Config, BeamFiles). cover_analyze(_Config, []) -> ok; -cover_analyze(_Config, BeamFiles) -> - Modules = [rebar_utils:beam_to_mod(?EUNIT_DIR, N) || N <- BeamFiles], +cover_analyze(_Config, Modules) -> + Suite = list_to_atom(rebar_config:get_global(suite, "")), + FilteredModules = [M || M <- Modules, M =/= Suite], + %% Generate coverage info for all the cover-compiled modules - Coverage = [cover_analyze_mod(M) || M <- Modules], + Coverage = [cover_analyze_mod(M) || M <- FilteredModules], %% Write index of coverage info cover_write_index(lists:sort(Coverage)), -- cgit v1.1