Browse code

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

Ian Wienand authored on 2015/07/22 09:05:32
Showing 2 changed files
... ...
@@ -13,7 +13,13 @@ set -e
13 13
 
14 14
 echo "Testing INI functions"
15 15
 
16
-cat >test.ini <<EOF
16
+INI_TMP_DIR=$(mktemp -d)
17
+INI_TMP_ETC_DIR=$INI_TMP_DIR/etc
18
+TEST_INI=${INI_TMP_ETC_DIR}/test.ini
19
+mkdir ${INI_TMP_ETC_DIR}
20
+
21
+echo "Creating $TEST_INI"
22
+cat >${TEST_INI} <<EOF
17 23
 [default]
18 24
 # comment an option
19 25
 #log_file=./log.conf
... ...
@@ -67,204 +73,98 @@ EOF
67 67
 
68 68
 # Test with missing arguments
69 69
 
70
-BEFORE=$(cat test.ini)
71
-
72
-echo -n "iniset: test missing attribute argument: "
73
-iniset test.ini aaa
74
-NO_ATTRIBUTE=$(cat test.ini)
75
-if [[ "$BEFORE" == "$NO_ATTRIBUTE" ]]; then
76
-    passed
77
-else
78
-    failed "failed"
79
-fi
70
+BEFORE=$(cat ${TEST_INI})
80 71
 
81
-echo -n "iniset: test missing section argument: "
82
-iniset test.ini
83
-NO_SECTION=$(cat test.ini)
84
-if [[ "$BEFORE" == "$NO_SECTION" ]]; then
85
-    passed
86
-else
87
-    failed "failed"
88
-fi
72
+iniset ${TEST_INI} aaa
73
+NO_ATTRIBUTE=$(cat ${TEST_INI})
74
+assert_equal "$BEFORE" "$NO_ATTRIBUTE" "test missing attribute argument"
89 75
 
90
-# Test with spaces
76
+iniset ${TEST_INI}
77
+NO_SECTION=$(cat ${TEST_INI})
78
+assert_equal "$BEFORE" "$NO_SECTION" "missing section argument"
91 79
 
92
-VAL=$(iniget test.ini aaa handlers)
93
-if [[ "$VAL" == "aa, bb" ]]; then
94
-    passed "OK: $VAL"
95
-else
96
-    failed "iniget failed: $VAL"
97
-fi
80
+# Test with spaces in values
81
+VAL=$(iniget ${TEST_INI} aaa handlers)
82
+assert_equal "$VAL" "aa, bb" "iniget spaces in option"
98 83
 
99
-iniset test.ini aaa handlers "11, 22"
100
-
101
-VAL=$(iniget test.ini aaa handlers)
102
-if [[ "$VAL" == "11, 22" ]]; then
103
-    passed "OK: $VAL"
104
-else
105
-    failed "iniget failed: $VAL"
106
-fi
84
+iniset ${TEST_INI} aaa handlers "11, 22"
85
+VAL=$(iniget ${TEST_INI} aaa handlers)
86
+assert_equal "$VAL" "11, 22" "iniset spaces in option"
107 87
 
108 88
 # Test with spaces in section header
89
+VAL=$(iniget ${TEST_INI} " ccc " spaces)
90
+assert_equal "$VAL" "yes" "iniget with section header space"
109 91
 
110
-VAL=$(iniget test.ini " ccc " spaces)
111
-if [[ "$VAL" == "yes" ]]; then
112
-    passed "OK: $VAL"
113
-else
114
-    failed "iniget failed: $VAL"
115
-fi
116
-
117
-iniset test.ini "b b" opt_ion 42
118
-
119
-VAL=$(iniget test.ini "b b" opt_ion)
120
-if [[ "$VAL" == "42" ]]; then
121
-    passed "OK: $VAL"
122
-else
123
-    failed "iniget failed: $VAL"
124
-fi
92
+iniset ${TEST_INI} "b b" opt_ion 42
93
+VAL=$(iniget ${TEST_INI} "b b" opt_ion)
94
+assert_equal "$VAL" "42" "iniset with section header space"
125 95
 
126 96
 # Test without spaces, end of file
97
+VAL=$(iniget ${TEST_INI} bbb handlers)
98
+assert_equal "$VAL" "ee,ff" "iniget at EOF"
127 99
 
128
-VAL=$(iniget test.ini bbb handlers)
129
-if [[ "$VAL" == "ee,ff" ]]; then
130
-    passed "OK: $VAL"
131
-else
132
-    failed "iniget failed: $VAL"
133
-fi
134
-
135
-iniset test.ini bbb handlers "33,44"
136
-
137
-VAL=$(iniget test.ini bbb handlers)
138
-if [[ "$VAL" == "33,44" ]]; then
139
-    passed "OK: $VAL"
140
-else
141
-    failed "iniget failed: $VAL"
142
-fi
100
+iniset ${TEST_INI} bbb handlers "33,44"
101
+VAL=$(iniget ${TEST_INI} bbb handlers)
102
+assert_equal "$VAL" "33,44" "inset at EOF"
143 103
 
