Skip to content

Commit

Permalink
enhance document
Browse files Browse the repository at this point in the history
  • Loading branch information
moritetu committed Mar 15, 2018
1 parent 491af27 commit d1a6a4b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
20 changes: 20 additions & 0 deletions docs/WritingTests.rst
Expand Up @@ -138,6 +138,26 @@ There may be when you want to read variables from all tests, in that case you ca
``setup_all`` function with ``@BeforeAll`` annotation is called only once before all tests start, and ``teardown_all`` function with ``@AfterAll`` annotation is called only once after all tests ends. These functions are executed in parent shell of tests, ``GLOBAL_VAR`` is visible from all tests. Outside of functions, ``EVALUATED_ONCE`` is also evaluated once with ``source`` command.

OneTimeSetup/OneTimeTearDown
============================

If ``_all.sh`` file exists in the project top directory, Baut loads it. In ``_all.sh``, you can define functions
called only once at the begin or the end of your test suite.

.. code-block:: bash
#!/usr/bin/env bash
# _all.sh
#
_setup() {
echo "called at the begin of test suite"
}
_cleanup() {
echo "called at end begin of test suite"
}
Commands
========
Expand Down
8 changes: 8 additions & 0 deletions libexec/baut-config
Expand Up @@ -36,6 +36,14 @@ BAUT_LOAD_PATH=( "${BAUT_HELPER:-$BAUT_ROOT/helpers}" "${BAUT_LIBEXEC:-}" "$PWD/
#: The directory into which compiled files are output
declare -r BAUT_TMPDIR=${BAUT_TMPDIR:-"${TMPDIR:-/tmp}"}

#:@BAUT_ONETIME_SETUP
#: The function called at the begin of test suite.
: ${BAUT_ONETIME_SETUP:="_setup"}

#:@BAUT_ONETIME_TEARDOWN
#: The function called at the end of test suite.
: ${BAUT_ONETIME_TEARDOWN:="_cleanup"}


# show_config
# Print baut configurations.
Expand Down
13 changes: 6 additions & 7 deletions libexec/baut-exec-run
Expand Up @@ -33,7 +33,6 @@ test_end_time_sec=0
# Test total count
test_total_count=0


#: parse_options [arg ...]
#: Parse command line options.
#:
Expand Down Expand Up @@ -148,11 +147,11 @@ baut_perform_test_suite() {
# If pre-process over all test exists, run it.
if [ -f "${options[wrapScript]}" ]; then
source "${options[wrapScript]}"
if type "_setup" &> /dev/null
if type "$BAUT_ONETIME_SETUP" &> /dev/null
then
_setup || status=$?
$BAUT_ONETIME_SETUP || status=$?
if [ $status -ne 0 ]; then
abort "error: failed to execute _setup in ${options[wrapScript]}"
abort "error: failed to execute ${BAUT_ONETIME_SETUP} in ${options[wrapScript]}"
fi
fi
fi
Expand All @@ -169,11 +168,11 @@ baut_perform_test_suite() {
test_end_time_sec="$(date +'%s')"

# If after-process over all test exists, run it.
if type "_cleanup" &> /dev/null
if type "$BAUT_ONETIME_TEARDOWN" &> /dev/null
then
_cleanup || status=$?
$BAUT_ONETIME_TEARDOWN || status=$?
if [ $status -ne 0 ]; then
error "warn: failed to execute _cleanup in ${options[wrapScript]}"
error "warn: failed to execute ${BAUT_ONETIME_TEARDOWN} in ${options[wrapScript]}"
fi
fi

Expand Down

0 comments on commit d1a6a4b

Please sign in to comment.