# vim:et:ft=sh:sts=2:sw=2 # # shUnit2 unit test common functions # # Copyright 2008 Kate Ward. All Rights Reserved. # Released under the Apache 2.0 license. # # Author: kate.ward@forestent.com (Kate Ward) # https://github.com/kward/shunit2 # ### ShellCheck (http://www.shellcheck.net/) # Commands are purposely escaped so they can be mocked outside shUnit2. # shellcheck disable=SC1001,SC1012 # expr may be antiquated, but it is the only solution in some cases. # shellcheck disable=SC2003 # $() are not fully portable (POSIX != portable). # shellcheck disable=SC2006 # Treat unset variables as an error when performing parameter expansion. set -u # Set shwordsplit for zsh. \[ -n "${ZSH_VERSION:-}" ] && setopt shwordsplit # # Constants. # # Path to shUnit2 library. Can be overridden by setting SHUNIT_INC. TH_SHUNIT=${SHUNIT_INC:-./shunit2}; export TH_SHUNIT # Configure debugging. Set the DEBUG environment variable to any # non-empty value to enable debug output, or TRACE to enable trace # output. TRACE=${TRACE:+'th_trace '} \[ -n "${TRACE}" ] && DEBUG=1 \[ -z "${TRACE}" ] && TRACE=':' DEBUG=${DEBUG:+'th_debug '} \[ -z "${DEBUG}" ] && DEBUG=':' # # Variables. # th_RANDOM=0 # # Functions. # # Logging functions. th_trace() { echo "${MY_NAME}:TRACE $*" >&2; } th_debug() { echo "${MY_NAME}:DEBUG $*" >&2; } th_info() { echo "${MY_NAME}:INFO $*" >&2; } th_warn() { echo "${MY_NAME}:WARN $*" >&2; } th_error() { echo "${MY_NAME}:ERROR $*" >&2; } th_fatal() { echo "${MY_NAME}:FATAL $*" >&2; } # Output subtest name. th_subtest() { echo " $*" >&2; } th_oneTimeSetUp() { # These files will be cleaned up automatically by shUnit2. stdoutF="${SHUNIT_TMPDIR}/stdout" stderrF="${SHUNIT_TMPDIR}/stderr" returnF="${SHUNIT_TMPDIR}/return" expectedF="${SHUNIT_TMPDIR}/expected" export stdoutF stderrF returnF expectedF } # Generate a random number. th_generateRandom() { tfgr_random=${th_RANDOM} while \[ "${tfgr_random}" = "${th_RANDOM}" ]; do # shellcheck disable=SC2039 if \[ -n "${RANDOM:-}" ]; then # $RANDOM works # shellcheck disable=SC2039 tfgr_random=${RANDOM}${RANDOM}${RANDOM}$$ elif \[ -r '/dev/urandom' ]; then tfgr_random=`od -vAn -N4 -tu4 >> STDOUT' >&2 \cat "${_th_stdout_}" >&2 fi if \[ -n "${_th_stderr_}" -a -s "${_th_stderr_}" ]; then echo '>>> STDERR' >&2 \cat "${_th_stderr_}" >&2 fi if \[ -n "${_th_stdout_}" -o -n "${_th_stderr_}" ]; then echo '<<< end output' >&2 fi fi unset _th_return_ _th_stdout_ _th_stderr_ } # # Main. # ${TRACE} 'trace output enabled' ${DEBUG} 'debug output enabled'