144 104
 # test empty option
145
-if ini_has_option test.ini ddd empty; then
146
-    passed "OK: ddd.empty present"
105
+if ini_has_option ${TEST_INI} ddd empty; then
106
+    passed "ini_has_option: ddd.empty present"
147 107
 else
148 108
     failed "ini_has_option failed: ddd.empty not found"
149 109
 fi
150 110
 
151 111
 # test non-empty option
152
-if ini_has_option test.ini bbb handlers; then
153
-    passed "OK: bbb.handlers present"
112
+if ini_has_option ${TEST_INI} bbb handlers; then
113
+    passed "ini_has_option: bbb.handlers present"
154 114
 else
155 115
     failed "ini_has_option failed: bbb.handlers not found"
156 116
 fi
157 117
 
158 118
 # test changing empty option
159
-iniset test.ini ddd empty "42"
160
-
161
-VAL=$(iniget test.ini ddd empty)
162
-if [[ "$VAL" == "42" ]]; then
163
-    passed "OK: $VAL"
164
-else
165
-    failed "iniget failed: $VAL"
166
-fi
119
+iniset ${TEST_INI} ddd empty "42"
120
+VAL=$(iniget ${TEST_INI} ddd empty)
121
+assert_equal "$VAL" "42" "change empty option"
167 122
 
168 123
 # test pipe in option
169
-iniset test.ini aaa handlers "a|b"
170
-
171
-VAL=$(iniget test.ini aaa handlers)
172
-if [[ "$VAL" == "a|b" ]]; then
173
-    passed "OK: $VAL"
174
-else
175
-    failed "iniget failed: $VAL"
176
-fi
177
-
178
-# test space in option
179
-iniset test.ini aaa handlers "a b"
180
-
181
-VAL="$(iniget test.ini aaa handlers)"
182
-if [[ "$VAL" == "a b" ]]; then
183
-    passed "OK: $VAL"
184
-else
185
-    failed "iniget failed: $VAL"
186
-fi
124
+iniset ${TEST_INI} aaa handlers "a|b"
125
+VAL=$(iniget ${TEST_INI} aaa handlers)
126
+assert_equal "$VAL" "a|b" "pipe in option"
187 127
 
188 128
 # Test section not exist
189
-
190
-VAL=$(iniget test.ini zzz handlers)
191
-if [[ -z "$VAL" ]]; then
192
-    passed "OK: zzz not present"
193
-else
194
-    failed "iniget failed: $VAL"
195
-fi
196
-
197
-iniset test.ini zzz handlers "999"
198
-
199
-VAL=$(iniget test.ini zzz handlers)
200
-if [[ -n "$VAL" ]]; then
201
-    passed "OK: zzz not present"
202
-else
203
-    failed "iniget failed: $VAL"
204
-fi
129
+VAL=$(iniget ${TEST_INI} zzz handlers)
130
+assert_empty VAL "section does not exist"
205 131
 
206 132
 # Test option not exist
133
+VAL=$(iniget ${TEST_INI} aaa debug)
134
+assert_empty VAL "option does not exist"
207 135
 
208
-VAL=$(iniget test.ini aaa debug)
209
-if [[ -z "$VAL" ]]; then
210
-    passed "OK aaa.debug not present"
211
-else
212
-    failed "iniget failed: $VAL"
213
-fi
214
-
215
-if ! ini_has_option test.ini aaa debug; then
216
-    passed "OK aaa.debug not present"
136
+if ! ini_has_option ${TEST_INI} aaa debug; then
137
+    passed "ini_has_option: aaa.debug not present"
217 138
 else
218 139
     failed "ini_has_option failed: aaa.debug"
219 140
 fi
220 141
 
221
-iniset test.ini aaa debug "999"
222
-
223
-VAL=$(iniget test.ini aaa debug)
224
-if [[ -n "$VAL" ]]; then
225
-    passed "OK aaa.debug present"
226
-else
227
-    failed "iniget failed: $VAL"
228
-fi
229
-
230 142
 # Test comments
231
-
232
-inicomment test.ini aaa handlers
233
-
234
-VAL=$(iniget test.ini aaa handlers)
235
-if [[ -z "$VAL" ]]; then
236
-    passed "OK"
237
-else
238
-    failed "inicomment failed: $VAL"
239
-fi
143
+inicomment ${TEST_INI} aaa handlers
144
+VAL=$(iniget ${TEST_INI} aaa handlers)
145
+assert_empty VAL "test inicomment"
240 146
 
241 147
 # Test multiple line iniset/iniget
