-
Notifications
You must be signed in to change notification settings - Fork 25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Extend stub_command to functions #92
base: master
Are you sure you want to change the base?
Changes from all commits
98c14c3
7a0d254
ce77361
8ce6275
b831754
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,11 +36,30 @@ end_describe() { | |
# any identifier as a function name. | ||
|
||
stub_command() { | ||
local func_decl="$(get_function_declaration "$1")" | ||
[ -n "${func_decl}" ] && eval "_shpec____${func_decl}" | ||
|
||
_shpec_stub_body="${2:-:}" | ||
eval "$1() { ${_shpec_stub_body}; }" | ||
} | ||
|
||
unstub_command() { unset -f "$1"; } | ||
unstub_command() { | ||
unset -f "$1"; | ||
local func_decl="$(get_function_declaration _shpec____"$1")" | ||
[ -n "${func_decl}" ] && { | ||
local reverted="$(echo "${func_decl}" | sed 's/_shpec____//')" | ||
eval "${reverted}" | ||
} | ||
} | ||
|
||
get_function_declaration() | ||
{ | ||
if [ "${SHELL}" = "dash" ]; then | ||
type "$1" | tail -n +2 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I might be misunderstanding this: I thought that everything except for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Given the discussion in #89, dash's own docs, and our previous conversation, I'm fine keeping use of the (non-POSIX) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's what I explained in my last comment #92 (comment), Unfortunately there's currently no way I see to retrieve the body of a function under Dash 😞 |
||
else | ||
typeset -f "$1" | ||
fi | ||
} | ||
|
||
it() { | ||
: $((_shpec_indent += 1)) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -86,6 +86,15 @@ line' | |
assert equal "$(curl)" "stubbed body" | ||
unstub_command "curl" | ||
end | ||
|
||
it "prevents loosing stubbed function" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. s/loosing/losing please! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It will be done if we can find a solution for the problem mentioned here #92 (comment) |
||
local expected="assert double" | ||
stub_command "assert" "echo '${expected}'" | ||
local result="$(assert)" | ||
unstub_command "assert" | ||
|
||
assert equal "${expected}" "${result}" | ||
end | ||
end | ||
|
||
describe "testing files" | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we ever unset this internal definition?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Local vars do not need to be unset as they are local but KSH does not support the
local
keyword, I corrected that in another pending branch...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not referring to
func_decl
, I'm referring to the function that iseval
d here. After callingstub_command foo; unstub_command foo
there will be still be a_shpec___foo
function in the namespace, correct?