Files
lush/testes/lush/config.lua
Cormac Shannon 51b39930ed Fix lush tests to work from testes/ directory (via all.lua)
Tests hardcoded ./lua and assumed CWD was the project root.
Add shared testutil.lua module to derive interpreter path from
arg table, and use temp directories for glob tests instead of
relying on .c files in CWD.
2026-03-22 23:17:17 +00:00

154 lines
4.1 KiB
Lua

-- testes/lush/config.lua
-- Tests for configuration file support (issue #08).
print "testing configuration"
local INTERP = require("lush.testutil").INTERP
-- helper: get a unique temp directory
local tmpbase = os.tmpname()
os.remove(tmpbase) -- tmpname creates the file on some systems
local function mkdir(path)
os.execute('mkdir -p "' .. path .. '"')
end
local function writefile(path, content)
local f = assert(io.open(path, "w"))
f:write(content)
f:close()
end
local function rmrf(path)
os.execute('rm -rf "' .. path .. '"')
end
-- helper: run interpreter -i with XDG_CONFIG_HOME set, feed it a command via stdin
local function run_with_config(xdg_dir, input)
local cmd = string.format(
'printf "%%s\\n" "%s" | XDG_CONFIG_HOME="%s" ' .. INTERP .. ' -i 2>&1',
input, xdg_dir)
local f = io.popen(cmd)
local output = f:read("*a")
f:close()
return output
end
-- helper: run interpreter -E -i with XDG_CONFIG_HOME set
local function run_with_config_E(xdg_dir, input)
local cmd = string.format(
'printf "%%s\\n" "%s" | XDG_CONFIG_HOME="%s" ' .. INTERP .. ' -E -i 2>&1',
input, xdg_dir)
local f = io.popen(cmd)
local output = f:read("*a")
f:close()
return output
end
-- ===== TEST 1: Main config file is loaded =====
do
local tmpdir = tmpbase .. "_t1"
mkdir(tmpdir .. "/lush")
writefile(tmpdir .. "/lush/config.lua", '_LUSH_TEST_MARKER = 42\n')
local out = run_with_config(tmpdir, "print(_LUSH_TEST_MARKER)")
assert(out:find("42"), "config file should set global: " .. out)
rmrf(tmpdir)
end
-- ===== TEST 2: config.d/ files are loaded in sorted order =====
do
local tmpdir = tmpbase .. "_t2"
mkdir(tmpdir .. "/lush/config.d")
writefile(tmpdir .. "/lush/config.d/02-second.lua",
'_LUSH_ORDER = (_LUSH_ORDER or "") .. "B"\n')
writefile(tmpdir .. "/lush/config.d/01-first.lua",
'_LUSH_ORDER = (_LUSH_ORDER or "") .. "A"\n')
writefile(tmpdir .. "/lush/config.d/03-third.lua",
'_LUSH_ORDER = (_LUSH_ORDER or "") .. "C"\n')
local out = run_with_config(tmpdir, "print(_LUSH_ORDER)")
assert(out:find("ABC"), "config.d files should load in order: " .. out)
rmrf(tmpdir)
end
-- ===== TEST 3: Both config and config.d/ are loaded =====
do
local tmpdir = tmpbase .. "_t3"
mkdir(tmpdir .. "/lush/config.d")
writefile(tmpdir .. "/lush/config.lua", '_LUSH_SEQ = "M"\n')
writefile(tmpdir .. "/lush/config.d/01.lua",
'_LUSH_SEQ = _LUSH_SEQ .. "D"\n')
local out = run_with_config(tmpdir, "print(_LUSH_SEQ)")
assert(out:find("MD"), "main config runs before config.d: " .. out)
rmrf(tmpdir)
end
-- ===== TEST 4: -E suppresses config loading =====
do
local tmpdir = tmpbase .. "_t4"
mkdir(tmpdir .. "/lush")
writefile(tmpdir .. "/lush/config.lua", '_LUSH_SUPPRESSED = true\n')
local out = run_with_config_E(tmpdir, "print(_LUSH_SUPPRESSED)")
assert(out:find("nil"), "-E should suppress config: " .. out)
rmrf(tmpdir)
end
-- ===== TEST 5: Missing config dir doesn't error =====
do
local tmpdir = tmpbase .. "_t5"
rmrf(tmpdir) -- make sure it doesn't exist
-- should start normally without errors
local out = run_with_config(tmpdir, "print(42)")
assert(out:find("42"), "missing config dir should not error: " .. out)
rmrf(tmpdir)
end
-- ===== TEST 6: Only .lua files in config.d/ are loaded =====
do
local tmpdir = tmpbase .. "_t6"
mkdir(tmpdir .. "/lush/config.d")
writefile(tmpdir .. "/lush/config.d/good.lua",
'_LUSH_LOADED = "yes"\n')
writefile(tmpdir .. "/lush/config.d/skip.txt",
'_LUSH_SKIPPED = "bad"\n')
writefile(tmpdir .. "/lush/config.d/README",
'_LUSH_README = "bad"\n')
local out = run_with_config(tmpdir, 'print(_LUSH_LOADED, _LUSH_SKIPPED)')
assert(out:find("yes"), "should load .lua files: " .. out)
assert(out:find("nil"), "should skip non-.lua files: " .. out)
rmrf(tmpdir)
end
-- ===== TEST 7: Config error is non-fatal =====
do
local tmpdir = tmpbase .. "_t7"
mkdir(tmpdir .. "/lush")
writefile(tmpdir .. "/lush/config.lua", 'error("config boom")\n')
local out = run_with_config(tmpdir, "print(99)")
assert(out:find("99"), "config error should be non-fatal: " .. out)
rmrf(tmpdir)
end
print "OK"