2024-04-20 19:24:10 +02:00
|
|
|
(local { : view } (require :fennel))
|
2024-04-20 18:45:40 +02:00
|
|
|
(import-macros { : expect= } :anoia.assert)
|
|
|
|
|
2024-04-20 19:24:10 +02:00
|
|
|
(fn parse-uevent [s]
|
|
|
|
(let [(nl nxt) (string.find s "\0" 1 true)]
|
|
|
|
(doto
|
|
|
|
(collect [k v (string.gmatch
|
|
|
|
(string.sub s (+ 1 nxt))
|
|
|
|
"(%g-)=(%g+)")]
|
|
|
|
(k:lower) v)
|
|
|
|
(tset :path (string.sub s 1 (- nl 1))))))
|
2024-04-20 18:45:40 +02:00
|
|
|
|
|
|
|
(fn database []
|
2024-04-20 19:24:10 +02:00
|
|
|
(let [db {}]
|
|
|
|
{
|
|
|
|
:find (fn [_ terms] [(. db (next db))])
|
|
|
|
:add (fn [_ event-string]
|
|
|
|
(let [e (parse-uevent event-string)]
|
|
|
|
(tset db e.path e)))
|
|
|
|
}))
|
2024-04-20 18:45:40 +02:00
|
|
|
|
|
|
|
(macro example [description & body]
|
|
|
|
`(do ,body))
|
|
|
|
|
|
|
|
(example
|
|
|
|
"given an empty database, search for some criteria matches no entries"
|
|
|
|
(let [db (database)]
|
|
|
|
(expect= (db:find {:partname "boot"}) [])))
|
|
|
|
|
2024-04-20 19:24:10 +02:00
|
|
|
(example
|
|
|
|
"when I add a device, I can find it"
|
|
|
|
(let [db (database)]
|
|
|
|
(db:add "add@/devices/pci0000:00/0000:00:13.0/usb1/1-1/1-1:1.0/host0/target0:0:0/0:0:0:0/block/sda\0ACTION=add
|
|
|
|
DEVPATH=/devices/pci0000:00/0000:00:13.0/usb1/1-1/1-1:1.0/host0/target0:0:0/0:0:0:0/block/sda
|
|
|
|
SUBSYSTEM=block
|
|
|
|
MAJOR=8
|
|
|
|
MINOR=0
|
|
|
|
DEVNAME=sda
|
|
|
|
DEVTYPE=disk
|
|
|
|
DISKSEQ=2
|
|
|
|
SEQNUM=1527")
|
|
|
|
(let [[m & more] (db:find {:devname "boot"})]
|
|
|
|
(expect= m.devname "sda")
|
|
|
|
(expect= m.major "8")
|
|
|
|
(expect= more []))))
|
|
|
|
|
2024-04-20 18:45:40 +02:00
|
|
|
|
|
|
|
|
|
|
|
(print "OK")
|