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.
This commit is contained in:
Cormac Shannon
2026-03-22 23:17:17 +00:00
parent cca6749d4d
commit 51b39930ed
5 changed files with 28 additions and 9 deletions

View File

@@ -3,6 +3,8 @@
print "testing shell builtins" print "testing shell builtins"
local interp = require("lush.testutil").INTERP
-- === __builtins is NOT in _G (internal, stored in registry) === -- === __builtins is NOT in _G (internal, stored in registry) ===
do do
@@ -132,7 +134,7 @@ end
-- exec replaces process (test in subprocess) -- exec replaces process (test in subprocess)
do do
local r = `sh -c './lua -e "os.exit(42)"'` local r = lush.capture("sh -c '" .. interp .. " -e \"os.exit(42)\"'")
assert(r.code == 42, "subprocess exit code: " .. tostring(r.code)) assert(r.code == 42, "subprocess exit code: " .. tostring(r.code))
end end

View File

@@ -3,6 +3,8 @@
print "testing configuration" print "testing configuration"
local INTERP = require("lush.testutil").INTERP
-- helper: get a unique temp directory -- helper: get a unique temp directory
local tmpbase = os.tmpname() local tmpbase = os.tmpname()
os.remove(tmpbase) -- tmpname creates the file on some systems os.remove(tmpbase) -- tmpname creates the file on some systems
@@ -21,10 +23,10 @@ local function rmrf(path)
os.execute('rm -rf "' .. path .. '"') os.execute('rm -rf "' .. path .. '"')
end end
-- helper: run ./lua -i with XDG_CONFIG_HOME set, feed it a command via stdin -- helper: run interpreter -i with XDG_CONFIG_HOME set, feed it a command via stdin
local function run_with_config(xdg_dir, input) local function run_with_config(xdg_dir, input)
local cmd = string.format( local cmd = string.format(
'printf "%%s\\n" "%s" | XDG_CONFIG_HOME="%s" ./lua -i 2>&1', 'printf "%%s\\n" "%s" | XDG_CONFIG_HOME="%s" ' .. INTERP .. ' -i 2>&1',
input, xdg_dir) input, xdg_dir)
local f = io.popen(cmd) local f = io.popen(cmd)
local output = f:read("*a") local output = f:read("*a")
@@ -32,10 +34,10 @@ local function run_with_config(xdg_dir, input)
return output return output
end end
-- helper: run ./lua -E -i with XDG_CONFIG_HOME set -- helper: run interpreter -E -i with XDG_CONFIG_HOME set
local function run_with_config_E(xdg_dir, input) local function run_with_config_E(xdg_dir, input)
local cmd = string.format( local cmd = string.format(
'printf "%%s\\n" "%s" | XDG_CONFIG_HOME="%s" ./lua -E -i 2>&1', 'printf "%%s\\n" "%s" | XDG_CONFIG_HOME="%s" ' .. INTERP .. ' -E -i 2>&1',
input, xdg_dir) input, xdg_dir)
local f = io.popen(cmd) local f = io.popen(cmd)
local output = f:read("*a") local output = f:read("*a")

View File

@@ -7,7 +7,7 @@ print "testing globbing and tilde expansion"
-- *.lua should expand to matching files (we know lush test files exist) -- *.lua should expand to matching files (we know lush test files exist)
do do
local r = `echo *.c` local r = `echo ../*.c`
local out = r.stdout:gsub("\n$", "") local out = r.stdout:gsub("\n$", "")
-- should NOT be the literal "*.c" since .c files exist in the project -- should NOT be the literal "*.c" since .c files exist in the project
assert(out ~= "*.c", "expected *.c to expand, got literal: " .. out) assert(out ~= "*.c", "expected *.c to expand, got literal: " .. out)
@@ -125,7 +125,7 @@ end
-- === expansion in pipelines === -- === expansion in pipelines ===
do do
local r = `echo *.c | head -1` local r = `echo ../*.c | head -1`
local out = r.stdout:gsub("\n$", "") local out = r.stdout:gsub("\n$", "")
-- pipeline should have expanded *.c before piping -- pipeline should have expanded *.c before piping
assert(out ~= "*.c", "glob not expanded in pipeline, got: " .. out) assert(out ~= "*.c", "glob not expanded in pipeline, got: " .. out)

View File

@@ -3,6 +3,8 @@
print "testing prompt system" print "testing prompt system"
local INTERP = require("lush.testutil").INTERP
-- helper: get a unique temp directory -- helper: get a unique temp directory
local tmpbase = os.tmpname() local tmpbase = os.tmpname()
os.remove(tmpbase) -- tmpname creates the file on some systems os.remove(tmpbase) -- tmpname creates the file on some systems
@@ -21,7 +23,7 @@ local function rmrf(path)
os.execute('rm -rf "' .. path .. '"') os.execute('rm -rf "' .. path .. '"')
end end
-- helper: run ./lua -i with config, use print() to reveal prompt state -- helper: run interpreter -i with config, use print() to reveal prompt state
local function run_lua(input, config_content) local function run_lua(input, config_content)
local tmpdir = tmpbase .. "_run" local tmpdir = tmpbase .. "_run"
mkdir(tmpdir .. "/lush") mkdir(tmpdir .. "/lush")
@@ -29,7 +31,7 @@ local function run_lua(input, config_content)
writefile(tmpdir .. "/lush/config.lua", config_content) writefile(tmpdir .. "/lush/config.lua", config_content)
end end
local cmd = string.format( local cmd = string.format(
'printf "%%s\\n" %s | XDG_CONFIG_HOME="%s" ./lua -i 2>&1', 'printf "%%s\\n" %s | XDG_CONFIG_HOME="%s" ' .. INTERP .. ' -i 2>&1',
input, tmpdir) input, tmpdir)
local f = io.popen(cmd) local f = io.popen(cmd)
local output = f:read("*a") local output = f:read("*a")

13
testes/lush/testutil.lua Normal file
View File

@@ -0,0 +1,13 @@
-- testes/lush/testutil.lua
-- Shared utilities for lush test files.
local M = {}
-- Derive the interpreter path from the arg table.
-- Works regardless of CWD (project root or testes/).
local a = arg or ARG
local i = 0
while a[i] do i = i - 1 end
M.INTERP = a[i + 1]
return M