Permalink
Browse files

redirect functions registered as mocks

Calling 'APRON_register rm' will define a function called 'mock_rm' with
the same function body and unset the original 'rm'.
  • Loading branch information...
1 parent 86a2b1a commit 4caba9407b1cf25b9dcd0ac753fc3dd70e0afd90 @falconindy committed Dec 26, 2012
Showing with 18 additions and 10 deletions.
  1. +18 −10 apron
View
@@ -100,11 +100,10 @@ APRON_disable() {
#
# APRON_register
#
-# Registers a mock function. The function should already, but not necessarily
-# already be defined when this function is called. Calling this function again
-# for a mock which is already defined will overwrite the definition of the
-# previously registered mock. Mocks can be individually unregistered with the
-# use of APRON_unregister.
+# Registers a mock function. The function must be defined when this function is
+# called. Calling this function again for a mock which is already defined will
+# overwrite the definition of the previously registered mock. Mocks can be
+# individually unregistered with the use of APRON_unregister.
#
# Args:
# $1: The name of the function to register as a mock
@@ -116,11 +115,15 @@ APRON_register() {
printf 'APRON: registering mock: %s\n' "$1"
fi
- if ! APRON_defined_mocks["$1"]=$(declare -f "$1"); then
+ if ! APRON_defined_mocks["mock_$1"]=mock_$(declare -f "$1"); then
printf '==> FATAL: failed to register mock: %s\n' "$1"
exit 1
fi
- export -f "$1"
+
+ # swap out the defined function for the mock
+ eval "${APRON_defined_mocks["mock_$1"]}"
+ export -f "mock_$1"
+ unset -f "$1"
}
#
@@ -140,9 +143,9 @@ APRON_unregister() {
printf 'APRON: unregistering mock: %s\n' "$1"
fi
- if [[ ${APRON_defined_mocks["$1"]} ]]; then
- unset "$1"
- unset APRON_defined_mocks["$1"]
+ if [[ ${APRON_defined_mocks["mock_$1"]} ]]; then
+ unset -f "mock_$1"
+ unset APRON_defined_mocks["mock_$1"]
fi
}
@@ -180,6 +183,11 @@ _APRON_pop_PATH() {
_APRON_register_cnf_handler() {
command_not_found_handle() {
+ if [[ ${APRON_defined_mocks["mock_$1"]} ]]; then
+ "mock_$@"
+ return
+ fi
+
printf 'APRON: function call was not mocked: %s\n' "$*" >&2
}
}

0 comments on commit 4caba94

Please sign in to comment.