merge master
This commit is contained in:
commit
f9e2e6a455
18 files changed
+16515
-1468
No files matched your search
@@ -0,0 +1,210 @@
|
||||
-- detect tests: lazy detection (Sniffs.luau, cost order, extension first) against the eager reference (every format
|
||||
-- module's own sniff, registry order) on samples of every fixture module, with and without names and categories;
|
||||
-- which modules a first detect loads. Fixtures: tools/fixtures/detect.py.
|
||||
local T = require("@tests/T")
|
||||
local Registry = require("@rosetta/Registry")
|
||||
|
||||
local loads: { string } = {}
|
||||
for _, e in Registry do
|
||||
local load = e.load
|
||||
e.load = function(): any
|
||||
table.insert(loads, e.path)
|
||||
return load()
|
||||
end
|
||||
end
|
||||
|
||||
local F = require("@rosetta")
|
||||
local Bytes = require("@rosetta/core/Bytes")
|
||||
local Input = require("@rosetta/core/Input")
|
||||
local Sniffs = require("@rosetta/Sniffs")
|
||||
local Blobs1 = require("@tests/gen/detect_blobs_1")
|
||||
local Blobs2 = require("@tests/gen/detect_blobs_2")
|
||||
local Blobs3 = require("@tests/gen/detect_blobs_3")
|
||||
local Blobs4 = require("@tests/gen/detect_blobs_4")
|
||||
|
||||
local t = T.suite("detect")
|
||||
|
||||
-- 1. a first detect of a PNG loads no format module, with or without its name
|
||||
local PNG = buffer.fromstring("\137PNG\r\n\26\n\0\0\0\rIHDR\0\0\0\1\0\0\0\1\8\6\0\0\0\31\21\196\137\0\0\0\rIDATx\218c\248\255\255?\0\5\254\2\254\167\53\129\132\0\0\0\0IEND\174B`\130")
|
||||
t:eq(F.detect(PNG, { name = "a.png" }), "png", "png with name")
|
||||
t:eq(F.detect(PNG), "png", "png without name")
|
||||
t:eq(F.detect(PNG, { name = "a.jpg" }), "png", "png named .jpg")
|
||||
t:eq(#loads, 0, "no format module loaded (" .. table.concat(loads, " ") .. ")")
|
||||
local junk = buffer.create(4096)
|
||||
for i = 0, 4095 do
|
||||
buffer.writeu8(junk, i, (i * 7919) % 251)
|
||||
end
|
||||
F.detect(junk)
|
||||
local need = 0
|
||||
for _, e in Registry do
|
||||
if e.sniff == 2 then
|
||||
need += 1
|
||||
end
|
||||
end
|
||||
t:check(#loads <= need + 1, "unknown data loads only the modules that sniff themselves: " .. table.concat(loads, " "))
|
||||
t:note("unknown data: %d format modules loaded (%s)", #loads, table.concat(loads, " "))
|
||||
|
||||
-- 2. the eager reference: each module's own sniff in registry order, the extension's format at 1 returns at once,
|
||||
-- archive/br (a trial decode, at most 0.3) last
|
||||
local modules: { [string]: any } = {}
|
||||
for _, e in Registry do
|
||||
modules[e.path] = e.load()
|
||||
end
|
||||
|
||||
local function refDetect(buf: buffer, name: string?, category: string?): any
|
||||
local ext = Input.extension(name)
|
||||
local best, bestScore, bestIdx = nil, 0, 0
|
||||
local late: { number } = {}
|
||||
for idx, e in Registry do
|
||||
if (category == nil or e.category == category) and (e.caps.decode or e.caps.open) then
|
||||
if e.name == "br" then
|
||||
table.insert(late, idx)
|
||||
continue
|
||||
end
|
||||
local ok, score = pcall(modules[e.path].sniff, Bytes.reader(buf))
|
||||
if ok and type(score) == "number" and score > 0 then
|
||||
local extHit = ext and table.find(e.extensions, ext)
|
||||
if extHit then
|
||||
score += 0.01
|
||||
end
|
||||
if score > bestScore then
|
||||
best, bestScore, bestIdx = e, score, idx
|
||||
if extHit and score >= 1.01 then
|
||||
return e
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
for _, idx in late do
|
||||
local e = Registry[idx]
|
||||
local extHit = ext and table.find(e.extensions, ext)
|
||||
local cap = 0.3 + (if extHit then 0.01 else 0)
|
||||
if cap > bestScore or (cap == bestScore and idx < bestIdx) then
|
||||
local ok, score = pcall(modules[e.path].sniff, Bytes.reader(buf))
|
||||
if ok and type(score) == "number" and score > 0 then
|
||||
if extHit then
|
||||
score += 0.01
|
||||
end
|
||||
if score > bestScore or (score == bestScore and idx < bestIdx) then
|
||||
best, bestScore, bestIdx = e, score, idx
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
if best then
|
||||
return best
|
||||
end
|
||||
if ext then
|
||||
for _, e in Registry do
|
||||
if (category == nil or e.category == category) and e.name == ext then
|
||||
return e
|
||||
end
|
||||
end
|
||||
for _, e in Registry do
|
||||
if (category == nil or e.category == category) and table.find(e.extensions, ext) then
|
||||
return e
|
||||
end
|
||||
end
|
||||
end
|
||||
return nil
|
||||
end
|
||||
|
||||
local function refName(buf: buffer, name: string?, category: string?): string?
|
||||
local e = refDetect(buf, name, category)
|
||||
if not e then
|
||||
return nil
|
||||
end
|
||||
local m = modules[e.path]
|
||||
if type(m.variant) == "function" then
|
||||
local ok, v = pcall(m.variant, Bytes.reader(buf))
|
||||
if ok and type(v) == "string" then
|
||||
return v
|
||||
end
|
||||
end
|
||||
return e.name
|
||||
end
|
||||
|
||||
local blobs: { { from: string, data: string } } = {}
|
||||
for _, part in { Blobs1, Blobs2, Blobs3, Blobs4 } do
|
||||
for _, b in part :: any do
|
||||
table.insert(blobs, b)
|
||||
end
|
||||
end
|
||||
local rng = T.rng(7)
|
||||
for n = 0, 40 do
|
||||
local b = buffer.create(n * 7)
|
||||
for i = 0, n * 7 - 1 do
|
||||
buffer.writeu8(b, i, rng(256) - 1)
|
||||
end
|
||||
table.insert(blobs, { from = "random", data = buffer.tostring(b) })
|
||||
end
|
||||
table.insert(blobs, { from = "junk", data = buffer.tostring(junk) })
|
||||
table.insert(blobs, { from = "png", data = buffer.tostring(PNG) })
|
||||
t:check(#blobs > 1000, "samples from the fixture modules (" .. #blobs .. ")")
|
||||
|
||||
-- 3. every copied sniff scores as the module's own, and every score is in 0..1 (the cost order relies on it)
|
||||
local copied, diff, over = 0, 0, 0
|
||||
for _, e in Registry do
|
||||
local s = (Sniffs :: any)[e.path]
|
||||
t:eq(s ~= nil, e.sniff < 2, e.path .. ": in Sniffs.luau as Registry.sniff says")
|
||||
if s then
|
||||
copied += 1
|
||||
end
|
||||
end
|
||||
for _, b in blobs do
|
||||
local buf = buffer.fromstring(b.data)
|
||||
for _, e in Registry do
|
||||
local own = modules[e.path].sniff
|
||||
local ok1, s1 = pcall(own, Bytes.reader(buf))
|
||||
if ok1 and type(s1) == "number" and s1 > 1 then
|
||||
over += 1
|
||||
t:note("%s scores %s on a sample of %s", e.path, tostring(s1), b.from)
|
||||
end
|
||||
local s = (Sniffs :: any)[e.path]
|
||||
if s then
|
||||
local ok2, s2 = pcall(s, Bytes.reader(buf))
|
||||
if ok1 ~= ok2 or (ok1 and s1 ~= s2) then
|
||||
diff += 1
|
||||
if diff <= 10 then
|
||||
t:note("%s on a sample of %s: module %s, copy %s", e.path, b.from, tostring(s1), tostring(s2))
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
t:eq(diff, 0, "copied sniffs score as the modules do")
|
||||
t:eq(over, 0, "no sniff scores over 1")
|
||||
|
||||
-- 4. F.detect = the reference: no name, the reference's own extension, a wrong one, an unknown one; and per category
|
||||
local runs, mismatch = 0, 0
|
||||
local function same(buf: buffer, from: string, name: string?, category: string?)
|
||||
local want = refName(buf, name, category)
|
||||
local got = F.detect(buf, { name = name, category = category })
|
||||
runs += 1
|
||||
if got ~= want then
|
||||
mismatch += 1
|
||||
if mismatch <= 10 then
|
||||
t:note("sample of %s, name %s, category %s: got %s, want %s", from, tostring(name), tostring(category), tostring(got), tostring(want))
|
||||
end
|
||||
end
|
||||
end
|
||||
for _, b in blobs do
|
||||
local buf = buffer.fromstring(b.data)
|
||||
same(buf, b.from, nil, nil)
|
||||
local e = refDetect(buf, nil, nil)
|
||||
if e then
|
||||
same(buf, b.from, "x." .. e.extensions[1], nil)
|
||||
same(buf, b.from, nil, e.category)
|
||||
end
|
||||
same(buf, b.from, "x.png", nil)
|
||||
same(buf, b.from, "x.txt", nil)
|
||||
same(buf, b.from, "x.bin", "audio")
|
||||
end
|
||||
t:eq(mismatch, 0, "F.detect matches the eager reference (" .. runs .. " detects)")
|
||||
t:note("%d samples, %d detects, %d of %d sniffs copied into Sniffs.luau", #blobs, runs, copied, #Registry)
|
||||
|
||||
t:fuzz("detect", function(b: buffer): any
|
||||
return F.detect(b)
|
||||
end, { PNG, junk })
|
||||
t:done()
|
||||
Reference in new issue
Block a user