santoku-test-runner
View on LuaRocksthe spec runner behind toku test, with Lua-pattern file matching and per-file interpreter isolation, plus santoku spec conventions
santoku-test-runner has one module, santoku.test.runner, which exports one function, and toku test delegates to it. The spec side lives in the base library as santoku.test, a tagged-block harness with deferred cleanup. This tab covers both halves.
Details
- santoku-test-runner is the smallest rock in the framework.
- Given files and directories, the runner walks them, prints a Test: line per file, and runs each file.
- A Lua file runs in-process, or as a subprocess under an interpreter of your choice.
- The runner takes a Lua-pattern file filter and an optional stop-on-failure.
- santoku.test is a harness of about 70 lines.
- The cards go from basics to advanced. They show how the santoku repos and their consumers write specs, and how the runner discovers and runs them.
- The santoku.test harness ships in the bundle of this page, so a spec runs live below.
- The runner spawns real processes, so its examples are display only.
Writing specs
A spec file
A spec file is a plain Lua script. It requires santoku.test, calls test(tag, fn) for each case, and asserts inside fn. Blocks nest, and their tags accumulate into a chain.
local test = require("santoku.test")
local fi = require("santoku.fracidx")
test("fracidx", function ()
test("between empty and empty returns canonical zero", function ()
assert(fi.between(nil, nil) == "a0")
end)
test("repeated mid-insertion stays sorted", function ()
local a, b = "a0", "a1"
for _ = 1, 50 do
local m = fi.between(a, b)
assert(m > a and m < b, "mid was: " .. tostring(m))
a = m
end
end)
end)
Details
- fn receives defer, which the next card covers.
- A passing suite is silent.
- A failing assertion prints the full tag chain, the error and a traceback once. Then the outermost test raises "test failed: <tag chain>".
- So no test after the first failure runs, and a plain lua exits with status 1.
- There's no registration and no runner object, and cleanup goes through defer. The file executes top to bottom.
- This spec is real: the fracidx suite of the base library.
Cleanup with defer
test passes fn one argument, defer. defer(f) registers f to run when that block ends, last registered first. On a pass, the list of the block runs as fn returns. On a failure, it runs too.
local test = require("santoku.test")
local fs = require("santoku.fs")
test("config loading", function (defer)
local fp = fs.tmpname()
defer(function ()
fs.rm(fp, true)
print("removed", fp)
end)
fs.writefile(fp, "port = 8080\n")
test("reads the port", function (defer)
defer(function () print("inner cleanup runs first") end)
assert(fs.readfile(fp) == "port = 8080\n")
end)
print("exists before cleanup:", fs.exists(fp))
end)
Details
- On a failure, santoku.test prints the tag chain, message and traceback once.
- Then each block runs its own list in reverse as the failure unwinds, innermost block first.
- After that, the outermost test raises "test failed: <tag chain>".
- A deferred function that raises during failure cleanup prints its error, and the rest still run.
- This run prints inner cleanup runs first, then exists before cleanup: true file, then the removed line.
A fuller spec
This spec of a suffix grammar shows the idioms that specs across the ecosystem share.
local test = require("santoku.test")
local err = require("santoku.error")
local assert = err.assert
local grammar = require("outline.grammar")
test("grammar", function ()
test("suffix renders dates, ends, reps, delay and done", function ()
assert(grammar.suffix(nil, false, nil, nil, nil) == "")
assert(grammar.suffix("2026-08-22", false, nil, nil, nil) == " @2026-08-22")
assert(grammar.suffix("2026-08-22", true, nil, nil, nil) == " @2026-08-22 @done")
end)
test("unsuffix inverts suffix", function ()
local cases = {
{ "plain text", nil, false, nil, nil, nil },
{ "due", "2026-08-22", false, nil, nil, nil },
{ "done", "2026-08-22", true, nil, nil, nil },
{ "rep", "2026-08-22", false, "1w", nil, nil },
}
for _, c in ipairs(cases) do
local line = c[1] .. grammar.suffix(c[2], c[3], c[4], c[5], c[6])
local text, due, done, rep = grammar.unsuffix(line)
assert(text == c[1], c[1] .. ": text")
assert(due == c[2], c[1] .. ": due")
assert(done == c[3], c[1] .. ": done")
assert(rep == c[4], c[1] .. ": rep")
end
end)
end)
Details
- Localize err.assert over the global, so failures carry structured values.
- Use one outer block named after the module.
- Use one inner block per behavior, stated as a sentence.
- Drive round-trip properties from a table of cases.
- Thread the case name into every assertion message, so a failure names the exact case and field.
Assertions that report a reason
The assertions come from the base library, and the harness adds none. err.assert passes its arguments through on success, so it wraps expressions inline.
local err = require("santoku.error")
local tbl = require("santoku.table")
local assert = err.assert
local pcall = err.pcall
local teq = tbl.equals
local function unsuffix (line)
local text, due = line:match("^(.-) @(%d%d%d%d%-%d%d%-%d%d)$")
return text or line, due
end
assert(teq(
{ "buy milk", "2026-08-22" },
{ unsuffix("buy milk @2026-08-22") }))
assert(teq(
{ "no suffix here" },
{ unsuffix("no suffix here") }))
local n = assert(tonumber("42"), "not a number")
print("passed through:", n)
local ok, why = pcall(function ()
assert(teq({ due = "2026-08-22" }, { due = "2026-08-23" }))
end)
print("caught:", ok, why)
return "all assertions passed"
Details
- When its first argument is falsy, err.assert raises the arguments after it as a structured error.
- tbl.equals compares deeply and returns the reason on a mismatch. err.assert then raises that reason verbatim.
- Specs pin down the full return signature of a function the standard way. They wrap the multi-return call in a table literal and deep-compare it.
- This example runs live.
Running specs
The test/spec contract
By convention, the specs of a project live under test/spec and mirror the module tree under lib/. The spec for santoku.fracidx sits at test/spec/santoku/fracidx.lua.
$ find lib test -name "*.lua" | sort
lib/santoku/fracidx.lua
lib/santoku/table.lua
test/spec/santoku/fracidx.lua
test/spec/santoku/table.lua
$ toku spec test/spec
Test: test/spec/santoku/fracidx.lua
Test: test/spec/santoku/table.lua
$ toku spec test/spec/santoku/table.lua /no/such/file.lua
toku spec: no such spec: /no/such/file.lua
Details
- Web projects add client/test/spec and server/test/spec.
- The runner takes any mix of files and directories.
- It walks directories recursively, and every regular file it finds is a test.
- It runs files as given.
- It skips paths that don't exist, silently. The own spec of the runner pins that down.
- toku spec checks each path before the runner starts, and fails on a missing one.
- Each file gets one Test: line before it runs.
The runner function
santoku.test.runner returns a single function that takes an array of paths and an optional opts table. It prints the report as files run, and returns true when every file passed and false otherwise.
local runner = require("santoku.test.runner")
runner({ "test/spec" })
runner({ "test/spec/santoku/fracidx.lua" }, { stop = true })
runner({ "test/spec", "client/test/spec" }, {
match = "%.lua$",
interp = { "luajit", "-l", "santoku.trace" },
stop = true,
})
Details
- opts takes exactly three keys:
- match: a Lua pattern that the path of a file must match to run.
- interp: an argv array to run each file under as a subprocess.
- stop: exit with status 1 at the first failing file, instead of continuing.
- The runner validates both arguments as indexable.
The opts.match filter
The filter is literally str.match(fp, match) against the full path of each candidate, after the directory walk. So an unanchored pattern selects by substring anywhere in the path.
local arr = require("santoku.array")
local str = require("santoku.string")
local candidates = {
"test/spec/santoku/array.lua",
"test/spec/santoku/fracidx.lua",
"test/spec/santoku/sqlite/db.lua",
"test/spec/fixtures/seed.sql",
}
local function selected (match)
local out = {}
for i = 1, #candidates do
local fp = candidates[i]
if (not match) or str.match(fp, match) then
out[#out + 1] = fp
end
end
return arr.concat(out, " ")
end
print("fracidx:", selected("fracidx"))
print("sqlite subtree:", selected("^test/spec/santoku/sqlite"))
print("lua only:", selected("%.lua$"))
return selected(nil)
Details
- Anchors and magic characters work as in any Lua pattern.
- This live snippet repeats the exact test of the runner over a sample tree.
- A module name picks its spec, a prefix anchor picks a subtree, and a suffix anchor keeps only Lua sources.
In-process or subprocess
Without opts.interp, the runner calls each .lua file in its own process through fs.runfile. With opts.interp, each file runs as its own subprocess: the argv array plus the file path.
$ toku spec test/spec
Test: test/spec/santoku/fracidx.lua
Test: test/spec/santoku/table.lua
$ toku spec -i lua test/spec
$ toku spec -i "luajit -l santoku.trace" test/spec
$ toku spec -i "$HOME/.local/share/toku/node/bin/node --expose-gc" test/spec
Details
- In-process files all share one environment table backed by _G. So accidental globals leak between spec files, and any os.exit takes the whole run down.
- santoku.test itself never exits. A failing spec raises "test failed: <tag chain>", the pcall of the runner catches it, and the run continues with the next file.
- A subprocess per file isolates failures per file.
- Any interpreter works: a plain lua, luajit, or, for WASM builds, the node that toku setup --node recorded.
- The generated project harness always passes -i for this reason.
Keep going or stop
Each file runs inside pcall. On a failure the runner prints the error and, by default, moves on to the next file. With stop, it exits 1 immediately.
$ toku spec -i lua test/spec
Test: test/spec/santoku/fracidx.lua
fracidx: between rejects prev >= next
test/spec/santoku/fracidx.lua:52: assertion failed!
stack traceback:
...
lua: test failed: fracidx: between rejects prev >= next
...
child process exited with unexpected status exited 1
Test: test/spec/santoku/table.lua
$ toku spec -s -i lua test/spec
Test: test/spec/santoku/fracidx.lua
...
child process exited with unexpected status exited 1
$ echo $?
1
Details
- Under an interpreter, the failing spec process prints the santoku.test block: tag chain, message and traceback.
- The process then runs the deferred functions, innermost block first, and raises "test failed: <tag chain>". lua reports that on stderr before it exits 1.
- The runner shows that exit as a structured child-process error. The code shows it with the traceback elided.
- Without -s the next Test: line follows. With -s the run ends there.
- The project harness passes -s only when toku test itself gets -s or --stop.
The per-file dispatch
For each file, the runner applies match, prints the Test: line, then takes one of three branches inside pcall. A trailing closure receives the results of pcall.
local function process_fp (state, fp, interp, match, stop)
if fp and ((not match) or smatch(fp, match)) then
print("Test:", fp)
return (function (ok, ...)
if not ok then
state.failed = true
print(...)
if stop then
sysexit(1)
end
end
end)(pcall(function ()
if interp then
execute(push(copy({}, interp), fp))
elseif endswith(fp, ".lua") then
runfile(fp, run_env)
else
execute({ fp })
end
end))
end
end
Details
- An interp takes precedence for every file. The runner copies its argv, appends the path and spawns the child.
- Otherwise, .lua files run in-process through fs.runfile against the shared run_env.
- Anything else goes to santoku.system.execute as a program of its own.
- On a failure, the closure records the failure, prints the error, and exits 1 only when stop is set.
- Without stop, the runner returns false after the last file if any file failed.
toku spec since santoku-cli 5.0.0
toku spec FILE... bypasses the project build entirely and hands files and directories straight to the runner. -s maps to stop, and -i splits on whitespace into the interp argv.
$ toku spec -s test/spec/santoku/fracidx.lua
Test: test/spec/santoku/fracidx.lua
$ toku spec -s -i "luajit -l santoku.trace" test/spec
$ eval "$(toku env)"
$ toku spec test/spec/santoku/sqlite
Details
- This is the quick loop against a tree that's already built, or against no tree at all when the specs only need installed rocks.
- After eval "$(toku env)", it runs inside the pinned lua_modules of the project.
- toku spec passes no match. Name the file or the directory to narrow the run.
The project harness
toku test builds the test tree. Its generated run.sh then invokes toku spec, with one interpreter subprocess per file and each selected spec file named on the command line.
toku spec $STOP -i "$LUA $MODS" "test/spec/santoku/fracidx.lua" "test/spec/santoku/table.lua"
toku spec $STOP -i "$HOME/.local/share/toku/node/bin/node --expose-gc --no-warnings" "test/spec/santoku/fracidx.js" "test/spec/santoku/table.js"
$ toku test test/spec/santoku/fracidx.lua
$ toku test --wasm test/spec/santoku/fracidx.lua
Test: test/spec/santoku/fracidx.js
Details
- run.sh passes -s only when toku test got -s or --stop.
- The interpreter is $LUA, plus -l santoku.profile with --profile and -l santoku.trace with --trace.
- run.sh names the spec files that the run selects, so a stale file in the rendered tree never runs.
- toku treats every file under test/spec as a spec, whatever its extension.
- The WASM variant runs the same suite as bundled .js files. It uses the node that toku setup --node recorded, by its absolute path.
- With no such node, the WASM variant stops with "wasm tests need the managed node; run: toku setup --node".
- A PATH such as toku test test/spec/santoku/fracidx.lua narrows the run to one spec, mapped to its .js counterpart in WASM mode.
- A spec that passes natively and fails under --wasm usually asserts a capability that the wasm build doesn't have.
- The santoku-make tab covers the gates for that. They include the case where a presence check reports a capability that raises when called: https://santoku.dev/santoku-make
The runner's own spec
Two cases from the spec of the runner check that the module is a callable. They also check that the runner skips nonexistent paths without spawning anything, with or without a match.
local test = require("santoku.test")
local validate = require("santoku.validate")
local isfunction = validate.isfunction
local runner = require("santoku.test.runner")
test("runner is the callable entrypoint", function ()
assert(isfunction(runner))
end)
test("non-existent paths are skipped without spawning", function ()
runner({ "/no/such/spec/file.lua" })
runner({ "/no/such/spec/file.lua" }, { match = "nomatch" })
end)
Details
- Calling the runner from inside a spec is safe here, because the missing paths short-circuit before any execution.