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

View File

@ -27,7 +27,7 @@ function passed {
msg="OK" msg="OK"
fi fi
PASS=$((PASS+1)) PASS=$((PASS+1))
echo "PASS: $function:L$lineno $msg" echo "PASS: $function:L$lineno - $msg"
} }
# fail a test, printing out MSG # fail a test, printing out MSG
@ -63,6 +63,27 @@ function assert_equal {
fi 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 # print a summary of passing and failing tests, exiting
# with an error if we have failed tests # with an error if we have failed tests
# usage: report_results # usage: report_results