summaryrefslogtreecommitdiff
path: root/src/rebar_prv_eunit.erl
diff options
context:
space:
mode:
Diffstat (limited to 'src/rebar_prv_eunit.erl')
-rw-r--r--src/rebar_prv_eunit.erl61
1 files changed, 45 insertions, 16 deletions
diff --git a/src/rebar_prv_eunit.erl b/src/rebar_prv_eunit.erl
index a1a4408..942fd10 100644
--- a/src/rebar_prv_eunit.erl
+++ b/src/rebar_prv_eunit.erl
@@ -51,19 +51,20 @@ do(State) ->
do(State, Tests) ->
?INFO("Performing EUnit tests...", []),
+ setup_name(State),
rebar_utils:update_code(rebar_state:code_paths(State, all_deps), [soft_purge]),
%% Run eunit provider prehooks
Providers = rebar_state:providers(State),
Cwd = rebar_dir:get_cwd(),
- rebar_hooks:run_all_hooks(Cwd, pre, ?PROVIDER, Providers, State),
+ rebar_hooks:run_project_and_app_hooks(Cwd, pre, ?PROVIDER, Providers, State),
case validate_tests(State, Tests) of
{ok, T} ->
case run_tests(State, T) of
{ok, State1} ->
%% Run eunit provider posthooks
- rebar_hooks:run_all_hooks(Cwd, post, ?PROVIDER, Providers, State1),
+ rebar_hooks:run_project_and_app_hooks(Cwd, post, ?PROVIDER, Providers, State1),
rebar_utils:cleanup_code_path(rebar_state:code_paths(State, default)),
{ok, State1};
Error ->
@@ -106,6 +107,10 @@ format_error({error, Error}) ->
%% Internal functions
%% ===================================================================
+setup_name(State) ->
+ {Long, Short, Opts} = rebar_dist_utils:find_options(State),
+ rebar_dist_utils:either(Long, Short, Opts).
+
prepare_tests(State) ->
%% parse and translate command line tests
CmdTests = resolve_tests(State),
@@ -190,7 +195,7 @@ dedupe_tests({AppMods, TestMods}) ->
%% in AppMods that will trigger it
F = fun(Mod) ->
M = filename:basename(Mod, ".erl"),
- MatchesTest = fun(Dir) -> filename:basename(Dir, ".erl") ++ "_tests" == M end,
+ 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
@@ -244,9 +249,27 @@ first_files(Opts) ->
EUnitFirstFiles = opts(Opts, eunit_first_files, []),
case append(EUnitFirstFiles, FirstFiles) of
{error, _} = Error -> Error;
- NewFirstFiles -> rebar_opts:set(Opts, erl_first_files, NewFirstFiles)
+ NewFirstFiles -> eunit_macro(rebar_opts:set(Opts, erl_first_files, NewFirstFiles))
end.
+eunit_macro(Opts) ->
+ ErlOpts = opts(Opts, erl_opts, []),
+ NewOpts = safe_define_eunit_macro(ErlOpts),
+ rebar_opts:set(Opts, erl_opts, NewOpts).
+
+safe_define_eunit_macro(Opts) ->
+ %% defining a compile macro twice results in an exception so
+ %% make sure 'EUNIT' is only defined once
+ case test_defined(Opts) of
+ true -> Opts;
+ false -> [{d, 'EUNIT'}|Opts]
+ end.
+
+test_defined([{d, 'EUNIT'}|_]) -> true;
+test_defined([{d, 'EUNIT', true}|_]) -> true;
+test_defined([_|Rest]) -> test_defined(Rest);
+test_defined([]) -> false.
+
append({error, _} = Error, _) -> Error;
append(_, {error, _} = Error) -> Error;
append(A, B) -> A ++ B.
@@ -457,15 +480,21 @@ eunit_opts(_State) ->
[{app, undefined, "app", string, help(app)},
{application, undefined, "application", string, help(app)},
{cover, $c, "cover", boolean, help(cover)},
- {dir, undefined, "dir", string, help(dir)},
- {file, undefined, "file", string, help(file)},
- {module, undefined, "module", string, help(module)},
- {suite, undefined, "suite", string, help(module)},
- {verbose, $v, "verbose", boolean, help(verbose)}].
-
-help(app) -> "Comma separated list of application test suites to run. Equivalent to `[{application, App}]`.";
-help(cover) -> "Generate cover data. Defaults to false.";
-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}]`.";
-help(verbose) -> "Verbose output. Defaults to false.".
+ {dir, $d, "dir", string, help(dir)},
+ {file, $f, "file", string, help(file)},
+ {module, $m, "module", string, help(module)},
+ {suite, $s, "suite", string, help(module)},
+ {verbose, $v, "verbose", boolean, help(verbose)},
+ {name, undefined, "name", atom, help(name)},
+ {sname, undefined, "sname", atom, help(sname)},
+ {setcookie, undefined, "setcookie", atom, help(setcookie)}].
+
+help(app) -> "Comma separated list of application test suites to run. Equivalent to `[{application, App}]`.";
+help(cover) -> "Generate cover data. Defaults to false.";
+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}]`.";
+help(verbose) -> "Verbose output. Defaults to false.";
+help(name) -> "Gives a long name to the node";
+help(sname) -> "Gives a short name to the node";
+help(setcookie) -> "Sets the cookie if the node is distributed".