| 1 |
#!/bin/bash |
| 2 |
# |
| 3 |
# While not directly TAP-specific, being able to mock stuff |
| 4 |
# in tests is pretty useful. |
| 5 |
# |
| 6 |
# If you're using bash-tap-bootstrap, then just source this |
| 7 |
# file in your tests from the bash-tap directory found by |
| 8 |
# the bootstrap by including this line after you've sourced |
| 9 |
# bash-tap-bootstrap: |
| 10 |
# |
| 11 |
# . "$BASH_TAP_ROOT/bash-tap-mock" |
| 12 |
# |
| 13 |
# If you're not using bash-tap-bootstrap then copy this file |
| 14 |
# to your test directory and source it with: |
| 15 |
# |
| 16 |
# . $(dirname $0)/bash-tap-mock |
| 17 |
# |
| 18 |
# It's important to note that if you're capturing the arguments |
| 19 |
# passed to your mock function in a variable, and want that |
| 20 |
# variable to be accessible to your tests, you must ensure that |
| 21 |
# the mocked function is executed in the current shell and not |
| 22 |
# a subshell. In particular, this means you cannot use $() or |
| 23 |
# `` to capture output of the function at the same time, as these |
| 24 |
# invoke a subshell - the mock will happen, but any variables you |
| 25 |
# set within your mock will only exist within the subshell. |
| 26 |
# If you wish to capture output at the same time, you need to |
| 27 |
# make use of the start_output_capture and finish_output_capture |
| 28 |
# helper functons in bash-tap, or manually use file-descriptor |
| 29 |
# redirects yourself to achieve the same effect. |
| 30 |
|
| 31 |
bash_tap_mock_version='1.0.2' |
| 32 |
|
| 33 |
if [ "${BASH_SOURCE[0]}" = "$0" ]; then |
| 34 |
# Being run directly, probably by test harness running entire dir. |
| 35 |
echo "1..0 # SKIP bash-tap-mock isn't a test file" |
| 36 |
exit 0 |
| 37 |
fi |
| 38 |
|
| 39 |
function mock_function() { |
| 40 |
local original_name="$1" |
| 41 |
local mock_name="$2" |
| 42 |
local save_original_as="_btm_mocked_${original_name}" |
| 43 |
|
| 44 |
if [ -z $(declare -F "$save_original_as") ]; then |
| 45 |
_btm_copy_function "$original_name" "$save_original_as" |
| 46 |
fi |
| 47 |
_btm_copy_function "$mock_name" "$original_name" |
| 48 |
} |
| 49 |
|
| 50 |
function restore_mocked_function() { |
| 51 |
local original_name="$1" |
| 52 |
local save_original_as="_btm_mocked_${original_name}" |
| 53 |
|
| 54 |
if [ ! -z $(declare -F "$save_original_as") ]; then |
| 55 |
_btm_copy_function "$save_original_as" "$original_name" |
| 56 |
unset -f "$save_original_as" |
| 57 |
else |
| 58 |
_btm_caller_error "Can't find saved original function '$original_name' to restore" |
| 59 |
fi |
| 60 |
} |
| 61 |
|
| 62 |
function mock_command() { |
| 63 |
local command_name="$1" |
| 64 |
local mock_name="$2" |
| 65 |
|
| 66 |
if [ ! -z $(declare -F "$command_name") ]; then |
| 67 |
# It's not actually a command, it's a function, mock that |
| 68 |
mock_function "$command_name" "$mock_name" |
| 69 |
else |
| 70 |
_btm_copy_function "$mock_name" "$command_name" |
| 71 |
fi |
| 72 |
} |
| 73 |
|
| 74 |
function restore_mocked_command() { |
| 75 |
local command_name="$1" |
| 76 |
|
| 77 |
local save_original_as="_btm_mocked_${command_name}" |
| 78 |
if [ ! -z $(declare -F "$save_original_as") ]; then |
| 79 |
# Was actually a function mock not a command mock. |
| 80 |
restore_mocked_function "$command_name" |
| 81 |
else |
| 82 |
unset -f "$command_name" >/dev/null |
| 83 |
fi |
| 84 |
} |
| 85 |
|
| 86 |
# Copied from http://stackoverflow.com/a/1203628/870000 |
| 87 |
function _btm_copy_function() { |
| 88 |
declare -F $1 >/dev/null || _btm_caller_error "Can't find function '$1' to copy" |
| 89 |
eval "$(echo "${2}()"; declare -f ${1} | tail -n +2)" |
| 90 |
} |
| 91 |
|
| 92 |
# Report an error from the POV of the first calling point outside this file |
| 93 |
function _btm_caller_error() { |
| 94 |
local message="$*" |
| 95 |
|
| 96 |
local thisfile="${BASH_SOURCE[0]}" |
| 97 |
local file="$thisfile" |
| 98 |
local frame_num=2 |
| 99 |
until [ "$file" != "$thisfile" ]; do |
| 100 |
frame=$(caller "$frame_num") |
| 101 |
IFS=' ' read line func file <<<"$frame" |
| 102 |
done |
| 103 |
|
| 104 |
echo "Error: $message, on line $line of $file" >&2 |
| 105 |
exit 255 |
| 106 |
} |