Fix a couple of INI whitespace bugs
* iniset() bails if no section or option (attribute) is supplied * merge_config_file() properly skips lines with only whitespace * Also split the ini-tests into their own script Bug 1257954 Change-Id: Ie31c5bd0df8dfed129fbcf1e37228aaf25e9305d
This commit is contained in:
parent
df8410c387
commit
2ac8b3f3c2
@ -729,6 +729,8 @@ function iniset() {
|
||||
local option=$3
|
||||
local value=$4
|
||||
|
||||
[[ -z $section || -z $option ]] && return
|
||||
|
||||
if ! grep -q "^\[$section\]" "$file" 2>/dev/null; then
|
||||
# Add section at the end
|
||||
echo -e "\n[$section]" >>"$file"
|
||||
|
@ -95,7 +95,7 @@ function merge_config_file() {
|
||||
/^ *\#/ {
|
||||
next
|
||||
}
|
||||
/^.+/ {
|
||||
/^[^ \t]+/ {
|
||||
split($0, d, " *= *")
|
||||
print "iniset " configfile " " section " " d[1] " \"" d[2] "\""
|
||||
}
|
||||
|
@ -38,195 +38,6 @@ if [[ $? = 0 ]]; then
|
||||
fi
|
||||
|
||||
|
||||
echo "Testing INI functions"
|
||||
|
||||
cat >test.ini <<EOF
|
||||
[default]
|
||||
# comment an option
|
||||
#log_file=./log.conf
|
||||
log_file=/etc/log.conf
|
||||
handlers=do not disturb
|
||||
|
||||
[aaa]
|
||||
# the commented option should not change
|
||||
#handlers=cc,dd
|
||||
handlers = aa, bb
|
||||
|
||||
[bbb]
|
||||
handlers=ee,ff
|
||||
|
||||
[ ccc ]
|
||||
spaces = yes
|
||||
|
||||
[ddd]
|
||||
empty =
|
||||
|
||||
[eee]
|
||||
multi = foo1
|
||||
multi = foo2
|
||||
EOF
|
||||
|
||||
# Test with spaces
|
||||
|
||||
VAL=$(iniget test.ini aaa handlers)
|
||||
if [[ "$VAL" == "aa, bb" ]]; then
|
||||
echo "OK: $VAL"
|
||||
else
|
||||
echo "iniget failed: $VAL"
|
||||
fi
|
||||
|
||||
iniset test.ini aaa handlers "11, 22"
|
||||
|
||||
VAL=$(iniget test.ini aaa handlers)
|
||||
if [[ "$VAL" == "11, 22" ]]; then
|
||||
echo "OK: $VAL"
|
||||
else
|
||||
echo "iniget failed: $VAL"
|
||||
fi
|
||||
|
||||
# Test with spaces in section header
|
||||
|
||||
VAL=$(iniget test.ini " ccc " spaces)
|
||||
if [[ "$VAL" == "yes" ]]; then
|
||||
echo "OK: $VAL"
|
||||
else
|
||||
echo "iniget failed: $VAL"
|
||||
fi
|
||||
|
||||
iniset test.ini "b b" opt_ion 42
|
||||
|
||||
VAL=$(iniget test.ini "b b" opt_ion)
|
||||
if [[ "$VAL" == "42" ]]; then
|
||||
echo "OK: $VAL"
|
||||
else
|
||||
echo "iniget failed: $VAL"
|
||||
fi
|
||||
|
||||
# Test without spaces, end of file
|
||||
|
||||
VAL=$(iniget test.ini bbb handlers)
|
||||
if [[ "$VAL" == "ee,ff" ]]; then
|
||||
echo "OK: $VAL"
|
||||
else
|
||||
echo "iniget failed: $VAL"
|
||||
fi
|
||||
|
||||
iniset test.ini bbb handlers "33,44"
|
||||
|
||||
VAL=$(iniget test.ini bbb handlers)
|
||||
if [[ "$VAL" == "33,44" ]]; then
|
||||
echo "OK: $VAL"
|
||||
else
|
||||
echo "iniget failed: $VAL"
|
||||
fi
|
||||
|
||||
# test empty option
|
||||
if ini_has_option test.ini ddd empty; then
|
||||
echo "OK: ddd.empty present"
|
||||
else
|
||||
echo "ini_has_option failed: ddd.empty not found"
|
||||
fi
|
||||
|
||||
# test non-empty option
|
||||
if ini_has_option test.ini bbb handlers; then
|
||||
echo "OK: bbb.handlers present"
|
||||
else
|
||||
echo "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
|
||||
echo "OK: $VAL"
|
||||
else
|
||||
echo "iniget failed: $VAL"
|
||||
fi
|
||||
|
||||
# Test section not exist
|
||||
|
||||
VAL=$(iniget test.ini zzz handlers)
|
||||
if [[ -z "$VAL" ]]; then
|
||||
echo "OK: zzz not present"
|
||||
else
|
||||
echo "iniget failed: $VAL"
|
||||
fi
|
||||
|
||||
iniset test.ini zzz handlers "999"
|
||||
|
||||
VAL=$(iniget test.ini zzz handlers)
|
||||
if [[ -n "$VAL" ]]; then
|
||||
echo "OK: zzz not present"
|
||||
else
|
||||
echo "iniget failed: $VAL"
|
||||
fi
|
||||
|
||||
# Test option not exist
|
||||
|
||||
VAL=$(iniget test.ini aaa debug)
|
||||
if [[ -z "$VAL" ]]; then
|
||||
echo "OK aaa.debug not present"
|
||||
else
|
||||
echo "iniget failed: $VAL"
|
||||
fi
|
||||
|
||||
if ! ini_has_option test.ini aaa debug; then
|
||||
echo "OK aaa.debug not present"
|
||||
else
|
||||
echo "ini_has_option failed: aaa.debug"
|
||||
fi
|
||||
|
||||
iniset test.ini aaa debug "999"
|
||||
|
||||
VAL=$(iniget test.ini aaa debug)
|
||||
if [[ -n "$VAL" ]]; then
|
||||
echo "OK aaa.debug present"
|
||||
else
|
||||
echo "iniget failed: $VAL"
|
||||
fi
|
||||
|
||||
# Test comments
|
||||
|
||||
inicomment test.ini aaa handlers
|
||||
|
||||
VAL=$(iniget test.ini aaa handlers)
|
||||
if [[ -z "$VAL" ]]; then
|
||||
echo "OK"
|
||||
else
|
||||
echo "inicomment failed: $VAL"
|
||||
fi
|
||||
|
||||
# Test multiple line iniset/iniget
|
||||
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
|
||||
echo "iniset_multiline failed: $VAL"
|
||||
fi
|
||||
|
||||
# Test iniadd with exiting values
|
||||
iniadd test.ini eee multi bar3
|
||||
VAL=$(iniget_multiline test.ini eee multi)
|
||||
if [[ "$VAL" == "bar1 bar2 bar3" ]]; then
|
||||
echo "OK: iniadd"
|
||||
else
|
||||
echo "iniadd failed: $VAL"
|
||||
fi
|
||||
|
||||
# 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
|
||||
echo "OK: iniadd with non-exiting value"
|
||||
else
|
||||
echo "iniadd with non-exsting failed: $VAL"
|
||||
fi
|
||||
|
||||
rm test.ini
|
||||
|
||||
# Enabling/disabling services
|
||||
|
||||
echo "Testing enable_service()"
|
||||
|
@ -70,6 +70,12 @@ additional=true
|
||||
|
||||
[[test1|test1c.conf]]
|
||||
$TEST_1C_ADD
|
||||
|
||||
[[test3|test-space.conf]]
|
||||
[DEFAULT]
|
||||
attribute=value
|
||||
|
||||
# the above line has a single space
|
||||
EOF
|
||||
|
||||
|
||||
@ -176,4 +182,14 @@ else
|
||||
echo "failed: $VAL != $EXPECT_VAL"
|
||||
fi
|
||||
|
||||
rm -f test.conf test1c.conf test2a.conf
|
||||
echo -n "merge_config_file test-space: "
|
||||
rm -f test-space.conf
|
||||
merge_config_file test.conf test3 test-space.conf
|
||||
VAL=$(cat test-space.conf)
|
||||
# iniset adds a blank line if it creates the file...
|
||||
EXPECT_VAL="
|
||||
[DEFAULT]
|
||||
attribute = value"
|
||||
check_result "$VAL" "$EXPECT_VAL"
|
||||
|
||||
rm -f test.conf test1c.conf test2a.conf test-space.conf
|
||||
|
220
tests/test_ini.sh
Executable file
220
tests/test_ini.sh
Executable file
@ -0,0 +1,220 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
# Tests for DevStack INI functions
|
||||
|
||||
TOP=$(cd $(dirname "$0")/.. && pwd)
|
||||
|
||||
# Import common functions
|
||||
source $TOP/functions
|
||||
|
||||
|
||||
echo "Testing INI functions"
|
||||
|
||||
cat >test.ini <<EOF
|
||||
[default]
|
||||
# comment an option
|
||||
#log_file=./log.conf
|
||||
log_file=/etc/log.conf
|
||||
handlers=do not disturb
|
||||
|
||||
[aaa]
|
||||
# the commented option should not change
|
||||
#handlers=cc,dd
|
||||
handlers = aa, bb
|
||||
|
||||
[bbb]
|
||||
handlers=ee,ff
|
||||
|
||||
[ ccc ]
|
||||
spaces = yes
|
||||
|
||||
[ddd]
|
||||
empty =
|
||||
|
||||
[eee]
|
||||
multi = foo1
|
||||
multi = foo2
|
||||
EOF
|
||||
|
||||
# Test with missing arguments
|
||||
|
||||
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
|
||||
echo "OK"
|
||||
else
|
||||
echo "failed"
|
||||
fi
|
||||
|
||||
echo -n "iniset: test missing section argument: "
|
||||
iniset test.ini
|
||||
NO_SECTION=$(cat test.ini)
|
||||
if [[ "$BEFORE" == "$NO_SECTION" ]]; then
|
||||
echo "OK"
|
||||
else
|
||||
echo "failed"
|
||||
fi
|
||||
|
||||
# Test with spaces
|
||||
|
||||
VAL=$(iniget test.ini aaa handlers)
|
||||
if [[ "$VAL" == "aa, bb" ]]; then
|
||||
echo "OK: $VAL"
|
||||
else
|
||||
echo "iniget failed: $VAL"
|
||||
fi
|
||||
|
||||
iniset test.ini aaa handlers "11, 22"
|
||||
|
||||
VAL=$(iniget test.ini aaa handlers)
|
||||
if [[ "$VAL" == "11, 22" ]]; then
|
||||
echo "OK: $VAL"
|
||||
else
|
||||
echo "iniget failed: $VAL"
|
||||
fi
|
||||
|
||||
# Test with spaces in section header
|
||||
|
||||
VAL=$(iniget test.ini " ccc " spaces)
|
||||
if [[ "$VAL" == "yes" ]]; then
|
||||
echo "OK: $VAL"
|
||||
else
|
||||
echo "iniget failed: $VAL"
|
||||
fi
|
||||
|
||||
iniset test.ini "b b" opt_ion 42
|
||||
|
||||
VAL=$(iniget test.ini "b b" opt_ion)
|
||||
if [[ "$VAL" == "42" ]]; then
|
||||
echo "OK: $VAL"
|
||||
else
|
||||
echo "iniget failed: $VAL"
|
||||
fi
|
||||
|
||||
# Test without spaces, end of file
|
||||
|
||||
VAL=$(iniget test.ini bbb handlers)
|
||||
if [[ "$VAL" == "ee,ff" ]]; then
|
||||
echo "OK: $VAL"
|
||||
else
|
||||
echo "iniget failed: $VAL"
|
||||
fi
|
||||
|
||||
iniset test.ini bbb handlers "33,44"
|
||||
|
||||
VAL=$(iniget test.ini bbb handlers)
|
||||
if [[ "$VAL" == "33,44" ]]; then
|
||||
echo "OK: $VAL"
|
||||
else
|
||||
echo "iniget failed: $VAL"
|
||||
fi
|
||||
|
||||
# test empty option
|
||||
if ini_has_option test.ini ddd empty; then
|
||||
echo "OK: ddd.empty present"
|
||||
else
|
||||
echo "ini_has_option failed: ddd.empty not found"
|
||||
fi
|
||||
|
||||
# test non-empty option
|
||||
if ini_has_option test.ini bbb handlers; then
|
||||
echo "OK: bbb.handlers present"
|
||||
else
|
||||
echo "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
|
||||
echo "OK: $VAL"
|
||||
else
|
||||
echo "iniget failed: $VAL"
|
||||
fi
|
||||
|
||||
# Test section not exist
|
||||
|
||||
VAL=$(iniget test.ini zzz handlers)
|
||||
if [[ -z "$VAL" ]]; then
|
||||
echo "OK: zzz not present"
|
||||
else
|
||||
echo "iniget failed: $VAL"
|
||||
fi
|
||||
|
||||
iniset test.ini zzz handlers "999"
|
||||
|
||||
VAL=$(iniget test.ini zzz handlers)
|
||||
if [[ -n "$VAL" ]]; then
|
||||
echo "OK: zzz not present"
|
||||
else
|
||||
echo "iniget failed: $VAL"
|
||||
fi
|
||||
|
||||
# Test option not exist
|
||||
|
||||
VAL=$(iniget test.ini aaa debug)
|
||||
if [[ -z "$VAL" ]]; then
|
||||
echo "OK aaa.debug not present"
|
||||
else
|
||||
echo "iniget failed: $VAL"
|
||||
fi
|
||||
|
||||
if ! ini_has_option test.ini aaa debug; then
|
||||
echo "OK aaa.debug not present"
|
||||
else
|
||||
echo "ini_has_option failed: aaa.debug"
|
||||
fi
|
||||
|
||||
iniset test.ini aaa debug "999"
|
||||
|
||||
VAL=$(iniget test.ini aaa debug)
|
||||
if [[ -n "$VAL" ]]; then
|
||||
echo "OK aaa.debug present"
|
||||
else
|
||||
echo "iniget failed: $VAL"
|
||||
fi
|
||||
|
||||
# Test comments
|
||||
|
||||
inicomment test.ini aaa handlers
|
||||
|
||||
VAL=$(iniget test.ini aaa handlers)
|
||||
if [[ -z "$VAL" ]]; then
|
||||
echo "OK"
|
||||
else
|
||||
echo "inicomment failed: $VAL"
|
||||
fi
|
||||
|
||||
# Test multiple line iniset/iniget
|
||||
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
|
||||
echo "iniset_multiline failed: $VAL"
|
||||
fi
|
||||
|
||||
# Test iniadd with exiting values
|
||||
iniadd test.ini eee multi bar3
|
||||
VAL=$(iniget_multiline test.ini eee multi)
|
||||
if [[ "$VAL" == "bar1 bar2 bar3" ]]; then
|
||||
echo "OK: iniadd"
|
||||
else
|
||||
echo "iniadd failed: $VAL"
|
||||
fi
|
||||
|
||||
# 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
|
||||
echo "OK: iniadd with non-exiting value"
|
||||
else
|
||||
echo "iniadd with non-exsting failed: $VAL"
|
||||
fi
|
||||
|
||||
rm test.ini
|
Loading…
Reference in New Issue
Block a user