Skip to content
Permalink
Browse files
Raise error for passthrough without abstract code
  • Loading branch information
eproxus committed May 3, 2018
1 parent b1a9cad commit 6a1dbc98973337c2ed50aa8d315f74b5829e1423
Showing 3 changed files with 27 additions and 5 deletions.
@@ -193,6 +193,15 @@ new(Mod) when is_list(Mod) -> lists:foreach(fun new/1, Mod), ok.
%% function clause list, meaning that pattern matching will be performed
%% in the order the expectations were added.</dd>
%% </dl>
%%
%% Possible exceptions:
%% <dl>
%% <dt>`error:{abstract_code_not_found, Mod}'</dt>
%% <dd>The option `passthrough' was used but the original module has no
%% abstract code which can be called. Make sure the module is compiled
%% with the compiler option `debug_info'.</dd>
%% </dl>

-spec new(Mods, Options) -> ok when
Mods :: Mod | [Mod],
Mod :: atom(),
@@ -209,7 +209,7 @@ init([Mod, Options]) ->
MergeExpects = proplists:get_bool(merge_expects, Options),
EnableOnLoad = proplists:get_bool(enable_on_load, Options),
Passthrough = proplists:get_bool(passthrough, Options),
Original = backup_original(Mod, NoPassCover, EnableOnLoad),
Original = backup_original(Mod, Passthrough, NoPassCover, EnableOnLoad),
NoHistory = proplists:get_bool(no_history, Options),
History = if NoHistory -> undefined; true -> [] end,
CanExpect = resolve_can_expect(Mod, Exports, Options),
@@ -363,13 +363,13 @@ expect_type(Mod, Func, Ari) ->
false -> normal
end.

-spec backup_original(Mod::atom(), NoPassCover::boolean(), EnableOnLoad::boolean()) ->
-spec backup_original(Mod::atom(), Passthrough::boolean(), NoPassCover::boolean(), EnableOnLoad::boolean()) ->
{Cover:: false |
{File::string(), Data::string(), CompiledOptions::[any()]},
Binary:: no_binary |
no_passthrough_cover |
binary()}.
backup_original(Mod, NoPassCover, EnableOnLoad) ->
backup_original(Mod, Passthrough, NoPassCover, EnableOnLoad) ->
Cover = get_cover_state(Mod),
try
Forms0 = meck_code:abstract_code(meck_code:beam_file(Mod)),
@@ -408,8 +408,11 @@ backup_original(Mod, NoPassCover, EnableOnLoad) ->
catch
throw:{object_code_not_found, _Module} ->
{Cover, no_binary}; % TODO: What to do here?
throw:no_abstract_code ->
{Cover, no_binary} % TODO: What to do here?
throw:no_abstract_code ->
case Passthrough of
true -> exit({abstract_code_not_found, Mod});
false -> {Cover, no_binary}
end
end.

-spec get_cover_state(Mod::atom()) ->
@@ -896,6 +896,16 @@ original_has_no_object_code_test() ->
ok = file:delete("meck_on_disk.beam"),
ok = meck:unload(meck_on_disk).

passthrough_with_no_object_code_test() ->
{ok, Mod, Beam} = compile:forms([{attribute, 1, module, no_abstract_code}]),
ok = file:write_file("no_abstract_code.beam", Beam),
{module, Mod} = code:load_binary(Mod, "no_abstract_code.beam", Beam),
?assertError(
{abstract_code_not_found, Mod},
meck:new(no_abstract_code, [passthrough, no_link])
),
ok = file:delete("no_abstract_code.beam").

passthrough_nonexisting_module_test() ->
ok = meck:new(mymod, [passthrough, non_strict]),
ok = meck:expect(mymod, test, fun() -> ok end),

0 comments on commit 6a1dbc9

Please sign in to comment.