Skip to content

Commit

Permalink
Merge bb6ae69 into cb07d4d
Browse files Browse the repository at this point in the history
  • Loading branch information
ko1nksm committed Feb 16, 2020
2 parents cb07d4d + bb6ae69 commit 5d55f35
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
13 changes: 12 additions & 1 deletion lib/core/modifiers/result.sh
Expand Up @@ -4,7 +4,13 @@ shellspec_syntax 'shellspec_modifier_result'

shellspec_modifier_result() {
if [ "${SHELLSPEC_SUBJECT+x}" ]; then
if ! SHELLSPEC_SUBJECT=$($SHELLSPEC_SUBJECT 2>&1); then
if ! shellspec_is_function "$SHELLSPEC_SUBJECT"; then
shellspec_output SYNTAX_ERROR "'$SHELLSPEC_SUBJECT' is not function name"
return 0
fi

shellspec_off UNHANDLED_STDOUT UNHANDLED_STDERR UNHANDLED_STATUS
if ! SHELLSPEC_SUBJECT=$(shellspec_modifier_result_invoke 2>&1); then
unset SHELLSPEC_SUBJECT ||:
fi
else
Expand All @@ -13,3 +19,8 @@ shellspec_modifier_result() {

eval shellspec_syntax_dispatch modifier ${1+'"$@"'}
}

shellspec_modifier_result_invoke() {
set -- "$SHELLSPEC_SUBJECT"
"$@" "${SHELLSPEC_STDOUT:-}" "${SHELLSPEC_STDERR:-}" "${SHELLSPEC_STATUS:-}"
}
10 changes: 7 additions & 3 deletions lib/core/utils.sh
Expand Up @@ -9,14 +9,18 @@ shellspec_get_nth() {
shellspec_is() {
case $1 in
number) case ${2:-} in ( '' | *[!0-9]* ) return 1; esac ;;
funcname)
case ${2:-} in ([a-zA-Z_]*) ;; (*) return 1; esac
case ${2:-} in (*[!a-zA-Z0-9_]*) return 1; esac ;;
funcname) shellspec_is_function "${2:-}" || return 1 ;;
*) shellspec_error "shellspec_is: invalid type name '$1'"
esac
return 0
}

shellspec_is_function() {
case ${1:-} in ([a-zA-Z_]*) ;; (*) return 1; esac
case ${1:-} in (*[!a-zA-Z0-9_]*) return 1; esac
return 0
}

shellspec_capture() {
SHELLSPEC_EVAL="
if $1=\"\$($2 && echo _)\"; then $1=\${$1%_}; else unset $1 ||:; fi
Expand Down
20 changes: 20 additions & 0 deletions spec/core/modifiers/result_spec.sh
Expand Up @@ -12,6 +12,20 @@ Describe "core/modifiers/result.sh"
The result of 'bar()' should equal ok # also capture stderr
End

Describe 'example with stdout, stderr and status'
foo() { echo stdout; echo stderr >&2; return 123; }
get_stdout() { echo "$1"; }
get_stderr() { echo "$2"; }
get_status() { echo "$3"; }

It "retrives result of evaluation"
When call foo
The result of 'get_stdout()' should equal "stdout"
The result of 'get_stderr()' should equal "stderr"
The result of 'get_status()' should equal 123
End
End

It 'gets stdout and stderr when subject is function that returns success'
subject() { %- "success_with_output"; }
success_with_output() { echo stdout; echo stderr >&2; true; }
Expand All @@ -34,6 +48,12 @@ Describe "core/modifiers/result.sh"
The status should be failure
End

It 'outputs error if invalid function name specified'
subject() { %- "foo -a"; }
When run shellspec_modifier_result
The stderr should equal SYNTAX_ERROR
End

It 'outputs error if next modifier is missing'
subject() { %- "success_with_stdout"; }
success_with_stdout() { echo ok; true; }
Expand Down

0 comments on commit 5d55f35

Please sign in to comment.