From 4ff52d991eb1240acbe82859d643295076f90727 Mon Sep 17 00:00:00 2001 From: Louis-Philippe Gauthier Date: Wed, 15 Jun 2016 08:05:21 -0400 Subject: [PATCH 1/3] Add support for cover_excl_mods --- rebar.config.sample | 3 +++ src/rebar_prv_cover.erl | 38 +++++++++++++++++++++++--------------- 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/rebar.config.sample b/rebar.config.sample index f57f8dc17..cc027f92d 100644 --- a/rebar.config.sample +++ b/rebar.config.sample @@ -57,6 +57,9 @@ %% is `false' {cover_enabled, false}. +%% Modules to exclude from cover +{cover_excl_mods, []}. + %% Options to pass to cover provider {cover_opts, [verbose]}. diff --git a/src/rebar_prv_cover.erl b/src/rebar_prv_cover.erl index e555a9b58..b04b1c1a3 100644 --- a/src/rebar_prv_cover.erl +++ b/src/rebar_prv_cover.erl @@ -302,18 +302,30 @@ cover_compile(State, Dirs) -> {ok, CoverPid} = start_cover(), %% redirect cover output true = redirect_cover_output(State, CoverPid), + ExclMods = rebar_state:get(State, cover_excl_mods, []), + lists:foreach(fun(Dir) -> - ?DEBUG("cover compiling ~p", [Dir]), - case catch(cover:compile_beam_directory(Dir)) of - {error, eacces} -> - ?WARN("Directory ~p not readable, modules will not be included in coverage", [Dir]); - {error, enoent} -> - ?WARN("Directory ~p not found", [Dir]); - {'EXIT', {Reason, _}} -> - ?WARN("Cover compilation for directory ~p failed: ~p", [Dir, Reason]); - Results -> - %% print any warnings about modules that failed to cover compile - lists:foreach(fun print_cover_warnings/1, lists:flatten(Results)) + case file:list_dir(Dir) of + {ok, Files} -> + Files2 = [F || F <- Files, filename:extension(F) == ".beam"], + lists:foreach(fun(File) -> + case lists:any(fun (Excl) -> + File =:= (atom_to_list(Excl) ++ ".beam") + end, ExclMods) of + true -> + ?DEBUG("cover ignoring ~p ~p", [Dir, File]); + _ -> + ?DEBUG("cover compiling ~p ~p", [Dir, File]), + case catch(cover:compile_beam(filename:join(Dir, File))) of + {error, Reason} -> + ?WARN("Cover compilation failed: ~p", [Reason]); + {ok, _} -> + ok + end + end + end, Files2); + {error, Reason} -> + ?WARN("Directory ~p error ~p", [Dir, Reason]) end end, Dirs). @@ -346,10 +358,6 @@ redirect_cover_output(State, CoverPid) -> [append]), group_leader(F, CoverPid). -print_cover_warnings({ok, _}) -> ok; -print_cover_warnings({error, Error}) -> - ?WARN("Cover compilation failed: ~p", [Error]). - write_coverdata(State, Task) -> DataDir = cover_dir(State), ok = filelib:ensure_dir(filename:join([DataDir, "dummy.log"])), From dc03bb4c2d8f9dc4cb72590a02a20635e9526aa5 Mon Sep 17 00:00:00 2001 From: Fred Hebert Date: Sat, 27 Aug 2016 07:32:09 -0400 Subject: [PATCH 2/3] Add test suite for cover_excl_mods option --- test/rebar_cover_SUITE.erl | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/test/rebar_cover_SUITE.erl b/test/rebar_cover_SUITE.erl index 841e29f8c..1289f19b9 100644 --- a/test/rebar_cover_SUITE.erl +++ b/test/rebar_cover_SUITE.erl @@ -12,7 +12,8 @@ root_extra_src_dirs/1, index_written/1, flag_verbose/1, - config_verbose/1]). + config_verbose/1, + excl_mods/1]). -include_lib("common_test/include/ct.hrl"). -include_lib("eunit/include/eunit.hrl"). @@ -35,7 +36,8 @@ all() -> basic_extra_src_dirs, release_extra_src_dirs, root_extra_src_dirs, index_written, - flag_verbose, config_verbose]. + flag_verbose, config_verbose, + excl_mods]. flag_coverdata_written(Config) -> AppDir = ?config(apps, Config), @@ -206,3 +208,27 @@ config_verbose(Config) -> {ok, [{app, Name}]}), true = filelib:is_file(filename:join([AppDir, "_build", "test", "cover", "index.html"])). + +excl_mods(Config) -> + AppDir = ?config(apps, Config), + + Name1 = rebar_test_utils:create_random_name("relapp1_"), + Vsn1 = rebar_test_utils:create_random_vsn(), + rebar_test_utils:create_app(filename:join([AppDir, "apps", Name1]), Name1, Vsn1, [kernel, stdlib]), + + Name2 = rebar_test_utils:create_random_name("relapp2_"), + Vsn2 = rebar_test_utils:create_random_vsn(), + rebar_test_utils:create_app(filename:join([AppDir, "apps", Name2]), Name2, Vsn2, [kernel, stdlib]), + + Mod1 = list_to_atom(Name1), + Mod2 = list_to_atom(Name2), + RebarConfig = [{erl_opts, [{d, some_define}]}, + {cover_excl_mods, [Mod2]}], + + rebar_test_utils:run_and_check(Config, + RebarConfig, + ["eunit", "--cover"], + {ok, [{app, Name1}, {app, Name2}]}), + + {file, _} = cover:is_compiled(Mod1), + false = cover:is_compiled(Mod2). From 4b0a07221de5e338cd71d774deff249f0528fa36 Mon Sep 17 00:00:00 2001 From: Fred Hebert Date: Sat, 27 Aug 2016 07:48:06 -0400 Subject: [PATCH 3/3] Re-format cover exclusion code - brings back former error handling and debug messages - keeps the filtering of excluded mods and debug messages - breaks up code into multiple functions and removes nesting --- src/rebar_prv_cover.erl | 43 +++++++++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/src/rebar_prv_cover.erl b/src/rebar_prv_cover.erl index 9772e9650..7c2597b36 100644 --- a/src/rebar_prv_cover.erl +++ b/src/rebar_prv_cover.erl @@ -308,23 +308,16 @@ cover_compile(State, Dirs) -> lists:foreach(fun(Dir) -> case file:list_dir(Dir) of {ok, Files} -> - Files2 = [F || F <- Files, filename:extension(F) == ".beam"], - lists:foreach(fun(File) -> - case lists:any(fun (Excl) -> - File =:= (atom_to_list(Excl) ++ ".beam") - end, ExclMods) of - true -> - ?DEBUG("cover ignoring ~p ~p", [Dir, File]); - _ -> - ?DEBUG("cover compiling ~p ~p", [Dir, File]), - case catch(cover:compile_beam(filename:join(Dir, File))) of - {error, Reason} -> - ?WARN("Cover compilation failed: ~p", [Reason]); - {ok, _} -> - ok - end - end - end, Files2); + ?DEBUG("cover compiling ~p", [Dir]), + [cover_compile_file(filename:join(Dir, File)) + || File <- Files, + filename:extension(File) == ".beam", + not is_ignored(Dir, File, ExclMods)], + ok; + {error, eacces} -> + ?WARN("Directory ~p not readable, modules will not be included in coverage", [Dir]); + {error, enoent} -> + ?WARN("Directory ~p not found", [Dir]); {error, Reason} -> ?WARN("Directory ~p error ~p", [Dir, Reason]) end @@ -332,6 +325,22 @@ cover_compile(State, Dirs) -> rebar_utils:cleanup_code_path(rebar_state:code_paths(State, default)), ok. +is_ignored(Dir, File, ExclMods) -> + Ignored = lists:any(fun(Excl) -> + File =:= atom_to_list(Excl) ++ ".beam" + end, + ExclMods), + Ignored andalso ?DEBUG("cover ignoring ~p ~p", [Dir, File]), + Ignored. + +cover_compile_file(FileName) -> + case catch(cover:compile_beam(FileName)) of + {error, Reason} -> + ?WARN("Cover compilation failed: ~p", [Reason]); + {ok, _} -> + ok + end. + app_dirs(Apps) -> lists:foldl(fun app_ebin_dirs/2, [], Apps).