diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 9e74f20..9f6656b 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -43,6 +43,9 @@ jobs: # Lua 5.3 combinations - lua: "lua 5.3" lpeg: "1.0.1-1" + - lua: "lua 5.3" + lpeg: "1.0.1-1" + strict_module: "pl.strict" - lua: "lua 5.3" lpeg: "0.12.2-1" @@ -93,7 +96,7 @@ jobs: - name: Run Tests env: - TEST_STRICT: 1 + TEST_STRICT: ${{ matrix.strict_module || '1' }} run: | ./run_tests.sh "${{ matrix.lua }}" "${{ matrix.lpeg }}" diff --git a/run_tests.sh b/run_tests.sh index b6a1566..43d653f 100755 --- a/run_tests.sh +++ b/run_tests.sh @@ -78,6 +78,14 @@ else source "$ENV_DIR/bin/activate" fi +# Ensure penlight is installed if testing under pl.strict +if [ "${TEST_STRICT:-}" = "pl.strict" ]; then + if ! luarocks list | grep -q penlight; then + echo "Installing penlight for pl.strict testing..." + luarocks install https://luarocks.org/penlight-1.15.0-1.src.rock + fi +fi + # 3. Run the tests echo "Running tests in the target environment..." # Clean up any residual coverage file from previous runs diff --git a/tests/hook_require.lua b/tests/hook_require.lua index a709663..e121b3e 100644 --- a/tests/hook_require.lua +++ b/tests/hook_require.lua @@ -1,64 +1,106 @@ local os = require("os") -if os.getenv("TEST_STRICT") then - local mt = getmetatable(_G) - if not mt then - mt = {} - setmetatable(_G, mt) - end - mt.__declared = mt.__declared or {} - for k in pairs(_G) do - mt.__declared[k] = true - end - mt.__declared["_ENV"] = true +local test_strict = os.getenv("TEST_STRICT") - local debug_getinfo = debug.getinfo - local function what() - local d = debug_getinfo(3, "S") - return d and d.what or "C" - end +local old_require = require +if os.getenv('LUA_OLD_INIT') then + local loadstring = rawget(_G or {}, "loadstring") or load + assert(loadstring(os.getenv('LUA_OLD_INIT')))() +else + require("luarocks.require") +end +local luarocks_require = require - local old_newindex = mt.__newindex - mt.__newindex = function(t, n, v) - if not mt.__declared[n] then - local w = what() - if w ~= "main" and w ~= "C" then - error("assign to undeclared variable '"..n.."'", 2) +if test_strict and test_strict ~= "" and test_strict ~= "0" and test_strict ~= "false" then + if test_strict == "pl.strict" then + require("pl.strict") + local mt = getmetatable(_G) + if mt then + local allowed = { + newproxy = true, + statsfile = true, + reportfile = true, + configfile = true, + runreport = true, + deletestats = true, + include = true, + exclude = true, + reporter = true, + tick = true, + modules = true, + unpack = true, + _M = true, + _NAME = true, + _PACKAGE = true, + setup = true, + teardown = true, + setfenv = true, + getfenv = true, + module = true, + } + mt.__declared = mt.__declared or {} + for k, v in pairs(allowed) do + mt.__declared[k] = v + end + local old_index = mt.__index + mt.__index = function(t, n) + if n == nil then + return nil + end + return old_index(t, n) end - mt.__declared[n] = true end - if old_newindex then - old_newindex(t, n, v) - else - rawset(t, n, v) + else + local mt = getmetatable(_G) + if not mt then + mt = {} + setmetatable(_G, mt) end - end + mt.__declared = mt.__declared or {} + for k in pairs(_G) do + mt.__declared[k] = true + end + mt.__declared["_ENV"] = true - local old_index = mt.__index - mt.__index = function(t, n) - if not mt.__declared[n] then - local d = debug_getinfo(2, "S") - if d and d.source and d.source:match("lua/json/") then - error("variable '"..n.."' is not declared", 2) + local debug_getinfo = debug.getinfo + local function what() + local d = debug_getinfo(3, "S") + return d and d.what or "C" + end + + local old_newindex = mt.__newindex + mt.__newindex = function(t, n, v) + if not mt.__declared[n] then + local w = what() + if w ~= "main" and w ~= "C" then + error("assign to undeclared variable '"..n.."'", 2) + end + mt.__declared[n] = true + end + if old_newindex then + old_newindex(t, n, v) + else + rawset(t, n, v) end end - if old_index then - return old_index(t, n) - else - return rawget(t, n) + + local old_index = mt.__index + mt.__index = function(t, n) + if not mt.__declared[n] then + local d = debug_getinfo(2, "S") + if d and d.source and d.source:match("lua/json/") then + error("variable '"..n.."' is not declared", 2) + end + end + if old_index then + return old_index(t, n) + else + return rawget(t, n) + end end end end -local old_require = require -if os.getenv('LUA_OLD_INIT') then - local loadstring = rawget(_G or {}, "loadstring") or load - assert(loadstring(os.getenv('LUA_OLD_INIT')))() -else - require("luarocks.require") -end -local luarocks_require = require - function require(module, ...) if module == "json" or module:match("^json%.") then return old_require(module, ...)