-- test_helpers.lua -- Shared helpers for the libglacier-ng Lua test suite. These operate on -- the real filesystem (/glacier) and real subprocesses — there is no -- mocking here, by design, since the whole point of these tests is to -- exercise real rename()/flock()/mount() behavior. local M = {} function M.quote(s) return "'" .. s:gsub("'", "'\\''") .. "'" end function M.sh(cmd) local ok = os.execute(cmd) local success = (ok == true) or (ok == 0) if not success then error(string.format("command failed (%s): %s", tostring(ok), cmd), 2) end end function M.exists(path) local ok = os.execute("test -e " .. M.quote(path)) return ok == true or ok == 0 end -- Wipes and recreates a bare /glacier tree so each test file starts -- clean. DESTRUCTIVE. Guarded behind an explicit env var so nobody runs -- this against a real system by accident. function M.reset_glacier_root() assert(os.getenv("GLACIER_TEST_CONFIRM") == "yes", "refusing to rm -rf /glacier: set GLACIER_TEST_CONFIRM=yes " .. "to confirm this is running in a throwaway test container") M.sh("rm -rf /glacier") -- gl_init_user creates /glacier/usr itself, and gl_init_sys the -- system tree; this just gives both the root-owned, 0755 parents -- the per-uid layout's safety check expects (see common.h). M.sh("mkdir -p /glacier/usr /glacier/sys/index /glacier/sys/store /glacier/sys/links") M.sh("chmod 755 /glacier /glacier/usr /glacier/sys") end -- Builds a minimal but real .gpkg (gzip tar with a libconfig manifest -- and a files/ payload) that gl_install_pkg can actually parse and -- extract. Returns (path_to_gpkg, workdir). function M.build_fixture_pkg(opts) opts = opts or {} local name = opts.name or "testpkg" local ver = opts.ver or "1.0.0" local repo = opts.repo or "core" local payload = opts.payload or ("hello from " .. name .. "\n") local work = os.tmpname() M.sh("rm -f " .. M.quote(work)) M.sh("mkdir -p " .. M.quote(work)) local pkgdir = string.format("%s/%s-%s", work, name, ver) M.sh("mkdir -p " .. M.quote(pkgdir .. "/files/usr/bin")) local manifest = assert(io.open(pkgdir .. "/manifest.gpm.cfg", "w")) manifest:write(string.format( 'PKG_NAME = "%s";\n' .. 'PKG_VER = "%s";\n' .. 'PKG_DESC = "Fixture package for tests";\n' .. 'PKG_MAINT = "test@example.com";\n' .. 'PKG_COPYRIGHT = "2026 Test";\n' .. 'PKG_REPO = "%s";\n', name, ver, repo)) manifest:close() local bin = assert(io.open(pkgdir .. "/files/usr/bin/" .. name, "w")) bin:write(payload) bin:close() -- opts.files = { ["usr/lib/libx.so"] = "content", ... } -- opts.symlinks = { ["usr/bin/alias"] = "target", ... } -- Both relative to files/. local function place(root, files, symlinks) for rel, content in pairs(files or {}) do local path = root .. "/" .. rel M.sh("mkdir -p " .. M.quote(path:match("^(.*)/"))) local f = assert(io.open(path, "w")) f:write(content) f:close() end for rel, target in pairs(symlinks or {}) do local path = root .. "/" .. rel M.sh("mkdir -p " .. M.quote(path:match("^(.*)/"))) M.sh("ln -s " .. M.quote(target) .. " " .. M.quote(path)) end end place(pkgdir .. "/files", opts.files, opts.symlinks) local out_path = opts.out or (work .. "/" .. name .. ".gpkg") local tar_path = work .. "/" .. name .. ".tar" M.sh(string.format("tar cf %s -C %s %s-%s", M.quote(tar_path), M.quote(work), name, ver)) -- opts.append = { { files = {...}, symlinks = {...} }, ... } -- Each batch is APPENDED to the archive as extra entries under the -- same files/ prefix, from a separate tree — the only way to get -- an archive no real filesystem could produce, e.g. a symlink `x` -- followed by a file `x/y`. Needs GNU tar (--append). for i, batch in ipairs(opts.append or {}) do local alt = string.format("%s/append%d", work, i) local alt_pkg = string.format("%s/%s-%s", alt, name, ver) place(alt_pkg .. "/files", batch.files, batch.symlinks) -- Leaf entries only (no directory entries), so e.g. `x/y` -- arrives with NO real directory `x` in front of it. local list = alt .. ".list" M.sh(string.format( "cd %s && find %s-%s/files ! -type d > %s && " .. "tar rf %s --no-recursion -C %s -T %s", M.quote(alt), name, ver, M.quote(list), M.quote(tar_path), M.quote(alt), M.quote(list))) end M.sh(string.format("gzip -c %s > %s", M.quote(tar_path), M.quote(out_path))) return out_path, work end local pass, fail = 0, 0 function M.check(cond, msg) if cond then pass = pass + 1 io.write(" ok - " .. msg .. "\n") else fail = fail + 1 io.write(" FAIL - " .. msg .. "\n") end return cond end function M.summary() io.write(string.format("\n%d passed, %d failed\n", pass, fail)) os.exit(fail == 0 and 0 or 1) end return M