diff options
-rw-r--r-- | src/rebar_prv_common_test.erl | 13 | ||||
-rw-r--r-- | src/rebar_prv_cover.erl | 61 | ||||
-rw-r--r-- | src/rebar_prv_eunit.erl | 5 | ||||
-rw-r--r-- | test/rebar_cover_SUITE.erl | 67 | ||||
-rw-r--r-- | test/rebar_ct_SUITE.erl | 27 |
5 files changed, 151 insertions, 22 deletions
diff --git a/src/rebar_prv_common_test.erl b/src/rebar_prv_common_test.erl index 9631a0d..f800610 100644 --- a/src/rebar_prv_common_test.erl +++ b/src/rebar_prv_common_test.erl @@ -8,8 +8,11 @@ -export([init/1, do/1, format_error/1]). -%% exported for test purposes, consider private --export([compile/2, prepare_tests/1, translate_paths/2]). + +-ifdef(TEST). +%% exported for test purposes +-export([compile/2, prepare_tests/1, translate_paths/2, maybe_write_coverdata/1]). +-endif. -include("rebar.hrl"). -include_lib("providers/include/providers.hrl"). @@ -728,7 +731,8 @@ maybe_write_coverdata(State) -> true -> rebar_state:set(State, cover_enabled, true); false -> State end, - rebar_prv_cover:maybe_write_coverdata(State1, ?PROVIDER). + Name = proplists:get_value(cover_export_name, RawOpts, ?PROVIDER), + rebar_prv_cover:maybe_write_coverdata(State1, Name). ct_opts(_State) -> [{dir, undefined, "dir", string, help(dir)}, %% comma-separated list @@ -744,6 +748,7 @@ ct_opts(_State) -> {logopts, undefined, "logopts", string, help(logopts)}, %% comma-separated list {verbosity, undefined, "verbosity", integer, help(verbosity)}, %% Integer {cover, $c, "cover", {boolean, false}, help(cover)}, + {cover_export_name, undefined, "cover_export_name", string, help(cover_export_name)}, {repeat, undefined, "repeat", integer, help(repeat)}, %% integer {duration, undefined, "duration", string, help(duration)}, % format: HHMMSS {until, undefined, "until", string, help(until)}, %% format: YYMoMoDD[HHMMSS] @@ -797,6 +802,8 @@ help(verbosity) -> "Verbosity"; help(cover) -> "Generate cover data"; +help(cover_export_name) -> + "Base name of the coverdata file to write"; help(repeat) -> "How often to repeat tests"; help(duration) -> diff --git a/src/rebar_prv_cover.erl b/src/rebar_prv_cover.erl index c890452..7412038 100644 --- a/src/rebar_prv_cover.erl +++ b/src/rebar_prv_cover.erl @@ -12,6 +12,7 @@ maybe_write_coverdata/2, format_error/1]). +-include_lib("providers/include/providers.hrl"). -include("rebar.hrl"). -define(PROVIDER, cover). @@ -62,6 +63,9 @@ maybe_write_coverdata(State, Task) -> end. -spec format_error(any()) -> iolist(). +format_error({min_coverage_failed, {PassRate, Total}}) -> + io_lib:format("Requiring ~p% coverage to pass. Only ~p% obtained", + [PassRate, Total]); format_error(Reason) -> io_lib:format("~p", [Reason]). @@ -102,13 +106,13 @@ do_analyze(State) -> %% redirect cover output true = redirect_cover_output(State, CoverPid), %% analyze! - ok = case analyze(State, CoverFiles) of - [] -> ok; + case analyze(State, CoverFiles) of + [] -> {ok, State}; Analysis -> print_analysis(Analysis, verbose(State)), - write_index(State, Analysis) - end, - {ok, State}. + write_index(State, Analysis), + maybe_fail_coverage(Analysis, State) + end. get_all_coverdata(CoverDir) -> ok = filelib:ensure_dir(filename:join([CoverDir, "dummy.log"])), @@ -213,11 +217,11 @@ format_table(Stats, CoverFiles) -> Header = header(MaxLength), Separator = separator(MaxLength), TotalLabel = format("total", MaxLength), - TotalCov = format(calculate_total(Stats), 8), + TotalCov = format(calculate_total_string(Stats), 8), [io_lib:format("~ts~n~ts~n~ts~n", [Separator, Header, Separator]), lists:map(fun({Mod, Coverage, _}) -> Name = format(Mod, MaxLength), - Cov = format(percentage(Coverage), 8), + Cov = format(percentage_string(Coverage), 8), io_lib:format(" | ~ts | ~ts |~n", [Name, Cov]) end, Stats), io_lib:format("~ts~n", [Separator]), @@ -239,6 +243,9 @@ separator(Width) -> format(String, Width) -> io_lib:format("~*.ts", [Width, String]). +calculate_total_string(Stats) -> + integer_to_list(calculate_total(Stats))++"%". + calculate_total(Stats) -> percentage(lists:foldl( fun({_Mod, {Cov, Not}, _File}, {CovAcc, NotAcc}) -> @@ -248,8 +255,10 @@ calculate_total(Stats) -> Stats )). -percentage({_, 0}) -> "100%"; -percentage({Cov, Not}) -> integer_to_list(trunc((Cov / (Cov + Not)) * 100)) ++ "%". +percentage_string(Data) -> integer_to_list(percentage(Data))++"%". + +percentage({_, 0}) -> 100; +percentage({Cov, Not}) -> trunc((Cov / (Cov + Not)) * 100). write_index(State, Coverage) -> CoverDir = cover_dir(State), @@ -279,14 +288,25 @@ write_index_section(F, [{Section, DataFile, Mods}|Rest]) -> FmtLink = fun({Mod, Cov, Report}) -> ?FMT("<tr><td><a href='~ts'>~ts</a></td><td>~ts</td>\n", - [strip_coverdir(Report), Mod, percentage(Cov)]) + [strip_coverdir(Report), Mod, percentage_string(Cov)]) end, lists:foreach(fun(M) -> ok = file:write(F, FmtLink(M)) end, Mods), ok = file:write(F, ?FMT("<tr><td><strong>Total</strong></td><td>~ts</td>\n", - [calculate_total(Mods)])), + [calculate_total_string(Mods)])), ok = file:write(F, "</table>\n"), write_index_section(F, Rest). +maybe_fail_coverage(Analysis, State) -> + {_, _CoverFiles, Stats} = lists:keyfind("aggregate", 1, Analysis), + Total = calculate_total(Stats), + PassRate = min_coverage(State), + ?DEBUG("Comparing ~p to pass rate ~p", [Total, PassRate]), + if Total >= PassRate -> + {ok, State} + ; Total < PassRate -> + ?PRV_ERROR({min_coverage_failed, {PassRate, Total}}) + end. + %% fix for r15b which doesn't put the correct path in the `source` section %% of `module_info(compile)` strip_coverdir([]) -> ""; @@ -372,10 +392,10 @@ redirect_cover_output(State, CoverPid) -> [append]), group_leader(F, CoverPid). -write_coverdata(State, Task) -> +write_coverdata(State, Name) -> DataDir = cover_dir(State), ok = filelib:ensure_dir(filename:join([DataDir, "dummy.log"])), - ExportFile = filename:join([DataDir, atom_to_list(Task) ++ ".coverdata"]), + ExportFile = filename:join([DataDir, rebar_utils:to_list(Name) ++ ".coverdata"]), case cover:export(ExportFile) of ok -> %% dump accumulated coverdata after writing @@ -401,12 +421,23 @@ verbose(State) -> {Verbose, _} -> Verbose end. +min_coverage(State) -> + Command = proplists:get_value(min_coverage, command_line_opts(State), undefined), + Config = proplists:get_value(min_coverage, config_opts(State), undefined), + case {Command, Config} of + {undefined, undefined} -> 0; + {undefined, Rate} -> Rate; + {Rate, _} -> Rate + end. + cover_dir(State) -> filename:join([rebar_dir:base_dir(State), "cover"]). cover_opts(_State) -> [{reset, $r, "reset", boolean, help(reset)}, - {verbose, $v, "verbose", boolean, help(verbose)}]. + {verbose, $v, "verbose", boolean, help(verbose)}, + {min_coverage, $m, "min_coverage", integer, help(min_coverage)}]. help(reset) -> "Reset all coverdata."; -help(verbose) -> "Print coverage analysis.". +help(verbose) -> "Print coverage analysis."; +help(min_coverage) -> "Mandate a coverage percentage required to succeed (0..100)". diff --git a/src/rebar_prv_eunit.erl b/src/rebar_prv_eunit.erl index 2361432..4b71416 100644 --- a/src/rebar_prv_eunit.erl +++ b/src/rebar_prv_eunit.erl @@ -472,7 +472,8 @@ maybe_write_coverdata(State) -> true -> rebar_state:set(State, cover_enabled, true); false -> State end, - rebar_prv_cover:maybe_write_coverdata(State1, ?PROVIDER). + Name = proplists:get_value(cover_export_name, RawOpts, ?PROVIDER), + rebar_prv_cover:maybe_write_coverdata(State1, Name). handle_results(ok) -> ok; handle_results(error) -> @@ -484,6 +485,7 @@ eunit_opts(_State) -> [{app, undefined, "app", string, help(app)}, {application, undefined, "application", string, help(app)}, {cover, $c, "cover", boolean, help(cover)}, + {cover_export_name, undefined, "cover_export_name", string, help(cover_export_name)}, {dir, $d, "dir", string, help(dir)}, {file, $f, "file", string, help(file)}, {module, $m, "module", string, help(module)}, @@ -495,6 +497,7 @@ eunit_opts(_State) -> help(app) -> "Comma separated list of application test suites to run. Equivalent to `[{application, App}]`."; help(cover) -> "Generate cover data. Defaults to false."; +help(cover_export_name) -> "Base name of the coverdata file to write"; help(dir) -> "Comma separated list of dirs to load tests from. Equivalent to `[{dir, Dir}]`."; help(file) -> "Comma separated list of files to load tests from. Equivalent to `[{file, File}]`."; help(module) -> "Comma separated list of modules to load tests from. Equivalent to `[{module, Module}]`."; diff --git a/test/rebar_cover_SUITE.erl b/test/rebar_cover_SUITE.erl index cc06c95..8d6429d 100644 --- a/test/rebar_cover_SUITE.erl +++ b/test/rebar_cover_SUITE.erl @@ -7,6 +7,7 @@ all/0, flag_coverdata_written/1, config_coverdata_written/1, + config_coverdata_overridden_name_written/1, basic_extra_src_dirs/1, release_extra_src_dirs/1, root_extra_src_dirs/1, @@ -14,7 +15,9 @@ flag_verbose/1, config_verbose/1, excl_mods_and_apps/1, - coverdata_is_reset_on_write/1]). + coverdata_is_reset_on_write/1, + flag_min_coverage/1, + config_min_coverage/1]). -include_lib("common_test/include/ct.hrl"). -include_lib("eunit/include/eunit.hrl"). @@ -34,11 +37,13 @@ init_per_testcase(_, Config) -> all() -> [flag_coverdata_written, config_coverdata_written, + config_coverdata_overridden_name_written, basic_extra_src_dirs, release_extra_src_dirs, root_extra_src_dirs, index_written, flag_verbose, config_verbose, - excl_mods_and_apps, coverdata_is_reset_on_write]. + excl_mods_and_apps, coverdata_is_reset_on_write, + flag_min_coverage, config_min_coverage]. flag_coverdata_written(Config) -> AppDir = ?config(apps, Config), @@ -70,6 +75,21 @@ config_coverdata_written(Config) -> true = filelib:is_file(filename:join([AppDir, "_build", "test", "cover", "eunit.coverdata"])). +config_coverdata_overridden_name_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_eunit_app(AppDir, Name, Vsn, [kernel, stdlib]), + + RebarConfig = [{erl_opts, [{d, some_define}]}, {cover_enabled, true}], + rebar_test_utils:run_and_check(Config, + RebarConfig, + ["eunit", "--cover_export_name=test_name"], + {ok, [{app, Name}]}), + + true = filelib:is_file(filename:join([AppDir, "_build", "test", "cover", "test_name.coverdata"])). + basic_extra_src_dirs(Config) -> AppDir = ?config(apps, Config), @@ -257,3 +277,46 @@ coverdata_is_reset_on_write(Config) -> Res = lists:map(fun(M) -> cover:analyse(M) end, cover:modules()), Ok = lists:foldl(fun({ok, R}, Acc) -> R ++ Acc end, [], Res), [] = lists:filter(fun({_, {0,_}}) -> false; (_) -> true end, Ok). + +flag_min_coverage(Config) -> + AppDir = ?config(apps, Config), + + Name = rebar_test_utils:create_random_name("min_cover_"), + Vsn = rebar_test_utils:create_random_vsn(), + rebar_test_utils:create_eunit_app(AppDir, Name, Vsn, [kernel, stdlib]), + + RebarConfig = [{erl_opts, [{d, some_define}]}], + ?assertMatch({ok, _}, + rebar_test_utils:run_and_check( + Config, RebarConfig, + ["do", "eunit", "--cover", ",", "cover", "--min_coverage=5"], + return)), + + ?assertMatch({error,{rebar_prv_cover,{min_coverage_failed,{65,_}}}}, + rebar_test_utils:run_and_check( + Config, RebarConfig, + ["do", "eunit", "--cover", ",", "cover", "--min_coverage=65"], + return)), + ok. + +config_min_coverage(Config) -> + AppDir = ?config(apps, Config), + + Name = rebar_test_utils:create_random_name("cover_"), + Vsn = rebar_test_utils:create_random_vsn(), + rebar_test_utils:create_eunit_app(AppDir, Name, Vsn, [kernel, stdlib]), + + RebarConfig1 = [{erl_opts, [{d, some_define}]}, {cover_opts, [{min_coverage,5}]}], + ?assertMatch({ok, _}, + rebar_test_utils:run_and_check( + Config, RebarConfig1, + ["do", "eunit", "--cover", ",", "cover"], + return)), + + RebarConfig2 = [{erl_opts, [{d, some_define}]}, {cover_opts, [{min_coverage,65}]}], + ?assertMatch({error,{rebar_prv_cover,{min_coverage_failed,{65,_}}}}, + rebar_test_utils:run_and_check( + Config, RebarConfig2, + ["do", "eunit", "--cover", ",", "cover"], + return)), + ok. diff --git a/test/rebar_ct_SUITE.erl b/test/rebar_ct_SUITE.erl index d7094ec..15dc63e 100644 --- a/test/rebar_ct_SUITE.erl +++ b/test/rebar_ct_SUITE.erl @@ -49,6 +49,7 @@ cfg_cover_spec/1, cfg_atom_suites/1, cover_compiled/1, + cover_export_name/1, misspecified_ct_opts/1, misspecified_ct_compile_opts/1, misspecified_ct_first_files/1, @@ -121,7 +122,8 @@ groups() -> [{basic_app, [], [basic_app_default_dirs, cmd_create_priv_dir, cmd_include_dir, cmd_sys_config]}, - {cover, [], [cover_compiled]}]. + {cover, [], [cover_compiled, + cover_export_name]}]. init_per_group(basic_app, Config) -> C = rebar_test_utils:init_rebar_state(Config, "ct_"), @@ -1251,6 +1253,29 @@ cover_compiled(Config) -> Mod = list_to_atom(Name), {file, _} = cover:is_compiled(Mod). +cover_export_name(Config) -> + State = ?config(result, Config), + + Providers = rebar_state:providers(State), + Namespace = rebar_state:namespace(State), + CommandProvider = providers:get_provider(ct, Providers, Namespace), + GetOptSpec = providers:opts(CommandProvider), + {ok, GetOptResult} = getopt:parse(GetOptSpec, ["--cover", "--cover_export_name=export_name"]), + + NewState = rebar_state:command_parsed_args(State, GetOptResult), + + Tests = rebar_prv_common_test:prepare_tests(NewState), + {ok, _} = rebar_prv_common_test:compile(NewState, Tests), + rebar_prv_common_test:maybe_write_coverdata(NewState), + + Name = ?config(name, Config), + Mod = list_to_atom(Name), + {file, _} = cover:is_compiled(Mod), + + Dir = rebar_dir:profile_dir(rebar_state:opts(NewState), [default, test]), + ct:pal("DIR ~s", [Dir]), + true = filelib:is_file(filename:join([Dir, "cover", "export_name.coverdata"])). + misspecified_ct_opts(Config) -> C = rebar_test_utils:init_rebar_state(Config, "ct_cfg_atom_suites_"), |