test.lua: disallow overwriting existing tests

i.e. if a function name is accidentally reused - previously, later test
functions would silently overwrite earlier ones
develop
lethosor 2023-08-15 01:36:20 -04:00
parent d40843de7e
commit c6c7331b1b
No known key found for this signature in database
GPG Key ID: 76A269552F4F58C1
1 changed files with 20 additions and 1 deletions

@ -247,6 +247,25 @@ local function load_test_config(config_file)
return config return config
end end
local function TestTable()
local inner = utils.OrderedTable()
local meta = copyall(getmetatable(inner))
function meta:__newindex(k, v)
if inner[k] then
error('Attempt to overwrite existing test: ' .. k)
elseif type(v) ~= 'function' then
error('Attempt to define test as non-function: ' .. k .. ' = ' .. tostring(v))
else
inner[k] = v
end
end
local self = {}
setmetatable(self, meta)
return self
end
-- we have to save and use the original dfhack.printerr here so our test harness -- we have to save and use the original dfhack.printerr here so our test harness
-- output doesn't trigger its own dfhack.printerr usage detection (see -- output doesn't trigger its own dfhack.printerr usage detection (see
-- detect_printerr below) -- detect_printerr below)
@ -291,7 +310,7 @@ end
local function build_test_env(path) local function build_test_env(path)
local env = { local env = {
test = utils.OrderedTable(), test = TestTable(),
-- config values can be overridden in the test file to define -- config values can be overridden in the test file to define
-- requirements for the tests in that file -- requirements for the tests in that file
config = { config = {