From c6c7331b1bddc7232942e562686cf33d4ac9671b Mon Sep 17 00:00:00 2001 From: lethosor Date: Tue, 15 Aug 2023 01:36:20 -0400 Subject: [PATCH] test.lua: disallow overwriting existing tests i.e. if a function name is accidentally reused - previously, later test functions would silently overwrite earlier ones --- ci/test.lua | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/ci/test.lua b/ci/test.lua index 01168800b..543334e72 100644 --- a/ci/test.lua +++ b/ci/test.lua @@ -247,6 +247,25 @@ local function load_test_config(config_file) return config 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 -- output doesn't trigger its own dfhack.printerr usage detection (see -- detect_printerr below) @@ -291,7 +310,7 @@ end local function build_test_env(path) local env = { - test = utils.OrderedTable(), + test = TestTable(), -- config values can be overridden in the test file to define -- requirements for the tests in that file config = {