mirror of
https://github.com/jimeh/stub.sh.git
synced 2026-02-19 13:46:40 +00:00
Ensure stub can be used multiple times on the same command
Ensures that the original bash function is preserved even if it is stubbed multiple times before a restore.
This commit is contained in:
10
stub.sh
10
stub.sh
@@ -69,10 +69,12 @@ stub_and_eval() {
|
||||
|
||||
# If stubbing a function, store non-stubbed copy of it required for restore.
|
||||
if [ -n "$(command -v "$cmd")" ]; then
|
||||
if [[ "$(type "$cmd" | head -1)" == *"is a function" ]]; then
|
||||
local source="$(type "$cmd" | tail -n +2)"
|
||||
source="${source/$cmd/non_stubbed_${cmd}}"
|
||||
eval "$source"
|
||||
if [ -z "$(command -v "non_stubbed_${cmd}")" ]; then
|
||||
if [[ "$(type "$cmd" | head -1)" == *"is a function" ]]; then
|
||||
local source="$(type "$cmd" | tail -n +2)"
|
||||
source="${source/$cmd/non_stubbed_${cmd}}"
|
||||
eval "$source"
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
|
||||
|
||||
@@ -43,5 +43,17 @@ restore "my-name-is"
|
||||
assert "my-name-is Edward Elric" "My name is Edward Elric."
|
||||
|
||||
|
||||
# Stubbing the same function multiple times and then restoring it.
|
||||
my-name-is() { echo "My name is $@."; }
|
||||
stub "my-name-is"
|
||||
assert "my-name-is Edward Elric" ""
|
||||
stub "my-name-is" stdout
|
||||
assert "my-name-is Edward Elric" "my-name-is stub: Edward Elric"
|
||||
|
||||
restore "my-name-is"
|
||||
assert "my-name-is Edward Elric" "My name is Edward Elric."
|
||||
|
||||
|
||||
|
||||
# End of tests.
|
||||
assert_end "restore()"
|
||||
|
||||
Reference in New Issue
Block a user