Files

76 lines
4.0 KiB
Python

# LHA / LZH fixtures (tools/fixtures.py): the test archives of lhasa 0.6.0's source (nixpkgs#lhasa.src, ISC; read
# from the nix store at fixture time, not committed: archives by LHarc 1.13 (lh1), LArc 3.33 (lzs / lz5 / lz4), LHA
# 2.13 / 2.55e / Amiga / OS-9 / OSK / Unix 1.14i / ExpLZH (lh0 / lh5 / lh6 / lh7, header levels 0 / 1 / 2, subdirectories,
# symlinks) and lhasa's lh1 / lz5 length tests) and archives made here by LHa for UNIX 1.14i (unfree, run as a tool:
# -o5 / -o6 / -o7 / -z, header levels 0 / 1 / 2); the reference data is what lhasa extracts (`lha -xw=`). Our writer's
# output (tools/fixtures/_arch2_ours.luau) is extracted by lhasa.
# tests/gen/lha.luau { cases = { { name, data, entries = { { name, size, crc } }, links = { { name, link } } } },
# ours = { ok, listing } }
import glob, importlib.util, os, shutil, subprocess, tarfile, zlib
_s = importlib.util.spec_from_file_location("_arch2", os.path.join(os.path.dirname(os.path.abspath(__file__)), "_arch2.py"))
A = importlib.util.module_from_spec(_s)
_s.loader.exec_module(A)
INPUTS = ["tools/fixtures/_arch2.py", "tools/fixtures/_arch2_ours.luau", "src/Rosetta/archive/lha"]
DIRS = ["lharc113", "larc333", "lha213", "lha255e", "lha_amiga_122", "lha_amiga_212", "lha_os9_211c", "lha_osk_201",
"lha_unix114i", "explzh_723", "lengths", "generated/lzs", "lha_x68k_213", "lh2_222", "lhmelt_16536"]
def lhasa(args, cwd):
return A.tool(["lhasa"], ["lha"] + args, cwd=cwd).decode(errors="replace")
def expected(path, tmp):
out = os.path.join(tmp, "x")
A.rmtree(out)
os.makedirs(out)
lhasa(["-xqfw=" + out, path], tmp)
ents, links = [], []
for d, _, fs in os.walk(out):
for f in fs:
p = os.path.join(d, f)
rel = os.path.relpath(p, out)
if os.path.islink(p):
links.append({"name": rel, "link": os.readlink(p)})
else:
data = open(p, "rb").read()
ents.append({"name": rel, "size": len(data), "crc": zlib.crc32(data)})
return ents, links
def generate(ctx):
src = subprocess.run(["nix", "build", "--no-link", "--print-out-paths", "nixpkgs#lhasa.src"], capture_output=True, check=True, text=True).stdout.strip()
tmp = A.tmpdir()
try:
with tarfile.open(src) as t:
t.extractall(tmp, filter="data")
root = glob.glob(os.path.join(tmp, "lhasa-*", "test", "archives"))[0]
cases = []
for d in DIRS:
for p in sorted(glob.glob(os.path.join(root, d, "*.lz[hs]"))):
try:
ents, links = expected(p, tmp)
except RuntimeError:
continue # lhasa cannot extract it here (read-only directory attributes from OS/2)
cases.append({"name": os.path.relpath(p, root), "data": open(p, "rb").read(), "entries": ents, "links": links})
# LHa for UNIX
work = os.path.join(tmp, "w")
rng = ctx.rng(5)
A.tree(A.FILES + [("dir/sub/text.txt", b"the quick brown fox " * 300), ("dir/rand.bin", rng.randbytes(70000))], work)
names = [n for n, _ in A.FILES] + ["dir"]
for level in (0, 1, 2):
for m in ("5", "6", "7", "z"):
arc = os.path.join(tmp, "u%d%s.lzh" % (level, m))
opt = ("-a%dz" % level) if m == "z" else ("-a%do%s" % (level, m))
A.tool([], ["env", "NIXPKGS_ALLOW_UNFREE=1", "nix", "shell", "--impure", "nixpkgs#lha", "-c", "lha", opt + "q", arc] + names, cwd=work)
ents, links = expected(arc, tmp)
cases.append({"name": "lha114i -h%d -%s" % (level, m), "data": open(arc, "rb").read(), "entries": ents, "links": links})
arc = os.path.join(tmp, "ours.lzh")
open(arc, "wb").write(A.ours(ctx, "lha"))
got = {e["name"]: e["crc"] for e in expected(arc, tmp)[0]}
ok = all(got.get(n) == zlib.crc32(d) for n, d in A.FILES)
ctx.emit("lha", {"cases": cases, "ours": {"ok": ok, "listing": lhasa(["-v", arc], tmp)}})
finally:
A.rmtree(tmp)