Files

74 lines
3.9 KiB
Python

# Commodore disk image fixtures (tools/fixtures.py): D64 / D71 / D81 images formatted and filled by VICE's `c1541`
# (PRG / SEQ / USR files, a multi-sector file, a deleted file, a full-ish directory), a D64 with the error-info block
# appended; every file read back by `c1541 -read` for the reference data. Our writer's D64 / D71 / D81 output
# (tools/fixtures/_arch2_ours.luau) is listed and read back by c1541.
# tests/gen/d64.luau { cases = { { name, data, disk, id, entries = { { name, size, crc, type } } } },
# ours = { { variant, ok, listing } } }
import importlib.util, os, shutil, 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/d64"]
PKG = ["vice"]
def c1541(args, cwd):
return A.tool(PKG, ["c1541"] + args, cwd=cwd).decode(errors="replace")
def generate(ctx):
rng = ctx.rng(64)
tmp = A.tmpdir()
try:
files = [("hello", ",p", b"\x01\x08hello prg\n"), ("big data", ",s", rng.randbytes(9000)), ("user file", ",u", b"USR " * 100),
("exact", ",p", rng.randbytes(254 * 3)), ("one", ",p", b"\x00")]
for i, (_, _, d) in enumerate(files):
open(os.path.join(tmp, "f%d" % i), "wb").write(d)
cases = []
for kind, label in (("d64", "test disk,ab"), ("d71", "double,cd"), ("d81", "three d,ef")):
img = "t." + kind
args = ["-format", label, kind, img]
for i, (name, suf, _) in enumerate(files):
args += ["-write", "f%d" % i, name + suf]
args += ["-write", "f0", "gone", "-delete", "gone"]
c1541(args, tmp)
ents = []
for i, (name, suf, d) in enumerate(files):
out = os.path.join(tmp, "out%d" % i)
c1541([img, "-read", name + suf, out], tmp)
rd = open(out, "rb").read()
assert rd == d, (kind, name, len(rd), len(d))
ents.append({"name": name, "size": len(rd), "crc": zlib.crc32(rd), "type": {"p": "prg", "s": "seq", "u": "usr"}[suf[1]]})
data = open(os.path.join(tmp, img), "rb").read()
disk, did = label.split(",")
cases.append({"name": kind, "data": data, "disk": disk, "id": did, "entries": ents})
if kind == "d64":
cases.append({"name": "d64 with error info", "data": data + b"\x01" * 683, "disk": disk, "id": did, "entries": ents})
# many files: several directory sectors
args = ["-format", "many,mm", "d64", "many.d64"]
for i in range(30):
args += ["-write", "f0", "file%02d" % i]
c1541(args, tmp)
d0 = files[0][2]
cases.append({"name": "30 files", "data": open(os.path.join(tmp, "many.d64"), "rb").read(), "disk": "many", "id": "mm",
"entries": [{"name": "file%02d" % i, "size": len(d0), "crc": zlib.crc32(d0), "type": "prg"} for i in range(30)]})
ours = []
for variant in ("d64", "d71", "d81"):
img = os.path.join(tmp, "ours." + variant)
open(img, "wb").write(A.ours(ctx, "d64", "variant=" + variant))
listing = c1541([img, "-list"], tmp)
ok = True
for name, data in A.FILES:
if not data:
continue # c1541 reads a one-sector file with no data bytes (link 0/1) as 254 bytes
out = os.path.join(tmp, "o-" + name)
c1541([img, "-read", name + suf, out], tmp)
got = open(out, "rb").read() if os.path.exists(out) else None
ok = ok and got == data
ours.append({"variant": variant, "ok": ok, "listing": listing})
ctx.emit("d64", {"cases": cases, "ours": ours})
finally:
shutil.rmtree(tmp)