Browse Source

allow using an alternate regex to locate test modules during eunit runs

{`eunit_test_regex`, Regex}` will use the supplied `Regex` instead of
the default to locate tests in test dirs. note this matches only the
filename, not the path. the regex is applied to all test dirs, recursively

fixes #1331
pull/1335/head
alisdair sullivan 8 years ago
parent
commit
b79e5da236
2 changed files with 37 additions and 8 deletions
  1. +12
    -7
      src/rebar_prv_eunit.erl
  2. +25
    -1
      test/rebar_eunit_SUITE.erl

+ 12
- 7
src/rebar_prv_eunit.erl View File

@ -18,6 +18,8 @@
%% we need to modify app_info state before compile
-define(DEPS, [lock]).
-define(DEFAULT_TEST_REGEX, "^[^._].*\\.erl\$").
%% ===================================================================
%% Public API
%% ===================================================================
@ -174,21 +176,24 @@ set_apps([App|Rest], Acc) ->
set_modules(Apps, State) -> set_modules(Apps, State, {[], []}).
set_modules([], State, {AppAcc, TestAcc}) ->
TestSrc = gather_src([filename:join([rebar_state:dir(State), "test"])]),
Regex = rebar_state:get(State, eunit_test_regex, ?DEFAULT_TEST_REGEX),
BareTestDir = [filename:join([rebar_state:dir(State), "test"])],
TestSrc = gather_src(BareTestDir, Regex),
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),
Regex = rebar_state:get(State, eunit_test_regex, ?DEFAULT_TEST_REGEX),
AppSrc = gather_src(AppDirs, Regex),
TestDirs = [filename:join([rebar_app_info:dir(App), "test"])],
TestSrc = gather_src(TestDirs),
TestSrc = gather_src(TestDirs, Regex),
set_modules(Rest, State, {AppSrc ++ AppAcc, TestSrc ++ TestAcc}).
gather_src(Dirs) -> gather_src(Dirs, []).
gather_src(Dirs, Regex) -> gather_src(Dirs, Regex, []).
gather_src([], Srcs) -> Srcs;
gather_src([Dir|Rest], Srcs) ->
gather_src(Rest, Srcs ++ rebar_utils:find_files(Dir, "^[^._].*\\.erl\$", true)).
gather_src([], _Regex, Srcs) -> Srcs;
gather_src([Dir|Rest], Regex, Srcs) ->
gather_src(Rest, Regex, Srcs ++ rebar_utils:find_files(Dir, Regex, true)).
dedupe_tests({AppMods, TestMods}) ->
%% for each modules in TestMods create a test if there is not a module

+ 25
- 1
test/rebar_eunit_SUITE.erl View File

@ -18,6 +18,7 @@
-export([misspecified_eunit_tests/1]).
-export([misspecified_eunit_compile_opts/1]).
-export([misspecified_eunit_first_files/1]).
-export([alternate_test_regex/1]).
-include_lib("common_test/include/ct.hrl").
-include_lib("eunit/include/eunit.hrl").
@ -27,7 +28,8 @@ all() ->
[{group, basic_app}, {group, multi_app}, {group, cmd_line_args},
misspecified_eunit_tests,
misspecified_eunit_compile_opts,
misspecified_eunit_first_files].
misspecified_eunit_first_files,
alternate_test_regex].
groups() ->
[{basic_app, [sequence], [basic_app_compiles, {group, basic_app_results}]},
@ -579,3 +581,25 @@ misspecified_eunit_first_files(Config) ->
{error, {rebar_prv_eunit, Error}} = rebar_test_utils:run_and_check(State, RebarConfig, ["eunit"], return),
{badconfig, {"Value `~p' of option `~p' must be a list", {some_file, eunit_first_files}}} = Error.
alternate_test_regex(Config) ->
State = rebar_test_utils:init_rebar_state(Config, "alternate_test_regex_"),
AppDir = ?config(apps, State),
PrivDir = ?config(priv_dir, State),
AppDirs = ["src", "include", "test"],
lists:foreach(fun(F) -> ec_file:copy(filename:join([PrivDir, "basic_app", F]),
filename:join([AppDir, F]),
[recursive]) end, AppDirs),
BaseConfig = [{erl_opts, [{d, config_define}]}, {eunit_compile_opts, [{d, eunit_compile_define}]}],
RebarConfig = [{eunit_test_regex, "basic_app_tests.erl"}|BaseConfig],
{ok, S} = rebar_test_utils:run_and_check(State, RebarConfig, ["as", "test", "lock"], return),
Set = {ok, [{application, basic_app},
{module, basic_app_tests}]},
Set = rebar_prv_eunit:prepare_tests(S).

Loading…
Cancel
Save