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 introspection of which modules are currently mocked #210

Merged
merged 1 commit into from
May 13, 2020
Merged
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
47 changes: 27 additions & 20 deletions src/meck.erl
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
-export([wait/4]).
-export([wait/5]).
-export([wait/6]).
-export([mocked/0]).

%% Syntactic sugar
-export([loop/1]).
Expand Down Expand Up @@ -460,7 +461,15 @@ history(Mod, OptCallerPid)
-spec unload() -> Unloaded when
Unloaded :: [Mod],
Mod :: atom().
unload() -> lists:foldl(fun unload_if_mocked/2, [], registered()).
unload() ->
foldl_mocks(fun(Mod, Acc) ->
try
unload(Mod),
[Mod | Acc]
catch error:{not_mocked, Mod} ->
Acc
end
end, []).

%% @doc Unload a mocked module or a list of mocked modules.
%%
Expand Down Expand Up @@ -723,6 +732,11 @@ capture(Occur, Mod, Func, OptArgsSpec, ArgNum, OptCallerPid) ->
capture(Occur, Mod, Func, OptArgsSpec, ArgNum) ->
meck_history:capture(Occur, '_', Mod, Func, OptArgsSpec, ArgNum).

%% @doc Returns the currently mocked modules.
-spec mocked() -> list(atom()).
mocked() ->
foldl_mocks(fun(M, Acc) -> [M | Acc] end, []).

%%%============================================================================
%%% Internal functions
%%%============================================================================
Expand All @@ -732,25 +746,18 @@ wait_for_exit(Mod) ->
MonitorRef = erlang:monitor(process, meck_util:proc_name(Mod)),
receive {'DOWN', MonitorRef, _Type, _Object, _Info} -> ok end.

-spec unload_if_mocked(Mod::atom() | string(), Unloaded::[atom()]) ->
NewUnloaded::[atom()].
unload_if_mocked(Mod, Unloaded) when is_atom(Mod) ->
unload_if_mocked(atom_to_list(Mod), Unloaded);
unload_if_mocked(ModName, Unloaded) when length(ModName) > 5 ->
case lists:split(length(ModName) - 5, ModName) of
{Name, "_meck"} ->
Mocked = erlang:list_to_existing_atom(Name),
try
unload(Mocked)
catch error:{not_mocked, Mocked} ->
ok
end,
[Mocked | Unloaded];
_Else ->
Unloaded
end;
unload_if_mocked(_P, Unloaded) ->
Unloaded.
-spec foldl_mocks(Fun, AccIn) -> AccOut when
Fun :: fun((Elem :: module(), AccIn) -> AccOut),
AccIn :: term(),
AccOut :: term().
foldl_mocks(Fun, Acc0) when is_function(Fun, 2) ->
Copy link

Choose a reason for hiding this comment

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

I would say l in foldl is not needed because the order is not defined :) See, for example, maps:fold / sets:fold

Copy link
Owner

Choose a reason for hiding this comment

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

Good point. I'll remove it on master.

lists:foldl(fun(Mod, Acc) ->
ModName = atom_to_list(Mod),
case lists:split(max(length(ModName) - 5, 0), ModName) of
{Name, "_meck"} -> Fun(erlang:list_to_existing_atom(Name), Acc);
_Else -> Acc
end
end, Acc0, erlang:registered()).

-spec check_expect_result(ok | {error, Reason::any()}) -> ok.
check_expect_result(ok) -> ok;
Expand Down
21 changes: 21 additions & 0 deletions test/meck_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1547,6 +1547,27 @@ wait_purge_expired_tracker_test() ->
%% Clean
meck:unload().

mocked_test() ->
%% At start, no modules should be mocked:
[] = meck:mocked(),

%% Mocking a new module adds it to the list of mocked modules:
meck:new(mocked_test, [non_strict]),
[mocked_test] = meck:mocked(),

%% Mocking with implicit `meck:new` also adds to the list of mocked modules:
meck:expect(meck_test_module, c, 2, ok),
[meck_test_module, mocked_test] = lists:sort(meck:mocked()),

meck:new([foo, bar], [non_strict]),
[bar, foo, meck_test_module, mocked_test] = lists:sort(meck:mocked()),

ok = meck:unload([foo, meck_test_module]),
[bar, mocked_test] = lists:sort(meck:mocked()),

meck:unload(),
%% After unload all, no modules should be mocked again
[] = meck:mocked().

meck_passthrough_test_() ->
{foreach, fun setup_passthrough/0, fun teardown/1,
Expand Down