tests: improve

This commit is contained in:
Jeremy Baxter 2024-08-11 18:22:08 +12:00
parent 0e4c48fda3
commit 75096fdf99

View file

@ -15,12 +15,8 @@ local oldenv = _ENV
local function test(f) local function test(f)
local _ENV = oldenv local _ENV = oldenv
local function mesg(...)
io.stdout:write(..., '\n')
end
assert(type(f) == "function") assert(type(f) == "function")
mesg(f()) printfmt(f())
end end
-- Table containing tests for each library. -- Table containing tests for each library.
@ -28,13 +24,13 @@ end
-- impossible to test without user interaction. -- impossible to test without user interaction.
local tests = { local tests = {
environ = { environ = {
getvar = function () get = function ()
local var = "HOME" local var = "HOME"
assert(environ[var]) assert(environ[var])
return 'environ["' .. var .. '"] ~= nil' return 'environ["' .. var .. '"] ~= nil'
end, end,
setvar = function () set = function ()
local var = "VAR" local var = "VAR"
local val = "hello" local val = "hello"
@ -80,31 +76,31 @@ local tests = {
directory = function () directory = function ()
local dir, subdir = "testdir", "testdir/sub" local dir, subdir = "testdir", "testdir/sub"
-- leftovers from last time?
if fs.isdirectory(dir) then
fs.remove(dir)
end
assert(fs.mkdir(dir)) assert(fs.mkdir(dir))
assert(fs.exists(dir) and fs.isdirectory(dir)) assert(fs.exists(dir) and fs.isdirectory(dir)
and fs.type(dir) == "directory")
assert(fs.rmdir(dir)) assert(fs.rmdir(dir))
assert(fs.mkpath(subdir)) assert(fs.mkpath(subdir))
assert(fs.exists(dir)) assert(fs.exists(dir) and fs.isdirectory(dir))
assert(fs.isdirectory(dir)) assert(fs.exists(subdir) and fs.isdirectory(subdir))
assert(fs.exists(subdir))
assert(fs.isdirectory(subdir))
assert(fs.rmdir(subdir)) assert(fs.remove(dir))
assert(fs.rmdir(dir))
return ([[ return ([[
fs.mkdir("%s") fs.mkdir("%s")
fs.rmdir("%s") fs.rmdir("%s")
fs.mkpath("%s") fs.mkpath("%s")
fs.rmdir("%s"") fs.remove("%s")]]):format(
fs.rmdir("%s")]]):format(
dir, dir,
dir, dir,
subdir, subdir,
subdir, dir)
dir
)
end, end,
move = function () move = function ()
local src, dst = "testfile", "testfile.new" local src, dst = "testfile", "testfile.new"
@ -124,14 +120,28 @@ fs.rmdir("%s")]]):format(
return 'fs.move("' .. src .. '", "' .. dst .. '")' return 'fs.move("' .. src .. '", "' .. dst .. '")'
end, end,
path = function () path = function ()
local bpath, base = "/etc/fstab", "fstab" local bpaths = {
local dpath, dir = "/usr/local/bin", "/usr/local" {"/etc/fstab", "fstab"},
{"/sbin/init", "init"},
{"init", "init"}
}
local dpaths = {
{"/usr/local/bin", "/usr/local"},
{"test.lua", "."}
}
local res = {}
assert(fs.basename(bpath) == base) for _, entry in ipairs(bpaths) do
assert(fs.dirname(dpath) == dir) assert(fs.basename(entry[1]) == entry[2])
res[#res + 1] = '\nfs.basename("' .. entry[1] .. '")'
end
for _, entry in ipairs(bpaths) do
assert(fs.basename(entry[1]) == entry[2])
res[#res + 1] = '\nfs.dirname("' .. entry[1] .. '")'
end
return 'fs.basename("' .. bpath .. [[") res[1] = res[1]:sub(2) -- strip leading newline
fs.dirname("]] .. dpath .. '")' return table.concat(res)
end, end,
remove = function () remove = function ()
local file = "testfile" local file = "testfile"
@ -147,9 +157,10 @@ fs.dirname("]] .. dpath .. '")'
return 'fs.remove("' .. file .. '")' return 'fs.remove("' .. file .. '")'
end, end,
workdir = function () workdir = function ()
local d = "/usr" local d = "/etc"
local wd = fs.workdir() local wd = fs.workdir()
assert(type(wd) == "string")
fs.workdir(d) fs.workdir(d)
assert(fs.workdir() == d) assert(fs.workdir() == d)
fs.workdir(wd) fs.workdir(wd)
@ -160,7 +171,7 @@ fs.dirname("]] .. dpath .. '")'
os = { os = {
hostname = function () hostname = function ()
assert(os.hostname()) assert(type(os.hostname()) == "string")
return "os.hostname()" return "os.hostname()"
end end
}, },
@ -179,12 +190,11 @@ fs.dirname("]] .. dpath .. '")'
signum = function () signum = function ()
local sig = "SIGKILL" local sig = "SIGKILL"
-- probably signal no.9
assert(math.type(process.signum(sig)) == "integer") assert(math.type(process.signum(sig)) == "integer")
return 'process.signum("' .. sig .. '")' return 'process.signum("' .. sig .. '")'
end, end,
send = function () send = function ()
local hdl = io.popen("sleep 8") local hdl = io.popen("sleep 2")
local pid = process.pidof("sleep") local pid = process.pidof("sleep")
assert(process.send(pid, "SIGKILL")) assert(process.send(pid, "SIGKILL"))
@ -202,8 +212,8 @@ do
local _ENV = env local _ENV = env
-- environ -- environ
test(environ.getvar) test(environ.get)
test(environ.setvar) test(environ.set)
test(environ.pairs) test(environ.pairs)
-- fs -- fs