Fixed PREFIX in Makefile, added several tests in tests/
This commit is contained in:
6
Makefile
6
Makefile
@@ -9,11 +9,7 @@ endif
|
||||
RPM=cmdarg-$(VERSION)-$(RELEASE).noarch.rpm
|
||||
|
||||
ifndef PREFIX
|
||||
PREFIX=/
|
||||
endif
|
||||
|
||||
ifeq ($(shell uname -o),Cygwin)
|
||||
PREFIX=
|
||||
PREFIX=''
|
||||
endif
|
||||
|
||||
DISTFILE_DEPS=$(shell find . -type f | grep -Ev '\.git|\./dist/|$(DISTFILE)')
|
||||
|
||||
42
cmdarg.sh
42
cmdarg.sh
@@ -46,14 +46,14 @@ function cmdarg
|
||||
if [[ "${1:2:4}" == "[]" ]]; then
|
||||
declare -p ${key} >/dev/null 2>&1
|
||||
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
|
||||
fi
|
||||
CMDARG_TYPES[$key]=$CMDARG_TYPE_ARRAY
|
||||
elif [[ "${1:2:4}" == "{}" ]]; then
|
||||
declare -p ${key} >/dev/null 2>&1
|
||||
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
|
||||
fi
|
||||
CMDARG_TYPES[$key]=$CMDARG_TYPE_HASH
|
||||
@@ -98,27 +98,28 @@ function cmdarg_info
|
||||
|
||||
function cmdarg_describe
|
||||
{
|
||||
echo "cmdarg_describe $@" >&2
|
||||
local key default
|
||||
key=${CMDARG[$1]}
|
||||
longopt=${CMDARG[$1]}
|
||||
opt=$1
|
||||
if [ "${CMDARG_DEFAULT[$key]}" != "" ]; then
|
||||
default="(Default \"${CMDARG_DEFAULT[$key]}\")"
|
||||
if [ "${CMDARG_DEFAULT[$longopt]}" != "" ]; then
|
||||
default="(Default \"${CMDARG_DEFAULT[$longopt]}\")"
|
||||
fi
|
||||
case ${CMDARG_TYPES[$key]} in
|
||||
case ${CMDARG_TYPES[$longopt]} in
|
||||
$CMDARG_TYPE_STRING)
|
||||
echo "-${opt} v : String. ${CMDARG_DESC[$key]} $default"
|
||||
echo "-${opt} v : String. ${CMDARG_DESC[$opt]} $default"
|
||||
;;
|
||||
$CMDARG_TYPE_BOOLEAN)
|
||||
echo "-${opt} : Boolean. ${CMDARG_DESC[$key]} $default"
|
||||
echo "-${opt} : Boolean. ${CMDARG_DESC[$opt]} $default"
|
||||
;;
|
||||
$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)
|
||||
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
|
||||
;;
|
||||
esac
|
||||
@@ -135,7 +136,6 @@ function cmdarg_usage
|
||||
echo
|
||||
local key
|
||||
if [[ "${!CMDARG_REQUIRED[@]}" != "" ]]; then
|
||||
echo "Required Arguments:"
|
||||
for key in "${CMDARG_REQUIRED[@]}"
|
||||
do
|
||||
echo " $(cmdarg_describe $key)"
|
||||
@@ -143,7 +143,6 @@ function cmdarg_usage
|
||||
echo
|
||||
fi
|
||||
if [[ "${!CMDARG_OPTIONAL[@]}" != "" ]]; then
|
||||
echo "Optional Arguments:"
|
||||
for key in "${CMDARG_OPTIONAL[@]}"
|
||||
do
|
||||
echo " $(cmdarg_describe $key)"
|
||||
@@ -316,6 +315,23 @@ function cmdarg_dump
|
||||
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
|
||||
export _DEFINED_CMDARG=0
|
||||
# Holds the final map of configuration options
|
||||
|
||||
58
tests/test_clean_state.sh
Normal file
58
tests/test_clean_state.sh
Normal 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
74
tests/test_types.sh
Normal 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 $?
|
||||
}
|
||||
Reference in New Issue
Block a user