Fixed PREFIX in Makefile, added several tests in tests/

This commit is contained in:
2013-10-09 22:42:16 -04:00
parent 034e79e9bf
commit 77d48c3425
4 changed files with 162 additions and 18 deletions

View File

@@ -9,11 +9,7 @@ endif
RPM=cmdarg-$(VERSION)-$(RELEASE).noarch.rpm RPM=cmdarg-$(VERSION)-$(RELEASE).noarch.rpm
ifndef PREFIX ifndef PREFIX
PREFIX=/ PREFIX=''
endif
ifeq ($(shell uname -o),Cygwin)
PREFIX=
endif endif
DISTFILE_DEPS=$(shell find . -type f | grep -Ev '\.git|\./dist/|$(DISTFILE)') DISTFILE_DEPS=$(shell find . -type f | grep -Ev '\.git|\./dist/|$(DISTFILE)')

View File

@@ -46,14 +46,14 @@ function cmdarg
if [[ "${1:2:4}" == "[]" ]]; then if [[ "${1:2:4}" == "[]" ]]; then
declare -p ${key} >/dev/null 2>&1 declare -p ${key} >/dev/null 2>&1
if [[ $? -ne 0 ]]; then if [[ $? -ne 0 ]]; then
echo 'Array variable cmdarg_'"${key}"' does not exist. Array variables MUST be declared by the user!' >&2 echo 'Array variable '"${key}"' does not exist. Array variables MUST be declared by the user!' >&2
exit 1 exit 1
fi fi
CMDARG_TYPES[$key]=$CMDARG_TYPE_ARRAY CMDARG_TYPES[$key]=$CMDARG_TYPE_ARRAY
elif [[ "${1:2:4}" == "{}" ]]; then elif [[ "${1:2:4}" == "{}" ]]; then
declare -p ${key} >/dev/null 2>&1 declare -p ${key} >/dev/null 2>&1
if [[ $? -ne 0 ]]; then if [[ $? -ne 0 ]]; then
echo 'Hash variable cmdarg_'"${key}"' does not exist. Hash variables MUST be declared by the user!' >&2 echo 'Hash variable '"${key}"' does not exist. Hash variables MUST be declared by the user!' >&2
exit 1 exit 1
fi fi
CMDARG_TYPES[$key]=$CMDARG_TYPE_HASH CMDARG_TYPES[$key]=$CMDARG_TYPE_HASH
@@ -98,27 +98,28 @@ function cmdarg_info
function cmdarg_describe function cmdarg_describe
{ {
echo "cmdarg_describe $@" >&2
local key default local key default
key=${CMDARG[$1]} longopt=${CMDARG[$1]}
opt=$1 opt=$1
if [ "${CMDARG_DEFAULT[$key]}" != "" ]; then if [ "${CMDARG_DEFAULT[$longopt]}" != "" ]; then
default="(Default \"${CMDARG_DEFAULT[$key]}\")" default="(Default \"${CMDARG_DEFAULT[$longopt]}\")"
fi fi
case ${CMDARG_TYPES[$key]} in case ${CMDARG_TYPES[$longopt]} in
$CMDARG_TYPE_STRING) $CMDARG_TYPE_STRING)
echo "-${opt} v : String. ${CMDARG_DESC[$key]} $default" echo "-${opt} v : String. ${CMDARG_DESC[$opt]} $default"
;; ;;
$CMDARG_TYPE_BOOLEAN) $CMDARG_TYPE_BOOLEAN)
echo "-${opt} : Boolean. ${CMDARG_DESC[$key]} $default" echo "-${opt} : Boolean. ${CMDARG_DESC[$opt]} $default"
;; ;;
$CMDARG_TYPE_ARRAY) $CMDARG_TYPE_ARRAY)
echo "-${opt} v[, ...] : Array. ${CMDARG_DESC[$key]}. Pass this argument multiple times for multiple values. $default" echo "-${opt} v[, ...] : Array. ${CMDARG_DESC[$opt]}. Pass this argument multiple times for multiple values. $default"
;; ;;
$CMDARG_TYPE_HASH) $CMDARG_TYPE_HASH)
echo "-${opt} k=v{, ..} : Hash. ${CMDARG_DESC[$key]}. Pass this argument multiple times for multiple key/value pairs. $default" echo "-${opt} k=v{, ..} : Hash. ${CMDARG_DESC[$opt]}. Pass this argument multiple times for multiple key/value pairs. $default"
;; ;;
*) *)
echo "Unable to return string description for ${key}; unknown type ${CMDARG_TYPES[$key]}" >&2 echo "Unable to return string description for ${key}; unknown type ${CMDARG_TYPES[$opt]}" >&2
exit 1 exit 1
;; ;;
esac esac
@@ -135,7 +136,6 @@ function cmdarg_usage
echo echo
local key local key
if [[ "${!CMDARG_REQUIRED[@]}" != "" ]]; then if [[ "${!CMDARG_REQUIRED[@]}" != "" ]]; then
echo "Required Arguments:"
for key in "${CMDARG_REQUIRED[@]}" for key in "${CMDARG_REQUIRED[@]}"
do do
echo " $(cmdarg_describe $key)" echo " $(cmdarg_describe $key)"
@@ -143,7 +143,6 @@ function cmdarg_usage
echo echo
fi fi
if [[ "${!CMDARG_OPTIONAL[@]}" != "" ]]; then if [[ "${!CMDARG_OPTIONAL[@]}" != "" ]]; then
echo "Optional Arguments:"
for key in "${CMDARG_OPTIONAL[@]}" for key in "${CMDARG_OPTIONAL[@]}"
do do
echo " $(cmdarg_describe $key)" echo " $(cmdarg_describe $key)"
@@ -316,6 +315,23 @@ function cmdarg_dump
done done
} }
function cmdarg_purge
{
arrays="cmdarg_cfg CMDARG CMDARG_REV CMDARG_OPTIONAL CMDARG_REQUIRED"
arrays="$arrays CMDARG_DESC CMDARG_DEFAULT CMDARG_VALIDATORS CMDARG_INFO"
arrays="$arrays CMDARG_FLAGS CMDARG_TYPES"
for arr in $arrays
do
str='${!'"$arr"'[@]}'
for key in $(eval "echo $str")
do
str="$arr[$key]"
eval "unset $str"
done
done
CMDARG_GETOPTLIST="h"
}
if [[ "${_DEFINED_CMDARG}" == "" ]]; then if [[ "${_DEFINED_CMDARG}" == "" ]]; then
export _DEFINED_CMDARG=0 export _DEFINED_CMDARG=0
# Holds the final map of configuration options # Holds the final map of configuration options

