#!/bin/bash ## Use this to test a function in functions.lib.sh ## Usage Examples: ## $0 {functionname} ## $0 {functionname} {testfile} ## $0 {functionname} {functiondataone} ## $0 {functionname} {functiondataone} {testfile} ## $0 {functionname} {functiondataone} {functiondatatwo} ## $0 {functionname} {functiondataone} {functiondatatwo} {testfile} ## functionname - The name of the function in functions.lib.sh to test ## functiondataone - The data to give to the function as first option ## functiondatatwo - The data to give to the function as second option ## testfile - The test file that contains starting data stored in variables ## The test file can also contain two functions 'preTest' and 'postTest' ## 'preTest' will be called before the given function is tested ## 'postTest' will be called after the given function is tested ## Example use of these two functions could be to output the contents ## of the variables in the test file, before and/or after the given ## function is tested. ## Exit code of this script is the same as the exit code of the tested function (0-127) ## Exit code 252 - If fourth option is given, it must be a file but the given file does not exist. ## Exit code 253 - Given function name does not exist in functions.lib.sh ## Exit code 254 - No function name was given ## Exit code 255 - Unknown error ## Change ${debugmode} between true and false to enable and disable debug messages. ## You can also redefine it in the test file, if a test file is to be used. declare debugmode=true source functions.lib.sh if [[ -z ${1} ]]; then echo "No function name given..." >&2 echo >&2 echo "Usage Examples:" >&2 echo " $0 {functionname}" >&2 echo " $0 {functionname} {testfile}" >&2 echo " $0 {functionname} {functiondataone}" >&2 echo " $0 {functionname} {functiondataone} {testfile}" >&2 echo " $0 {functionname} {functiondataone} {functiondatatwo}" >&2 echo " $0 {functionname} {functiondataone} {functiondatatwo} {testfile}" >&2 echo >&2 echo " functionname - The name of the function in functions.lib.sh to test" >&2 echo " functiondataone - The data to give to the function as first option" >&2 echo " functiondatatwo - The data to give to the function as second option" >&2 echo " testfile - The test file that contains starting data stored in variables" >&2 echo " The test file can also contain two functions 'preTest' and 'postTest'" >&2 echo " 'preTest' will be called before the given function is tested" >&2 echo " 'postTest' will be called after the given function is tested" >&2 echo " Example use of these two functions could be to output the contents" >&2 echo " of the variables in the test file, before and/or after the given" >&2 echo " function is tested." >&2 echo >&2 echo "You can also redefine the boolean value of \$debugmode from within the test file." >&2 echo "declare debugmode=true" >&2 echo " > This will turn debug messages on." >&2 echo "declare debugmode=false" >&2 echo " > This will turn debug messages off." >&2 echo >&2 echo "Exit code of this script is the same as the exit code of the tested function (0-127)" >&2 echo "Exit code 252 - If fourth option is given, it must be a file but the given file does not exist." >&2 echo "Exit code 253 - Given function name does not exist in functions.lib.sh" >&2 echo "Exit code 254 - No function name was given" >&2 echo "Exit code 255 - Unknown error" >&2 exit 254 fi functionname="${1}" if ! declare -F ${functionname} > /dev/null; then echo "Function '${functionname}' does not exist in 'functions.lib.sh'..." >&2 exit 253 fi if [[ -z ${2} ]]; then ${functionname} exitcode=$? else if [[ -f "${2}" ]]; then source "${2}" declare -F preTest > /dev/null && preTest ${functionname} exitcode=$? else functiondataone="${2}" if [[ -z ${3} ]]; then ${functionname} "${functiondataone}" exitcode=$? else if [[ -f "${3}" ]]; then source "${3}" declare -F preTest > /dev/null && preTest ${functionname} "${functiondataone}" exitcode=$? else functiondatatwo="${3}" if [[ -z ${4} ]]; then ${functionname} "${functiondataone}" "${functiondatatwo}" exitcode=$? else if [[ -f "${4}" ]]; then source "${4}" declare -F preTest > /dev/null && preTest ${functionname} "${functiondataone}" "${functiondatatwo}" exitcode=$? else echo "File '${4}' does not exist and cannot be sourced..." exit 252 fi fi fi fi fi fi declare -F postTest > /dev/null && postTest echo "Function '${functionname}' returned exit code ${exitcode}" && exit ${exitcode} exit 255