Skip to content

Commit

Permalink
Merge pull request #73 from shellspec/fix_before_after_all
Browse files Browse the repository at this point in the history
Fixes BeforeAll / AfterAll to share states
  • Loading branch information
ko1nksm committed Jun 6, 2020
2 parents de1a79c + 411ee91 commit a05da89
Show file tree
Hide file tree
Showing 8 changed files with 209 additions and 108 deletions.
85 changes: 42 additions & 43 deletions lib/core/dsl.sh
Expand Up @@ -14,7 +14,6 @@ SHELLSPEC_ENABLED=''
SHELLSPEC_FOCUSED=''
SHELLSPEC_SPECFILE=''
SHELLSPEC_SPEC_NO=''
SHELLSPEC_WORKDIR="$SHELLSPEC_TMPBASE"
SHELLSPEC_GROUP_ID=''
SHELLSPEC_BLOCK_NO=''
SHELLSPEC_EXAMPLE_ID=''
Expand All @@ -28,7 +27,6 @@ SHELLSPEC_SKIP_ID=''
SHELLSPEC_SKIP_REASON=''
SHELLSPEC_PENDING_REASON=''
SHELLSPEC_SHELL_OPTIONS=''
SHELLSPEC_STDIO_FILE_BASE="$SHELLSPEC_WORKDIR"

shellspec_group_id() {
# shellcheck disable=SC2034
Expand Down Expand Up @@ -78,6 +76,17 @@ shellspec_end() {
# shellcheck disable=SC2034
SHELLSPEC_EXAMPLE_COUNT=${1:-}
shellspec_output END
}

shellspec_before_first_block() {
shellspec_mark_group "$SHELLSPEC_BLOCK_NO" ""
[ "$SHELLSPEC_DRYRUN" ] && return 0
shellspec_if SKIP && return 0
shellspec_call_before_hooks ALL
shellspec_mark_group "$SHELLSPEC_BLOCK_NO" 1
}

shellspec_after_last_block() {
shellspec_call_after_hooks ALL
}

Expand All @@ -92,7 +101,6 @@ shellspec_description() {
shellspec_example_group() {
shellspec_description "example_group" "${1:-}"
shellspec_yield
shellspec_call_after_hooks ALL
}

shellspec_example_block() {
Expand Down Expand Up @@ -134,47 +142,14 @@ shellspec_example() {
done
fi

if [ "$SHELLSPEC_ENABLED" ] && [ "$SHELLSPEC_FILTER" ]; then
if [ "$SHELLSPEC_DRYRUN" ]; then
shellspec_output EXAMPLE
shellspec_output SUCCEEDED
else
if ! shellspec_if SKIP; then
shellspec_call_before_hooks ALL
shellspec_mark_group "$SHELLSPEC_GROUP_ID"
fi
shellspec_profile_start
case $- in
*e*)
set +e
( set -e
case $# in
0) shellspec_invoke_example ;;
*) shellspec_invoke_example "$@" ;;
esac
)
set -e -- $?
;;
*)
( set -e
case $# in
0) shellspec_invoke_example ;;
*) shellspec_invoke_example "$@" ;;
esac
)
set -- $?
esac
if [ "$1" -ne 0 ]; then
shellspec_output ABORTED "$1"
shellspec_output FAILED
fi
shellspec_profile_end
fi
fi
}
[ "$SHELLSPEC_ENABLED" ] || return 0
[ "$SHELLSPEC_FILTER" ] || return 0

shellspec_invoke_example() {
shellspec_output EXAMPLE
if [ "$SHELLSPEC_DRYRUN" ]; then
shellspec_output EXAMPLE
shellspec_output SUCCEEDED
return 0
fi

# shellcheck disable=SC2034
{
Expand All @@ -183,6 +158,30 @@ shellspec_invoke_example() {
SHELLSPEC_STDERR_FILE="$SHELLSPEC_STDIO_FILE_BASE.stderr"
}

shellspec_profile_start
case $- in
*e*) eval "set -- -e ${1+\"\$@\"}" ;;
*) eval "set -- +e ${1+\"\$@\"}" ;;
esac
set +e
( set -e
shift
case $# in
0) shellspec_invoke_example ;;
*) shellspec_invoke_example "$@" ;;
esac
)
set "$1" -- $? "$@"
if [ "$1" -ne 0 ]; then
shellspec_output ABORTED "$1"
shellspec_output FAILED
fi
shellspec_profile_end
}

shellspec_invoke_example() {
shellspec_output EXAMPLE

shellspec_on NOT_IMPLEMENTED
shellspec_off FAILED WARNED EXPECTATION
shellspec_off UNHANDLED_STATUS UNHANDLED_STDOUT UNHANDLED_STDERR
Expand Down
20 changes: 7 additions & 13 deletions lib/core/hook.sh
Expand Up @@ -20,21 +20,21 @@ shellspec_proxy shellspec_register_before_hook "shellspec_register_hook BEFORE"
shellspec_proxy shellspec_register_after_hook "shellspec_register_hook AFTER"

shellspec_hook_index() {
eval "SHELLSPEC_$1_$2_$3=\$SHELLSPEC_GROUP_ID#\$SHELLSPEC_AUX_LINENO:\$4"
eval "SHELLSPEC_$1_$2_$3=\$SHELLSPEC_BLOCK_NO#\$SHELLSPEC_AUX_LINENO:\$4"
}

shellspec_call_hook() {
eval "set -- \"\$1\" \"\${SHELLSPEC_$1_$2#*:}\" \"\${SHELLSPEC_$1_$2%%:*}\""
# shellcheck disable=SC2034
SHELLSPEC_HOOK_GROUP_ID=${3%\#*} SHELLSPEC_HOOK_LINENO=${3#*\#}
SHELLSPEC_HOOK_BLOCK_NO=${3%\#*} SHELLSPEC_HOOK_LINENO=${3#*\#}

case $1 in
BEFORE_ALL)
shellspec_is_marked_group "$SHELLSPEC_HOOK_GROUP_ID" && return 0
shellspec_is_marked_group "$SHELLSPEC_HOOK_BLOCK_NO" && return 0
;;
AFTER_ALL)
[ "$SHELLSPEC_HOOK_GROUP_ID" = "$SHELLSPEC_GROUP_ID" ] || return 0
shellspec_is_marked_group "$SHELLSPEC_HOOK_GROUP_ID" || return 0
[ "$SHELLSPEC_HOOK_BLOCK_NO" = "$SHELLSPEC_BLOCK_NO" ] || return 0
shellspec_is_marked_group "$SHELLSPEC_HOOK_BLOCK_NO" || return 0
;;
esac

Expand Down Expand Up @@ -65,17 +65,11 @@ shellspec_call_after_hooks() {
}

shellspec_mark_group() {
until shellspec_is_marked_group "$1"; do
: > "$SHELLSPEC_WORKDIR/@$1"
case $1 in
*-*) set -- "${1%-*}" ;;
*) set -- "" ;;
esac
done
eval "SHELLSPEC_MARK_${1:-0}=\${2:-}"
}

shellspec_is_marked_group() {
[ -e "$SHELLSPEC_WORKDIR/@$1" ]
eval "[ \"\$SHELLSPEC_MARK_${1:-0}\" ] &&:" &&:
}

shellspec_create_hook EACH
Expand Down
21 changes: 17 additions & 4 deletions lib/libexec/translator.sh
Expand Up @@ -14,10 +14,14 @@ initialize() {
}

finalize() {
[ "$_block_no_stack" ] || return 0
syntax_error "Unexpected end of file (expecting 'End')"
lineno=
while [ "$_block_no_stack" ]; do block_end; done
if [ "$_block_no_stack" ]; then
syntax_error "Unexpected end of file (expecting 'End')"
lineno=
while [ "$_block_no_stack" ]; do block_end; done
fi
if [ ! "$block_id_increased" ]; then
trans after_last_block ""
fi
}

