summaryrefslogtreecommitdiff
path: root/Data/Libraries/Penlight/tests/test-config.lua
blob: 13fc1acca12948b5e65d22a3c5994f53c45280e7 (plain)
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
local config = require 'pl.config'
local stringio = require 'pl.stringio'
asserteq = require 'pl.test'.asserteq

function testconfig(test,tbl,cfg)
    local f = stringio.open(test)
    local c = config.read(f,cfg)
    f:close()
    if not tbl then
        print(pretty.write(c))
    else
        asserteq(c,tbl)
    end
end

testconfig ([[
 ; comment 2 (an ini file)
[section!]
bonzo.dog=20,30
config_parm=here we go again
depth = 2
[another]
felix="cat"
]],{
  section_ = {
    bonzo_dog = { -- comma-sep values get split by default
      20,
      30
    },
    depth = 2,
    config_parm = "here we go again"
  },
  another = {
    felix = "\"cat\""
  }
})


testconfig ([[
# this is a more Unix-y config file
fred = 1
alice = 2
home.dog = /bonzo/dog/etc
]],{
  home_dog = "/bonzo/dog/etc",  -- note the default is {variablilize = true}
  fred = 1,
  alice = 2
})

-- backspace line continuation works, thanks to config.lines function
testconfig ([[
foo=frodo,a,c,d, \
  frank, alice, boyo
]],
{
  foo = {
    "frodo",
    "a",
    "c",
    "d",
    "frank",
    "alice",
    "boyo"
  }
}
)

------ options to control default behaviour -----

-- want to keep key names as is!
testconfig ([[
alpha.dog=10
# comment here
]],{
    ["alpha.dog"]=10
},{variabilize=false})

-- don't convert strings to numbers
testconfig ([[
alpha.dog=10
; comment here
]],{
    alpha_dog="10"
},{convert_numbers=false})

-- convert strings to booleans
testconfig ([[
alpha.dog=false
alpha.cat=true
; comment here
]],{
    alpha_dog=false,
    alpha_cat=true
},{convert_boolean=true})

-- don't split comma-lists by setting the list delimiter to something else
testconfig ([[
extra=10,'hello',42
]],{
    extra="10,'hello',42"
},{list_delim='@'})

-- Unix-style password file
testconfig([[
lp:x:7:7:lp:/var/spool/lpd:/bin/sh
mail:x:8:8:mail:/var/mail:/bin/sh
news:x:9:9:news:/var/spool/news:/bin/sh
]],
{
  {
    "lp",
    "x",
    7,
    7,
    "lp",
    "/var/spool/lpd",
    "/bin/sh"
  },
  {
    "mail",
    "x",
    8,
    8,
    "mail",
    "/var/mail",
    "/bin/sh"
  },
  {
    "news",
    "x",
    9,
    9,
    "news",
    "/var/spool/news",
    "/bin/sh"
  }
},
{list_delim=':'})

-- Unix updatedb.conf is in shell script form, but config.read
-- copes by extracting the variables as keys and the export
-- commands as the array part; there is an option to remove quotes
-- from values
testconfig([[
# Global options for invocations of find(1)
FINDOPTIONS='-ignore_readdir_race'
export FINDOPTIONS
]],{
  "export FINDOPTIONS",
  FINDOPTIONS = "-ignore_readdir_race"
},{trim_quotes=true})

-- Unix fstab format. No key/value assignments so use `ignore_assign`;
-- list values are separated by a number of spaces
testconfig([[
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
proc            /proc           proc    defaults        0       0
/dev/sda1       /               ext3    defaults,errors=remount-ro 0       1
]],
{
  {
    "proc",
    "/proc",
    "proc",
    "defaults",
    0,
    0
  },
  {
    "/dev/sda1",
    "/",
    "ext3",
    "defaults,errors=remount-ro",
    0,
    1
  }
},
{list_delim='%s+',ignore_assign=true}
)

