diff options
author | kate.ward <kate.ward@forestent.com> | 2011-06-10 11:13:10 +0000 |
---|---|---|
committer | kate.ward <kate.ward@forestent.com> | 2011-06-10 11:13:10 +0000 |
commit | 43e7ca1318332abbaede7f34f44531f0cc783949 (patch) | |
tree | 4939ecccba50536dab658916129e4be0f4c9bf79 | |
parent | 929261fb95f6949701e1d93c8159c055fb21face (diff) | |
download | shflags-43e7ca1318332abbaede7f34f44531f0cc783949.tar.gz |
upgraded shunit2 to 2.1.6
-rw-r--r-- | source/1.0/lib/shunit2 | 185 |
1 files changed, 149 insertions, 36 deletions
diff --git a/source/1.0/lib/shunit2 b/source/1.0/lib/shunit2 index 68f6e19..590a5a8 100644 --- a/source/1.0/lib/shunit2 +++ b/source/1.0/lib/shunit2 @@ -1,4 +1,5 @@ -# $Id: shunit2 296 2010-03-17 23:49:03Z kate.ward@forestent.com $ +#! /bin/sh +# $Id: shunit2 322 2011-04-24 00:09:45Z kate.ward@forestent.com $ # vim:et:ft=sh:sts=2:sw=2 # # Copyright 2008 Kate Ward. All Rights Reserved. @@ -12,27 +13,31 @@ # shUnit2 is a xUnit based unit test framework for Bourne shell scripts. It is # based on the popular JUnit unit testing framework for Java. -SHUNIT_VERSION='2.1.6pre' +# return if shunit already loaded +[ -n "${SHUNIT_VERSION:-}" ] && exit 0 + +SHUNIT_VERSION='2.1.6' SHUNIT_TRUE=0 SHUNIT_FALSE=1 SHUNIT_ERROR=2 +# enable strict mode by default +SHUNIT_STRICT=${SHUNIT_STRICT:-${SHUNIT_TRUE}} + _shunit_warn() { echo "shunit2:WARN $@" >&2; } _shunit_error() { echo "shunit2:ERROR $@" >&2; } -_shunit_fatal() { echo "shunit2:FATAL $@" >&2; } +_shunit_fatal() { echo "shunit2:FATAL $@" >&2; exit ${SHUNIT_ERROR}; } # specific shell checks if [ -n "${ZSH_VERSION:-}" ]; then setopt |grep "^shwordsplit$" >/dev/null if [ $? -ne ${SHUNIT_TRUE} ]; then _shunit_fatal 'zsh shwordsplit option is required for proper operation' - exit ${SHUNIT_ERROR} fi if [ -z "${SHUNIT_PARENT:-}" ]; then _shunit_fatal "zsh does not pass \$0 through properly. please declare \ \"SHUNIT_PARENT=\$0\" before calling shUnit2" - exit ${SHUNIT_ERROR} fi fi @@ -41,12 +46,14 @@ fi # __SHUNIT_ASSERT_MSG_PREFIX='ASSERT:' +__SHUNIT_MODE_SOURCED='sourced' +__SHUNIT_MODE_STANDALONE='standalone' __SHUNIT_PARENT=${SHUNIT_PARENT:-$0} # set the constants readonly shunit_constants_=`set |grep '^__SHUNIT_' |cut -d= -f1` -echo "${shunit_constants_}" |grep '^Binary file' >/dev/null \ - && shunit_constants_=`set |grep -a '^__SHUNIT_' |cut -d= -f1` +echo "${shunit_constants_}" |grep '^Binary file' >/dev/null && \ + shunit_constants_=`set |grep -a '^__SHUNIT_' |cut -d= -f1` for shunit_constant_ in ${shunit_constants_}; do shunit_ro_opts_='' case ${ZSH_VERSION:-} in @@ -59,8 +66,12 @@ done unset shunit_constant_ shunit_constants_ shunit_ro_opts_ # variables -__shunit_skip=${SHUNIT_FALSE} -__shunit_suite='' +__shunit_lineno='' # line number of executed test +__shunit_mode=${__SHUNIT_MODE_SOURCED} # operating mode +__shunit_reportGenerated=${SHUNIT_FALSE} # is report generated +__shunit_script='' # filename of unittest script (standalone mode) +__shunit_skip=${SHUNIT_FALSE} # is skipping enabled +__shunit_suite='' # suite of tests to execute # counts of tests __shunit_testSuccess=${SHUNIT_TRUE} @@ -74,9 +85,6 @@ __shunit_assertsPassed=0 __shunit_assertsFailed=0 __shunit_assertsSkipped=0 -__shunit_lineno='' -__shunit_reportGenerated=${SHUNIT_FALSE} - # macros _SHUNIT_LINENO_='eval __shunit_lineno=""; if [ "${1:-}" = "--lineno" ]; then [ -n "$2" ] && __shunit_lineno="[$2] "; shift 2; fi' @@ -145,18 +153,18 @@ assertNotEquals() shunit_message_="${shunit_message_}$1" shift fi - shunit_unexpected_=$1 + shunit_expected_=$1 shunit_actual_=$2 shunit_return=${SHUNIT_TRUE} - if [ "${shunit_unexpected_}" != "${shunit_actual_}" ]; then + if [ "${shunit_expected_}" != "${shunit_actual_}" ]; then _shunit_assertPass else failSame "${shunit_message_}" "$@" shunit_return=${SHUNIT_FALSE} fi - unset shunit_message_ shunit_unexpected_ shunit_actual_ + unset shunit_message_ shunit_expected_ shunit_actual_ return ${shunit_return} } _ASSERT_NOT_EQUALS_='eval assertNotEquals --lineno "${LINENO:-}"' @@ -211,10 +219,12 @@ assertNotNull() shunit_message_="${shunit_message_}$1" shift fi - assertTrue "${shunit_message_}" "[ -n '${1:-}' ]" + shunit_actual_=`_shunit_escapeCharactersInString "${1:-}"` + test -n "${shunit_actual_}" + assertTrue "${shunit_message_}" $? shunit_return=$? - unset shunit_message_ + unset shunit_actual_ shunit_message_ return ${shunit_return} } _ASSERT_NOT_NULL_='eval assertNotNull --lineno "${LINENO:-}"' @@ -231,7 +241,7 @@ assertSame() { ${_SHUNIT_LINENO_} if [ $# -lt 2 -o $# -gt 3 ]; then - _shunit_error "assertSame() requires one or two arguments; $# given" + _shunit_error "assertSame() requires two or three arguments; $# given" return ${SHUNIT_ERROR} fi _shunit_shouldSkip && return ${SHUNIT_TRUE} @@ -322,7 +332,8 @@ assertTrue() if [ -z "${shunit_condition_}" ]; then # null condition shunit_return=${SHUNIT_FALSE} - elif [ "${shunit_condition_}" = "${shunit_match_}" ]; then + elif [ -n "${shunit_match_}" -a "${shunit_condition_}" = "${shunit_match_}" ] + then # possible return value. treating 0 as true, and non-zero as false. [ ${shunit_condition_} -ne 0 ] && shunit_return=${SHUNIT_FALSE} else @@ -386,7 +397,8 @@ assertFalse() if [ -z "${shunit_condition_}" ]; then # null condition shunit_return=${SHUNIT_FALSE} - elif [ "${shunit_condition_}" = "${shunit_match_}" ]; then + elif [ -n "${shunit_match_}" -a "${shunit_condition_}" = "${shunit_match_}" ] + then # possible return value. treating 0 as true, and non-zero as false. [ ${shunit_condition_} -eq 0 ] && shunit_return=${SHUNIT_FALSE} else @@ -443,7 +455,7 @@ _FAIL_='eval fail --lineno "${LINENO:-}"' # # Args: # message: string: failure message [optional] -# unexpected: string: unexpected value +# expected: string: expected value # actual: string: actual value # Returns: # integer: success (TRUE/FALSE/ERROR constant) @@ -461,12 +473,12 @@ failNotEquals() shunit_message_="${shunit_message_}$1" shift fi - shunit_unexpected_=$1 + shunit_expected_=$1 shunit_actual_=$2 - _shunit_assertFail "${shunit_message_:+${shunit_message_} }expected:<${shunit_unexpected_}> but was:<${shunit_actual_}>" + _shunit_assertFail "${shunit_message_:+${shunit_message_} }expected:<${shunit_expected_}> but was:<${shunit_actual_}>" - unset shunit_message_ shunit_unexpected_ shunit_actual_ + unset shunit_message_ shunit_expected_ shunit_actual_ return ${SHUNIT_FALSE} } _FAIL_NOT_EQUALS_='eval failNotEquals --lineno "${LINENO:-}"' @@ -507,7 +519,7 @@ _FAIL_SAME_='eval failSame --lineno "${LINENO:-}"' # # Args: # message: string: failure message [optional] -# unexpected: string: unexpected value +# expected: string: expected value # actual: string: actual value # Returns: # integer: success (TRUE/FALSE/ERROR constant) @@ -687,22 +699,21 @@ _shunit_mktempDir() fi _shunit_tmpDir_="${TMPDIR:-/tmp}/shunit.${_shunit_random_}" - ( umask 077 && mkdir "${_shunit_tmpDir_}" ) || { - _shunit_fatal 'could not create temporary directory! exiting' - return ${SHUNIT_ERROR} - } + ( umask 077 && mkdir "${_shunit_tmpDir_}" ) || \ + _shunit_fatal 'could not create temporary directory! exiting' echo ${_shunit_tmpDir_} unset _shunit_date_ _shunit_random_ _shunit_tmpDir_ } -# This function is here to work around issues in Cygwin +# This function is here to work around issues in Cygwin. # # Args: # None _shunit_mktempFunc() { - for _shunit_func_ in oneTimeSetUp oneTimeTearDown setUp tearDown suite; do + for _shunit_func_ in oneTimeSetUp oneTimeTearDown setUp tearDown suite noexec + do _shunit_file_="${__shunit_tmpDir}/${_shunit_func_}" cat <<EOF >"${_shunit_file_}" #! /bin/sh @@ -883,17 +894,109 @@ _shunit_assertSkip() __shunit_assertsTotal=`expr ${__shunit_assertsTotal} + 1` } +# Prepare a script filename for sourcing. +# +# Args: +# script: string: path to a script to source +# Returns: +# string: filename prefixed with ./ (if necessary) +_shunit_prepForSourcing() +{ + _shunit_script_=$1 + case "${_shunit_script_}" in + /*|./*) echo "${_shunit_script_}" ;; + *) echo "./${_shunit_script_}" ;; + esac + unset _shunit_script_ +} + +# Escape a character in a string. +# +# Args: +# c: string: unescaped character +# s: string: to escape character in +# Returns: +# string: with escaped character(s) +_shunit_escapeCharInStr() +{ + [ -n "$2" ] || return # no point in doing work on an empty string + + # Note: using shorter variable names to prevent conflicts with + # _shunit_escapeCharactersInString(). + _shunit_c_=$1 + _shunit_s_=$2 + + + # escape the character + echo ''${_shunit_s_}'' |sed 's/\'${_shunit_c_}'/\\\'${_shunit_c_}'/g' + + unset _shunit_c_ _shunit_s_ +} + +# Escape a character in a string. +# +# Args: +# str: string: to escape characters in +# Returns: +# string: with escaped character(s) +_shunit_escapeCharactersInString() +{ + [ -n "$1" ] || return # no point in doing work on an empty string + + _shunit_str_=$1 + + # Note: using longer variable names to prevent conflicts with + # _shunit_escapeCharInStr(). + for _shunit_char_ in '"' '$' "'" '`'; do + _shunit_str_=`_shunit_escapeCharInStr "${_shunit_char_}" "${_shunit_str_}"` + done + + echo "${_shunit_str_}" + unset _shunit_char_ _shunit_str_ +} + +# Extract list of functions to run tests against. +# +# Args: +# script: string: name of script to extract functions from +# Returns: +# string: of function names +_shunit_extractTestFunctions() +{ + _shunit_script_=$1 + + # extract the lines with test function names, strip of anything besides the + # function name, and output everything on a single line. + _shunit_regex_='^[ ]*(function )*test[A-Za-z0-9_]* *\(\)' + egrep "${_shunit_regex_}" "${_shunit_script_}" \ + |sed 's/^[^A-Za-z0-9_]*//;s/^function //;s/\([A-Za-z0-9_]*\).*/\1/g' \ + |xargs + + unset _shunit_regex_ _shunit_script_ +} + #------------------------------------------------------------------------------ # main # +# determine the operating mode +if [ $# -eq 0 ]; then + __shunit_script=${__SHUNIT_PARENT} + __shunit_mode=${__SHUNIT_MODE_SOURCED} +else + __shunit_script=$1 + [ -r "${__shunit_script}" ] || \ + _shunit_fatal "unable to read from ${__shunit_script}" + __shunit_mode=${__SHUNIT_MODE_STANDALONE} +fi + # create a temporary storage location -__shunit_tmpDir=`_shunit_mktempDir` || exit ${SHUNIT_ERROR} +__shunit_tmpDir=`_shunit_mktempDir` # provide a public temporary directory for unit test scripts # TODO(kward): document this -shunit_tmpDir="${__shunit_tmpDir}/tmp" -mkdir "${shunit_tmpDir}" +SHUNIT_TMPDIR="${__shunit_tmpDir}/tmp" +mkdir "${SHUNIT_TMPDIR}" # setup traps to clean up after ourselves trap '_shunit_cleanup EXIT' 0 @@ -904,6 +1007,17 @@ trap '_shunit_cleanup TERM' 15 _shunit_mktempFunc PATH="${__shunit_tmpDir}:${PATH}" +# make sure phantom functions are executable. this will bite if /tmp (or the +# current $TMPDIR) points to a path on a partition that was mounted with the +# 'noexec' option. the noexec command was created with _shunit_mktempFunc(). +noexec 2>/dev/null || _shunit_fatal \ + 'please declare TMPDIR with path on partition with exec permission' + +# we must manually source the tests in standalone mode +if [ "${__shunit_mode}" = "${__SHUNIT_MODE_STANDALONE}" ]; then + . "`_shunit_prepForSourcing \"${__shunit_script}\"`" +fi + # execute the oneTimeSetUp function (if it exists) oneTimeSetUp @@ -913,8 +1027,7 @@ suite # if no suite function was defined, dynamically build a list of functions if [ -z "${__shunit_suite}" ]; then - shunit_funcs_=`grep "^[ \t]*test[A-Za-z0-9_]* *()" ${__SHUNIT_PARENT} \ - |sed 's/[^A-Za-z0-9_]//g'` + shunit_funcs_=`_shunit_extractTestFunctions "${__shunit_script}"` for shunit_func_ in ${shunit_funcs_}; do suite_addTest ${shunit_func_} done |