From 945543da19facd2dc7d9284a2df3e934f28800db Mon Sep 17 00:00:00 2001 From: alisdair sullivan Date: Wed, 25 Mar 2015 06:38:00 -0700 Subject: execute `cover` task as `test` profile and write generated data to the appropriate `_build` dir --- src/rebar_prv_cover.erl | 6 ++++-- test/rebar_cover_SUITE.erl | 22 +++++++++++----------- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/src/rebar_prv_cover.erl b/src/rebar_prv_cover.erl index d6e18dc..45d5b81 100644 --- a/src/rebar_prv_cover.erl +++ b/src/rebar_prv_cover.erl @@ -29,7 +29,8 @@ init(State) -> {example, "rebar3 cover"}, {short_desc, "Perform coverage analysis."}, {desc, ""}, - {opts, cover_opts(State)}])), + {opts, cover_opts(State)}, + {profiles, [test]}])), {ok, State1}. -spec do(rebar_state:t()) -> {ok, rebar_state:t()} | {error, string()}. @@ -333,7 +334,8 @@ verbose(State) -> end. cover_dir(State) -> - rebar_state:get(State, cover_data_dir, filename:join(["_build", "cover"])). + rebar_state:get(State, cover_data_dir, filename:join([rebar_dir:base_dir(State), + "cover"])). cover_opts(_State) -> [{reset, $r, "reset", boolean, help(reset)}, diff --git a/test/rebar_cover_SUITE.erl b/test/rebar_cover_SUITE.erl index f3c3248..0bead99 100644 --- a/test/rebar_cover_SUITE.erl +++ b/test/rebar_cover_SUITE.erl @@ -39,7 +39,7 @@ flag_coverdata_written(Config) -> Name = rebar_test_utils:create_random_name("cover_"), Vsn = rebar_test_utils:create_random_vsn(), - rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]), + rebar_test_utils:create_eunit_app(AppDir, Name, Vsn, [kernel, stdlib]), RebarConfig = [{erl_opts, [{d, some_define}]}], rebar_test_utils:run_and_check(Config, @@ -47,14 +47,14 @@ flag_coverdata_written(Config) -> ["eunit", "--cover"], {ok, [{app, Name}]}), - true = filelib:is_file(filename:join(["_build", "cover", "eunit.coverdata"])). + true = filelib:is_file(filename:join([AppDir, "_build", "test", "cover", "eunit.coverdata"])). config_coverdata_written(Config) -> AppDir = ?config(apps, Config), Name = rebar_test_utils:create_random_name("cover_"), Vsn = rebar_test_utils:create_random_vsn(), - rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]), + rebar_test_utils:create_eunit_app(AppDir, Name, Vsn, [kernel, stdlib]), RebarConfig = [{erl_opts, [{d, some_define}]}, {cover_enabled, true}], rebar_test_utils:run_and_check(Config, @@ -62,14 +62,14 @@ config_coverdata_written(Config) -> ["eunit"], {ok, [{app, Name}]}), - true = filelib:is_file(filename:join(["_build", "cover", "eunit.coverdata"])). + true = filelib:is_file(filename:join([AppDir, "_build", "test", "cover", "eunit.coverdata"])). index_written(Config) -> AppDir = ?config(apps, Config), Name = rebar_test_utils:create_random_name("cover_"), Vsn = rebar_test_utils:create_random_vsn(), - rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]), + rebar_test_utils:create_eunit_app(AppDir, Name, Vsn, [kernel, stdlib]), RebarConfig = [{erl_opts, [{d, some_define}]}], rebar_test_utils:run_and_check(Config, @@ -77,14 +77,14 @@ index_written(Config) -> ["do", "eunit", "--cover", ",", "cover"], {ok, [{app, Name}]}), - true = filelib:is_file(filename:join(["_build", "cover", "index.html"])). + true = filelib:is_file(filename:join([AppDir, "_build", "test", "cover", "index.html"])). config_alt_coverdir(Config) -> AppDir = ?config(apps, Config), Name = rebar_test_utils:create_random_name("cover_"), Vsn = rebar_test_utils:create_random_vsn(), - rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]), + rebar_test_utils:create_eunit_app(AppDir, Name, Vsn, [kernel, stdlib]), CoverDir = filename:join(["coverage", "goes", "here"]), @@ -101,7 +101,7 @@ flag_verbose(Config) -> Name = rebar_test_utils:create_random_name("cover_"), Vsn = rebar_test_utils:create_random_vsn(), - rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]), + rebar_test_utils:create_eunit_app(AppDir, Name, Vsn, [kernel, stdlib]), RebarConfig = [{erl_opts, [{d, some_define}]}], rebar_test_utils:run_and_check(Config, @@ -109,14 +109,14 @@ flag_verbose(Config) -> ["do", "eunit", "--cover", ",", "cover", "--verbose"], {ok, [{app, Name}]}), - true = filelib:is_file(filename:join(["_build", "cover", "index.html"])). + true = filelib:is_file(filename:join([AppDir, "_build", "test", "cover", "index.html"])). config_verbose(Config) -> AppDir = ?config(apps, Config), Name = rebar_test_utils:create_random_name("cover_"), Vsn = rebar_test_utils:create_random_vsn(), - rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]), + rebar_test_utils:create_eunit_app(AppDir, Name, Vsn, [kernel, stdlib]), RebarConfig = [{erl_opts, [{d, some_define}]}, {cover_print_enabled, true}], rebar_test_utils:run_and_check(Config, @@ -124,4 +124,4 @@ config_verbose(Config) -> ["do", "eunit", "--cover", ",", "cover"], {ok, [{app, Name}]}), - true = filelib:is_file(filename:join(["_build", "cover", "index.html"])). + true = filelib:is_file(filename:join([AppDir, "_build", "test", "cover", "index.html"])). -- cgit v1.1 From df4673d378ed9417a79d22aea7251f7f04468f77 Mon Sep 17 00:00:00 2001 From: alisdair sullivan Date: Wed, 25 Mar 2015 15:57:38 -0700 Subject: attempt to load paths to test modules in cover modules to generate line by line coverage reports --- src/rebar_prv_cover.erl | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/src/rebar_prv_cover.erl b/src/rebar_prv_cover.erl index 45d5b81..13c12d1 100644 --- a/src/rebar_prv_cover.erl +++ b/src/rebar_prv_cover.erl @@ -14,7 +14,7 @@ -include("rebar.hrl"). -define(PROVIDER, cover). --define(DEPS, []). +-define(DEPS, [app_discovery]). %% =================================================================== %% Public API @@ -136,14 +136,25 @@ import(CoverData) -> end. analysis(State, Task) -> + OldPath = code:get_path(), + ok = restore_cover_paths(State), Mods = cover:imported_modules(), - lists:map( - fun(Mod) -> - {ok, Answer} = cover:analyze(Mod, coverage, line), - {ok, File} = analyze_to_file(Mod, State, Task), - {Mod, process(Answer), File} - end, - Mods). + Analysis = lists:map(fun(Mod) -> + {ok, Answer} = cover:analyze(Mod, coverage, line), + {ok, File} = analyze_to_file(Mod, State, Task), + {Mod, process(Answer), File} + end, + Mods), + true = code:set_path(OldPath), + Analysis. + +restore_cover_paths(State) -> + lists:foreach(fun(App) -> + AppDir = rebar_app_info:out_dir(App), + _ = code:add_path(filename:join([AppDir, "ebin"])) + end, rebar_state:project_apps(State)), + _ = code:add_path(filename:join([rebar_dir:base_dir(State), "ebin"])), + ok. analyze_to_file(Mod, State, Task) -> CoverDir = cover_dir(State), @@ -320,8 +331,7 @@ write_coverdata(State, Task) -> ExportFile = filename:join([DataDir, atom_to_list(Task) ++ ".coverdata"]), case cover:export(ExportFile) of ok -> - ?DEBUG("Cover data written to ~p.", [ExportFile]), - ok; + ?DEBUG("Cover data written to ~p.", [ExportFile]); {error, Reason} -> ?WARN("Cover data export failed: ~p", [Reason]) end. -- cgit v1.1