Cleanup of ini test-case

Various cleanup to this file.

Firstly create a temporary space to test, rather than working in the
source directory.

We have "assert_equal" which simplifies a lot.  Add "assert_empty"
that is used in a couple of tests too.  Remove a couple of duplicate
tests.

Change-Id: I7fd476ed63026e67d66a8ac2891b2e4a6687d09c
This commit is contained in:
Ian Wienand 2015-07-22 10:05:32 +10:00
parent 92884ede5d
commit b997db602e
2 changed files with 87 additions and 170 deletions

View File

@ -13,7 +13,13 @@ set -e
echo "Testing INI functions"
cat >test.ini <<EOF
INI_TMP_DIR=$(mktemp -d)
INI_TMP_ETC_DIR=$INI_TMP_DIR/etc
TEST_INI=${INI_TMP_ETC_DIR}/test.ini
mkdir ${INI_TMP_ETC_DIR}
echo "Creating $TEST_INI"
cat >${TEST_INI} <<EOF
[default]
# comment an option
#log_file=./log.conf
@ -67,204 +73,98 @@ EOF
# Test with missing arguments
BEFORE=$(cat test.ini)
BEFORE=$(cat ${TEST_INI})
echo -n "iniset: test missing attribute argument: "
iniset test.ini aaa
NO_ATTRIBUTE=$(cat test.ini)
if [[ "$BEFORE" == "$NO_ATTRIBUTE" ]]; then
passed
else
failed "failed"
fi
iniset ${TEST_INI} aaa
NO_ATTRIBUTE=$(cat ${TEST_INI})
assert_equal "$BEFORE" "$NO_ATTRIBUTE" "test missing attribute argument"
echo -n "iniset: test missing section argument: "
iniset test.ini
NO_SECTION=$(cat test.ini)
if [[ "$BEFORE" == "$NO_SECTION" ]]; then
passed
else
failed "failed"
fi
iniset ${TEST_INI}
NO_SECTION=$(cat ${TEST_INI})
assert_equal "$BEFORE" "$NO_SECTION" "missing section argument"
# Test with spaces
# Test with spaces in values
VAL=$(iniget ${TEST_INI} aaa handlers)
assert_equal "$VAL" "aa, bb" "iniget spaces in option"
VAL=$(iniget test.ini aaa handlers)
if [[ "$VAL" == "aa, bb" ]]; then
passed "OK: $VAL"
else
failed "iniget failed: $VAL"
fi
iniset test.ini aaa handlers "11, 22"
VAL=$(iniget test.ini aaa handlers)
if [[ "$VAL" == "11, 22" ]]; then
passed "OK: $VAL"
else
failed "iniget failed: $VAL"
fi
iniset ${TEST_INI} aaa handlers "11, 22"
VAL=$(iniget ${TEST_INI} aaa handlers)
assert_equal "$VAL" "11, 22" "iniset spaces in option"
# Test with spaces in section header
VAL=$(iniget ${TEST_INI} " ccc " spaces)
assert_equal "$VAL" "yes" "iniget with section header space"
VAL=$(iniget test.ini " ccc " spaces)
if [[ "$VAL" == "yes" ]]; then
passed "OK: $VAL"
else
failed "iniget failed: $VAL"
fi
iniset test.ini "b b" opt_ion 42
VAL=$(iniget test.ini "b b" opt_ion)
if [[ "$VAL" == "42" ]]; then
passed "OK: $VAL"
else
failed "iniget failed: $VAL"
fi
iniset ${TEST_INI} "b b" opt_ion 42
VAL=$(iniget ${TEST_INI} "b b" opt_ion)
assert_equal "$VAL" "42" "iniset with section header space"
# Test without spaces, end of file
VAL=$(iniget ${TEST_INI} bbb handlers)
assert_equal "$VAL" "ee,ff" "iniget at EOF"
VAL=$(iniget test.ini bbb handlers)
if [[ "$VAL" == "ee,ff" ]]; then
passed "OK: $VAL"
else
failed "iniget failed: $VAL"
fi
iniset test.ini bbb handlers "33,44"
VAL=$(iniget test.ini bbb handlers)
if [[ "$VAL" == "33,44" ]]; then
passed "OK: $VAL"
else
failed "iniget failed: $VAL"
fi
iniset ${TEST_INI} bbb handlers "33,44"
VAL=$(iniget ${TEST_INI} bbb handlers)
assert_equal "$VAL" "33,44" "inset at EOF"
# test empty option
if ini_has_option test.ini ddd empty; then
passed "OK: ddd.empty present"
if ini_has_option ${TEST_INI} ddd empty; then
passed "ini_has_option: ddd.empty present"
else
failed "ini_has_option failed: ddd.empty not found"
fi
# test non-empty option
if ini_has_option test.ini bbb handlers; then
passed "OK: bbb.handlers present"
if ini_has_option ${TEST_INI} bbb handlers; then
passed "ini_has_option: bbb.handlers present"
else
failed "ini_has_option failed: bbb.handlers not found"
fi
# test changing empty option
iniset test.ini ddd empty "42"
VAL=$(iniget test.ini ddd empty)
if [[ "$VAL" == "42" ]]; then
passed "OK: $VAL"
else
failed "iniget failed: $VAL"
fi
iniset ${TEST_INI} ddd empty "42"
VAL=$(iniget ${TEST_INI} ddd empty)
assert_equal "$VAL" "42" "change empty option"
# test pipe in option
iniset test.ini aaa handlers "a|b"
VAL=$(iniget test.ini aaa handlers)
if [[ "$VAL" == "a|b" ]]; then
passed "OK: $VAL"
else
failed "iniget failed: $VAL"
fi
# test space in option
iniset test.ini aaa handlers "a b"
VAL="$(iniget test.ini aaa handlers)"
if [[ "$VAL" == "a b" ]]; then
passed "OK: $VAL"
else
failed "iniget failed: $VAL"
fi
iniset ${TEST_INI} aaa handlers "a|b"
VAL=$(iniget ${TEST_INI} aaa handlers)
assert_equal "$VAL" "a|b" "pipe in option"
# Test section not exist
VAL=$(iniget test.ini zzz handlers)
if [[ -z "$VAL" ]]; then
passed "OK: zzz not present"
else
failed "iniget failed: $VAL"
fi
iniset test.ini zzz handlers "999"
VAL=$(iniget test.ini zzz handlers)
if [[ -n "$VAL" ]]; then
passed "OK: zzz not present"
else
failed "iniget failed: $VAL"
fi
VAL=$(iniget ${TEST_INI} zzz handlers)
assert_empty VAL "section does not exist"
# Test option not exist
VAL=$(iniget ${TEST_INI} aaa debug)
assert_empty VAL "option does not exist"
VAL=$(iniget test.ini aaa debug)
if [[ -z "$VAL" ]]; then
passed "OK aaa.debug not present"
else
failed "iniget failed: $VAL"
fi
if ! ini_has_option test.ini aaa debug; then
passed "OK aaa.debug not present"
if ! ini_has_option ${TEST_INI} aaa debug; then
passed "ini_has_option: aaa.debug not present"
else
failed "ini_has_option failed: aaa.debug"
fi
iniset test.ini aaa debug "999"
VAL=$(iniget test.ini aaa debug)
if [[ -n "$VAL" ]]; then
passed "OK aaa.debug present"
else
failed "iniget failed: $VAL"
fi
# Test comments
inicomment test.ini aaa handlers
VAL=$(iniget test.ini aaa handlers)
if [[ -z "$VAL" ]]; then
passed "OK"
else
failed "inicomment failed: $VAL"
fi
inicomment ${TEST_INI} aaa handlers
VAL=$(iniget ${TEST_INI} aaa handlers)
assert_empty VAL "test inicomment"
# Test multiple line iniset/iniget
iniset_multiline test.ini eee multi bar1 bar2
iniset_multiline ${TEST_INI} eee multi bar1 bar2
VAL=$(iniget_multiline test.ini eee multi)
if [[ "$VAL" == "bar1 bar2" ]]; then
echo "OK: iniset_multiline"
else
failed "iniset_multiline failed: $VAL"
fi
VAL=$(iniget_multiline ${TEST_INI} eee multi)
assert_equal "$VAL" "bar1 bar2" "iniget_multiline"
# Test iniadd with exiting values
iniadd test.ini eee multi bar3
VAL=$(iniget_multiline test.ini eee multi)
if [[ "$VAL" == "bar1 bar2 bar3" ]]; then
passed "OK: iniadd"
else
failed "iniadd failed: $VAL"
fi
iniadd ${TEST_INI} eee multi bar3
VAL=$(iniget_multiline ${TEST_INI} eee multi)
assert_equal "$VAL" "bar1 bar2 bar3" "iniadd with existing values"
# Test iniadd with non-exiting values
iniadd test.ini eee non-multi foobar1 foobar2
VAL=$(iniget_multiline test.ini eee non-multi)
if [[ "$VAL" == "foobar1 foobar2" ]]; then
passed "OK: iniadd with non-exiting value"
else
failed "iniadd with non-exsting failed: $VAL"
fi
iniadd ${TEST_INI} eee non-multi foobar1 foobar2
VAL=$(iniget_multiline ${TEST_INI} eee non-multi)
assert_equal "$VAL" "foobar1 foobar2" "iniadd non-existing values"
# Test inidelete
del_cases="
@ -276,25 +176,21 @@ del_cases="
del_no_section"
for x in $del_cases; do
inidelete test.ini $x a
VAL=$(iniget_multiline test.ini $x a)
if [ -z "$VAL" ]; then
passed "OK: inidelete $x"
else
failed "inidelete $x failed: $VAL"
fi
inidelete ${TEST_INI} $x a
VAL=$(iniget_multiline ${TEST_INI} $x a)
assert_empty VAL "inidelete $x"
if [ "$x" = "del_separate_options" -o \
"$x" = "del_missing_option" -o \
"$x" = "del_missing_option_multi" ]; then
VAL=$(iniget_multiline test.ini $x b)
VAL=$(iniget_multiline ${TEST_INI} $x b)
if [ "$VAL" = "c" -o "$VAL" = "c d" ]; then
passed "OK: inidelete other_options $x"
passed "inidelete other_options $x"
else
failed "inidelete other_option $x failed: $VAL"
failed "inidelete other_option $x: $VAL"
fi
fi
done
rm test.ini
rm -rf ${INI_TMP_DIR}
report_results

View File

@ -27,7 +27,7 @@ function passed {
msg="OK"
fi
PASS=$((PASS+1))
echo "PASS: $function:L$lineno $msg"
echo "PASS: $function:L$lineno - $msg"
}
# fail a test, printing out MSG
@ -63,6 +63,27 @@ function assert_equal {
fi
}
# assert variable is empty/blank, printing out msg
# usage: assert_empty VAR msg
function assert_empty {
local lineno=`caller 0 | awk '{print $1}'`
local function=`caller 0 | awk '{print $2}'`
local msg=$2
if [ -z "$msg" ]; then
msg="OK"
fi
if [[ ! -z ${!1} ]]; then
FAILED_FUNCS+="$function:L$lineno\n"
echo "ERROR: $1 not empty in $function:L$lineno!"
echo " $msg"
ERROR=$((ERROR+1))
else
PASS=$((PASS+1))
echo "PASS: $function:L$lineno - $msg"
fi
}
# print a summary of passing and failing tests, exiting
# with an error if we have failed tests
# usage: report_results