242
-iniset_multiline test.ini eee multi bar1 bar2
148
+iniset_multiline ${TEST_INI} eee multi bar1 bar2
243 149
 
244
-VAL=$(iniget_multiline test.ini eee multi)
245
-if [[ "$VAL" == "bar1 bar2" ]]; then
246
-    echo "OK: iniset_multiline"
247
-else
248
-    failed "iniset_multiline failed: $VAL"
249
-fi
150
+VAL=$(iniget_multiline ${TEST_INI} eee multi)
151
+assert_equal "$VAL" "bar1 bar2" "iniget_multiline"
250 152
 
251 153
 # Test iniadd with exiting values
252
-iniadd test.ini eee multi bar3
253
-VAL=$(iniget_multiline test.ini eee multi)
254
-if [[ "$VAL" == "bar1 bar2 bar3" ]]; then
255
-    passed "OK: iniadd"
256
-else
257
-    failed "iniadd failed: $VAL"
258
-fi
154
+iniadd ${TEST_INI} eee multi bar3
155
+VAL=$(iniget_multiline ${TEST_INI} eee multi)
156
+assert_equal "$VAL" "bar1 bar2 bar3" "iniadd with existing values"
259 157
 
260 158
 # Test iniadd with non-exiting values
261
-iniadd test.ini eee non-multi foobar1 foobar2
262
-VAL=$(iniget_multiline test.ini eee non-multi)
263
-if [[ "$VAL" == "foobar1 foobar2" ]]; then
264
-    passed "OK: iniadd with non-exiting value"
265
-else
266
-    failed "iniadd with non-exsting failed: $VAL"
267
-fi
159
+iniadd ${TEST_INI} eee non-multi foobar1 foobar2
160
+VAL=$(iniget_multiline ${TEST_INI} eee non-multi)
161
+assert_equal "$VAL" "foobar1 foobar2" "iniadd non-existing values"
268 162
 
269 163
 # Test inidelete
270 164
 del_cases="
... ...
@@ -276,25 +176,21 @@ del_cases="
276 276
     del_no_section"
277 277
 
278 278
 for x in $del_cases; do
279
-    inidelete test.ini $x a
280
-    VAL=$(iniget_multiline test.ini $x a)
281
-    if [ -z "$VAL" ]; then
282
-        passed "OK: inidelete $x"
283
-    else
284
-        failed "inidelete $x failed: $VAL"
285
-    fi
279
+    inidelete ${TEST_INI} $x a
280
+    VAL=$(iniget_multiline ${TEST_INI} $x a)
281
+    assert_empty VAL "inidelete $x"
286 282
     if [ "$x" = "del_separate_options" -o \
287 283
         "$x" = "del_missing_option" -o \
288 284
         "$x" = "del_missing_option_multi" ]; then
289
-        VAL=$(iniget_multiline test.ini $x b)
285
+        VAL=$(iniget_multiline ${TEST_INI} $x b)
290 286
         if [ "$VAL" = "c" -o "$VAL" = "c d" ]; then
291
-            passed "OK: inidelete other_options $x"
287
+            passed "inidelete other_options $x"
292 288
         else
293
-            failed "inidelete other_option $x failed: $VAL"
289
+            failed "inidelete other_option $x: $VAL"
294 290
         fi
295 291
     fi
296 292
 done
297 293
 
298
-rm test.ini
294
+rm -rf ${INI_TMP_DIR}
299 295
 
300 296
 report_results
... ...
@@ -27,7 +27,7 @@ function passed {
27 27
         msg="OK"
28 28
     fi
29 29
     PASS=$((PASS+1))
30
-    echo "PASS: $function:L$lineno $msg"
30
+    echo "PASS: $function:L$lineno - $msg"
31 31
 }
32 32
 
33 33
 # fail a test, printing out MSG
... ...
@@ -63,6 +63,27 @@ function assert_equal {
63 63
     fi
64 64
 }
65 65
 
66
+# assert variable is empty/blank, printing out msg
67
+#  usage: assert_empty VAR msg
68
+function assert_empty {
69
+    local lineno=`caller 0 | awk '{print $1}'`
70
+    local function=`caller 0 | awk '{print $2}'`
71
+    local msg=$2
72
+
73
+    if [ -z "$msg" ]; then
74
+        msg="OK"
75
+    fi
76
+    if [[ ! -z ${!1} ]]; then
77
+        FAILED_FUNCS+="$function:L$lineno\n"
78
+        echo "ERROR: $1 not empty in $function:L$lineno!"
79
+        echo "  $msg"
80
+        ERROR=$((ERROR+1))
81
+    else
82
+        PASS=$((PASS+1))
83
+        echo "PASS: $function:L$lineno - $msg"
84
+    fi
85
+}
86
+
66 87
 # print a summary of passing and failing tests, exiting
67 88
 # with an error if we have failed tests
68 89
 #  usage: report_results