Commit 444a6e5f authored by robbuckley's avatar robbuckley
Browse files

add utility functions

parent a01ddf72
...@@ -141,6 +141,39 @@ function suppress { ...@@ -141,6 +141,39 @@ function suppress {
return "$ret" return "$ret"
} }
function expect_return {
# Run a command, succeeding (returning 0) only if the commend returns a specified code
# Parameters
# retcode expected return code (which may be zero)
# command the command called
#
# any further arguments are passed to the called command
#
# Returns 1 if called with less than 2 arguments
(( $# < 2 )) && echo "Must have at least 2 arguments" && return 1
local retcode=$1
local retval
( "${@:2}" ) || retval=$?
[[ $retcode == ${retval:-0} ]] && return 0
return ${retval:-1}
}
function cmd_notexit {
# wraps a command, capturing its return code and preventing it
# from exiting the shell. Handles -e / +e modes.
# Parameters
# cmd - command
# any further parameters are passed to the wrapped command
# If called without an argument, it will exit the shell with an error
local cmd=$1
if [ -z "$cmd" ];then echo "no command"; exit 1; fi
if [[ $- = *e* ]]; then errexit_set=true; fi
set +e
("${@:1}") ; retval=$?
[[ -n $errexit_set ]] && set -e
return $retval
}
function rm_mkdir { function rm_mkdir {
# Remove directory if present, then make directory # Remove directory if present, then make directory
local path=$1 local path=$1
......
...@@ -88,6 +88,17 @@ actual="$(set -e; suppress bad_mid_cmd)" ...@@ -88,6 +88,17 @@ actual="$(set -e; suppress bad_mid_cmd)"
# Reset options # Reset options
set_opts $ORIG_OPTS set_opts $ORIG_OPTS
! expect_return 1 || ingest "Too few arguments"
! expect_return 1 good_cmd || ingest "unexpected success"
! expect_return 0 bad_cmd || ingest "unexpected failure"
expect_return 1 bad_cmd || ingest "fail with expected error 1"
! expect_return 2 bad_cmd || ingest "fail with unexpected error"
expect_return 0 good_cmd || ingest "succeed as expected"
cmd_notexit good_cmd || ingest
! cmd_notexit bad_cmd || ingest
! cmd_notexit exit 1 || ingest
# On Linux docker containers in travis, can be x86_64, i686, s390x, ppc64le, or # On Linux docker containers in travis, can be x86_64, i686, s390x, ppc64le, or
# aarch64 # aarch64
[ "$(get_platform)" == x86_64 ] || \ [ "$(get_platform)" == x86_64 ] || \
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment