浏览代码

deduplicate default test set generated by `rebar3 eunit`

this ONLY attempts to deduplicate test sets that are generated by
rebar in the absence of any user specified tests
pull/1024/head
alisdair sullivan 9 年前
父节点
当前提交
a8dc8ce6e6
共有 2 个文件被更改,包括 48 次插入15 次删除
  1. +39
    -10
      src/rebar_prv_eunit.erl
  2. +9
    -5
      test/rebar_eunit_SUITE.erl

+ 39
- 10
src/rebar_prv_eunit.erl 查看文件

@ -153,22 +153,51 @@ select_tests(_State, _ProjectApps, [], Tests) -> {ok, Tests};
select_tests(_State, _ProjectApps, Tests, _) -> {ok, Tests}.
default_tests(State, Apps) ->
Tests = set_apps(Apps, []),
BareTest = filename:join([rebar_state:dir(State), "test"]),
F = fun(App) -> rebar_app_info:dir(App) == rebar_state:dir(State) end,
case filelib:is_dir(BareTest) andalso not lists:any(F, Apps) of
%% `test` dir at root of project is already scheduled to be
%% included or `test` does not exist
false -> lists:reverse(Tests);
%% need to add `test` dir at root to dirs to be included
true -> lists:reverse([{dir, BareTest}|Tests])
end.
%% use `{application, App}` for each app in project
AppTests = set_apps(Apps),
%% additional test modules in `test` dir of each app
ModTests = set_modules(Apps, State),
AppTests ++ ModTests.
set_apps(Apps) -> set_apps(Apps, []).
set_apps([], Acc) -> Acc;
set_apps([App|Rest], Acc) ->
AppName = list_to_atom(binary_to_list(rebar_app_info:name(App))),
set_apps(Rest, [{application, AppName}|Acc]).
set_modules(Apps, State) -> set_modules(Apps, State, {[], []}).
set_modules([], State, {AppAcc, TestAcc}) ->
TestSrc = gather_src([filename:join([rebar_state:dir(State), "test"])]),
dedupe_tests({AppAcc, TestAcc ++ TestSrc});
set_modules([App|Rest], State, {AppAcc, TestAcc}) ->
F = fun(Dir) -> filename:join([rebar_app_info:dir(App), Dir]) end,
AppDirs = lists:map(F, rebar_dir:src_dirs(rebar_app_info:opts(App), ["src"])),
AppSrc = gather_src(AppDirs),
TestDirs = [filename:join([rebar_app_info:dir(App), "test"])],
TestSrc = gather_src(TestDirs),
set_modules(Rest, State, {AppSrc ++ AppAcc, TestSrc ++ TestAcc}).
gather_src(Dirs) -> gather_src(Dirs, []).
gather_src([], Srcs) -> Srcs;
gather_src([Dir|Rest], Srcs) ->
gather_src(Rest, Srcs ++ rebar_utils:find_files(Dir, "^[^._].*\\.erl\$", true)).
dedupe_tests({AppMods, TestMods}) ->
%% for each modules in TestMods create a test if there is not a module
%% in AppMods that will trigger it
F = fun(Mod) ->
M = filename:basename(Mod, ".erl"),
MatchesTest = fun(Dir) -> filename:basename(Dir, ".erl") ++ "_tests" == M end,
case lists:any(MatchesTest, AppMods) of
false -> {true, {module, list_to_atom(M)}};
true -> false
end
end,
lists:usort(rebar_utils:filtermap(F, TestMods)).
inject_eunit_state(State, {ok, Tests}) ->
Apps = rebar_state:project_apps(State),
case inject_eunit_state(State, Apps, []) of

+ 9
- 5
test/rebar_eunit_SUITE.erl 查看文件

@ -156,7 +156,9 @@ basic_app_exports(_Config) ->
basic_app_testset(Config) ->
Result = ?config(result, Config),
{ok, [{application, basic_app}]} = rebar_prv_eunit:prepare_tests(Result).
Set = {ok, [{application, basic_app},
{module, basic_app_tests_helper}]},
Set = rebar_prv_eunit:prepare_tests(Result).
@ -208,12 +210,14 @@ multi_app_exports(_Config) ->
%% check that the correct tests are schedule to run for project
multi_app_testset(Config) ->
AppDir = ?config(apps, Config),
Result = ?config(result, Config),
Set = {ok, [{application, multi_app_bar},
{application, multi_app_baz},
{dir, filename:join([AppDir, "test"])}]},
Set = {ok, [{application, multi_app_baz},
{application, multi_app_bar},
{module, multi_app_bar_tests_helper},
{module, multi_app_baz_tests_helper},
{module, multi_app_tests},
{module, multi_app_tests_helper}]},
Set = rebar_prv_eunit:prepare_tests(Result).

正在加载...
取消
保存