Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for reseting the Eunit environment after each test #385

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/rebar_config.erl
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,14 @@ get_global(Config, Key, Default) ->
error ->
Default;
{ok, Value} ->
Value
case string:to_lower(Value) of
"true" ->
true;
"false" ->
false;
_ ->
Value
end
end.

is_verbose(Config) ->
Expand Down
4 changes: 2 additions & 2 deletions src/rebar_deps.erl
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ preprocess(Config, _) ->
%% Also, if skip_deps=comma,separated,app,list, then only the given
%% dependencies are skipped.
NewConfig = case rebar_config:get_global(Config3, skip_deps, false) of
"true" ->
true ->
lists:foldl(
fun(#dep{dir = Dir}, C) ->
rebar_config:set_skip_dir(C, Dir)
Expand All @@ -106,7 +106,7 @@ preprocess(Config, _) ->
false -> C
end
end, Config3, AvailableDeps);
_ ->
false ->
Config3
end,

Expand Down
40 changes: 30 additions & 10 deletions src/rebar_eunit.erl
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@
%% <li>Reset OTP application environment variables</li>
%% </ul>
%% </li>
%% <li>reset_after_each_eunit::boolean() - default = false.
%% If true, try to "reset" VM state to approximate state prior to
%% running each EUnit test in the contstructed list of tests:
%% <ul>
%% <li>Stop net_kernel if it was started</li>
%% <li>Stop OTP applications not running before EUnit tests were run</li>
%% <li>Kill processes not running before EUnit tests were run</li>
%% <li>Reset OTP application environment variables</li>
%% </ul>
%% </li>
%% </ul>
%% The following Global options are supported:
%% <ul>
Expand Down Expand Up @@ -164,19 +174,21 @@ run_eunit(Config, CodePath, SrcErls) ->
{ok, CoverLog} = cover_init(Config, ModuleBeamFiles),

StatusBefore = status_before_eunit(),
EunitResult = perform_eunit(Config, Tests),

perform_cover(Config, FilteredModules, SrcModules),
cover_close(CoverLog),
DoClean = rebar_config:get_global(Config, reset_after_eunit, true),

case proplists:get_value(reset_after_eunit, get_eunit_opts(Config),
true) of
true ->
reset_after_eunit(StatusBefore);
EunitResult = case rebar_config:get_global(Config, reset_after_each_eunit, false) of
false ->
ok
?DEBUG("running all tests~n", []),
perform_eunit(Config, Tests, StatusBefore, DoClean);
true ->
?DEBUG("running cleanup after each test~n", []),
[perform_eunit(Config, T, StatusBefore, true) || T <- Tests]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please put a newline in the above debug. the debug macro isn't nice enough to do one for you.

end,

perform_cover(Config, FilteredModules, SrcModules),
cover_close(CoverLog),

%% Stop cover to clean the cover_server state. This is important if we want
%% eunit+cover to not slow down when analyzing many Erlang modules.
ok = cover:stop(),
Expand All @@ -192,6 +204,11 @@ run_eunit(Config, CodePath, SrcErls) ->
true = code:set_path(CodePath),
ok.

maybe_cleanup(_StatusBefore, false) ->
ok;
maybe_cleanup(StatusBefore, true) ->
reset_after_eunit(StatusBefore).

ensure_dirs() ->
%% Make sure ?EUNIT_DIR/ and ebin/ directory exists (append dummy module)
ok = filelib:ensure_dir(filename:join(eunit_dir(), "dummy")),
Expand Down Expand Up @@ -391,18 +408,20 @@ pre15b02_eunit_primitive(generator, M, F) ->
%% == run tests ==
%%

perform_eunit(Config, Tests) ->
perform_eunit(Config, Tests, StatusBeforeEunit, DoClean) ->
EunitOpts = get_eunit_opts(Config),

%% Move down into ?EUNIT_DIR while we run tests so any generated files
%% are created there (versus in the source dir)
Cwd = rebar_utils:get_cwd(),
ok = file:set_cwd(?EUNIT_DIR),


?DEBUG("running tests:~w with options:~w~n", [Tests, EunitOpts]),
EunitResult = (catch eunit:test(Tests, EunitOpts)),

%% Return to original working dir
ok = file:set_cwd(Cwd),
maybe_cleanup(StatusBeforeEunit, DoClean),

EunitResult.

Expand All @@ -417,6 +436,7 @@ get_eunit_opts(Config) ->

BaseOpts ++ rebar_config:get_list(Config, eunit_opts, []).


%%
%% == code coverage ==
%%
Expand Down