aboutsummaryrefslogtreecommitdiffstats
path: root/test/test_data.lua
diff options
context:
space:
mode:
authorDanilo M. <danix@danix.xyz>2026-09-17 12:10:22 +0200
committerDanilo M. <danix@danix.xyz>2026-09-17 12:10:22 +0200
commit518b2969108ed3d728e562614c05e40400129240 (patch)
tree502a61a38db9d9ee35eb0e3f770e28596847ace3 /test/test_data.lua
parent3690d0ee9ceb5c0287cfe72eb4dcfd8f43324f0e (diff)
downloadconky-theme-udt-518b2969108ed3d728e562614c05e40400129240.tar.gz
conky-theme-udt-518b2969108ed3d728e562614c05e40400129240.zip
feat: parse df and du output for the disk and cache cards
Diffstat (limited to 'test/test_data.lua')
-rw-r--r--test/test_data.lua59
1 files changed, 59 insertions, 0 deletions
diff --git a/test/test_data.lua b/test/test_data.lua
index e5b589f..71baef0 100644
--- a/test/test_data.lua
+++ b/test/test_data.lua
@@ -91,4 +91,63 @@ assert(#data.per_cpu_times(nil) == 0, 'nil gives an empty list')
-- rather than producing a nonsense total.
assert(#data.per_cpu_times('cpu0 1 2\n') == 0, 'a short line is skipped')
+-- === Cache files ==========================================================
+-- df -P guarantees one line per mount with the mountpoint LAST, which is why
+-- the parser takes the last field rather than the sixth: an NFS device is
+-- 'server:/export' and a device name can carry surprises, but the mountpoint
+-- is always at the end.
+local fs = data.df_parse(read('test/fixtures/df_output'))
+assert(#fs == 6, 'one entry per mount, got ' .. tostring(#fs))
+assert(fs[1].mount == '/', 'first mount, got ' .. tostring(fs[1].mount))
+assert(fs[1].pct == 84, 'root percent, got ' .. tostring(fs[1].pct))
+assert(fs[1].size == 263174213632, 'root size in bytes, got ' .. tostring(fs[1].size))
+assert(fs[1].used == 208111570944, 'root used, got ' .. tostring(fs[1].used))
+
+-- The NFS rows are the reason the last field matters: a colon in the device
+-- would break a parser splitting on punctuation.
+assert(fs[4].mount == '/mnt/nfs/Library', 'nfs mount, got ' .. tostring(fs[4].mount))
+assert(fs[4].pct == 79, 'nfs percent, got ' .. tostring(fs[4].pct))
+
+-- The device field carries the server, which is what lets the disks card
+-- label the two shares without an address hardcoded in the repo.
+assert(fs[4].host == 'server-a', 'nfs host, got ' .. tostring(fs[4].host))
+assert(fs[1].host == nil, 'a local device has no host, got ' .. tostring(fs[1].host))
+
+-- A full filesystem is a real state, not an error.
+assert(fs[6].pct == 100, 'a full mount reads 100, got ' .. tostring(fs[6].pct))
+
+-- The header line must not become a row.
+for _, e in ipairs(fs) do
+ assert(e.mount ~= 'on' and e.mount ~= 'Mounted', 'the header leaked in as a row')
+end
+
+assert(#data.df_parse('') == 0, 'empty input gives an empty list')
+assert(#data.df_parse(nil) == 0, 'nil gives an empty list')
+-- A header with no rows is what a failed df produces.
+assert(#data.df_parse('Filesystem 1-blocks Used Available Capacity Mounted on\n') == 0,
+ 'a header alone gives an empty list')
+
+-- du -sh: total first, then children largest-first. The suffixes must be
+-- converted, not string-sorted: '1.1G' outranks '245M' numerically and loses
+-- to it alphabetically.
+local cache = data.du_parse(read('test/fixtures/du_output'))
+assert(cache.total, 'a total is parsed')
+assert(cache.total.label == '1.6G', 'total label, got ' .. tostring(cache.total.label))
+assert(#cache.items == 4, 'four children, got ' .. tostring(#cache.items))
+assert(cache.items[1].name == 'mozilla', 'largest child, got ' .. tostring(cache.items[1].name))
+assert(cache.items[1].label == '1.1G', 'its label, got ' .. tostring(cache.items[1].label))
+assert(cache.items[2].name == 'pip', 'second child, got ' .. tostring(cache.items[2].name))
+
+-- Sizes are compared as bytes, which is what makes 'biggest' meaningful.
+assert(cache.items[1].bytes > cache.items[2].bytes, '1.1G must outrank 245M')
+assert(cache.items[1].bytes > 1e9, 'a G suffix is about a billion bytes')
+assert(cache.items[4].bytes < 1e8, 'an M suffix is far smaller')
+
+-- Share of the total drives the highlight colour.
+assert(cache.items[1].bytes / cache.total.bytes > 0.5,
+ 'mozilla dominates this fixture, which is what the highlight keys on')
+
+assert(data.du_parse('') == nil, 'empty input gives nil')
+assert(data.du_parse(nil) == nil, 'nil gives nil')
+
print('test_data: all assertions passed')