58
tests/test_clean_state.sh Normal file
View File

@@ -0,0 +1,58 @@
source $(dirname ${BASH_SOURCE})/../cmdarg.sh
function shunittest_clean_state()
{
# Tests that cmdarg_purge ensures an empty config state
function parse1()
{
cmdarg 'a:' 'a' 'some arg'
cmdarg 'b' 'b' 'some arg'
cmdarg_parse "$@"
}
function parse2()
{
cmdarg_purge
cmdarg_parse "$@"
}
# This cleans the state from shunit
cmdarg_purge
parse1 -a 3 -b
parse2
if [[ "${cmdarg_cfg['a']}" == "" ]]; then
return 0
else
cmdarg_dump
return 1
fi
}
function shunittest_clean_state_subshells()
{
# Ensures that, when subsequent cmdarg invocations occur in subshells,
# that the initial state is empty even without having called cmdarg_purge
# This is just here to clean the state from shunit
cmdarg_purge
function parse1()
{
cmdarg 'a:' 'a' 'some arg'
cmdarg 'b' 'b' 'some arg'
cmdarg_parse "$@"
}
function parse2()
{
cmdarg_parse "$@"
}
(parse1 -a 3 -b)
(parse2)
if [[ "${cmdarg_cfg['a']}" == "" ]]; then
return 0
else
cmdarg_dump
return 1
fi
}

74
tests/test_types.sh Normal file
View File

@@ -0,0 +1,74 @@
source $(dirname ${BASH_SOURCE})/../cmdarg.sh
function shunittest_array_undefined()
{
# Tests that cmdarg and cmdarg_parse return an error when an array
# is undefined
cmdarg_purge
err=$(cmdarg 'a:[]' 'missingarray' 2>&1)
if [[ $? -eq 0 ]]; then
echo "cmdarg fails to throw an error for undefined array variables"
else
echo "$err" | grep "Array variable missingarray does not exist" >/dev/null
if [[ $? -ne 0 ]]; then
echo "cmdarg does not report errors on stderr for undefined arrays"
echo "$err"
return 1
fi
fi
return 0
}
function shunittest_array_values
{
cmdarg_purge
declare -a array
cmdarg 'a:[]' 'array'
cmdarg_parse -a a -a b -a c
if [[ "${array[@]}" != "a b c" ]]; then
echo "Array does not contain expected arguments"
cmdarg_dump >&2
return 1
fi
return $?
}
function shunittest_hash_undefined()
{
# Tests that cmdarg and cmdarg_parse return an error when an array
# is undefined
cmdarg_purge
err=$(cmdarg 'a:{}' 'missingarray' 2>&1)
if [[ $? -eq 0 ]]; then
echo "cmdarg fails to throw an error for undefined hash variables"
else
echo "$err" | grep "Hash variable missingarray does not exist" >/dev/null
if [[ $? -ne 0 ]]; then
echo "cmdarg does not report errors on stderr for undefined hashes"
echo "$err"
return 1
fi
fi
return 0
}
function shunittest_hash_values
{
cmdarg_purge
declare -A hash
cmdarg 'H:{}' 'hash'
cmdarg_parse -H a=1 -H b=2 -H c=3
base="a=1 b=2 c=3"
cmp=""
for k in a b c
do
cmp="$cmp ${k}=${hash[$k]}"
done
cmp=$(echo "$cmp" | sed s/'^\s*'//)
if [[ "$cmp" != "$base" ]]; then
echo "Hash does not contain expected arguments ($cmp vs $base)"
cmdarg_dump >&2
return 1
fi
return $?
}