read_specfile() {
Expand Down Expand Up @@ -111,6 +115,9 @@ block_example_group() {

check_filter "$1" && filter=1

if [ "$block_id_increased" ]; then
trans before_first_block "$block_id"
fi
increase_block_id
_block_no=$(($_block_no + 1))
block_no=$_block_no lineno_begin=$lineno
Expand All @@ -137,6 +144,9 @@ block_example() {

check_filter "$1" && filter=1

if [ "$block_id_increased" ]; then
trans before_first_block "$block_id"
fi
increase_block_id
_block_no=$(($_block_no + 1))
block_no=$_block_no lineno_begin=$lineno
Expand All @@ -162,6 +172,9 @@ block_end() {
return 0
fi

if [ ! "$block_id_increased" ]; then
trans after_last_block "${block_id%-*}"
fi
decrease_block_id
block_no=${_block_no_stack##* } lineno_end=$lineno
eval "block_lineno_end${block_no}=$lineno"
Expand Down
10 changes: 10 additions & 0 deletions libexec/shellspec-translate.sh
Expand Up @@ -51,6 +51,14 @@ trans_block_end() {
putsn "shellspec_block${block_no}) ${1# }"
}

trans_before_first_block() {
putsn "shellspec_before_first_block"
}

trans_after_last_block() {
putsn "shellspec_after_last_block"
}

trans_evaluation() {
putsn "SHELLSPEC_LINENO=$lineno"
putsn "if [ \$# -eq 0 ]"
Expand Down Expand Up @@ -185,6 +193,8 @@ filter=1
[ "$SHELLSPEC_EXAMPLE_FILTER" ] && filter=''

putsn "#!/bin/sh"
putsn "SHELLSPEC_WORKDIR=\"\$SHELLSPEC_TMPBASE\""
putsn "SHELLSPEC_STDIO_FILE_BASE=\"\$SHELLSPEC_WORKDIR\""
putsn "shellspec_coverage_start() { :; }"
putsn "shellspec_coverage_stop() { :; }"
if [ "$coverage" ]; then
Expand Down
19 changes: 19 additions & 0 deletions spec/core/dsl/before_after_all_spec.sh
@@ -0,0 +1,19 @@
#shellcheck shell=sh disable=SC2016

BeforeAll 'var=0'
Describe 'BeforeAll / AfterAll hook'
BeforeAll '[ "$var" = 0 ] && var=$(($var+1))'
AfterAll 'var=$(($var-1)) && [ "$var" = 0 ]'

Specify "BeforeAll calls once per block"
The variable var should eq 1
End

Specify "BeforeAll shares the state"
The variable var should eq 1
End
End

Example
The variable var should eq 0
End
44 changes: 44 additions & 0 deletions spec/core/dsl_spec.sh
Expand Up @@ -117,6 +117,50 @@ Describe "core/dsl.sh"
End
End

Describe "shellspec_before_first_block()"
BeforeRun SHELLSPEC_BLOCK_NO=12345
before_first_block() {
shellspec_call_before_hooks() { echo "$@"; }
shellspec_before_first_block
shellspec_call_before_hooks() { :; }
}

It 'calls before all hooks'
When run before_first_block
The stdout should eq "ALL"
End

Context 'when dry-run mode'
BeforeRun SHELLSPEC_DRYRUN=1
It 'does not call before all hooks'
When run before_first_block
The stdout should eq ""
End
End

Context 'when skipeed'
BeforeRun "shellspec_on SKIP"
It 'does not call before all hooks'
When run before_first_block
The stdout should eq ""
End
End
End

Describe "shellspec_after_last_block()"
BeforeRun SHELLSPEC_BLOCK_NO=12345
after_last_block() {
shellspec_call_after_hooks() { echo "$@"; }
shellspec_after_last_block
shellspec_call_after_hooks() { :; }
}

It 'calls after all hooks'
When run after_last_block
The stdout should eq "ALL"
End
End

Describe "shellspec_end()"
mock() { shellspec_output() { echo "$1"; }; }
echo_example_count() { echo "$SHELLSPEC_EXAMPLE_COUNT"; }
Expand Down
39 changes: 22 additions & 17 deletions spec/core/hook_spec.sh
Expand Up @@ -2,8 +2,6 @@

Describe "core/hook.sh"
Before 'shellspec_create_hook EXAMPLE'
BeforeAll :
AfterAll :

Describe 'shellspec_call_before_hooks()'
Describe "before each"
Expand Down Expand Up @@ -102,8 +100,8 @@ Describe "core/hook.sh"
The stdout should eq foo
End

Context 'when SHELLSPEC_GROUP_ID is not matched'
BeforeRun 'SHELLSPEC_GROUP_ID=99999'
Context 'when SHELLSPEC_BLOCK_NO is not matched'
BeforeRun 'SHELLSPEC_BLOCK_NO=99999'
It 'does not call after all hooks'
When run shellspec_call_after_hooks ALL
The stdout should be blank
Expand All @@ -114,19 +112,26 @@ Describe "core/hook.sh"
End

Describe 'shellspec_mark_group()'
setup() { mkdir "$SHELLSPEC_TMPBASE/workdir"; }
cleanup() { rm -rf "$SHELLSPEC_TMPBASE/workdir"; }
Before setup
After cleanup

BeforeRun 'SHELLSPEC_WORKDIR=$SHELLSPEC_TMPBASE/workdir'

It 'creates mark group files'
When run shellspec_mark_group 1-2-3
The file "$SHELLSPEC_TMPBASE/workdir/@" should be exist
The file "$SHELLSPEC_TMPBASE/workdir/@1" should be exist
The file "$SHELLSPEC_TMPBASE/workdir/@1-2" should be exist
The file "$SHELLSPEC_TMPBASE/workdir/@1-2-3" should be exist
Before "shellspec_mark_group 12345"

It 'marks to group'
When call shellspec_mark_group 12345 1
The variable SHELLSPEC_MARK_12345 should eq 1
End
End

Describe 'shellspec_is_marked_group()'
Before "shellspec_mark_group 12345 1"
Before "shellspec_mark_group 12346"

Parameters
12345 success
12346 failure
End

It "checks mark of group ($1)"
When call shellspec_is_marked_group "$1"
The status should be "$2"
End
End
End

0 comments on commit a05da89

Please sign in to comment.