Skip to content
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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion bin/shpec
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Copy link
Owner

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?

Copy link
Author

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...

Copy link
Owner

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 is evald here. After calling stub_command foo; unstub_command foo there will be still be a _shpec___foo function in the namespace, correct?


_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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might be misunderstanding this: I thought that everything except for dash can use type; this seems to indicate the opposite.

Copy link
Owner

Choose a reason for hiding this comment

The 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) type and typeset commands. If this becomes a problem, we can revisit (or move this feature to a plugin).

Copy link
Author

Choose a reason for hiding this comment

The 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))
Expand Down
9 changes: 9 additions & 0 deletions shpec/shpec_shpec.sh
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,15 @@ line'
assert equal "$(curl)" "stubbed body"
unstub_command "curl"
end

it "prevents loosing stubbed function"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/loosing/losing please!

Copy link
Author

Choose a reason for hiding this comment

The 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"
Expand Down