This is an automated email from the ASF dual-hosted git repository.

vatamane pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/couchdb-meck.git

commit b9a3e6ed78c242a8203244a56f2a522b614e1610
Author: Per Gustafsson <[email protected]>
AuthorDate: Tue Aug 24 13:51:47 2021 +0100

    Fix calling mocked modules from expectation fun.
    
    Before this change calling a function in a mocked module
     from inside an expectation fun, and then calling
     meck:passthrough/1 raises an exception. This happens
      because the current function state is invalidated by
       the mocked function.
---
 CHANGELOG.md          | 1 +
 src/meck_code_gen.erl | 3 ++-
 test/meck_tests.erl   | 6 ++++++
 3 files changed, 9 insertions(+), 1 deletion(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 1fd384c..d5034f2 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -14,6 +14,7 @@ The format is based on [Keep a Changelog], and this project 
adheres to
 ### Fixed
 
 - Fix misleading not_mocked errors when when unloading a mock 
[\#231](https://github.com/eproxus/meck/pull/231)
+- Fix calling mocked modules from expectations fun 
[\#232](https://github.com/eproxus/meck/pull/232)
 
 ### Removed
 
diff --git a/src/meck_code_gen.erl b/src/meck_code_gen.erl
index 6f7257a..e06ed54 100644
--- a/src/meck_code_gen.erl
+++ b/src/meck_code_gen.erl
@@ -173,6 +173,7 @@ exec(Pid, Mod, Func, Args) ->
 -spec eval(Pid::pid(), Mod::atom(), Func::atom(), Args::[any()],
            ResultSpec::any()) -> Result::any() | no_return().
 eval(Pid, Mod, Func, Args, ResultSpec) ->
+    PreviousCall = get(?CURRENT_CALL),
     put(?CURRENT_CALL, {Mod, Func}),
     try
         Result = meck_ret_spec:eval_result(Mod, Func, Args, ResultSpec),
@@ -183,7 +184,7 @@ eval(Pid, Mod, Func, Args, ResultSpec) ->
             handle_exception(Pid, Mod, Func, Args,
                              Class, Reason, ?_get_stacktrace_(StackToken))
     after
-        erase(?CURRENT_CALL)
+        put(?CURRENT_CALL, PreviousCall)
     end.
 
 -spec handle_exception(CallerPid::pid(), Mod::atom(), Func::atom(),
diff --git a/test/meck_tests.erl b/test/meck_tests.erl
index 946ba7b..ca9d53d 100644
--- a/test/meck_tests.erl
+++ b/test/meck_tests.erl
@@ -1376,6 +1376,12 @@ can_mock_sticky_modules_test() ->
     ?assert(code:is_sticky(meck_test_module)),
     code:unstick_mod(meck_test_module).
 
+meck_reentrant_test() ->
+    meck:new(string, [unstick, passthrough]),
+    meck:expect(string, strip, 
+        fun(String) -> meck:passthrough([string:reverse(String)]) end),
+    ?assertEqual(string:strip("  ABC  "), "CBA"),
+    meck:unload(string).
 
 sticky_directory_test_() ->
     {foreach, fun sticky_setup/0, fun sticky_teardown/1,

Reply via email to