Skip to content

Commit 09fcb39

Browse files
authored
Make mecked modules debuggable by EDB (#254)
1 parent 05f0c70 commit 09fcb39

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

src/meck_proc.erl

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,17 @@
7171
reply_to :: {Caller::pid(), Tag::any()},
7272
expire_at :: erlang:timestamp()}).
7373

74+
%%%============================================================================
75+
%%% Macros
76+
%%%============================================================================
77+
-define(DEBUGGER, edb_server).
78+
7479
%%%============================================================================
7580
%%% Types
7681
%%%============================================================================
7782

7883
-type tracker() :: #tracker{}.
84+
-type action() :: backup | restore.
7985

8086
%%%============================================================================
8187
%%% API
@@ -230,6 +236,7 @@ init([Mod, Options]) ->
230236
History = if NoHistory -> undefined; true -> [] end,
231237
CanExpect = resolve_can_expect(Mod, Exports, Options),
232238
Expects = init_expects(Exports, Options),
239+
try_handle_original_breakpoints(Mod, ?DEBUGGER, backup),
233240
process_flag(trap_exit, true),
234241
try
235242
Forms = meck_code_gen:to_forms(Mod, Expects),
@@ -352,6 +359,7 @@ terminate(_Reason, #state{mod = Mod, original = OriginalState,
352359
was_sticky = WasSticky, restore = Restore}) ->
353360
BackupCover = export_original_cover(Mod, OriginalState),
354361
cleanup(Mod),
362+
try_handle_original_breakpoints(Mod, ?DEBUGGER, restore),
355363
restore_original(Mod, OriginalState, WasSticky, BackupCover),
356364
case Restore andalso false =:= code:is_loaded(Mod) of
357365
true ->
@@ -634,6 +642,23 @@ restore_original(Mod, {{File, OriginalCover, Options}, _Bin}, WasSticky, BackupC
634642
ok = file:delete(OriginalCover),
635643
ok.
636644

645+
-spec try_handle_original_breakpoints(module(), Debugger :: atom(), Action :: action()) -> ok.
646+
try_handle_original_breakpoints(Mod, Debugger, Action) ->
647+
try code:is_loaded(Debugger) of
648+
false -> ok;
649+
_ -> handle_original_breakpoints(Mod, Debugger, Action)
650+
catch
651+
_:_ -> ok
652+
end.
653+
654+
-spec handle_original_breakpoints(module(), Debugger :: atom(), Action :: action()) -> ok.
655+
handle_original_breakpoints(Mod, Debugger, backup) ->
656+
Debugger:add_module_substitute(Mod, meck_util:original_name(Mod), [
657+
{meck_code_gen, eval, 5}
658+
]);
659+
handle_original_breakpoints(Mod, Debugger, restore) ->
660+
Debugger:remove_module_substitute(meck_util:original_name(Mod)).
661+
637662
%% @doc Export the cover data for `<name>_meck_original' and modify
638663
%% the data so it can be imported under `<name>'.
639664
export_original_cover(Mod, {_, Bin}) when is_binary(Bin) ->

0 commit comments

Comments
 (0)