-- Linux procfs 'files' often use ':' as the key/pair separator;
-- a custom convert_numbers handles the units properly!
-- Here is the first two lines from /proc/meminfo
testconfig([[
MemTotal:        1024748 kB
MemFree:          220292 kB
]],
{ MemTotal = 1024748, MemFree = 220292 },
{
 keysep = ':',
 convert_numbers = function(s)
    s = s:gsub(' kB$','')
    return tonumber(s)
  end
 }
)

-- altho this works, rather use pl.data.read for this kind of purpose.
testconfig ([[
# this is just a set of comma-separated values
1000,444,222
44,555,224
]],{
  {
    1000,
    444,
    222
  },
  {
    44,
    555,
    224
  }
})

--- new with 1.0.3: smart configuration file reading
-- handles a number of common Unix file formats automatically

function smart(f)
    f = stringio.open(f)
    return config.read(f,{smart=true})
end

-- /etc/fstab
asserteq (smart[[
# /etc/fstab: static file system information.
#
# Use 'blkid -o value -s UUID' to print the universally unique identifier
# for a device; this may be used with UUID= as a more robust way to name
# devices that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
proc            /proc           proc    nodev,noexec,nosuid 0       0
/dev/sdb2       /               ext2    errors=remount-ro 0       1
/dev/fd0        /media/floppy0  auto    rw,user,noauto,exec,utf8 0       0
]],{
  proc = {
    "/proc",
    "proc",
    "nodev,noexec,nosuid",
    0,
    0
  },
  ["/dev/sdb2"] = {
    "/",
    "ext2",
    "errors=remount-ro",
    0,
    1
  },
  ["/dev/fd0"] = {
    "/media/floppy0",
    "auto",
    "rw,user,noauto,exec,utf8",
    0,
    0
  }
})

-- /proc/XXXX/status
asserteq (smart[[
Name:	bash
State:	S (sleeping)
Tgid:	30071
Pid:	30071
PPid:	1587
TracerPid:	0
Uid:	1000	1000	1000	1000
Gid:	1000	1000	1000	1000
FDSize:	256
Groups:	4 20 24 46 105 119 122 1000
VmPeak:     6780 kB
VmSize:     6716 kB
]],{
  Pid = 30071,
  VmSize = 6716,
  PPid = 1587,
  Tgid = 30071,
  State = "S (sleeping)",
  Uid = "1000	1000	1000	1000",
  Name = "bash",
  Gid = "1000	1000	1000	1000",
  Groups = "4 20 24 46 105 119 122 1000",
  FDSize = 256,
  VmPeak = 6780,
  TracerPid = 0
})

-- ssh_config
asserteq (smart[[
Host *
#   ForwardAgent no
#   ForwardX11 no
#   Tunnel no
#   TunnelDevice any:any
#   PermitLocalCommand no
#   VisualHostKey no
    SendEnv LANG LC_*
    HashKnownHosts yes
    GSSAPIAuthentication yes
    GSSAPIDelegateCredentials no
]],{
  Host = "*",
  GSSAPIAuthentication = "yes",
  SendEnv = "LANG LC_*",
  HashKnownHosts = "yes",
  GSSAPIDelegateCredentials = "no"
})

-- updatedb.conf
asserteq (smart[[
PRUNE_BIND_MOUNTS="yes"
# PRUNENAMES=".git .bzr .hg .svn"
PRUNEPATHS="/tmp /var/spool /media"
PRUNEFS="NFS nfs nfs4 rpc_pipefs afs binfmt_misc proc smbfs autofs iso9660 ncpfs coda devpts ftpfs devfs mfs shfs sysfs cifs lustre_lite tmpfs usbfs udf fuse.glusterfs fuse.sshfs ecryptfs fusesmb devtmpfs"
]],{
  PRUNEPATHS = "/tmp /var/spool /media",
  PRUNE_BIND_MOUNTS = "yes",
  PRUNEFS = "NFS nfs nfs4 rpc_pipefs afs binfmt_misc proc smbfs autofs iso9660 ncpfs coda devpts ftpfs devfs mfs shfs sysfs cifs lustre_lite tmpfs usbfs udf fuse.glusterfs fuse.sshfs ecryptfs fusesmb devtmpfs"
})