|
71 | 71 | reply_to :: {Caller::pid(), Tag::any()}, |
72 | 72 | expire_at :: erlang:timestamp()}). |
73 | 73 |
|
| 74 | +%%%============================================================================ |
| 75 | +%%% Macros |
| 76 | +%%%============================================================================ |
| 77 | +-define(DEBUGGER, edb_server). |
| 78 | + |
74 | 79 | %%%============================================================================ |
75 | 80 | %%% Types |
76 | 81 | %%%============================================================================ |
77 | 82 |
|
78 | 83 | -type tracker() :: #tracker{}. |
| 84 | +-type action() :: backup | restore. |
79 | 85 |
|
80 | 86 | %%%============================================================================ |
81 | 87 | %%% API |
@@ -230,6 +236,7 @@ init([Mod, Options]) -> |
230 | 236 | History = if NoHistory -> undefined; true -> [] end, |
231 | 237 | CanExpect = resolve_can_expect(Mod, Exports, Options), |
232 | 238 | Expects = init_expects(Exports, Options), |
| 239 | + try_handle_original_breakpoints(Mod, ?DEBUGGER, backup), |
233 | 240 | process_flag(trap_exit, true), |
234 | 241 | try |
235 | 242 | Forms = meck_code_gen:to_forms(Mod, Expects), |
@@ -352,6 +359,7 @@ terminate(_Reason, #state{mod = Mod, original = OriginalState, |
352 | 359 | was_sticky = WasSticky, restore = Restore}) -> |
353 | 360 | BackupCover = export_original_cover(Mod, OriginalState), |
354 | 361 | cleanup(Mod), |
| 362 | + try_handle_original_breakpoints(Mod, ?DEBUGGER, restore), |
355 | 363 | restore_original(Mod, OriginalState, WasSticky, BackupCover), |
356 | 364 | case Restore andalso false =:= code:is_loaded(Mod) of |
357 | 365 | true -> |
@@ -634,6 +642,23 @@ restore_original(Mod, {{File, OriginalCover, Options}, _Bin}, WasSticky, BackupC |
634 | 642 | ok = file:delete(OriginalCover), |
635 | 643 | ok. |
636 | 644 |
|
| 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 | + |
637 | 662 | %% @doc Export the cover data for `<name>_meck_original' and modify |
638 | 663 | %% the data so it can be imported under `<name>'. |
639 | 664 | export_original_cover(Mod, {_, Bin}) when is_binary(Bin) -> |
|
0 commit comments