Files

76 lines
2.1 KiB
Luau

-- shared by the archive tests: entry listings compared with a reference tool's, and the standard file set.
local Crc32 = require("@rosetta/core/Crc32")
local E = {}
function E.check(t: any, a: any, want: { any }, label: string, exact: boolean?)
local list = a:list()
if exact ~= false then
t:eq(#list, #want, label .. ": entry count")
end
local by = {}
for _, e in list do
by[e.name] = e
end
for _, w in want do
local e = by[w.name]
t:check(e, label .. ": has " .. tostring(w.name))
if e then
for _, k in { "size", "mode", "mtime", "dir", "link" } do
if w[k] ~= nil then
t:eq(e[k], w[k], label .. ": " .. w.name .. " " .. k)
end
end
if w.crc then
local b, err = a:read(w.name)
t:check(b, label .. ": read " .. w.name .. ": " .. tostring(err))
if b then
t:eq(Crc32.of(b), w.crc, label .. ": " .. w.name .. " data")
end
end
end
end
end
-- the standard file set of tools/fixtures/_arch2_ours.luau / _arch2.py
local function content(n: number, seed: number): buffer
local b = buffer.create(n)
local s = seed
for i = 0, n - 1 do
s = (s * 75 + 74) % 65537
buffer.writeu8(b, i, if (i // 64) % 3 == 0 then (i // 7) % 256 else s % 256)
end
return b
end
E.content = content
E.FILES = {
{ name = "hello.txt", data = buffer.fromstring("hello, archive\n") },
{ name = "data.bin", data = content(5000, 7) },
{ name = "empty.txt", data = buffer.create(0) },
}
function E.roundTrip(t: any, mod: any, opts: any?, label: string): buffer?
local Bytes = require("@rosetta/core/Bytes")
local a = mod.new()
for _, f in E.FILES do
a:add(f.name, f.data, { mtime = 1700000000, mode = 0x1A4 })
end
local out, err = a:write(opts)
t:check(out, label .. ": write: " .. tostring(err))
if not out then
return nil
end
local back, berr = mod.decode(Bytes.reader(out), {})
t:check(back, label .. ": decode: " .. tostring(berr))
if back then
local want = {}
for _, f in E.FILES do
table.insert(want, { name = f.name, size = buffer.len(f.data), crc = Crc32.of(f.data) })
end
E.check(t, back, want, label, false)
end
return out
end
return E