Implement startup configuration file support (issue #08)

Load ~/.config/lush/config and config.d/*.lua at REPL startup,
respecting XDG_CONFIG_HOME. Suppressed by -E flag.
This commit is contained in:
Cormac Shannon
2026-03-03 22:59:51 +00:00
parent db858b3f68
commit bbc0f24009
3 changed files with 233 additions and 2 deletions

151
testes/lush/config.lua Normal file
View File

@@ -0,0 +1,151 @@
-- testes/lush/config.lua
-- Tests for configuration file support (issue #08).
print "testing configuration"
-- 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 ./lua -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" ./lua -i 2>&1',
input, xdg_dir)
local f = io.popen(cmd)
local output = f:read("*a")
f:close()
return output
end
-- helper: run ./lua -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" ./lua -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", '_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", '_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", '_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", '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"