forked from hercules-team/augeas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_shellvars.aug
400 lines (332 loc) · 9.53 KB
/
test_shellvars.aug
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
(* Test for shell lens *)
module Test_shellvars =
let eth_static = "# Intel Corporation PRO/100 VE Network Connection
DEVICE=eth0
BOOTPROTO=static
BROADCAST=172.31.0.255
HWADDR=ab:cd:ef:12:34:56
export IPADDR=172.31.0.31 # this is our IP
#DHCP_HOSTNAME=host.example.com
NETMASK=255.255.255.0
NETWORK=172.31.0.0
unset ONBOOT # We do not want this var
"
let empty_val = "EMPTY=\nDEVICE=eth0\n"
let key_brack = "SOME_KEY[1]=\nDEVICE=eth0\n"
test Shellvars.lns get eth_static =
{ "#comment" = "Intel Corporation PRO/100 VE Network Connection" }
{ "DEVICE" = "eth0" }
{ "BOOTPROTO" = "static" }
{ "BROADCAST" = "172.31.0.255" }
{ "HWADDR" = "ab:cd:ef:12:34:56" }
{ "IPADDR" = "172.31.0.31"
{ "export" }
{ "#comment" = "this is our IP" } }
{ "#comment" = "DHCP_HOSTNAME=host.example.com" }
{ "NETMASK" = "255.255.255.0" }
{ "NETWORK" = "172.31.0.0" }
{ "@unset" = "ONBOOT"
{ "#comment" = "We do not want this var" } }
test Shellvars.lns put eth_static after
set "BOOTPROTO" "dhcp" ;
rm "IPADDR" ;
rm "BROADCAST" ;
rm "NETMASK" ;
rm "NETWORK"
= "# Intel Corporation PRO/100 VE Network Connection
DEVICE=eth0
BOOTPROTO=dhcp
HWADDR=ab:cd:ef:12:34:56
#DHCP_HOSTNAME=host.example.com
unset ONBOOT # We do not want this var
"
test Shellvars.lns get empty_val =
{ "EMPTY" = "" } { "DEVICE" = "eth0" }
test Shellvars.lns get key_brack =
{ "SOME_KEY[1]" = "" } { "DEVICE" = "eth0" }
test Shellvars.lns get "smartd_opts=\"-q never\"\n" =
{ "smartd_opts" = "\"-q never\"" }
test Shellvars.lns get "var=val \n" = { "var" = "val" }
test Shellvars.lns get ". /etc/java/java.conf\n" =
{ ".source" = "/etc/java/java.conf" }
(* Quoted strings and other oddities *)
test Shellvars.lns get "var=\"foo 'bar'\"\n" =
{ "var" = "\"foo 'bar'\"" }
test Shellvars.lns get "var='Some \"funny\" value'\n" =
{ "var" = "'Some \"funny\" value'" }
test Shellvars.lns get "var=\"\\\"\"\n" =
{ "var" = "\"\\\"\"" }
test Shellvars.lns get "var=\\\"\n" =
{ "var" = "\\\"" }
test Shellvars.lns get "var=ab#c\n" =
{ "var" = "ab#c" }
test Shellvars.lns get "var=ab #c\n" =
{ "var" = "ab"
{ "#comment" = "c" } }
test Shellvars.lns get "var='ab#c'\n" =
{ "var" = "'ab#c'" }
test Shellvars.lns get "var=\"ab#c\"\n" =
{ "var" = "\"ab#c\"" }
test Shellvars.lns get "ESSID='Joe'\"'\"'s net'\n" =
{ "ESSID" = "'Joe'\"'\"'s net'" }
test Shellvars.lns get "var=`ab#c`\n" =
{ "var" = "`ab#c`" }
test Shellvars.lns get "var=`grep nameserver /etc/resolv.conf | head -1`\n" =
{ "var" = "`grep nameserver /etc/resolv.conf | head -1`" }
test Shellvars.lns put "var=ab #c\n"
after rm "/var/#comment" = "var=ab\n"
test Shellvars.lns put "var=ab\n"
after set "/var/#comment" "this is a var" =
"var=ab # this is a var\n"
(* Handling of arrays *)
test Shellvars.lns get "var=(val1 \"val\\\"2\\\"\" val3)\n" =
{ "var"
{ "1" = "val1" }
{ "2" = "\"val\\\"2\\\"\"" }
{ "3" = "val3" } }
test Shellvars.lns get "var=()\n" = { "var" = "()" }
test Shellvars.lns put "var=()\n" after
set "var" "value"
= "var=value\n"
test Shellvars.lns put "var=(v1 v2)\n" after
rm "var/*" ;
set "var" "value"
= "var=value\n"
test Shellvars.lns put "var=(v1 v2)\n" after
set "var/3" "v3"
= "var=(v1 v2 v3)\n"
test Shellvars.lns get "var=(v1 v2 \n \t v3)\n" =
{ "var"
{ "1" = "v1" }
{ "2" = "v2" }
{ "3" = "v3" } }
(* Allow spaces after/before opening/closing parens for array *)
test Shellvars.lns get "config_eth1=( \"10.128.0.48/24\" )\n" =
{ "config_eth1" { "1" = "\"10.128.0.48/24\"" } }
(* Bug 109: allow a bare export *)
test Shellvars.lns get "export FOO\n" =
{ "@export" = "FOO" }
(* Bug 73: allow ulimit builtin *)
test Shellvars.lns get "ulimit -c unlimited\n" =
{ "@builtin" = "ulimit" { "args" = "-c unlimited" } }
(* Allow shift builtin *)
test Shellvars.lns get "shift\nshift 2\n" =
{ "@builtin" = "shift" }
{ "@builtin" = "shift" { "args" = "2" } }
(* Allow exit builtin *)
test Shellvars.lns get "exit\nexit 2\n" =
{ "@builtin" = "exit" }
{ "@builtin" = "exit" { "args" = "2" } }
(* Test semicolons *)
test Shellvars.lns get "VAR1=\"this;is;a;test\"\nVAR2=this;\n" =
{ "VAR1" = "\"this;is;a;test\"" }
{ "VAR2" = "this" }
(* Bug 230: parse conditions *)
test Shellvars.lns get "if [ -f /etc/default/keyboard ]; then\n. /etc/default/keyboard\nfi\n" =
{ "@if" = "[ -f /etc/default/keyboard ]" { ".source" = "/etc/default/keyboard" } }
(* Recursive condition *)
test Shellvars.lns get "if [ -f /tmp/file1 ]; then
if [ -f /tmp/file2 ]
then
. /tmp/file2
elif [ -f /tmp/file3 ]; then
. /tmp/file3; else; . /tmp/file4
fi
else
. /tmp/file3
fi\n" =
{ "@if" = "[ -f /tmp/file1 ]"
{ "@if" = "[ -f /tmp/file2 ]"
{ ".source" = "/tmp/file2" }
{ "@elif" = "[ -f /tmp/file3 ]"
{ ".source" = "/tmp/file3" } }
{ "@else"
{ ".source" = "/tmp/file4" }
}
}
{ "@else"
{ ".source" = "/tmp/file3" }
}
}
(* Multiple elif *)
test Shellvars.lns get "if [ -f /tmp/file1 ]; then
. /tmp/file1
elif [ -f /tmp/file2 ]; then
. /tmp/file2
elif [ -f /tmp/file3 ]; then
. /tmp/file3
fi\n" =
{ "@if" = "[ -f /tmp/file1 ]"
{ ".source" = "/tmp/file1" }
{ "@elif" = "[ -f /tmp/file2 ]"
{ ".source" = "/tmp/file2" }
}
{ "@elif" = "[ -f /tmp/file3 ]"
{ ".source" = "/tmp/file3" }
}
}
(* Comment or eol *)
test Shellvars.lns get "VAR=value # eol-comment\n" =
{ "VAR" = "value"
{ "#comment" = "eol-comment" }
}
(* One-liners *)
test Shellvars.lns get "if [ -f /tmp/file1 ]; then . /tmp/file1; else . /tmp/file2; fi\n" =
{ "@if" = "[ -f /tmp/file1 ]"
{ ".source" = "/tmp/file1" }
{ "@else"
{ ".source" = "/tmp/file2" }
}
}
(* Loops *)
test Shellvars.lns get "for f in /tmp/file*; do
while [ 1 ]; do . $f; done
done\n" =
{ "@for" = "f in /tmp/file*"
{ "@while" = "[ 1 ]"
{ ".source" = "$f" }
}
}
(* Case *)
test Shellvars.lns get "case $f in
/tmp/file1)
. /tmp/file1
;;
/tmp/file2)
. /tmp/file2
;;
*)
unset f
;;
esac\n" =
{ "@case" = "$f"
{ "@case_entry" = "/tmp/file1"
{ ".source" = "/tmp/file1" } }
{ "@case_entry" = "/tmp/file2"
{ ".source" = "/tmp/file2" } }
{ "@case_entry" = "*"
{ "@unset" = "f" } } }
(* Select *)
test Shellvars.lns get "select i in a b c; do . /tmp/file$i
done\n" =
{ "@select" = "i in a b c"
{ ".source" = "/tmp/file$i" }
}
(* Return *)
test Shellvars.lns get "return\nreturn 2\n" =
{ "@return" }
{ "@return" = "2" }
(* Functions *)
test Shellvars.lns get "foo() {
. /tmp/bar
}\n" =
{ "@function" = "foo"
{ ".source" = "/tmp/bar" }
}
test Shellvars.lns get "function foo () {
. /tmp/bar
}\n" =
{ "@function" = "foo"
{ ".source" = "/tmp/bar" }
}
(* Dollar assignment *)
test Shellvars.lns get "FOO=$(bar arg)\n" =
{ "FOO" = "$(bar arg)" }
(* Empty lines before esac *)
test Shellvars.lns get "case $f in
a)
B=C
;;
esac\n" =
{ "@case" = "$f"
{ "@case_entry" = "a"
{ "B" = "C" } }
}
(* Empty lines before a case_entry *)
test Shellvars.lns get "case $f in
a)
B=C
;;
b)
A=D
;;
esac\n" =
{ "@case" = "$f"
{ "@case_entry" = "a"
{ "B" = "C" } }
{ "@case_entry" = "b"
{ "A" = "D" } } }
(* Comments anywhere *)
test Shellvars.lns get "case ${INTERFACE} in
# comment before
eth0)
# comment in
OPTIONS=()
;;
# comment before 2
*)
# comment in 2
unset f
;;
# comment after
esac\n" =
{ "@case" = "${INTERFACE}"
{ "#comment" = "comment before" }
{ "@case_entry" = "eth0"
{ "#comment" = "comment in" }
{ "OPTIONS" = "()" } }
{ "#comment" = "comment before 2" }
{ "@case_entry" = "*"
{ "#comment" = "comment in 2" }
{ "@unset" = "f" } }
{ "#comment" = "comment after" } }
(* Empty case *)
test Shellvars.lns get "case $a in
*)
;;
esac\n" =
{ "@case" = "$a"
{ "@case_entry" = "*" } }
(* case variables can be surrounded by double quotes *)
test Shellvars.lns get "case \"${options}\" in
*debug*)
shift
;;
esac\n" =
{ "@case" = "\"${options}\""
{ "@case_entry" = "*debug*"
{ "@builtin" = "shift" } } }
(* Double quoted values can have newlines *)
test Shellvars.lns get "FOO=\"123\n456\"\n" =
{ "FOO" = "\"123\n456\"" }
(* Single quoted values can have newlines *)
test Shellvars.lns get "FOO='123\n456'\n" =
{ "FOO" = "'123\n456'" }
(* bquoted values can have semi-colons *)
test Shellvars.lns get "FOO=`bar=date;$bar`\n" =
{ "FOO" = "`bar=date;$bar`" }
(* dollar-assigned values can have semi-colons *)
test Shellvars.lns get "FOO=$(bar=date;$bar)\n" =
{ "FOO" = "$(bar=date;$bar)" }
(* dollar-assigned value in bquot *)
test Shellvars.lns get "FOO=`echo $(date)`\n" =
{ "FOO" = "`echo $(date)`" }
(* bquot value in dollar-assigned value *)
test Shellvars.lns get "FOO=$(echo `date`)\n" =
{ "FOO" = "$(echo `date`)" }
(* dbquot *)
test Shellvars.lns get "FOO=``bar``\n" =
{ "FOO" = "``bar``" }
(* unset can be used on wildcard variables *)
test Shellvars.lns get "unset ${!LC_*}\n" =
{ "@unset" = "${!LC_*}" }
(* Empty comment before entries *)
test Shellvars.lns get "# \nfoo=bar\n" =
{ }
{ "foo" = "bar" }
(* Empty comment after entries *)
test Shellvars.lns get "foo=bar\n# \n\n" =
{ "foo" = "bar" }
(* Local Variables: *)
(* mode: caml *)
(* End: *)