diff options
author | Kate Ward <kate.ward@forestent.com> | 2020-04-11 16:40:34 +0200 |
---|---|---|
committer | Kate Ward <kate.ward@forestent.com> | 2020-04-11 16:40:34 +0200 |
commit | 417b8e76d29a207da015b164b9cdd4f319b7ab6c (patch) | |
tree | a5c30b465fc4a380a5cdb251522a4b6b9c36fb76 | |
parent | 11da4af85ca0445d9d95a1e7cbd39655f8ea014f (diff) | |
parent | 1962cd8cea3fe6811373b289fb374fd41fc1277e (diff) | |
download | shflags-417b8e76d29a207da015b164b9cdd4f319b7ab6c.tar.gz |
Merge branch 'master' of github.com:kward/shflags
-rwxr-xr-x | lib/shunit2 | 357 | ||||
-rwxr-xr-x | shflags_parsing_test.sh | 25 | ||||
-rw-r--r-- | shflags_test_helpers | 5 |
3 files changed, 236 insertions, 151 deletions
diff --git a/lib/shunit2 b/lib/shunit2 index 530b2d6..26f743e 100755 --- a/lib/shunit2 +++ b/lib/shunit2 @@ -19,7 +19,9 @@ # shellcheck disable=SC2003 # Return if shunit2 already loaded. -(command [ -n "${SHUNIT_VERSION:-}" ]) && exit 0 +if test -n "${SHUNIT_VERSION:-}"; then + exit 0 +fi SHUNIT_VERSION='2.1.9pre' # Return values that scripts can use. @@ -27,25 +29,19 @@ SHUNIT_TRUE=0 SHUNIT_FALSE=1 SHUNIT_ERROR=2 -# Logging functions. -_shunit_warn() { - ${__SHUNIT_CMD_ECHO_ESC} \ - "${__shunit_ansi_yellow}shunit2:WARN${__shunit_ansi_none} $*" >&2 -} -_shunit_error() { - ${__SHUNIT_CMD_ECHO_ESC} \ - "${__shunit_ansi_red}shunit2:ERROR${__shunit_ansi_none} $*" >&2 -} -_shunit_fatal() { - ${__SHUNIT_CMD_ECHO_ESC} \ - "${__shunit_ansi_red}shunit2:FATAL${__shunit_ansi_none} $*" >&2 - exit ${SHUNIT_ERROR} -} +# Determine if `builtin` command exists. +__SHUNIT_BUILTIN='builtin' +# shellcheck disable=2039 +if ! ("${__SHUNIT_BUILTIN}" echo 123 >/dev/null 2>&1); then + __SHUNIT_BUILTIN='' +fi # Determine some reasonable command defaults. __SHUNIT_CMD_ECHO_ESC='echo -e' # shellcheck disable=SC2039 -(command [ "`echo -e test`" = '-e test' ]) && __SHUNIT_CMD_ECHO_ESC='echo' +if ${__SHUNIT_BUILTIN} [ "`echo -e test`" = '-e test' ]; then + __SHUNIT_CMD_ECHO_ESC='echo' +fi __SHUNIT_UNAME_S=`uname -s` case "${__SHUNIT_UNAME_S}" in @@ -61,13 +57,25 @@ SHUNIT_CMD_TPUT=${SHUNIT_CMD_TPUT:-${__SHUNIT_CMD_TPUT}} # Enable color output. Options are 'never', 'always', or 'auto'. SHUNIT_COLOR=${SHUNIT_COLOR:-auto} +# Logging functions. +_shunit_warn() { + ${__SHUNIT_CMD_ECHO_ESC} "${__shunit_ansi_yellow}shunit2:WARN${__shunit_ansi_none} $*" >&2 +} +_shunit_error() { + ${__SHUNIT_CMD_ECHO_ESC} "${__shunit_ansi_red}shunit2:ERROR${__shunit_ansi_none} $*" >&2 +} +_shunit_fatal() { + ${__SHUNIT_CMD_ECHO_ESC} "${__shunit_ansi_red}shunit2:FATAL${__shunit_ansi_none} $*" >&2 + exit ${SHUNIT_ERROR} +} + # Specific shell checks. -if (command [ -n "${ZSH_VERSION:-}" ]); then +if ${__SHUNIT_BUILTIN} [ -n "${ZSH_VERSION:-}" ]; then setopt |grep "^shwordsplit$" >/dev/null - if (command [ $? -ne ${SHUNIT_TRUE} ]); then + if ${__SHUNIT_BUILTIN} [ $? -ne ${SHUNIT_TRUE} ]; then _shunit_fatal 'zsh shwordsplit option is required for proper operation' fi - if (command [ -z "${SHUNIT_PARENT:-}" ]); then + if ${__SHUNIT_BUILTIN} [ -z "${SHUNIT_PARENT:-}" ]; then _shunit_fatal "zsh does not pass \$0 through properly. please declare \ \"SHUNIT_PARENT=\$0\" before calling shUnit2" fi @@ -97,12 +105,15 @@ __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` for __shunit_const in ${__shunit_constants}; do - if (command [ -z "${ZSH_VERSION:-}" ]); then + if ${__SHUNIT_BUILTIN} [ -z "${ZSH_VERSION:-}" ]; then readonly "${__shunit_const}" else case ${ZSH_VERSION} in [123].*) readonly "${__shunit_const}" ;; - *) readonly -g "${__shunit_const}" # Declare readonly constants globally. + *) + # Declare readonly constants globally. + # shellcheck disable=SC2039 + readonly -g "${__shunit_const}" esac fi done @@ -145,7 +156,7 @@ __shunit_assertsSkipped=0 # # shellcheck disable=SC2016,SC2089 -_SHUNIT_LINENO_='eval __shunit_lineno=""; if (command [ "${1:-}" = "--lineno" ]); then (command [ -n "$2" ]) && __shunit_lineno="[$2] "; shift 2; fi' +_SHUNIT_LINENO_='eval __shunit_lineno=""; if ${__SHUNIT_BUILTIN} [ "${1:-}" = "--lineno" ]; then if ${__SHUNIT_BUILTIN} [ -n "$2" ]; then __shunit_lineno="[$2] "; fi; shift 2; fi' #----------------------------------------------------------------------------- # Assertion functions. @@ -162,15 +173,17 @@ _SHUNIT_LINENO_='eval __shunit_lineno=""; if (command [ "${1:-}" = "--lineno" ]) assertEquals() { # shellcheck disable=SC2090 ${_SHUNIT_LINENO_} - if (command [ $# -lt 2 -o $# -gt 3 ]); then + if ${__SHUNIT_BUILTIN} [ $# -lt 2 -o $# -gt 3 ]; then _shunit_error "assertEquals() requires two or three arguments; $# given" _shunit_assertFail return ${SHUNIT_ERROR} fi - _shunit_shouldSkip && return ${SHUNIT_TRUE} + if _shunit_shouldSkip; then + return ${SHUNIT_TRUE} + fi shunit_message_=${__shunit_lineno} - if (command [ $# -eq 3 ]); then + if ${__SHUNIT_BUILTIN} [ $# -eq 3 ]; then shunit_message_="${shunit_message_}$1" shift fi @@ -178,7 +191,7 @@ assertEquals() { shunit_actual_=$2 shunit_return=${SHUNIT_TRUE} - if (command [ "${shunit_expected_}" = "${shunit_actual_}" ]); then + if ${__SHUNIT_BUILTIN} [ "${shunit_expected_}" = "${shunit_actual_}" ]; then _shunit_assertPass else failNotEquals "${shunit_message_}" "${shunit_expected_}" "${shunit_actual_}" @@ -202,15 +215,17 @@ _ASSERT_EQUALS_='eval assertEquals --lineno "${LINENO:-}"' assertNotEquals() { # shellcheck disable=SC2090 ${_SHUNIT_LINENO_} - if (command [ $# -lt 2 -o $# -gt 3 ]); then + if ${__SHUNIT_BUILTIN} [ $# -lt 2 -o $# -gt 3 ]; then _shunit_error "assertNotEquals() requires two or three arguments; $# given" _shunit_assertFail return ${SHUNIT_ERROR} fi - _shunit_shouldSkip && return ${SHUNIT_TRUE} + if _shunit_shouldSkip; then + return ${SHUNIT_TRUE} + fi shunit_message_=${__shunit_lineno} - if (command [ $# -eq 3 ]); then + if ${__SHUNIT_BUILTIN} [ $# -eq 3 ]; then shunit_message_="${shunit_message_}$1" shift fi @@ -218,7 +233,7 @@ assertNotEquals() { shunit_actual_=$2 shunit_return=${SHUNIT_TRUE} - if (command [ "${shunit_expected_}" != "${shunit_actual_}" ]); then + if ${__SHUNIT_BUILTIN} [ "${shunit_expected_}" != "${shunit_actual_}" ]; then _shunit_assertPass else failSame "${shunit_message_}" "${shunit_expected_}" "${shunit_actual_}" @@ -242,23 +257,24 @@ _ASSERT_NOT_EQUALS_='eval assertNotEquals --lineno "${LINENO:-}"' assertContains() { # shellcheck disable=SC2090 ${_SHUNIT_LINENO_} - if (command [ $# -lt 2 -o $# -gt 3 ]); then + if ${__SHUNIT_BUILTIN} [ $# -lt 2 -o $# -gt 3 ]; then _shunit_error "assertContains() requires two or three arguments; $# given" _shunit_assertFail return ${SHUNIT_ERROR} fi - _shunit_shouldSkip && return ${SHUNIT_TRUE} + if _shunit_shouldSkip; then + return ${SHUNIT_TRUE} + fi shunit_message_=${__shunit_lineno} - if (command [ $# -eq 3 ]); then + if ${__SHUNIT_BUILTIN} [ $# -eq 3 ]; then shunit_message_="${shunit_message_}$1" shift fi shunit_container_=$1 shunit_content_=$2 - shunit_return=${SHUNIT_TRUE} - if echo "$shunit_container_" | grep -F -- "$shunit_content_" > /dev/null; then + if echo "${shunit_container_}" |grep -F -- "${shunit_content_}" >/dev/null; then _shunit_assertPass else failNotFound "${shunit_message_}" "${shunit_content_}" @@ -282,15 +298,17 @@ _ASSERT_CONTAINS_='eval assertContains --lineno "${LINENO:-}"' assertNotContains() { # shellcheck disable=SC2090 ${_SHUNIT_LINENO_} - if (command [ $# -lt 2 -o $# -gt 3 ]); then + if ${__SHUNIT_BUILTIN} [ $# -lt 2 -o $# -gt 3 ]; then _shunit_error "assertNotContains() requires two or three arguments; $# given" _shunit_assertFail return ${SHUNIT_ERROR} fi - _shunit_shouldSkip && return ${SHUNIT_TRUE} + if _shunit_shouldSkip; then + return ${SHUNIT_TRUE} + fi shunit_message_=${__shunit_lineno} - if (command [ $# -eq 3 ]); then + if ${__SHUNIT_BUILTIN} [ $# -eq 3 ]; then shunit_message_="${shunit_message_}$1" shift fi @@ -321,15 +339,17 @@ _ASSERT_NOT_CONTAINS_='eval assertNotContains --lineno "${LINENO:-}"' assertNull() { # shellcheck disable=SC2090 ${_SHUNIT_LINENO_} - if (command [ $# -lt 1 -o $# -gt 2 ]); then + if ${__SHUNIT_BUILTIN} [ $# -lt 1 -o $# -gt 2 ]; then _shunit_error "assertNull() requires one or two arguments; $# given" _shunit_assertFail return ${SHUNIT_ERROR} fi - _shunit_shouldSkip && return ${SHUNIT_TRUE} + if _shunit_shouldSkip; then + return ${SHUNIT_TRUE} + fi shunit_message_=${__shunit_lineno} - if (command [ $# -eq 2 ]); then + if ${__SHUNIT_BUILTIN} [ $# -eq 2 ]; then shunit_message_="${shunit_message_}$1" shift fi @@ -352,16 +372,18 @@ _ASSERT_NULL_='eval assertNull --lineno "${LINENO:-}"' assertNotNull() { # shellcheck disable=SC2090 ${_SHUNIT_LINENO_} - if (command [ $# -gt 2 ]); then + if ${__SHUNIT_BUILTIN} [ $# -gt 2 ]; then # Allowing 0 arguments as $1 might actually be null. _shunit_error "assertNotNull() requires one or two arguments; $# given" _shunit_assertFail return ${SHUNIT_ERROR} fi - _shunit_shouldSkip && return ${SHUNIT_TRUE} + if _shunit_shouldSkip; then + return ${SHUNIT_TRUE} + fi shunit_message_=${__shunit_lineno} - if (command [ $# -eq 2 ]); then + if ${__SHUNIT_BUILTIN} [ $# -eq 2 ]; then shunit_message_="${shunit_message_}$1" shift fi @@ -387,15 +409,17 @@ _ASSERT_NOT_NULL_='eval assertNotNull --lineno "${LINENO:-}"' assertSame() { # shellcheck disable=SC2090 ${_SHUNIT_LINENO_} - if (command [ $# -lt 2 -o $# -gt 3 ]); then + if ${__SHUNIT_BUILTIN} [ $# -lt 2 -o $# -gt 3 ]; then _shunit_error "assertSame() requires two or three arguments; $# given" _shunit_assertFail return ${SHUNIT_ERROR} fi - _shunit_shouldSkip && return ${SHUNIT_TRUE} + if _shunit_shouldSkip; then + return ${SHUNIT_TRUE} + fi shunit_message_=${__shunit_lineno} - if (command [ $# -eq 3 ]); then + if ${__SHUNIT_BUILTIN} [ $# -eq 3 ]; then shunit_message_="${shunit_message_}$1" shift fi @@ -419,15 +443,17 @@ _ASSERT_SAME_='eval assertSame --lineno "${LINENO:-}"' assertNotSame() { # shellcheck disable=SC2090 ${_SHUNIT_LINENO_} - if (command [ $# -lt 2 -o $# -gt 3 ]); then + if ${__SHUNIT_BUILTIN} [ $# -lt 2 -o $# -gt 3 ]; then _shunit_error "assertNotSame() requires two or three arguments; $# given" _shunit_assertFail return ${SHUNIT_ERROR} fi - _shunit_shouldSkip && return ${SHUNIT_TRUE} + if _shunit_shouldSkip; then + return ${SHUNIT_TRUE} + fi shunit_message_=${__shunit_lineno} - if (command [ $# -eq 3 ]); then + if ${__SHUNIT_BUILTIN} [ $# -eq 3 ]; then shunit_message_="${shunit_message_:-}$1" shift fi @@ -464,15 +490,17 @@ _ASSERT_NOT_SAME_='eval assertNotSame --lineno "${LINENO:-}"' assertTrue() { # shellcheck disable=SC2090 ${_SHUNIT_LINENO_} - if (command [ $# -lt 1 -o $# -gt 2 ]); then + if ${__SHUNIT_BUILTIN} [ $# -lt 1 -o $# -gt 2 ]; then _shunit_error "assertTrue() takes one or two arguments; $# given" _shunit_assertFail return ${SHUNIT_ERROR} fi - _shunit_shouldSkip && return ${SHUNIT_TRUE} + if _shunit_shouldSkip; then + return ${SHUNIT_TRUE} + fi shunit_message_=${__shunit_lineno} - if (command [ $# -eq 2 ]); then + if ${__SHUNIT_BUILTIN} [ $# -eq 2 ]; then shunit_message_="${shunit_message_}$1" shift fi @@ -480,21 +508,24 @@ assertTrue() { # See if condition is an integer, i.e. a return value. shunit_return=${SHUNIT_TRUE} - if (command [ -z "${shunit_condition_}" ]); then + if ${__SHUNIT_BUILTIN} [ -z "${shunit_condition_}" ]; then # Null condition. shunit_return=${SHUNIT_FALSE} elif (expr \( "${shunit_condition_}" + '0' \) '=' "${shunit_condition_}" >/dev/null 2>&1) then # Possible return value. Treating 0 as true, and non-zero as false. - (command [ "${shunit_condition_}" -ne 0 ]) && shunit_return=${SHUNIT_FALSE} + if ${__SHUNIT_BUILTIN} [ "${shunit_condition_}" -ne 0 ]; then + shunit_return=${SHUNIT_FALSE} + fi else # Hopefully... a condition. - (eval "${shunit_condition_}" >/dev/null 2>&1) - (command [ $? -ne 0 ]) && shunit_return=${SHUNIT_FALSE} + if ! eval "${shunit_condition_}" >/dev/null 2>&1; then + shunit_return=${SHUNIT_FALSE} + fi fi # Record the test. - if (command [ ${shunit_return} -eq ${SHUNIT_TRUE} ]); then + if ${__SHUNIT_BUILTIN} [ ${shunit_return} -eq ${SHUNIT_TRUE} ]; then _shunit_assertPass else _shunit_assertFail "${shunit_message_}" @@ -530,15 +561,17 @@ _ASSERT_TRUE_='eval assertTrue --lineno "${LINENO:-}"' assertFalse() { # shellcheck disable=SC2090 ${_SHUNIT_LINENO_} - if (command [ $# -lt 1 -o $# -gt 2 ]); then + if ${__SHUNIT_BUILTIN} [ $# -lt 1 -o $# -gt 2 ]; then _shunit_error "assertFalse() requires one or two arguments; $# given" _shunit_assertFail return ${SHUNIT_ERROR} fi - _shunit_shouldSkip && return ${SHUNIT_TRUE} + if _shunit_shouldSkip; then + return ${SHUNIT_TRUE} + fi shunit_message_=${__shunit_lineno} - if (command [ $# -eq 2 ]); then + if ${__SHUNIT_BUILTIN} [ $# -eq 2 ]; then shunit_message_="${shunit_message_}$1" shift fi @@ -546,21 +579,24 @@ assertFalse() { # See if condition is an integer, i.e. a return value. shunit_return=${SHUNIT_TRUE} - if (command [ -z "${shunit_condition_}" ]); then + if ${__SHUNIT_BUILTIN} [ -z "${shunit_condition_}" ]; then # Null condition. shunit_return=${SHUNIT_TRUE} - elif (expr \( "${shunit_condition_}" + '0' \) '=' "${shunit_condition_}" >/dev/null 2>&1) - then + elif (expr \( "${shunit_condition_}" + '0' \) '=' "${shunit_condition_}" >/dev/null 2>&1); then # Possible return value. Treating 0 as true, and non-zero as false. - (command [ "${shunit_condition_}" -eq 0 ]) && shunit_return=${SHUNIT_FALSE} + if ${__SHUNIT_BUILTIN} [ "${shunit_condition_}" -eq 0 ]; then + shunit_return=${SHUNIT_FALSE} + fi else # Hopefully... a condition. - (eval "${shunit_condition_}" >/dev/null 2>&1) - (command [ $? -eq 0 ]) && shunit_return=${SHUNIT_FALSE} + # shellcheck disable=SC2086 + if eval ${shunit_condition_} >/dev/null 2>&1; then + shunit_return=${SHUNIT_FALSE} + fi fi # Record the test. - if (command [ "${shunit_return}" -eq "${SHUNIT_TRUE}" ]); then + if ${__SHUNIT_BUILTIN} [ "${shunit_return}" -eq "${SHUNIT_TRUE}" ]; then _shunit_assertPass else _shunit_assertFail "${shunit_message_}" @@ -585,14 +621,16 @@ _ASSERT_FALSE_='eval assertFalse --lineno "${LINENO:-}"' fail() { # shellcheck disable=SC2090 ${_SHUNIT_LINENO_} - if (command [ $# -gt 1 ]); then + if ${__SHUNIT_BUILTIN} [ $# -gt 1 ]; then _shunit_error "fail() requires zero or one arguments; $# given" return ${SHUNIT_ERROR} fi - _shunit_shouldSkip && return ${SHUNIT_TRUE} + if _shunit_shouldSkip; then + return ${SHUNIT_TRUE} + fi shunit_message_=${__shunit_lineno} - if (command [ $# -eq 1 ]); then + if ${__SHUNIT_BUILTIN} [ $# -eq 1 ]; then shunit_message_="${shunit_message_}$1" shift fi @@ -616,14 +654,16 @@ _FAIL_='eval fail --lineno "${LINENO:-}"' failNotEquals() { # shellcheck disable=SC2090 ${_SHUNIT_LINENO_} - if (command [ $# -lt 2 -o $# -gt 3 ]); then + if ${__SHUNIT_BUILTIN} [ $# -lt 2 -o $# -gt 3 ]; then _shunit_error "failNotEquals() requires one or two arguments; $# given" return ${SHUNIT_ERROR} fi - _shunit_shouldSkip && return ${SHUNIT_TRUE} + if _shunit_shouldSkip; then + return ${SHUNIT_TRUE} + fi shunit_message_=${__shunit_lineno} - if (command [ $# -eq 3 ]); then + if ${__SHUNIT_BUILTIN} [ $# -eq 3 ]; then shunit_message_="${shunit_message_}$1" shift fi @@ -649,14 +689,16 @@ _FAIL_NOT_EQUALS_='eval failNotEquals --lineno "${LINENO:-}"' failFound() { # shellcheck disable=SC2090 ${_SHUNIT_LINENO_} - if (command [ $# -lt 1 -o $# -gt 2 ]); then + if ${__SHUNIT_BUILTIN} [ $# -lt 1 -o $# -gt 2 ]; then _shunit_error "failFound() requires one or two arguments; $# given" return ${SHUNIT_ERROR} fi - _shunit_shouldSkip && return ${SHUNIT_TRUE} + if _shunit_shouldSkip; then + return ${SHUNIT_TRUE} + fi shunit_message_=${__shunit_lineno} - if (command [ $# -eq 2 ]); then + if ${__SHUNIT_BUILTIN} [ $# -eq 2 ]; then shunit_message_="${shunit_message_}$1" shift fi @@ -680,14 +722,16 @@ _FAIL_FOUND_='eval failFound --lineno "${LINENO:-}"' failNotFound() { # shellcheck disable=SC2090 ${_SHUNIT_LINENO_} - if (command [ $# -lt 1 -o $# -gt 2 ]); then + if ${__SHUNIT_BUILTIN} [ $# -lt 1 -o $# -gt 2 ]; then _shunit_error "failNotFound() requires one or two arguments; $# given" return ${SHUNIT_ERROR} fi - _shunit_shouldSkip && return ${SHUNIT_TRUE} + if _shunit_shouldSkip; then + return ${SHUNIT_TRUE} + fi shunit_message_=${__shunit_lineno} - if (command [ $# -eq 2 ]); then + if ${__SHUNIT_BUILTIN} [ $# -eq 2 ]; then shunit_message_="${shunit_message_}$1" shift fi @@ -710,18 +754,19 @@ _FAIL_NOT_FOUND_='eval failNotFound --lineno "${LINENO:-}"' # actual: string: actual value # Returns: # integer: success (TRUE/FALSE/ERROR constant) -failSame() -{ +failSame() { # shellcheck disable=SC2090 ${_SHUNIT_LINENO_} - if (command [ $# -lt 2 -o $# -gt 3 ]); then + if ${__SHUNIT_BUILTIN} [ $# -lt 2 -o $# -gt 3 ]; then _shunit_error "failSame() requires two or three arguments; $# given" return ${SHUNIT_ERROR} fi - _shunit_shouldSkip && return ${SHUNIT_TRUE} + if _shunit_shouldSkip; then + return ${SHUNIT_TRUE} + fi shunit_message_=${__shunit_lineno} - if (command [ $# -eq 3 ]); then + if ${__SHUNIT_BUILTIN} [ $# -eq 3 ]; then shunit_message_="${shunit_message_}$1" shift fi @@ -748,14 +793,16 @@ _FAIL_SAME_='eval failSame --lineno "${LINENO:-}"' failNotSame() { # shellcheck disable=SC2090 ${_SHUNIT_LINENO_} - if (command [ $# -lt 2 -o $# -gt 3 ]); then + if ${__SHUNIT_BUILTIN} [ $# -lt 2 -o $# -gt 3 ]; then _shunit_error "failNotSame() requires one or two arguments; $# given" return ${SHUNIT_ERROR} fi - _shunit_shouldSkip && return ${SHUNIT_TRUE} + if _shunit_shouldSkip; then + return ${SHUNIT_TRUE} + fi shunit_message_=${__shunit_lineno} - if (command [ $# -eq 3 ]); then + if ${__SHUNIT_BUILTIN} [ $# -eq 3 ]; then shunit_message_="${shunit_message_}$1" shift fi @@ -895,14 +942,15 @@ suite_addTest() { # string: the temporary directory that was created _shunit_mktempDir() { # Try the standard `mktemp` function. - ( exec mktemp -dqt shunit.XXXXXX 2>/dev/null ) && return + if ( exec mktemp -dqt shunit.XXXXXX 2>/dev/null ); then + return + fi # The standard `mktemp` didn't work. Use our own. # shellcheck disable=SC2039 - if (command [ -r '/dev/urandom' -a -x '/usr/bin/od' ]); then - _shunit_random_=`/usr/bin/od -vAn -N4 -tx4 </dev/urandom \ - |command sed 's/^[^0-9a-f]*//'` - elif (command [ -n "${RANDOM:-}" ]); then + if ${__SHUNIT_BUILTIN} [ -r '/dev/urandom' -a -x '/usr/bin/od' ]; then + _shunit_random_=`/usr/bin/od -vAn -N4 -tx4 </dev/urandom |command sed 's/^[^0-9a-f]*//'` + elif ${__SHUNIT_BUILTIN} [ -n "${RANDOM:-}" ]; then # $RANDOM works _shunit_random_=${RANDOM}${RANDOM}${RANDOM}$$ else @@ -912,8 +960,9 @@ _shunit_mktempDir() { fi _shunit_tmpDir_="${TMPDIR:-/tmp}/shunit.${_shunit_random_}" - ( umask 077 && command mkdir "${_shunit_tmpDir_}" ) || \ - _shunit_fatal 'could not create temporary directory! exiting' + if ! ( umask 077 && command mkdir "${_shunit_tmpDir_}" ); then + _shunit_fatal 'could not create temporary directory! exiting' + fi echo "${_shunit_tmpDir_}" unset _shunit_date_ _shunit_random_ _shunit_tmpDir_ @@ -957,12 +1006,12 @@ _shunit_cleanup() { _shunit_error "unrecognized trap value (${_shunit_name_})" ;; esac - if (command [ "${_shunit_name_}" != 'EXIT' ]); then + if ${__SHUNIT_BUILTIN} [ "${_shunit_name_}" != 'EXIT' ]; then _shunit_warn "trapped and now handling the (${_shunit_name_}) signal" fi # Do our work. - if (command [ ${__shunit_clean} -eq ${SHUNIT_FALSE} ]); then + if ${__SHUNIT_BUILTIN} [ ${__shunit_clean} -eq ${SHUNIT_FALSE} ]; then # Ensure tear downs are only called once. __shunit_clean=${SHUNIT_TRUE} @@ -973,11 +1022,11 @@ _shunit_cleanup() { command rm -fr "${__shunit_tmpDir}" fi - if (command [ "${_shunit_name_}" != 'EXIT' ]); then + if ${__SHUNIT_BUILTIN} [ "${_shunit_name_}" != 'EXIT' ]; then # Handle all non-EXIT signals. trap - 0 # Disable EXIT trap. exit ${_shunit_signal_} - elif (command [ ${__shunit_reportGenerated} -eq ${SHUNIT_FALSE} ]); then + elif ${__SHUNIT_BUILTIN} [ ${__shunit_reportGenerated} -eq ${SHUNIT_FALSE} ]; then _shunit_assertFail 'unknown failure encountered running a test' _shunit_generateReport exit ${SHUNIT_ERROR} @@ -995,7 +1044,9 @@ _shunit_configureColor() { case $1 in 'always') _shunit_color_=${SHUNIT_TRUE} ;; 'auto') - (command [ "`_shunit_colors`" -ge 8 ]) && _shunit_color_=${SHUNIT_TRUE} + if ${__SHUNIT_BUILTIN} [ "`_shunit_colors`" -ge 8 ]; then + _shunit_color_=${SHUNIT_TRUE} + fi ;; 'none') ;; *) _shunit_fatal "unrecognized color option '$1'" ;; @@ -1023,8 +1074,7 @@ _shunit_configureColor() { # colors returns the number of supported colors for the TERM. _shunit_colors() { - _shunit_tput_=`${SHUNIT_CMD_TPUT} colors 2>/dev/null` - if (command [ $? -eq 0 ]); then + if _shunit_tput_=`${SHUNIT_CMD_TPUT} colors 2>/dev/null`; then echo "${_shunit_tput_}" else echo 16 @@ -1044,22 +1094,26 @@ _shunit_execSuite() { endSkipping # Execute the per-test setup function. - setUp || _shunit_fatal "setup() returned non-zero return code." + if ! setUp; then + _shunit_fatal "setup() returned non-zero return code." + fi # Execute the test. echo "${__SHUNIT_TEST_PREFIX}${_shunit_test_}" - eval "${_shunit_test_}" - if (command [ $? -ne ${SHUNIT_TRUE} ]); then + # shellcheck disable=SC2086 + if ! eval ${_shunit_test_}; then _shunit_error "${_shunit_test_}() returned non-zero return code." __shunit_testSuccess=${SHUNIT_ERROR} _shunit_incFailedCount fi # Execute the per-test tear-down function. - tearDown || _shunit_fatal "tearDown() returned non-zero return code." + if ! tearDown; then + _shunit_fatal "tearDown() returned non-zero return code." + fi # Update stats. - if (command [ ${__shunit_testSuccess} -eq ${SHUNIT_TRUE} ]); then + if ${__SHUNIT_BUILTIN} [ ${__shunit_testSuccess} -eq ${SHUNIT_TRUE} ]; then __shunit_testsPassed=`expr ${__shunit_testsPassed} + 1` else __shunit_testsFailed=`expr ${__shunit_testsFailed} + 1` @@ -1076,32 +1130,36 @@ _shunit_execSuite() { # Output: # string: the report of successful and failed tests, as well as totals. _shunit_generateReport() { - (command [ "${__shunit_reportGenerated}" -eq ${SHUNIT_TRUE} ]) && return + if ${__SHUNIT_BUILTIN} [ "${__shunit_reportGenerated}" -eq ${SHUNIT_TRUE} ]; then + return + fi _shunit_ok_=${SHUNIT_TRUE} # If no exit code was provided, determine an appropriate one. - (command [ "${__shunit_testsFailed}" -gt 0 \ - -o ${__shunit_testSuccess} -eq ${SHUNIT_FALSE} ]) \ - && _shunit_ok_=${SHUNIT_FALSE} + if ${__SHUNIT_BUILTIN} [ "${__shunit_testsFailed}" -gt 0 -o ${__shunit_testSuccess} -eq ${SHUNIT_FALSE} ]; then + _shunit_ok_=${SHUNIT_FALSE} + fi echo _shunit_msg_="Ran ${__shunit_ansi_cyan}${__shunit_testsTotal}${__shunit_ansi_none}" - if (command [ "${__shunit_testsTotal}" -eq 1 ]); then + if ${__SHUNIT_BUILTIN} [ "${__shunit_testsTotal}" -eq 1 ]; then ${__SHUNIT_CMD_ECHO_ESC} "${_shunit_msg_} test." else ${__SHUNIT_CMD_ECHO_ESC} "${_shunit_msg_} tests." fi - if (command [ ${_shunit_ok_} -eq ${SHUNIT_TRUE} ]); then + if ${__SHUNIT_BUILTIN} [ ${_shunit_ok_} -eq ${SHUNIT_TRUE} ]; then _shunit_msg_="${__shunit_ansi_green}OK${__shunit_ansi_none}" - (command [ "${__shunit_assertsSkipped}" -gt 0 ]) \ - && _shunit_msg_="${_shunit_msg_} (${__shunit_ansi_yellow}skipped=${__shunit_assertsSkipped}${__shunit_ansi_none})" + if ${__SHUNIT_BUILTIN} [ "${__shunit_assertsSkipped}" -gt 0 ]; then + _shunit_msg_="${_shunit_msg_} (${__shunit_ansi_yellow}skipped=${__shunit_assertsSkipped}${__shunit_ansi_none})" + fi else _shunit_msg_="${__shunit_ansi_red}FAILED${__shunit_ansi_none}" _shunit_msg_="${_shunit_msg_} (${__shunit_ansi_red}failures=${__shunit_assertsFailed}${__shunit_ansi_none}" - (command [ "${__shunit_assertsSkipped}" -gt 0 ]) \ - && _shunit_msg_="${_shunit_msg_},${__shunit_ansi_yellow}skipped=${__shunit_assertsSkipped}${__shunit_ansi_none}" + if ${__SHUNIT_BUILTIN} [ "${__shunit_assertsSkipped}" -gt 0 ]; then + _shunit_msg_="${_shunit_msg_},${__shunit_ansi_yellow}skipped=${__shunit_assertsSkipped}${__shunit_ansi_none}" + fi _shunit_msg_="${_shunit_msg_})" fi @@ -1119,7 +1177,9 @@ _shunit_generateReport() { # Returns: # boolean: whether the test should be skipped (TRUE/FALSE constant) _shunit_shouldSkip() { - (command [ ${__shunit_skip} -eq ${SHUNIT_FALSE} ]) && return ${SHUNIT_FALSE} + if test ${__shunit_skip} -eq ${SHUNIT_FALSE}; then + return ${SHUNIT_FALSE} + fi _shunit_assertSkip } @@ -1140,8 +1200,9 @@ _shunit_assertFail() { __shunit_testSuccess=${SHUNIT_FALSE} _shunit_incFailedCount - \[ $# -gt 0 ] && ${__SHUNIT_CMD_ECHO_ESC} \ - "${__shunit_ansi_red}ASSERT:${__shunit_ansi_none}$*" + if ${__SHUNIT_BUILTIN} [ $# -gt 0 ]; then + ${__SHUNIT_CMD_ECHO_ESC} "${__shunit_ansi_red}ASSERT:${__shunit_ansi_none}$*" + fi } # Increment the count of failed asserts. @@ -1153,7 +1214,6 @@ _shunit_incFailedCount() { __shunit_assertsTotal=`expr "${__shunit_assertsTotal}" + 1` } - # Records a skipped test. # # Args: @@ -1163,6 +1223,19 @@ _shunit_assertSkip() { __shunit_assertsTotal=`expr "${__shunit_assertsTotal}" + 1` } +# Dump the current test metrics. +# +# Args: +# none +_shunit_metrics() { + echo "< \ +total: ${__shunit_assertsTotal} \ +passed: ${__shunit_assertsPassed} \ +failed: ${__shunit_assertsFailed} \ +skipped: ${__shunit_assertsSkipped} \ +>" +} + # Prepare a script filename for sourcing. # # Args: @@ -1186,7 +1259,9 @@ _shunit_prepForSourcing() { # Returns: # string: with escaped character(s) _shunit_escapeCharInStr() { - (command [ -n "$2" ]) || return # No point in doing work on an empty string. + if ${__SHUNIT_BUILTIN} [ -z "$2" ]; then + return # No point in doing work on an empty string. + fi # Note: using shorter variable names to prevent conflicts with # _shunit_escapeCharactersInString(). @@ -1207,7 +1282,9 @@ _shunit_escapeCharInStr() { # Returns: # string: with escaped character(s) _shunit_escapeCharactersInString() { - (command [ -n "$1" ]) || return # No point in doing work on an empty string. + if ${__SHUNIT_BUILTIN} [ -z "$1" ]; then + return # No point in doing work on an empty string. + fi _shunit_str_=$1 @@ -1246,13 +1323,14 @@ _shunit_extractTestFunctions() { # # Determine the operating mode. -if (command [ $# -eq 0 -o "${1:-}" = '--' ]); then +if ${__SHUNIT_BUILTIN} [ $# -eq 0 -o "${1:-}" = '--' ]; then __shunit_script=${__SHUNIT_PARENT} __shunit_mode=${__SHUNIT_MODE_SOURCED} else __shunit_script=$1 - (command [ -r "${__shunit_script}" ]) || \ - _shunit_fatal "unable to read from ${__shunit_script}" + if ! ${__SHUNIT_BUILTIN} [ -r "${__shunit_script}" ]; then + _shunit_fatal "unable to read from ${__shunit_script}" + fi __shunit_mode=${__SHUNIT_MODE_STANDALONE} fi @@ -1262,7 +1340,9 @@ __shunit_tmpDir=`_shunit_mktempDir` # Provide a public temporary directory for unit test scripts. # TODO(kward): document this. SHUNIT_TMPDIR="${__shunit_tmpDir}/tmp" -command mkdir "${SHUNIT_TMPDIR}" +if ! command mkdir "${SHUNIT_TMPDIR}"; then + _shunit_fatal "error creating SHUNIT_TMPDIR '${SHUNIT_TMPDIR}'" +fi # Setup traps to clean up after ourselves. trap '_shunit_cleanup EXIT' 0 @@ -1280,20 +1360,21 @@ 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 (command [ "${__shunit_mode}" = "${__SHUNIT_MODE_STANDALONE}" ]); then +if ${__SHUNIT_BUILTIN} [ "${__shunit_mode}" = "${__SHUNIT_MODE_STANDALONE}" ]; then # shellcheck disable=SC1090 - command . "`_shunit_prepForSourcing \"${__shunit_script}\"`" + ${__SHUNIT_BUILTIN} . "`_shunit_prepForSourcing \"${__shunit_script}\"`" fi # Configure default output coloring behavior. _shunit_configureColor "${SHUNIT_COLOR}" # Execute the oneTimeSetUp function (if it exists). -oneTimeSetUp || \ - _shunit_fatal "oneTimeSetUp() returned non-zero return code." +if ! oneTimeSetUp; then + _shunit_fatal "oneTimeSetUp() returned non-zero return code." +fi # Command line selected tests or suite selected tests -if (command [ "$#" -ge 2 ]); then +if ${__SHUNIT_BUILTIN} [ "$#" -ge 2 ]; then # Argument $1 is either the filename of tests or '--'; either way, skip it. shift # Remaining arguments ($2 .. $#) are assumed to be test function names. @@ -1310,7 +1391,7 @@ else fi # If no tests or suite specified, dynamically build a list of functions. -if (command [ -z "${__shunit_suite}" ]); then +if ${__SHUNIT_BUILTIN} [ -z "${__shunit_suite}" ]; then shunit_funcs_=`_shunit_extractTestFunctions "${__shunit_script}"` for shunit_func_ in ${shunit_funcs_}; do suite_addTest "${shunit_func_}" @@ -1322,12 +1403,14 @@ unset shunit_func_ shunit_funcs_ _shunit_execSuite # Execute the oneTimeTearDown function (if it exists). -oneTimeTearDown || \ - _shunit_fatal "oneTimeTearDown() returned non-zero return code." +if ! oneTimeTearDown; then + _shunit_fatal "oneTimeTearDown() returned non-zero return code." +fi # Generate a report summary. _shunit_generateReport # That's it folks. -(command [ "${__shunit_testsFailed}" -eq 0 ]) -exit $? +if ! ${__SHUNIT_BUILTIN} [ "${__shunit_testsFailed}" -eq 0 ]; then + return ${SHUNIT_FALSE} +fi diff --git a/shflags_parsing_test.sh b/shflags_parsing_test.sh index 1623434..e8bc89d 100755 --- a/shflags_parsing_test.sh +++ b/shflags_parsing_test.sh @@ -3,7 +3,7 @@ # # shFlags unit test for the flag definition methods # -# Copyright 2008-2017 Kate Ward. All Rights Reserved. +# Copyright 2008-2020 Kate Ward. All Rights Reserved. # Released under the Apache 2.0 license. # # Author: kate.ward@forestent.com (Kate Ward) @@ -16,12 +16,6 @@ # TODO(kward): assert on FLAGS errors # TODO(kward): testNonStandardIFS() -# Exit immediately if a pipeline or subshell exits with a non-zero status. -#set -e - -# Treat unset variables as an error. -set -u - # These variables will be overridden by the test helpers. returnF="${TMPDIR:-/tmp}/return" stdoutF="${TMPDIR:-/tmp}/STDOUT" @@ -31,13 +25,15 @@ stderrF="${TMPDIR:-/tmp}/STDERR" . ./shflags_test_helpers testGetoptStandard() { - _flags_getoptStandard '-b' >"${stdoutF}" 2>"${stderrF}" - rslt=$? - assertTrue "didn't parse valid flag 'b'" ${rslt} - th_showOutput ${rslt} "${stdoutF}" "${stderrF}" + if ! _flags_getoptStandard '-b' >"${stdoutF}" 2>"${stderrF}"; then + fail "didn't parse valid flag 'b'" + _showTestOutput + fi - _flags_getoptStandard '-x' >"${stdoutF}" 2>"${stderrF}" - assertFalse "parsed invalid flag 'x'" $? + if _flags_getoptStandard '-x' >"${stdoutF}" 2>"${stderrF}"; then + fail "parsed invalid flag 'x'" + _showTestOutput + fi } testGetoptEnhanced() { @@ -359,6 +355,9 @@ tearDown() { flags_reset } +# showTestOutput for the most recently run test. +_showTestOutput() { th_showOutput "${SHUNIT_FALSE}" "${stdoutF}" "${stderrF}"; } + # Load and run shUnit2. # shellcheck disable=SC2034 [ -n "${ZSH_VERSION:-}" ] && SHUNIT_PARENT=$0 diff --git a/shflags_test_helpers b/shflags_test_helpers index 36f651f..d808d0f 100644 --- a/shflags_test_helpers +++ b/shflags_test_helpers @@ -20,7 +20,10 @@ # Disagree with [ p ] && [ q ] vs [ p -a -q ] recommendation. # shellcheck disable=SC2166 -# Treat unset variables as an error. +# Exit immediately if a simple command exits with a non-zero status. +set -e + +# Treat unset variables as an error when performing parameter expansion. set -u # Set shwordsplit for zsh. |